Add functions rtc::AtomicOps::Load and rtc::RefCountedObject::HasOneRef

R=tommi@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/40819005

Cr-Commit-Position: refs/heads/master@{#8452}
git-svn-id: http://webrtc.googlecode.com/svn/trunk@8452 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
magjed@webrtc.org 2015-02-21 13:23:27 +00:00
parent 2af3057b24
commit a43fce6e02
2 changed files with 24 additions and 7 deletions

View File

@ -152,19 +152,26 @@ class AtomicOps {
public: public:
#if defined(WEBRTC_WIN) #if defined(WEBRTC_WIN)
// Assumes sizeof(int) == sizeof(LONG), which it is on Win32 and Win64. // Assumes sizeof(int) == sizeof(LONG), which it is on Win32 and Win64.
static int Increment(int* i) { static int Increment(volatile int* i) {
return ::InterlockedIncrement(reinterpret_cast<LONG*>(i)); return ::InterlockedIncrement(reinterpret_cast<volatile LONG*>(i));
} }
static int Decrement(int* i) { static int Decrement(volatile int* i) {
return ::InterlockedDecrement(reinterpret_cast<LONG*>(i)); return ::InterlockedDecrement(reinterpret_cast<volatile LONG*>(i));
}
static int Load(volatile const int* i) {
return *i;
} }
#else #else
static int Increment(int* i) { static int Increment(volatile int* i) {
return __sync_add_and_fetch(i, 1); return __sync_add_and_fetch(i, 1);
} }
static int Decrement(int* i) { static int Decrement(volatile int* i) {
return __sync_sub_and_fetch(i, 1); return __sync_sub_and_fetch(i, 1);
} }
static int Load(volatile const int* i) {
// Adding 0 is a no-op, so const_cast is fine.
return __sync_add_and_fetch(const_cast<volatile int*>(i), 0);
}
#endif #endif
}; };

View File

@ -66,11 +66,21 @@ class RefCountedObject : public T {
return count; return count;
} }
// Return whether the reference count is one. If the reference count is used
// in the conventional way, a reference count of 1 implies that the current
// thread owns the reference and no other thread shares it. This call
// performs the test for a reference count of one, and performs the memory
// 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 rtc::AtomicOps::Load(&ref_count_) == 1;
}
protected: protected:
virtual ~RefCountedObject() { virtual ~RefCountedObject() {
} }
int ref_count_; volatile int ref_count_;
}; };
} // namespace rtc } // namespace rtc