MongoDB C++ Driver  legacy-1.1.2
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 
24 namespace mongo {
25 
46 template <typename _WordType>
47 class AtomicWord {
48  MONGO_DISALLOW_COPYING(AtomicWord);
49 
50 public:
54  typedef _WordType WordType;
55 
59  explicit AtomicWord(WordType value = WordType(0)) : _value(value) {}
60 
66  WordType load() const {
67  return AtomicIntrinsics<WordType>::load(&_value);
68  }
69 
75  WordType loadRelaxed() const {
77  }
78 
84  void store(WordType newValue) {
85  AtomicIntrinsics<WordType>::store(&_value, newValue);
86  }
87 
95  WordType swap(WordType newValue) {
96  return AtomicIntrinsics<WordType>::swap(&_value, newValue);
97  }
98 
107  WordType compareAndSwap(WordType expected, WordType newValue) {
108  return AtomicIntrinsics<WordType>::compareAndSwap(&_value, expected, newValue);
109  }
110 
118  WordType fetchAndAdd(WordType increment) {
119  return AtomicIntrinsics<WordType>::fetchAndAdd(&_value, increment);
120  }
121 
129  WordType fetchAndSubtract(WordType decrement) {
130  return fetchAndAdd(WordType(0) - decrement);
131  }
132 
140  WordType addAndFetch(WordType increment) {
141  return fetchAndAdd(increment) + increment;
142  }
143 
151  WordType subtractAndFetch(WordType decrement) {
152  return fetchAndSubtract(decrement) - decrement;
153  }
154 
155 private:
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
WordType load() const
Gets the current value of this AtomicWord.
Definition: atomic_word_intrinsics.h:66
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
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
WordType compareAndSwap(WordType expected, WordType newValue)
Atomic compare and swap.
Definition: atomic_word_intrinsics.h:107
WordType addAndFetch(WordType increment)
Get the current value of this, add "increment" and store it, atomically.
Definition: atomic_word_intrinsics.h:140
AtomicWord(WordType value=WordType(0))
Construct a new word with the given initial value.
Definition: atomic_word_cxx11.h:60
WordType fetchAndSubtract(WordType decrement)
Get the current value of this, subtract "decrement" and store it, atomically.
Definition: atomic_word_intrinsics.h:129
WordType swap(WordType newValue)
Atomically swaps the current value of this with "newValue".
Definition: atomic_word_intrinsics.h:95
WordType subtractAndFetch(WordType decrement)
Get the current value of this, subtract "decrement" and store it, atomically.
Definition: atomic_word_intrinsics.h:151
WordType fetchAndAdd(WordType increment)
Get the current value of this, add "increment" and store it, atomically.
Definition: atomic_word_intrinsics.h:118
Instantiation of AtomicIntrinsics<> for all word types T.
Definition: atomic_intrinsics_gcc_atomic.h:29
void store(WordType newValue)
Sets the value of this AtomicWord to "newValue".
Definition: atomic_word_intrinsics.h:84