Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(484)

Side by Side Diff: webrtc/base/atomicops.h

Issue 1420043008: Create rtc::AtomicInt POD struct. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: fix comments Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 22
23 namespace rtc { 23 namespace rtc {
24
24 class AtomicOps { 25 class AtomicOps {
25 public: 26 public:
26 #if defined(WEBRTC_WIN) 27 #if defined(WEBRTC_WIN)
27 // Assumes sizeof(int) == sizeof(LONG), which it is on Win32 and Win64. 28 // Assumes sizeof(int) == sizeof(LONG), which it is on Win32 and Win64.
28 static int Increment(volatile int* i) { 29 static int Increment(volatile int* i) {
29 return ::InterlockedIncrement(reinterpret_cast<volatile LONG*>(i)); 30 return ::InterlockedIncrement(reinterpret_cast<volatile LONG*>(i));
30 } 31 }
31 static int Decrement(volatile int* i) { 32 static int Decrement(volatile int* i) {
32 return ::InterlockedDecrement(reinterpret_cast<volatile LONG*>(i)); 33 return ::InterlockedDecrement(reinterpret_cast<volatile LONG*>(i));
33 } 34 }
(...skipping 20 matching lines...) Expand all
54 } 55 }
55 static void ReleaseStore(volatile int* i, int value) { 56 static void ReleaseStore(volatile int* i, int value) {
56 __atomic_store_n(i, value, __ATOMIC_RELEASE); 57 __atomic_store_n(i, value, __ATOMIC_RELEASE);
57 } 58 }
58 static int CompareAndSwap(volatile int* i, int old_value, int new_value) { 59 static int CompareAndSwap(volatile int* i, int old_value, int new_value) {
59 return __sync_val_compare_and_swap(i, old_value, new_value); 60 return __sync_val_compare_and_swap(i, old_value, new_value);
60 } 61 }
61 #endif 62 #endif
62 }; 63 };
63 64
65 // POD struct version of AtomicOps, prevents accidental non-atomic operator
66 // usage (such as ++, -- or =). Functions are static, so that the AtomicInt::
67 // prefix must be present in the code, clearly labeling the operations as
68 // atomic.
69 // Do not copy-initialize, since that performs non-atomic reads of value_. The
70 // copy constructor needs to be present for brace initialization.
tommi 2015/11/06 22:53:36 why do we need brace initialization? (are you sure
71 struct AtomicInt {
72 AtomicInt() = delete;
73 // TODO(pbos): When MSVC allows brace initialization (or we move to
tommi 2015/11/06 22:53:36 ah... so, how can we use it?
74 // std::atomic), remove copy constructor (or have it implicitly removed by
75 // std::atomic).
76 void operator=(const AtomicInt&) = delete;
64 77
78 // value_ is public to permit brace initialization. Should not be accessed
79 // directly.
tommi 2015/11/06 22:53:36 The documentation doesn't clearly explain why we a
80 volatile int value_;
65 81
82 // Atomically increments |i|, returns the resulting incremented value.
83 static int Increment(AtomicInt* i) {
84 return AtomicOps::Increment(&i->value_);
85 }
86
87 // Atomically decrements |i|, returns the resulting decremented value.
88 static int Decrement(AtomicInt* i) {
89 return AtomicOps::Decrement(&i->value_);
90 }
91
92 // Atomically loads |i|.
93 static int AcquireLoad(const AtomicInt* i) {
94 return AtomicOps::AcquireLoad(&i->value_);
95 }
96
97 // Atomically stores |value| in |i|.
98 static void ReleaseStore(AtomicInt* i, int value) {
99 AtomicOps::ReleaseStore(&i->value_, value);
100 }
101
102 // Attempts to compare-and-swaps |old_value| for |new_value| in |i| , returns
103 // |i|'s initial value. If equal to |old_value|, then the CAS succeeded,
104 // otherwise no operation is performed.
105 static int CompareAndSwap(AtomicInt* i, int old_value, int new_value) {
106 return AtomicOps::CompareAndSwap(&i->value_, old_value, new_value);
107 }
108 };
66 } 109 }
67 110
68 #endif // WEBRTC_BASE_ATOMICOPS_H_ 111 #endif // WEBRTC_BASE_ATOMICOPS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698