aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/cxxsupp/libcxx/patches/58-memory.patch
blob: e5735899e75f9f5974e5cc01813de57ff392c678 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
diff --git a/src/memory.cpp b/src/memory.cpp
index 4b2a369..e318eee 100644
--- a/src/memory.cpp
+++ b/src/memory.cpp
@@ -21,7 +21,11 @@
 #  endif
 #endif
 
+#if !defined(_LIBCPP_HAS_NO_THREADS)
+#include <atomic>
+#else
 #include "include/atomic_support.h"
+#endif
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
@@ -45,13 +49,21 @@ __shared_weak_count::~__shared_weak_count()
 void
 __shared_count::__add_shared() noexcept
 {
+#ifdef _LIBCPP_HAS_NO_THREADS
     __libcpp_atomic_refcount_increment(__shared_owners_);
+#else
+    ++__shared_owners_;
+#endif
 }
 
 bool
 __shared_count::__release_shared() noexcept
 {
+#ifdef _LIBCPP_HAS_NO_THREADS
     if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1)
+#else
+    if (--__shared_owners_ == -1)
+#endif
     {
         __on_zero_shared();
         return true;
@@ -68,7 +80,11 @@ __shared_weak_count::__add_shared() noexcept
 void
 __shared_weak_count::__add_weak() noexcept
 {
+#ifdef _LIBCPP_HAS_NO_THREADS
     __libcpp_atomic_refcount_increment(__shared_weak_owners_);
+#else
+    ++__shared_weak_owners_;
+#endif
 }
 
 void
@@ -103,26 +119,42 @@ __shared_weak_count::__release_weak() noexcept
     // threads, and have them all get copied at once.  The argument
     // also doesn't apply for __release_shared, because an outstanding
     // weak_ptr::lock() could read / modify the shared count.
+#ifdef _LIBCPP_HAS_NO_THREADS
     if (__libcpp_atomic_load(&__shared_weak_owners_, _AO_Acquire) == 0)
+#else
+    if (__shared_weak_owners_.load(memory_order_acquire) == 0)
+#endif
     {
         // no need to do this store, because we are about
         // to destroy everything.
         //__libcpp_atomic_store(&__shared_weak_owners_, -1, _AO_Release);
         __on_zero_shared_weak();
     }
+#ifdef _LIBCPP_HAS_NO_THREADS
     else if (__libcpp_atomic_refcount_decrement(__shared_weak_owners_) == -1)
+#else
+    else if (--__shared_weak_owners_ == -1)
+#endif
         __on_zero_shared_weak();
 }
 
 __shared_weak_count*
 __shared_weak_count::lock() noexcept
 {
+#ifdef _LIBCPP_HAS_NO_THREADS
     long object_owners = __libcpp_atomic_load(&__shared_owners_);
+#else
+    long object_owners = __shared_owners_.load();
+#endif
     while (object_owners != -1)
     {
+#ifdef _LIBCPP_HAS_NO_THREADS
         if (__libcpp_atomic_compare_exchange(&__shared_owners_,
                                              &object_owners,
                                              object_owners+1))
+#else
+        if (__shared_owners_.compare_exchange_weak(object_owners, object_owners+1))
+#endif
             return this;
     }
     return nullptr;