2018-10-03 14:34:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 The WebRTC Project Authors. All rights reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
*/
|
|
|
|
|
2019-02-06 18:22:38 +00:00
|
|
|
#ifndef RTC_BASE_ATOMICOPS_H_
|
|
|
|
#define RTC_BASE_ATOMICOPS_H_
|
2018-10-03 14:34:43 +00:00
|
|
|
|
|
|
|
#if defined(WEBRTC_WIN)
|
2019-02-06 18:22:38 +00:00
|
|
|
// clang-format off
|
|
|
|
// clang formating would change include order.
|
|
|
|
|
2018-10-03 14:34:43 +00:00
|
|
|
// Include winsock2.h before including <windows.h> to maintain consistency with
|
2019-02-06 18:22:38 +00:00
|
|
|
// win32.h. To include win32.h directly, it must be broken out into its own
|
|
|
|
// build target.
|
2018-10-03 14:34:43 +00:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <windows.h>
|
2019-02-06 18:22:38 +00:00
|
|
|
// clang-format on
|
2018-10-03 14:34:43 +00:00
|
|
|
#endif // defined(WEBRTC_WIN)
|
|
|
|
|
|
|
|
namespace rtc {
|
|
|
|
class AtomicOps {
|
|
|
|
public:
|
|
|
|
#if defined(WEBRTC_WIN)
|
|
|
|
// Assumes sizeof(int) == sizeof(LONG), which it is on Win32 and Win64.
|
|
|
|
static int Increment(volatile int* i) {
|
|
|
|
return ::InterlockedIncrement(reinterpret_cast<volatile LONG*>(i));
|
|
|
|
}
|
|
|
|
static int Decrement(volatile int* i) {
|
|
|
|
return ::InterlockedDecrement(reinterpret_cast<volatile LONG*>(i));
|
|
|
|
}
|
2019-02-06 18:22:38 +00:00
|
|
|
static int AcquireLoad(volatile const int* i) { return *i; }
|
|
|
|
static void ReleaseStore(volatile int* i, int value) { *i = value; }
|
2018-10-03 14:34:43 +00:00
|
|
|
static int CompareAndSwap(volatile int* i, int old_value, int new_value) {
|
|
|
|
return ::InterlockedCompareExchange(reinterpret_cast<volatile LONG*>(i),
|
2019-02-06 18:22:38 +00:00
|
|
|
new_value, old_value);
|
2018-10-03 14:34:43 +00:00
|
|
|
}
|
|
|
|
// Pointer variants.
|
|
|
|
template <typename T>
|
|
|
|
static T* AcquireLoadPtr(T* volatile* ptr) {
|
|
|
|
return *ptr;
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
static T* CompareAndSwapPtr(T* volatile* ptr, T* old_value, T* new_value) {
|
|
|
|
return static_cast<T*>(::InterlockedCompareExchangePointer(
|
|
|
|
reinterpret_cast<PVOID volatile*>(ptr), new_value, old_value));
|
|
|
|
}
|
|
|
|
#else
|
2019-02-06 18:22:38 +00:00
|
|
|
static int Increment(volatile int* i) { return __sync_add_and_fetch(i, 1); }
|
|
|
|
static int Decrement(volatile int* i) { return __sync_sub_and_fetch(i, 1); }
|
2018-10-03 14:34:43 +00:00
|
|
|
static int AcquireLoad(volatile const int* i) {
|
|
|
|
return __atomic_load_n(i, __ATOMIC_ACQUIRE);
|
|
|
|
}
|
|
|
|
static void ReleaseStore(volatile int* i, int value) {
|
|
|
|
__atomic_store_n(i, value, __ATOMIC_RELEASE);
|
|
|
|
}
|
|
|
|
static int CompareAndSwap(volatile int* i, int old_value, int new_value) {
|
|
|
|
return __sync_val_compare_and_swap(i, old_value, new_value);
|
|
|
|
}
|
|
|
|
// Pointer variants.
|
|
|
|
template <typename T>
|
|
|
|
static T* AcquireLoadPtr(T* volatile* ptr) {
|
|
|
|
return __atomic_load_n(ptr, __ATOMIC_ACQUIRE);
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
static T* CompareAndSwapPtr(T* volatile* ptr, T* old_value, T* new_value) {
|
|
|
|
return __sync_val_compare_and_swap(ptr, old_value, new_value);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2019-02-06 18:22:38 +00:00
|
|
|
} // namespace rtc
|
2018-10-03 14:34:43 +00:00
|
|
|
|
2019-02-06 18:22:38 +00:00
|
|
|
#endif // RTC_BASE_ATOMICOPS_H_
|