• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 RTC_BASE_ATOMIC_OPS_H_
12 #define RTC_BASE_ATOMIC_OPS_H_
13 
14 #if defined(WEBRTC_WIN)
15 // clang-format off
16 // clang formating would change include order.
17 
18 // Include winsock2.h before including <windows.h> to maintain consistency with
19 // win32.h. To include win32.h directly, it must be broken out into its own
20 // build target.
21 #include <winsock2.h>
22 #include <windows.h>
23 // clang-format on
24 #endif  // defined(WEBRTC_WIN)
25 
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.
Increment(volatile int * i)31   static int Increment(volatile int* i) {
32     return ::InterlockedIncrement(reinterpret_cast<volatile LONG*>(i));
33   }
Decrement(volatile int * i)34   static int Decrement(volatile int* i) {
35     return ::InterlockedDecrement(reinterpret_cast<volatile LONG*>(i));
36   }
AcquireLoad(volatile const int * i)37   static int AcquireLoad(volatile const int* i) { return *i; }
ReleaseStore(volatile int * i,int value)38   static void ReleaseStore(volatile int* i, int value) { *i = value; }
CompareAndSwap(volatile int * i,int old_value,int new_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   // Pointer variants.
44   template <typename T>
AcquireLoadPtr(T * volatile * ptr)45   static T* AcquireLoadPtr(T* volatile* ptr) {
46     return *ptr;
47   }
48   template <typename T>
CompareAndSwapPtr(T * volatile * ptr,T * old_value,T * new_value)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   // 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 
77 }  // namespace rtc
78 
79 #endif  // RTC_BASE_ATOMIC_OPS_H_
80