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 word. Be careful with |
| 55 // data races. |
| 56 template <typename T> |
| 57 class NoBarrierAtomicValue { |
| 58 public: |
| 59 NoBarrierAtomicValue() : value_(0) {} |
| 60 |
| 61 explicit NoBarrierAtomicValue(T initial) |
| 62 : value_(cast_helper<T>::to_storage_type(initial)) {} |
| 63 |
| 64 static NoBarrierAtomicValue* FromAddress(void* address) { |
| 65 return reinterpret_cast<base::NoBarrierAtomicValue<T>*>(address); |
| 66 } |
| 67 |
| 68 V8_INLINE T Value() const { |
| 69 return cast_helper<T>::to_return_type(base::NoBarrier_Load(&value_)); |
| 70 } |
| 71 |
| 72 V8_INLINE void SetValue(T new_value) { |
| 73 base::NoBarrier_Store(&value_, cast_helper<T>::to_storage_type(new_value)); |
| 74 } |
| 75 |
| 76 private: |
| 77 STATIC_ASSERT(sizeof(T) <= sizeof(base::AtomicWord)); |
| 78 |
| 79 template <typename S> |
| 80 struct cast_helper { |
| 81 static base::AtomicWord to_storage_type(S value) { |
| 82 return static_cast<base::AtomicWord>(value); |
| 83 } |
| 84 static S to_return_type(base::AtomicWord value) { |
| 85 return static_cast<S>(value); |
| 86 } |
| 87 }; |
| 88 |
| 89 template <typename S> |
| 90 struct cast_helper<S*> { |
| 91 static base::AtomicWord to_storage_type(S* value) { |
| 92 return reinterpret_cast<base::AtomicWord>(value); |
| 93 } |
| 94 static S* to_return_type(base::AtomicWord value) { |
| 95 return reinterpret_cast<S*>(value); |
| 96 } |
| 97 }; |
| 98 |
| 99 base::AtomicWord value_; |
| 100 }; |
54 | 101 |
55 // Flag using T atomically. Also accepts void* as T. | 102 // Flag using T atomically. Also accepts void* as T. |
56 template <typename T> | 103 template <typename T> |
57 class AtomicValue { | 104 class AtomicValue { |
58 public: | 105 public: |
59 AtomicValue() : value_(0) {} | 106 AtomicValue() : value_(0) {} |
60 | 107 |
61 explicit AtomicValue(T initial) | 108 explicit AtomicValue(T initial) |
62 : value_(cast_helper<T>::to_storage_type(initial)) {} | 109 : value_(cast_helper<T>::to_storage_type(initial)) {} |
63 | 110 |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 return static_cast<base::AtomicWord>(1) << element; | 238 return static_cast<base::AtomicWord>(1) << element; |
192 } | 239 } |
193 | 240 |
194 base::AtomicWord bits_; | 241 base::AtomicWord bits_; |
195 }; | 242 }; |
196 | 243 |
197 } // namespace base | 244 } // namespace base |
198 } // namespace v8 | 245 } // namespace v8 |
199 | 246 |
200 #endif // #define V8_ATOMIC_UTILS_H_ | 247 #endif // #define V8_ATOMIC_UTILS_H_ |
OLD | NEW |