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 | |
| 11 | #ifndef WEBRTC_BASE_ATOMICOPS_H_ |
| 12 | #define WEBRTC_BASE_ATOMICOPS_H_ |
| 13 | |
Peter Boström | ff019b0 | 2015-04-30 14:16:07 +0200 | [diff] [blame] | 14 | #if defined(WEBRTC_WIN) |
| 15 | // Include winsock2.h before including <windows.h> to maintain consistency with |
| 16 | // win32.h. We can't include win32.h directly here since it pulls in |
| 17 | // headers such as basictypes.h which causes problems in Chromium where webrtc |
| 18 | // exists as two separate projects, webrtc and libjingle. |
| 19 | #include <winsock2.h> |
| 20 | #include <windows.h> |
| 21 | #endif // defined(WEBRTC_WIN) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 22 | |
| 23 | namespace rtc { |
Peter Boström | 84f0970 | 2015-12-07 23:07:01 +0100 | [diff] [blame] | 24 | |
Peter Boström | ff019b0 | 2015-04-30 14:16:07 +0200 | [diff] [blame] | 25 | class AtomicOps { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 26 | public: |
Peter Boström | ff019b0 | 2015-04-30 14:16:07 +0200 | [diff] [blame] | 27 | #if defined(WEBRTC_WIN) |
| 28 | // Assumes sizeof(int) == sizeof(LONG), which it is on Win32 and Win64. |
| 29 | static int Increment(volatile int* i) { |
| 30 | return ::InterlockedIncrement(reinterpret_cast<volatile LONG*>(i)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 31 | } |
Peter Boström | ff019b0 | 2015-04-30 14:16:07 +0200 | [diff] [blame] | 32 | static int Decrement(volatile int* i) { |
| 33 | return ::InterlockedDecrement(reinterpret_cast<volatile LONG*>(i)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 34 | } |
pbos | 235c35f | 2015-07-22 08:34:59 -0700 | [diff] [blame] | 35 | static int AcquireLoad(volatile const int* i) { |
Peter Boström | ff019b0 | 2015-04-30 14:16:07 +0200 | [diff] [blame] | 36 | return *i; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 37 | } |
pbos | 235c35f | 2015-07-22 08:34:59 -0700 | [diff] [blame] | 38 | static void ReleaseStore(volatile int* i, int value) { |
Peter Boström | ff019b0 | 2015-04-30 14:16:07 +0200 | [diff] [blame] | 39 | *i = value; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 40 | } |
Peter Boström | ff019b0 | 2015-04-30 14:16:07 +0200 | [diff] [blame] | 41 | static int CompareAndSwap(volatile int* i, int old_value, int new_value) { |
| 42 | return ::InterlockedCompareExchange(reinterpret_cast<volatile LONG*>(i), |
| 43 | new_value, |
| 44 | old_value); |
| 45 | } |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame^] | 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 | } |
Peter Boström | ff019b0 | 2015-04-30 14:16:07 +0200 | [diff] [blame] | 56 | #else |
| 57 | static int Increment(volatile int* i) { |
| 58 | return __sync_add_and_fetch(i, 1); |
| 59 | } |
| 60 | static int Decrement(volatile int* i) { |
| 61 | return __sync_sub_and_fetch(i, 1); |
| 62 | } |
pbos | 235c35f | 2015-07-22 08:34:59 -0700 | [diff] [blame] | 63 | static int AcquireLoad(volatile const int* i) { |
| 64 | return __atomic_load_n(i, __ATOMIC_ACQUIRE); |
Peter Boström | ff019b0 | 2015-04-30 14:16:07 +0200 | [diff] [blame] | 65 | } |
pbos | 235c35f | 2015-07-22 08:34:59 -0700 | [diff] [blame] | 66 | static void ReleaseStore(volatile int* i, int value) { |
| 67 | __atomic_store_n(i, value, __ATOMIC_RELEASE); |
Peter Boström | ff019b0 | 2015-04-30 14:16:07 +0200 | [diff] [blame] | 68 | } |
| 69 | static int CompareAndSwap(volatile int* i, int old_value, int new_value) { |
| 70 | return __sync_val_compare_and_swap(i, old_value, new_value); |
| 71 | } |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame^] | 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 | } |
Peter Boström | ff019b0 | 2015-04-30 14:16:07 +0200 | [diff] [blame] | 81 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
Peter Boström | 84f0970 | 2015-12-07 23:07:01 +0100 | [diff] [blame] | 84 | // POD struct version of AtomicOps, prevents accidental non-atomic operator |
| 85 | // usage (such as ++, -- or =). Functions are static, so that the AtomicInt:: |
| 86 | // prefix must be present in the code, clearly labeling the operations as |
| 87 | // atomic. |
| 88 | // Do not copy-initialize, since that performs non-atomic reads of value_. The |
| 89 | // copy constructor needs to be present for brace initialization. |
| 90 | struct AtomicInt { |
| 91 | AtomicInt() = delete; |
| 92 | // TODO(pbos): When MSVC allows brace initialization (or we move to |
| 93 | // std::atomic), remove copy constructor (or have it implicitly removed by |
| 94 | // std::atomic). |
| 95 | void operator=(const AtomicInt&) = delete; |
Peter Boström | ff019b0 | 2015-04-30 14:16:07 +0200 | [diff] [blame] | 96 | |
Peter Boström | 84f0970 | 2015-12-07 23:07:01 +0100 | [diff] [blame] | 97 | // value_ is public to permit brace initialization. Should not be accessed |
| 98 | // directly. |
| 99 | volatile int value_; |
Peter Boström | ff019b0 | 2015-04-30 14:16:07 +0200 | [diff] [blame] | 100 | |
Peter Boström | 84f0970 | 2015-12-07 23:07:01 +0100 | [diff] [blame] | 101 | // Atomically increments |i|, returns the resulting incremented value. |
| 102 | static int Increment(AtomicInt* i) { |
| 103 | return AtomicOps::Increment(&i->value_); |
| 104 | } |
| 105 | |
| 106 | // Atomically decrements |i|, returns the resulting decremented value. |
| 107 | static int Decrement(AtomicInt* i) { |
| 108 | return AtomicOps::Decrement(&i->value_); |
| 109 | } |
| 110 | |
| 111 | // Atomically loads |i|. |
| 112 | static int AcquireLoad(const AtomicInt* i) { |
| 113 | return AtomicOps::AcquireLoad(&i->value_); |
| 114 | } |
| 115 | |
| 116 | // Atomically stores |value| in |i|. |
| 117 | static void ReleaseStore(AtomicInt* i, int value) { |
| 118 | AtomicOps::ReleaseStore(&i->value_, value); |
| 119 | } |
| 120 | |
| 121 | // Attempts to compare-and-swaps |old_value| for |new_value| in |i| , returns |
| 122 | // |i|'s initial value. If equal to |old_value|, then the CAS succeeded, |
| 123 | // otherwise no operation is performed. |
| 124 | static int CompareAndSwap(AtomicInt* i, int old_value, int new_value) { |
| 125 | return AtomicOps::CompareAndSwap(&i->value_, old_value, new_value); |
| 126 | } |
| 127 | }; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | #endif // WEBRTC_BASE_ATOMICOPS_H_ |