Skip to content

Commit 1af4b02

Browse files
authored
Merge pull request #3006 from compnerd/atomics
2 parents 7575e66 + a113666 commit 1af4b02

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

include/swift/Basic/ThreadSafeRefCounted.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#ifndef SWIFT_BASIC_THREADSAFEREFCOUNTED_H
1414
#define SWIFT_BASIC_THREADSAFEREFCOUNTED_H
1515

16-
#include "llvm/Support/Atomic.h"
16+
#include <atomic>
1717
#include <cassert>
1818

1919
namespace swift {
@@ -28,18 +28,19 @@ namespace swift {
2828
/// FIXME: This should eventually move to llvm.
2929
template <class Derived>
3030
class ThreadSafeRefCountedBase {
31-
mutable llvm::sys::cas_flag ref_cnt;
31+
mutable std::atomic<unsigned> ref_cnt;
3232

3333
protected:
3434
ThreadSafeRefCountedBase() : ref_cnt(0) {}
3535

3636
public:
3737
void Retain() const {
38-
llvm::sys::AtomicIncrement(&ref_cnt);
38+
ref_cnt.fetch_add(1, std::memory_order_acq_rel);
3939
}
4040

4141
void Release() const {
42-
int refCount = static_cast<int>(llvm::sys::AtomicDecrement(&ref_cnt));
42+
int refCount =
43+
static_cast<int>(ref_cnt.fetch_sub(1, std::memory_order_acq_rel));
4344
assert(refCount >= 0 && "Reference count was already zero.");
4445
if (refCount == 0) delete static_cast<const Derived*>(this);
4546
}
@@ -52,7 +53,7 @@ class ThreadSafeRefCountedBase {
5253
/// already have virtual methods to enforce dynamic allocation via 'new'.
5354
/// FIXME: This should eventually move to llvm.
5455
class ThreadSafeRefCountedBaseVPTR {
55-
mutable llvm::sys::cas_flag ref_cnt;
56+
mutable std::atomic<unsigned> ref_cnt;
5657
virtual void anchor();
5758

5859
protected:
@@ -61,11 +62,12 @@ class ThreadSafeRefCountedBaseVPTR {
6162

6263
public:
6364
void Retain() const {
64-
llvm::sys::AtomicIncrement(&ref_cnt);
65+
ref_cnt.fetch_add(1, std::memory_order_acq_rel);
6566
}
6667

6768
void Release() const {
68-
int refCount = static_cast<int>(llvm::sys::AtomicDecrement(&ref_cnt));
69+
int refCount =
70+
static_cast<int>(ref_cnt.fetch_sub(1, std::memory_order_acq_rel));
6971
assert(refCount >= 0 && "Reference count was already zero.");
7072
if (refCount == 0) delete this;
7173
}

0 commit comments

Comments
 (0)