| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#ifndef RTC_BASE_ATOMIC_OPS_H_ |
| 12 |
#define RTC_BASE_ATOMIC_OPS_H_ |
| 13 |
|
| 14 |
#if defined(WEBRTC_WIN) |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include <winsock2.h> |
| 22 |
#include <windows.h> |
| 23 |
|
| 24 |
#endif |
| 25 |
|
| 26 |
namespace rtc { |
| 27 |
class AtomicOps { |
| 28 |
public: |
| 29 |
#if defined(WEBRTC_WIN) |
| 30 |
|
| 31 |
static int Increment(volatile int* i) { |
| 32 |
return ::InterlockedIncrement(reinterpret_cast<volatile LONG*>(i)); |
| 33 |
} |
| 34 |
static int Decrement(volatile int* i) { |
| 35 |
return ::InterlockedDecrement(reinterpret_cast<volatile LONG*>(i)); |
| 36 |
} |
| 37 |
static int AcquireLoad(volatile const int* i) { return *i; } |
| 38 |
static void ReleaseStore(volatile int* i, int value) { *i = value; } |
| 39 |
static int CompareAndSwap(volatile int* i, int old_value, int new_value) { |
| 40 |
return ::InterlockedCompareExchange(reinterpret_cast<volatile LONG*>(i), |
| 41 |
new_value, old_value); |
| 42 |
} |
| 43 |
|
| 44 |
template <typename T> |
| 45 |
static T* AcquireLoadPtr(T* volatile* ptr) { |
| 46 |
return *ptr; |
| 47 |
} |
| 48 |
template <typename T> |
| 49 |
static T* CompareAndSwapPtr(T* volatile* ptr, T* old_value, T* new_value) { |
| 50 |
return static_cast<T*>(::InterlockedCompareExchangePointer( |
| 51 |
reinterpret_cast<PVOID volatile*>(ptr), new_value, old_value)); |
| 52 |
} |
| 53 |
#else |
| 54 |
static int Increment(volatile int* i) { return __sync_add_and_fetch(i, 1); } |
| 55 |
static int Decrement(volatile int* i) { return __sync_sub_and_fetch(i, 1); } |
| 56 |
static int AcquireLoad(volatile const int* i) { |
| 57 |
return __atomic_load_n(i, __ATOMIC_ACQUIRE); |
| 58 |
} |
| 59 |
static void ReleaseStore(volatile int* i, int value) { |
| 60 |
__atomic_store_n(i, value, __ATOMIC_RELEASE); |
| 61 |
} |
| 62 |
static int CompareAndSwap(volatile int* i, int old_value, int new_value) { |
| 63 |
return __sync_val_compare_and_swap(i, old_value, new_value); |
| 64 |
} |
| 65 |
|
| 66 |
template <typename T> |
| 67 |
static T* AcquireLoadPtr(T* volatile* ptr) { |
| 68 |
return __atomic_load_n(ptr, __ATOMIC_ACQUIRE); |
| 69 |
} |
| 70 |
template <typename T> |
| 71 |
static T* CompareAndSwapPtr(T* volatile* ptr, T* old_value, T* new_value) { |
| 72 |
return __sync_val_compare_and_swap(ptr, old_value, new_value); |
| 73 |
} |
| 74 |
#endif |
| 75 |
}; |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
#endif |