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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: webrtc/base/atomicops.h
diff --git a/webrtc/base/atomicops.h b/webrtc/base/atomicops.h
index a863566a670aad0b3d4904f0040c3c20ad845461..b21bd999f304d2f9cd9742fceebd80b3722e7ce3 100644
--- a/webrtc/base/atomicops.h
+++ b/webrtc/base/atomicops.h
@@ -21,6 +21,7 @@
#endif // defined(WEBRTC_WIN)
namespace rtc {
+
class AtomicOps {
public:
#if defined(WEBRTC_WIN)
@@ -61,8 +62,50 @@ class AtomicOps {
#endif
};
+// POD struct version of AtomicOps, prevents accidental non-atomic operator
+// usage (such as ++, -- or =). Functions are static, so that the AtomicInt::
+// prefix must be present in the code, clearly labeling the operations as
+// atomic.
+// Do not copy-initialize, since that performs non-atomic reads of value_. The
+// 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
+struct AtomicInt {
+ AtomicInt() = delete;
+ // 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?
+ // std::atomic), remove copy constructor (or have it implicitly removed by
+ // std::atomic).
+ void operator=(const AtomicInt&) = delete;
+
+ // value_ is public to permit brace initialization. Should not be accessed
+ // directly.
tommi 2015/11/06 22:53:36 The documentation doesn't clearly explain why we a
+ volatile int value_;
+
+ // Atomically increments |i|, returns the resulting incremented value.
+ static int Increment(AtomicInt* i) {
+ return AtomicOps::Increment(&i->value_);
+ }
+ // Atomically decrements |i|, returns the resulting decremented value.
+ static int Decrement(AtomicInt* i) {
+ return AtomicOps::Decrement(&i->value_);
+ }
+ // Atomically loads |i|.
+ static int AcquireLoad(const AtomicInt* i) {
+ return AtomicOps::AcquireLoad(&i->value_);
+ }
+
+ // Atomically stores |value| in |i|.
+ static void ReleaseStore(AtomicInt* i, int value) {
+ AtomicOps::ReleaseStore(&i->value_, value);
+ }
+
+ // Attempts to compare-and-swaps |old_value| for |new_value| in |i| , returns
+ // |i|'s initial value. If equal to |old_value|, then the CAS succeeded,
+ // otherwise no operation is performed.
+ static int CompareAndSwap(AtomicInt* i, int old_value, int new_value) {
+ return AtomicOps::CompareAndSwap(&i->value_, old_value, new_value);
+ }
+};
}
#endif // WEBRTC_BASE_ATOMICOPS_H_

Powered by Google App Engine
This is Rietveld 408576698