MongoDB C++ Driver legacy-1.1.2
Loading...
Searching...
No Matches
atomic_word_intrinsics.h
1/* Copyright 2012 10gen Inc.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#pragma once
17
18#include <boost/static_assert.hpp>
19
20#include "mongo/base/disallow_copying.h"
21#include "mongo/platform/atomic_intrinsics.h"
22#include "mongo/platform/compiler.h"
23
24namespace mongo {
25
46template <typename _WordType>
47class AtomicWord {
48 MONGO_DISALLOW_COPYING(AtomicWord);
49
50public:
54 typedef _WordType WordType;
55
59 explicit AtomicWord(WordType value = WordType(0)) : _value(value) {}
60
66 WordType load() const {
68 }
69
77 }
78
84 void store(WordType newValue) {
85 AtomicIntrinsics<WordType>::store(&_value, newValue);
86 }
87
96 return AtomicIntrinsics<WordType>::swap(&_value, newValue);
97 }
98
108 return AtomicIntrinsics<WordType>::compareAndSwap(&_value, expected, newValue);
109 }
110
119 return AtomicIntrinsics<WordType>::fetchAndAdd(&_value, increment);
120 }
121
130 return fetchAndAdd(WordType(0) - decrement);
131 }
132
141 return fetchAndAdd(increment) + increment;
142 }
143
152 return fetchAndSubtract(decrement) - decrement;
153 }
154
155private:
156 volatile WordType _value;
157};
158
159#define _ATOMIC_WORD_DECLARE(NAME, WTYPE) \
160 typedef class AtomicWord<WTYPE> NAME; \
161 namespace { \
162 BOOST_STATIC_ASSERT(sizeof(NAME) == sizeof(WTYPE)); \
163 }
164
165_ATOMIC_WORD_DECLARE(AtomicUInt32, unsigned);
166_ATOMIC_WORD_DECLARE(AtomicUInt64, unsigned long long);
167_ATOMIC_WORD_DECLARE(AtomicInt32, int);
168_ATOMIC_WORD_DECLARE(AtomicInt64, long long);
169#undef _ATOMIC_WORD_DECLARE
170
171} // namespace mongo
Instantiation of AtomicIntrinsics<> for all word types T.
Definition atomic_intrinsics_gcc_atomic.h:29
WordType load() const
Gets the current value of this AtomicWord.
Definition atomic_word_intrinsics.h:66
WordType swap(WordType newValue)
Atomically swaps the current value of this with "newValue".
Definition atomic_word_intrinsics.h:95
WordType fetchAndAdd(WordType increment)
Get the current value of this, add "increment" and store it, atomically.
Definition atomic_word_intrinsics.h:118
WordType compareAndSwap(WordType expected, WordType newValue)
Atomic compare and swap.
Definition atomic_word_intrinsics.h:107
void store(WordType newValue)
Sets the value of this AtomicWord to "newValue".
Definition atomic_word_intrinsics.h:84
WordType subtractAndFetch(WordType decrement)
Get the current value of this, subtract "decrement" and store it, atomically.
Definition atomic_word_intrinsics.h:151
AtomicWord(WordType value=WordType(0))
Construct a new word with the given initial value.
Definition atomic_word_intrinsics.h:59
WordType fetchAndSubtract(WordType decrement)
Get the current value of this, subtract "decrement" and store it, atomically.
Definition atomic_word_intrinsics.h:129
WordType addAndFetch(WordType increment)
Get the current value of this, add "increment" and store it, atomically.
Definition atomic_word_intrinsics.h:140
WordType loadRelaxed() const
Gets the current value of this AtomicWord.
Definition atomic_word_intrinsics.h:75
_WordType WordType
Underlying value type.
Definition atomic_word_intrinsics.h:54
Utility functions for parsing numbers from strings.
Definition compare_numbers.h:20