blob: a286bf01cc8c0f4d7516c50a0ecb9dda5fc84d39 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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ömff019b02015-04-30 12:16:0714#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.orgf0488722014-05-13 18:00:2622
23namespace rtc {
Peter Boströmff019b02015-04-30 12:16:0724class AtomicOps {
henrike@webrtc.orgf0488722014-05-13 18:00:2625 public:
Peter Boströmff019b02015-04-30 12:16:0726#if defined(WEBRTC_WIN)
27 // Assumes sizeof(int) == sizeof(LONG), which it is on Win32 and Win64.
28 static int Increment(volatile int* i) {
29 return ::InterlockedIncrement(reinterpret_cast<volatile LONG*>(i));
henrike@webrtc.orgf0488722014-05-13 18:00:2630 }
Peter Boströmff019b02015-04-30 12:16:0731 static int Decrement(volatile int* i) {
32 return ::InterlockedDecrement(reinterpret_cast<volatile LONG*>(i));
henrike@webrtc.orgf0488722014-05-13 18:00:2633 }
pbos235c35f2015-07-22 15:34:5934 static int AcquireLoad(volatile const int* i) {
Peter Boströmff019b02015-04-30 12:16:0735 return *i;
henrike@webrtc.orgf0488722014-05-13 18:00:2636 }
pbos235c35f2015-07-22 15:34:5937 static void ReleaseStore(volatile int* i, int value) {
Peter Boströmff019b02015-04-30 12:16:0738 *i = value;
henrike@webrtc.orgf0488722014-05-13 18:00:2639 }
Peter Boströmff019b02015-04-30 12:16:0740 static int CompareAndSwap(volatile int* i, int old_value, int new_value) {
41 return ::InterlockedCompareExchange(reinterpret_cast<volatile LONG*>(i),
42 new_value,
43 old_value);
44 }
Peter Boström6f28cf02015-12-07 22:17:1545 // Pointer variants.
46 template <typename T>
Peter Boström455a2522015-12-18 16:00:2547 static T* AcquireLoadPtr(T* volatile* ptr) {
Peter Boström6f28cf02015-12-07 22:17:1548 return *ptr;
49 }
50 template <typename T>
51 static T* CompareAndSwapPtr(T* volatile* ptr, T* old_value, T* new_value) {
52 return static_cast<T*>(::InterlockedCompareExchangePointer(
Peter Boström455a2522015-12-18 16:00:2553 reinterpret_cast<PVOID volatile*>(ptr), new_value, old_value));
Peter Boström6f28cf02015-12-07 22:17:1554 }
Peter Boströmff019b02015-04-30 12:16:0755#else
56 static int Increment(volatile int* i) {
57 return __sync_add_and_fetch(i, 1);
58 }
59 static int Decrement(volatile int* i) {
60 return __sync_sub_and_fetch(i, 1);
61 }
pbos235c35f2015-07-22 15:34:5962 static int AcquireLoad(volatile const int* i) {
63 return __atomic_load_n(i, __ATOMIC_ACQUIRE);
Peter Boströmff019b02015-04-30 12:16:0764 }
pbos235c35f2015-07-22 15:34:5965 static void ReleaseStore(volatile int* i, int value) {
66 __atomic_store_n(i, value, __ATOMIC_RELEASE);
Peter Boströmff019b02015-04-30 12:16:0767 }
68 static int CompareAndSwap(volatile int* i, int old_value, int new_value) {
69 return __sync_val_compare_and_swap(i, old_value, new_value);
70 }
Peter Boström6f28cf02015-12-07 22:17:1571 // Pointer variants.
72 template <typename T>
Peter Boström455a2522015-12-18 16:00:2573 static T* AcquireLoadPtr(T* volatile* ptr) {
Peter Boström6f28cf02015-12-07 22:17:1574 return __atomic_load_n(ptr, __ATOMIC_ACQUIRE);
75 }
76 template <typename T>
77 static T* CompareAndSwapPtr(T* volatile* ptr, T* old_value, T* new_value) {
78 return __sync_val_compare_and_swap(ptr, old_value, new_value);
79 }
Peter Boströmff019b02015-04-30 12:16:0780#endif
henrike@webrtc.orgf0488722014-05-13 18:00:2681};
82
Peter Boströmff019b02015-04-30 12:16:0783
84
henrike@webrtc.orgf0488722014-05-13 18:00:2685}
86
87#endif // WEBRTC_BASE_ATOMICOPS_H_