Chromium Code Reviews| 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 #include "webrtc/base/constructormagic.h" | |
| 15 | |
| 14 #if defined(WEBRTC_WIN) | 16 #if defined(WEBRTC_WIN) |
| 15 // Include winsock2.h before including <windows.h> to maintain consistency with | 17 // 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 | 18 // 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 | 19 // headers such as basictypes.h which causes problems in Chromium where webrtc |
| 18 // exists as two separate projects, webrtc and libjingle. | 20 // exists as two separate projects, webrtc and libjingle. |
| 19 #include <winsock2.h> | 21 #include <winsock2.h> |
| 20 #include <windows.h> | 22 #include <windows.h> |
| 21 #endif // defined(WEBRTC_WIN) | 23 #endif // defined(WEBRTC_WIN) |
| 22 | 24 |
| 23 namespace rtc { | 25 namespace rtc { |
| 26 | |
| 24 class AtomicOps { | 27 class AtomicOps { |
| 25 public: | 28 public: |
| 26 #if defined(WEBRTC_WIN) | 29 #if defined(WEBRTC_WIN) |
| 27 // Assumes sizeof(int) == sizeof(LONG), which it is on Win32 and Win64. | 30 // Assumes sizeof(int) == sizeof(LONG), which it is on Win32 and Win64. |
| 28 static int Increment(volatile int* i) { | 31 static int Increment(volatile int* i) { |
| 29 return ::InterlockedIncrement(reinterpret_cast<volatile LONG*>(i)); | 32 return ::InterlockedIncrement(reinterpret_cast<volatile LONG*>(i)); |
| 30 } | 33 } |
| 31 static int Decrement(volatile int* i) { | 34 static int Decrement(volatile int* i) { |
| 32 return ::InterlockedDecrement(reinterpret_cast<volatile LONG*>(i)); | 35 return ::InterlockedDecrement(reinterpret_cast<volatile LONG*>(i)); |
| 33 } | 36 } |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 54 } | 57 } |
| 55 static void ReleaseStore(volatile int* i, int value) { | 58 static void ReleaseStore(volatile int* i, int value) { |
| 56 __atomic_store_n(i, value, __ATOMIC_RELEASE); | 59 __atomic_store_n(i, value, __ATOMIC_RELEASE); |
| 57 } | 60 } |
| 58 static int CompareAndSwap(volatile int* i, int old_value, int new_value) { | 61 static int CompareAndSwap(volatile int* i, int old_value, int new_value) { |
| 59 return __sync_val_compare_and_swap(i, old_value, new_value); | 62 return __sync_val_compare_and_swap(i, old_value, new_value); |
| 60 } | 63 } |
| 61 #endif | 64 #endif |
| 62 }; | 65 }; |
| 63 | 66 |
| 67 // POD struct version of AtomicOps, prevents accidental non-atomic usage (such | |
| 68 // as ++, =, reads or writes). Operations are still explicit to not hide that | |
| 69 // the operations are atomic. | |
|
kwiberg-webrtc
2015/11/02 12:41:49
Maybe "Functions are static, so that the AtomicInt
pbos-webrtc
2015/11/02 15:21:15
Done, yep that's the plan. I'm not sure if that sh
| |
| 70 struct AtomicInt { | |
| 71 AtomicInt() = delete; | |
| 64 | 72 |
| 73 // value_ is public to permit brace initialization. Should not be accessed | |
| 74 // directly. | |
| 75 volatile int value_; | |
| 65 | 76 |
| 77 static int Increment(AtomicInt* i) { | |
| 78 return AtomicOps::Increment(&i->value_); | |
| 79 } | |
| 80 static int Decrement(AtomicInt* i) { | |
| 81 return AtomicOps::Decrement(&i->value_); | |
| 82 } | |
| 83 static int AcquireLoad(const AtomicInt* i) { | |
| 84 return AtomicOps::AcquireLoad(&i->value_); | |
| 85 } | |
| 86 static void ReleaseStore(AtomicInt* i, int value) { | |
| 87 AtomicOps::ReleaseStore(&i->value_, value); | |
| 88 } | |
| 89 static int CompareAndSwap(AtomicInt* i, int old_value, int new_value) { | |
| 90 return AtomicOps::CompareAndSwap(&i->value_, old_value, new_value); | |
| 91 } | |
|
kwiberg-webrtc
2015/11/02 12:41:50
I'd like a short comment on each of these, explain
pbos-webrtc
2015/11/02 15:21:15
Done.
| |
| 92 | |
| 93 RTC_DISALLOW_COPY_AND_ASSIGN(AtomicInt); | |
| 94 }; | |
| 66 } | 95 } |
| 67 | 96 |
| 68 #endif // WEBRTC_BASE_ATOMICOPS_H_ | 97 #endif // WEBRTC_BASE_ATOMICOPS_H_ |
| OLD | NEW |