Reland of "Create rtc::AtomicInt POD struct."
Relands https://codereview.webrtc.org/1420043008/ with brace initializers
instead of constructors hoping that they won't introduce static
initializers.
BUG=
R=tommi@webrtc.org
Review URL: https://codereview.webrtc.org/1498953002 .
Cr-Commit-Position: refs/heads/master@{#10920}
diff --git a/webrtc/base/atomicops.h b/webrtc/base/atomicops.h
index a863566..b21bd99 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 @@
#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.
+struct AtomicInt {
+ AtomicInt() = delete;
+ // TODO(pbos): When MSVC allows brace initialization (or we move to
+ // 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.
+ 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_
diff --git a/webrtc/base/criticalsection.cc b/webrtc/base/criticalsection.cc
index 1f50c23..97322f4 100644
--- a/webrtc/base/criticalsection.cc
+++ b/webrtc/base/criticalsection.cc
@@ -139,7 +139,7 @@
const struct timespec ts_null = {0};
#endif
- while (AtomicOps::CompareAndSwap(&lock_acquired, 0, 1)) {
+ while (AtomicInt::CompareAndSwap(&lock_acquired, 0, 1)) {
#if defined(WEBRTC_WIN)
::Sleep(0);
#else
@@ -149,16 +149,13 @@
}
void GlobalLockPod::Unlock() {
- int old_value = AtomicOps::CompareAndSwap(&lock_acquired, 1, 0);
+ int old_value = AtomicInt::CompareAndSwap(&lock_acquired, 1, 0);
RTC_DCHECK_EQ(1, old_value) << "Unlock called without calling Lock first";
}
-GlobalLock::GlobalLock() {
- lock_acquired = 0;
-}
+GlobalLock::GlobalLock() : GlobalLockPod({{0}}) {}
-GlobalLockScope::GlobalLockScope(GlobalLockPod* lock)
- : lock_(lock) {
+GlobalLockScope::GlobalLockScope(GlobalLockPod* lock) : lock_(lock) {
lock_->Lock();
}
diff --git a/webrtc/base/criticalsection.h b/webrtc/base/criticalsection.h
index ddbf857..ed3998b 100644
--- a/webrtc/base/criticalsection.h
+++ b/webrtc/base/criticalsection.h
@@ -106,7 +106,7 @@
void Unlock() UNLOCK_FUNCTION();
- volatile int lock_acquired;
+ AtomicInt lock_acquired;
};
class GlobalLock : public GlobalLockPod {
diff --git a/webrtc/base/refcount.h b/webrtc/base/refcount.h
index 55ce23a..f655602 100644
--- a/webrtc/base/refcount.h
+++ b/webrtc/base/refcount.h
@@ -29,78 +29,128 @@
template <class T>
class RefCountedObject : public T {
public:
- RefCountedObject() : ref_count_(0) {
- }
+ RefCountedObject() : ref_count_({0}) {}
- template<typename P>
- explicit RefCountedObject(P p) : T(p), ref_count_(0) {
- }
+ template <typename P>
+ explicit RefCountedObject(P p)
+ : T(p), ref_count_({0}) {}
- template<typename P1, typename P2>
- RefCountedObject(P1 p1, P2 p2) : T(p1, p2), ref_count_(0) {
- }
+ template <typename P1, typename P2>
+ RefCountedObject(P1 p1, P2 p2)
+ : T(p1, p2), ref_count_({0}) {}
- template<typename P1, typename P2, typename P3>
- RefCountedObject(P1 p1, P2 p2, P3 p3) : T(p1, p2, p3), ref_count_(0) {
- }
+ template <typename P1, typename P2, typename P3>
+ RefCountedObject(P1 p1, P2 p2, P3 p3)
+ : T(p1, p2, p3), ref_count_({0}) {}
- template<typename P1, typename P2, typename P3, typename P4>
+ template <typename P1, typename P2, typename P3, typename P4>
RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4)
- : T(p1, p2, p3, p4), ref_count_(0) {
- }
+ : T(p1, p2, p3, p4), ref_count_({0}) {}
- template<typename P1, typename P2, typename P3, typename P4, typename P5>
+ template <typename P1, typename P2, typename P3, typename P4, typename P5>
RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5)
- : T(p1, p2, p3, p4, p5), ref_count_(0) {
- }
+ : T(p1, p2, p3, p4, p5), ref_count_({0}) {}
- template<typename P1, typename P2, typename P3, typename P4, typename P5,
- typename P6>
+ template <typename P1,
+ typename P2,
+ typename P3,
+ typename P4,
+ typename P5,
+ typename P6>
RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6)
- : T(p1, p2, p3, p4, p5, p6), ref_count_(0) {
- }
+ : T(p1, p2, p3, p4, p5, p6), ref_count_({0}) {}
- template<typename P1, typename P2, typename P3, typename P4, typename P5,
- typename P6, typename P7>
+ template <typename P1,
+ typename P2,
+ typename P3,
+ typename P4,
+ typename P5,
+ typename P6,
+ typename P7>
RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7)
- : T(p1, p2, p3, p4, p5, p6, p7), ref_count_(0) {
- }
+ : T(p1, p2, p3, p4, p5, p6, p7), ref_count_({0}) {}
- template<typename P1, typename P2, typename P3, typename P4, typename P5,
- typename P6, typename P7, typename P8>
+ template <typename P1,
+ typename P2,
+ typename P3,
+ typename P4,
+ typename P5,
+ typename P6,
+ typename P7,
+ typename P8>
RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8)
- : T(p1, p2, p3, p4, p5, p6, p7, p8), ref_count_(0) {
- }
+ : T(p1, p2, p3, p4, p5, p6, p7, p8), ref_count_({0}) {}
- template<typename P1, typename P2, typename P3, typename P4, typename P5,
- typename P6, typename P7, typename P8, typename P9>
- RefCountedObject(
- P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9)
- : T(p1, p2, p3, p4, p5, p6, p7, p8, p9), ref_count_(0) {
- }
+ template <typename P1,
+ typename P2,
+ typename P3,
+ typename P4,
+ typename P5,
+ typename P6,
+ typename P7,
+ typename P8,
+ typename P9>
+ RefCountedObject(P1 p1,
+ P2 p2,
+ P3 p3,
+ P4 p4,
+ P5 p5,
+ P6 p6,
+ P7 p7,
+ P8 p8,
+ P9 p9)
+ : T(p1, p2, p3, p4, p5, p6, p7, p8, p9), ref_count_({0}) {}
- template<typename P1, typename P2, typename P3, typename P4, typename P5,
- typename P6, typename P7, typename P8, typename P9, typename P10>
- RefCountedObject(
- P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9, P10 p10)
- : T(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10), ref_count_(0) {
- }
+ template <typename P1,
+ typename P2,
+ typename P3,
+ typename P4,
+ typename P5,
+ typename P6,
+ typename P7,
+ typename P8,
+ typename P9,
+ typename P10>
+ RefCountedObject(P1 p1,
+ P2 p2,
+ P3 p3,
+ P4 p4,
+ P5 p5,
+ P6 p6,
+ P7 p7,
+ P8 p8,
+ P9 p9,
+ P10 p10)
+ : T(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10), ref_count_({0}) {}
- template<typename P1, typename P2, typename P3, typename P4, typename P5,
- typename P6, typename P7, typename P8, typename P9, typename P10,
- typename P11>
- RefCountedObject(
- P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9, P10 p10,
- P11 p11)
- : T(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11), ref_count_(0) {
- }
+ template <typename P1,
+ typename P2,
+ typename P3,
+ typename P4,
+ typename P5,
+ typename P6,
+ typename P7,
+ typename P8,
+ typename P9,
+ typename P10,
+ typename P11>
+ RefCountedObject(P1 p1,
+ P2 p2,
+ P3 p3,
+ P4 p4,
+ P5 p5,
+ P6 p6,
+ P7 p7,
+ P8 p8,
+ P9 p9,
+ P10 p10,
+ P11 p11)
+ : T(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11), ref_count_({0}) {}
- virtual int AddRef() const {
- return AtomicOps::Increment(&ref_count_);
- }
+ virtual int AddRef() const { return AtomicInt::Increment(&ref_count_); }
virtual int Release() const {
- int count = AtomicOps::Decrement(&ref_count_);
+ int count = AtomicInt::Decrement(&ref_count_);
if (!count) {
delete this;
}
@@ -114,14 +164,14 @@
// barrier needed for the owning thread to act on the object, knowing that it
// has exclusive access to the object.
virtual bool HasOneRef() const {
- return AtomicOps::AcquireLoad(&ref_count_) == 1;
+ return AtomicInt::AcquireLoad(&ref_count_) == 1;
}
protected:
virtual ~RefCountedObject() {
}
- mutable volatile int ref_count_;
+ mutable AtomicInt ref_count_;
};
} // namespace rtc