OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_BASE_ATOMICOPS_H_ | 11 #ifndef WEBRTC_BASE_ATOMICOPS_H_ |
12 #define WEBRTC_BASE_ATOMICOPS_H_ | 12 #define WEBRTC_BASE_ATOMICOPS_H_ |
13 | 13 |
14 #if defined(WEBRTC_WIN) | 14 #if defined(WEBRTC_WIN) |
15 // Include winsock2.h before including <windows.h> to maintain consistency with | 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 | 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 | 17 // headers such as basictypes.h which causes problems in Chromium where webrtc |
18 // exists as two separate projects, webrtc and libjingle. | 18 // exists as two separate projects, webrtc and libjingle. |
19 #include <winsock2.h> | 19 #include <winsock2.h> |
20 #include <windows.h> | 20 #include <windows.h> |
21 #endif // defined(WEBRTC_WIN) | 21 #endif // defined(WEBRTC_WIN) |
22 #if defined(THREAD_SANITIZER) | |
23 #include <sanitizer/tsan_interface_atomic.h> | |
24 #include <stdint.h> | |
25 #endif // defined(THREAD_SANITIZER) | |
22 | 26 |
23 namespace rtc { | 27 namespace rtc { |
24 class AtomicOps { | 28 class AtomicOps { |
25 public: | 29 public: |
26 #if defined(WEBRTC_WIN) | 30 #if defined(WEBRTC_WIN) |
27 // Assumes sizeof(int) == sizeof(LONG), which it is on Win32 and Win64. | 31 // Assumes sizeof(int) == sizeof(LONG), which it is on Win32 and Win64. |
28 static int Increment(volatile int* i) { | 32 static int Increment(volatile int* i) { |
29 return ::InterlockedIncrement(reinterpret_cast<volatile LONG*>(i)); | 33 return ::InterlockedIncrement(reinterpret_cast<volatile LONG*>(i)); |
30 } | 34 } |
31 static int Decrement(volatile int* i) { | 35 static int Decrement(volatile int* i) { |
32 return ::InterlockedDecrement(reinterpret_cast<volatile LONG*>(i)); | 36 return ::InterlockedDecrement(reinterpret_cast<volatile LONG*>(i)); |
33 } | 37 } |
34 static int Load(volatile const int* i) { | 38 static int AcquireLoad(volatile const int* i) { |
35 return *i; | 39 return *i; |
36 } | 40 } |
37 static void Store(volatile int* i, int value) { | 41 static void ReleaseStore(volatile int* i, int value) { |
38 *i = value; | 42 *i = value; |
39 } | 43 } |
40 static int CompareAndSwap(volatile int* i, int old_value, int new_value) { | 44 static int CompareAndSwap(volatile int* i, int old_value, int new_value) { |
41 return ::InterlockedCompareExchange(reinterpret_cast<volatile LONG*>(i), | 45 return ::InterlockedCompareExchange(reinterpret_cast<volatile LONG*>(i), |
42 new_value, | 46 new_value, |
43 old_value); | 47 old_value); |
44 } | 48 } |
45 #else | 49 #else |
46 static int Increment(volatile int* i) { | 50 static int Increment(volatile int* i) { |
47 return __sync_add_and_fetch(i, 1); | 51 return __sync_add_and_fetch(i, 1); |
48 } | 52 } |
49 static int Decrement(volatile int* i) { | 53 static int Decrement(volatile int* i) { |
50 return __sync_sub_and_fetch(i, 1); | 54 return __sync_sub_and_fetch(i, 1); |
51 } | 55 } |
52 static int Load(volatile const int* i) { | 56 #if defined(THREAD_SANITIZER) |
53 // Adding 0 is a no-op, so const_cast is fine. | 57 // TSan doesn't understand the __sync_synchronize barrier used below. These |
54 return __sync_add_and_fetch(const_cast<volatile int*>(i), 0); | 58 // versions are supplied to prevent false-positive data races. |
59 static int32_t AcquireLoad(volatile const int32_t* i) { | |
Alexander Potapenko
2015/07/22 12:57:37
It's better for TSan and non-TSan versions of thes
pbos-webrtc
2015/07/22 13:04:40
I need the compiler to pick 32/64 bit depending on
Alexander Potapenko
2015/07/22 13:10:08
Yeah, I was saying the latter. Or you can explicit
| |
60 return __tsan_atomic32_load(i, __tsan_memory_order_acquire); | |
55 } | 61 } |
56 static void Store(volatile int* i, int value) { | 62 static int64_t AcquireLoad(volatile const int64_t* i) { |
63 return __tsan_atomic64_load(i, __tsan_memory_order_acquire); | |
64 } | |
65 static void ReleaseStore(volatile int32_t* i, int32_t value) { | |
66 __tsan_atomic32_store(i, value, __tsan_memory_order_release); | |
67 } | |
68 static void ReleaseStore(volatile int64_t* i, int64_t value) { | |
69 __tsan_atomic64_store(i, value, __tsan_memory_order_release); | |
70 } | |
71 #else | |
72 static int AcquireLoad(volatile const int* i) { | |
73 int value = *i; | |
74 __sync_synchronize(); | |
75 return value; | |
76 } | |
77 static void ReleaseStore(volatile int* i, int value) { | |
57 __sync_synchronize(); | 78 __sync_synchronize(); |
58 *i = value; | 79 *i = value; |
59 } | 80 } |
81 #endif | |
60 static int CompareAndSwap(volatile int* i, int old_value, int new_value) { | 82 static int CompareAndSwap(volatile int* i, int old_value, int new_value) { |
61 return __sync_val_compare_and_swap(i, old_value, new_value); | 83 return __sync_val_compare_and_swap(i, old_value, new_value); |
62 } | 84 } |
63 #endif | 85 #endif |
64 }; | 86 }; |
65 | 87 |
66 | 88 |
67 | 89 |
68 } | 90 } |
69 | 91 |
70 #endif // WEBRTC_BASE_ATOMICOPS_H_ | 92 #endif // WEBRTC_BASE_ATOMICOPS_H_ |
OLD | NEW |