OLD | NEW |
---|---|
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_ATOMIC_UTILS_H_ | 5 #ifndef V8_ATOMIC_UTILS_H_ |
6 #define V8_ATOMIC_UTILS_H_ | 6 #define V8_ATOMIC_UTILS_H_ |
7 | 7 |
8 #include <limits.h> | 8 #include <limits.h> |
9 | 9 |
10 #include "src/base/atomicops.h" | 10 #include "src/base/atomicops.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
44 | 44 |
45 V8_INLINE T operator+=(T value) { return Increment(value); } | 45 V8_INLINE T operator+=(T value) { return Increment(value); } |
46 V8_INLINE T operator-=(T value) { return Decrement(value); } | 46 V8_INLINE T operator-=(T value) { return Decrement(value); } |
47 | 47 |
48 private: | 48 private: |
49 STATIC_ASSERT(sizeof(T) <= sizeof(base::AtomicWord)); | 49 STATIC_ASSERT(sizeof(T) <= sizeof(base::AtomicWord)); |
50 | 50 |
51 base::AtomicWord value_; | 51 base::AtomicWord value_; |
52 }; | 52 }; |
53 | 53 |
54 // This type uses no barrier accessors to change atomic value. Be careful with | |
55 // data races. | |
56 template <typename T> | |
57 class NonAtomicValue { | |
Michael Lippautz
2016/10/12 08:04:15
You sure about this name?
I'd rather call it NoBa
ulan
2016/10/12 08:32:39
+1 to NoBarrierAtomicValue
Hannes Payer (out of office)
2016/10/12 11:27:45
Changed the name. Note that the no barrier accesso
| |
58 public: | |
59 NonAtomicValue() : value_(0) {} | |
60 | |
61 explicit NonAtomicValue(T initial) | |
62 : value_(cast_helper<T>::to_storage_type(initial)) {} | |
63 | |
64 V8_INLINE T Value() const { | |
65 return cast_helper<T>::to_return_type(base::NoBarrier_Load(&value_)); | |
66 } | |
67 | |
68 V8_INLINE void SetValue(T new_value) { | |
69 base::NoBarrier_Store(&value_, cast_helper<T>::to_storage_type(new_value)); | |
70 } | |
71 | |
72 private: | |
73 STATIC_ASSERT(sizeof(T) <= sizeof(base::AtomicWord)); | |
74 | |
75 template <typename S> | |
76 struct cast_helper { | |
77 static base::AtomicWord to_storage_type(S value) { | |
78 return static_cast<base::AtomicWord>(value); | |
79 } | |
80 static S to_return_type(base::AtomicWord value) { | |
81 return static_cast<S>(value); | |
82 } | |
83 }; | |
84 | |
85 template <typename S> | |
86 struct cast_helper<S*> { | |
87 static base::AtomicWord to_storage_type(S* value) { | |
88 return reinterpret_cast<base::AtomicWord>(value); | |
89 } | |
90 static S* to_return_type(base::AtomicWord value) { | |
91 return reinterpret_cast<S*>(value); | |
92 } | |
93 }; | |
94 | |
95 base::AtomicWord value_; | |
96 }; | |
54 | 97 |
55 // Flag using T atomically. Also accepts void* as T. | 98 // Flag using T atomically. Also accepts void* as T. |
56 template <typename T> | 99 template <typename T> |
57 class AtomicValue { | 100 class AtomicValue { |
58 public: | 101 public: |
59 AtomicValue() : value_(0) {} | 102 AtomicValue() : value_(0) {} |
60 | 103 |
61 explicit AtomicValue(T initial) | 104 explicit AtomicValue(T initial) |
62 : value_(cast_helper<T>::to_storage_type(initial)) {} | 105 : value_(cast_helper<T>::to_storage_type(initial)) {} |
63 | 106 |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
191 return static_cast<base::AtomicWord>(1) << element; | 234 return static_cast<base::AtomicWord>(1) << element; |
192 } | 235 } |
193 | 236 |
194 base::AtomicWord bits_; | 237 base::AtomicWord bits_; |
195 }; | 238 }; |
196 | 239 |
197 } // namespace base | 240 } // namespace base |
198 } // namespace v8 | 241 } // namespace v8 |
199 | 242 |
200 #endif // #define V8_ATOMIC_UTILS_H_ | 243 #endif // #define V8_ATOMIC_UTILS_H_ |
OLD | NEW |