Skip to content

Commit

Permalink
Switch starboard/common RefCounted to standard atomics (#3864)
Browse files Browse the repository at this point in the history
b/353387554
  • Loading branch information
kaidokert authored Jul 24, 2024
1 parent 6e5c6cc commit 338ea21
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
9 changes: 4 additions & 5 deletions starboard/common/ref_counted.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ bool RefCountedBase::Release() const {
}

bool RefCountedThreadSafeBase::HasOneRef() const {
return (SbAtomicAcquire_Load(
&const_cast<RefCountedThreadSafeBase*>(this)->ref_count_) == 1);
return ref_count_.load(std::memory_order_acquire) == 1;
}

RefCountedThreadSafeBase::RefCountedThreadSafeBase() : ref_count_(0) {
Expand All @@ -75,15 +74,15 @@ void RefCountedThreadSafeBase::AddRef() const {
#ifndef NDEBUG
SB_DCHECK(!in_dtor_);
#endif
SbAtomicNoBarrier_Increment(&ref_count_, 1);
ref_count_.fetch_add(1, std::memory_order_relaxed);
}

bool RefCountedThreadSafeBase::Release() const {
#ifndef NDEBUG
SB_DCHECK(!in_dtor_);
SB_DCHECK(!(SbAtomicAcquire_Load(&ref_count_) == 0));
SB_DCHECK(!(ref_count_.load(std::memory_order_relaxed) == 0));
#endif
if (SbAtomicBarrier_Increment(&ref_count_, -1) == 0) {
if (ref_count_.fetch_sub(1, std::memory_order_acq_rel) == 1) {
#ifndef NDEBUG
in_dtor_ = true;
#endif
Expand Down
7 changes: 2 additions & 5 deletions starboard/common/ref_counted.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
#define STARBOARD_COMMON_REF_COUNTED_H_

#include <algorithm>
#include <atomic>
#include <utility>

#include "starboard/atomic.h"
#include "starboard/common/log.h"
#include "starboard/common/thread_collision_warner.h"

namespace starboard {
namespace subtle {
Expand All @@ -33,8 +32,6 @@ class RefCountedBase {
#ifndef NDEBUG
mutable bool in_dtor_;
#endif

DFAKE_MUTEX(add_release_);
};

class RefCountedThreadSafeBase {
Expand All @@ -51,7 +48,7 @@ class RefCountedThreadSafeBase {
bool Release() const;

private:
mutable SbAtomic32 ref_count_;
mutable std::atomic<int32_t> ref_count_;
#ifndef NDEBUG
mutable bool in_dtor_;
#endif
Expand Down

0 comments on commit 338ea21

Please sign in to comment.