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 | ff019b0 | 2015-04-30 14:16:07 +0200 | [diff] [blame] | 24 | class AtomicOps { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 25 | public: |
Peter Boström | ff019b0 | 2015-04-30 14:16:07 +0200 | [diff] [blame] | 26 | #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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 30 | } |
Peter Boström | ff019b0 | 2015-04-30 14:16:07 +0200 | [diff] [blame] | 31 | static int Decrement(volatile int* i) { |
| 32 | return ::InterlockedDecrement(reinterpret_cast<volatile LONG*>(i)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 33 | } |
Peter Boström | ff019b0 | 2015-04-30 14:16:07 +0200 | [diff] [blame] | 34 | static int Load(volatile const int* i) { |
| 35 | return *i; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 36 | } |
Peter Boström | ff019b0 | 2015-04-30 14:16:07 +0200 | [diff] [blame] | 37 | static void Store(volatile int* i, int value) { |
| 38 | *i = value; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 39 | } |
Peter Boström | ff019b0 | 2015-04-30 14:16:07 +0200 | [diff] [blame] | 40 | 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 | } |
| 45 | #else |
| 46 | static int Increment(volatile int* i) { |
| 47 | return __sync_add_and_fetch(i, 1); |
| 48 | } |
| 49 | static int Decrement(volatile int* i) { |
| 50 | return __sync_sub_and_fetch(i, 1); |
| 51 | } |
| 52 | static int Load(volatile const int* i) { |
| 53 | // Adding 0 is a no-op, so const_cast is fine. |
| 54 | return __sync_add_and_fetch(const_cast<volatile int*>(i), 0); |
| 55 | } |
| 56 | static void Store(volatile int* i, int value) { |
| 57 | __sync_synchronize(); |
| 58 | *i = value; |
| 59 | } |
| 60 | static int CompareAndSwap(volatile int* i, int old_value, int new_value) { |
| 61 | return __sync_val_compare_and_swap(i, old_value, new_value); |
| 62 | } |
| 63 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
Peter Boström | ff019b0 | 2015-04-30 14:16:07 +0200 | [diff] [blame] | 66 | |
| 67 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | #endif // WEBRTC_BASE_ATOMICOPS_H_ |