henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef RTC_BASE_ATOMIC_OPS_H_ |
| 12 | #define RTC_BASE_ATOMIC_OPS_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 14 | #if defined(WEBRTC_WIN) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 15 | // clang-format off |
| 16 | // clang formating would change include order. |
| 17 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 18 | // Include winsock2.h before including <windows.h> to maintain consistency with |
Niels Möller | b06b0a6 | 2018-05-25 10:05:34 +0200 | [diff] [blame] | 19 | // win32.h. To include win32.h directly, it must be broken out into its own |
| 20 | // build target. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 21 | #include <winsock2.h> |
| 22 | #include <windows.h> |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 23 | // clang-format on |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 24 | #endif // defined(WEBRTC_WIN) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 25 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 26 | namespace rtc { |
| 27 | class AtomicOps { |
| 28 | public: |
| 29 | #if defined(WEBRTC_WIN) |
| 30 | // Assumes sizeof(int) == sizeof(LONG), which it is on Win32 and Win64. |
| 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 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 37 | static int AcquireLoad(volatile const int* i) { return *i; } |
| 38 | static void ReleaseStore(volatile int* i, int value) { *i = value; } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 39 | static int CompareAndSwap(volatile int* i, int old_value, int new_value) { |
| 40 | return ::InterlockedCompareExchange(reinterpret_cast<volatile LONG*>(i), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 41 | new_value, old_value); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 42 | } |
| 43 | // Pointer variants. |
| 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 |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 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); } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 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 | // Pointer variants. |
| 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 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 77 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 78 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 79 | #endif // RTC_BASE_ATOMIC_OPS_H_ |