blob: 18a24a8e2e479ab1e819641c7288813ec8b78a84 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_ATOMIC_OPS_H_
12#define RTC_BASE_ATOMIC_OPS_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#if defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +020015// clang-format off
16// clang formating would change include order.
17
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020018// Include winsock2.h before including <windows.h> to maintain consistency with
Niels Möllerb06b0a62018-05-25 10:05:34 +020019// win32.h. To include win32.h directly, it must be broken out into its own
20// build target.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020021#include <winsock2.h>
22#include <windows.h>
Yves Gerey665174f2018-06-19 15:03:05 +020023// clang-format on
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020024#endif // defined(WEBRTC_WIN)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000025
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020026namespace rtc {
27class 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 Gerey665174f2018-06-19 15:03:05 +020037 static int AcquireLoad(volatile const int* i) { return *i; }
38 static void ReleaseStore(volatile int* i, int value) { *i = value; }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020039 static int CompareAndSwap(volatile int* i, int old_value, int new_value) {
40 return ::InterlockedCompareExchange(reinterpret_cast<volatile LONG*>(i),
Yves Gerey665174f2018-06-19 15:03:05 +020041 new_value, old_value);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020042 }
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 Gerey665174f2018-06-19 15:03:05 +020054 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 Kjellanderec78f1c2017-06-29 07:52:50 +020056 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 Gerey665174f2018-06-19 15:03:05 +020077} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000078
Steve Anton10542f22019-01-11 09:11:00 -080079#endif // RTC_BASE_ATOMIC_OPS_H_