OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 25 matching lines...) Expand all Loading... |
36 return *i; | 36 return *i; |
37 } | 37 } |
38 static void ReleaseStore(volatile int* i, int value) { | 38 static void ReleaseStore(volatile int* i, int value) { |
39 *i = value; | 39 *i = value; |
40 } | 40 } |
41 static int CompareAndSwap(volatile int* i, int old_value, int new_value) { | 41 static int CompareAndSwap(volatile int* i, int old_value, int new_value) { |
42 return ::InterlockedCompareExchange(reinterpret_cast<volatile LONG*>(i), | 42 return ::InterlockedCompareExchange(reinterpret_cast<volatile LONG*>(i), |
43 new_value, | 43 new_value, |
44 old_value); | 44 old_value); |
45 } | 45 } |
| 46 // Pointer variants. |
| 47 template <typename T> |
| 48 static T* AtomicLoadPtr(T* volatile* ptr) { |
| 49 return *ptr; |
| 50 } |
| 51 template <typename T> |
| 52 static T* CompareAndSwapPtr(T* volatile* ptr, T* old_value, T* new_value) { |
| 53 return static_cast<T*>(::InterlockedCompareExchangePointer( |
| 54 reinterpret_cast<PVOID volatile*>(ptr), old_value, new_value)); |
| 55 } |
46 #else | 56 #else |
47 static int Increment(volatile int* i) { | 57 static int Increment(volatile int* i) { |
48 return __sync_add_and_fetch(i, 1); | 58 return __sync_add_and_fetch(i, 1); |
49 } | 59 } |
50 static int Decrement(volatile int* i) { | 60 static int Decrement(volatile int* i) { |
51 return __sync_sub_and_fetch(i, 1); | 61 return __sync_sub_and_fetch(i, 1); |
52 } | 62 } |
53 static int AcquireLoad(volatile const int* i) { | 63 static int AcquireLoad(volatile const int* i) { |
54 return __atomic_load_n(i, __ATOMIC_ACQUIRE); | 64 return __atomic_load_n(i, __ATOMIC_ACQUIRE); |
55 } | 65 } |
56 static void ReleaseStore(volatile int* i, int value) { | 66 static void ReleaseStore(volatile int* i, int value) { |
57 __atomic_store_n(i, value, __ATOMIC_RELEASE); | 67 __atomic_store_n(i, value, __ATOMIC_RELEASE); |
58 } | 68 } |
59 static int CompareAndSwap(volatile int* i, int old_value, int new_value) { | 69 static int CompareAndSwap(volatile int* i, int old_value, int new_value) { |
60 return __sync_val_compare_and_swap(i, old_value, new_value); | 70 return __sync_val_compare_and_swap(i, old_value, new_value); |
61 } | 71 } |
| 72 // Pointer variants. |
| 73 template <typename T> |
| 74 static T* AtomicLoadPtr(T* volatile* ptr) { |
| 75 return __atomic_load_n(ptr, __ATOMIC_ACQUIRE); |
| 76 } |
| 77 template <typename T> |
| 78 static T* CompareAndSwapPtr(T* volatile* ptr, T* old_value, T* new_value) { |
| 79 return __sync_val_compare_and_swap(ptr, old_value, new_value); |
| 80 } |
62 #endif | 81 #endif |
63 }; | 82 }; |
64 | 83 |
65 // POD struct version of AtomicOps, prevents accidental non-atomic operator | 84 // POD struct version of AtomicOps, prevents accidental non-atomic operator |
66 // usage (such as ++, -- or =). Functions are static, so that the AtomicInt:: | 85 // usage (such as ++, -- or =). Functions are static, so that the AtomicInt:: |
67 // prefix must be present in the code, clearly labeling the operations as | 86 // prefix must be present in the code, clearly labeling the operations as |
68 // atomic. | 87 // atomic. |
69 // Do not copy-initialize, since that performs non-atomic reads of value_. The | 88 // Do not copy-initialize, since that performs non-atomic reads of value_. The |
70 // copy constructor needs to be present for brace initialization. | 89 // copy constructor needs to be present for brace initialization. |
71 struct AtomicInt { | 90 struct AtomicInt { |
(...skipping 30 matching lines...) Expand all Loading... |
102 // Attempts to compare-and-swaps |old_value| for |new_value| in |i| , returns | 121 // Attempts to compare-and-swaps |old_value| for |new_value| in |i| , returns |
103 // |i|'s initial value. If equal to |old_value|, then the CAS succeeded, | 122 // |i|'s initial value. If equal to |old_value|, then the CAS succeeded, |
104 // otherwise no operation is performed. | 123 // otherwise no operation is performed. |
105 static int CompareAndSwap(AtomicInt* i, int old_value, int new_value) { | 124 static int CompareAndSwap(AtomicInt* i, int old_value, int new_value) { |
106 return AtomicOps::CompareAndSwap(&i->value_, old_value, new_value); | 125 return AtomicOps::CompareAndSwap(&i->value_, old_value, new_value); |
107 } | 126 } |
108 }; | 127 }; |
109 } | 128 } |
110 | 129 |
111 #endif // WEBRTC_BASE_ATOMICOPS_H_ | 130 #endif // WEBRTC_BASE_ATOMICOPS_H_ |
OLD | NEW |