2727
2828#include <threading/threading_atomic.h>
2929
30+ #if defined(__THREAD_SANITIZER__ )
31+ #include <threading/threading_mutex.h>
32+ #endif
33+
3034#ifdef __cplusplus
3135extern "C" {
3236#endif
@@ -44,7 +48,12 @@ extern "C" {
4448
4549struct threading_atomic_ref_count_type
4650{
51+ #if defined(__THREAD_SANITIZER__ )
52+ uintmax_t count ;
53+ struct threading_mutex_type m ;
54+ #else
4755 atomic_uintmax_t count ;
56+ #endif
4857};
4958
5059/* -- Type Definitions -- */
@@ -55,28 +64,68 @@ typedef struct threading_atomic_ref_count_type *threading_atomic_ref_count;
5564
5665inline void threading_atomic_ref_count_store (threading_atomic_ref_count ref , uintmax_t v )
5766{
67+ #if defined(__THREAD_SANITIZER__ )
68+ threading_mutex_store (& ref -> m , & ref -> count , & v , sizeof (uintmax_t ));
69+ #else
5870 atomic_store (& ref -> count , v );
71+ #endif
72+ }
73+
74+ inline void threading_atomic_ref_count_initialize (threading_atomic_ref_count ref )
75+ {
76+ #if defined(__THREAD_SANITIZER__ )
77+ uintmax_t init = THREADING_ATOMIC_REF_COUNT_MIN ;
78+
79+ threading_mutex_initialize (& ref -> m );
80+
81+ threading_mutex_store (& ref -> m , & ref -> count , & init , sizeof (uintmax_t ));
82+ #else
83+ threading_atomic_ref_count_store (ref , THREADING_ATOMIC_REF_COUNT_MIN );
84+ #endif
5985}
6086
6187inline uintmax_t threading_atomic_ref_count_load (threading_atomic_ref_count ref )
6288{
89+ #if defined(__THREAD_SANITIZER__ )
90+ uintmax_t result = 0 ;
91+
92+ threading_mutex_store (& ref -> m , & result , & ref -> count , sizeof (uintmax_t ));
93+
94+ return result ;
95+ #else
6396 return atomic_load_explicit (& ref -> count , memory_order_relaxed );
97+ #endif
6498}
6599
66100inline int threading_atomic_ref_count_increment (threading_atomic_ref_count ref )
67101{
102+ #if defined(__THREAD_SANITIZER__ )
103+ threading_mutex_lock (& ref -> m );
104+ {
105+ ++ ref -> count ;
106+ }
107+ threading_mutex_unlock (& ref -> m );
108+ #else
68109 if (atomic_load_explicit (& ref -> count , memory_order_relaxed ) == THREADING_ATOMIC_REF_COUNT_MAX )
69110 {
70111 return 1 ;
71112 }
72113
73114 atomic_fetch_add_explicit (& ref -> count , 1 , memory_order_relaxed );
115+ #endif
74116
75117 return 0 ;
76118}
77119
78120inline int threading_atomic_ref_count_decrement (threading_atomic_ref_count ref )
79121{
122+ #if defined(__THREAD_SANITIZER__ )
123+ threading_mutex_lock (& ref -> m );
124+ {
125+ -- ref -> count ;
126+ }
127+ threading_mutex_unlock (& ref -> m );
128+ #else
80129 if (atomic_load_explicit (& ref -> count , memory_order_relaxed ) == THREADING_ATOMIC_REF_COUNT_MIN )
81130 {
82131 return 1 ;
@@ -88,10 +137,20 @@ inline int threading_atomic_ref_count_decrement(threading_atomic_ref_count ref)
88137 {
89138 atomic_thread_fence (memory_order_acquire );
90139 }
140+ #endif
91141
92142 return 0 ;
93143}
94144
145+ inline void threading_atomic_ref_count_destroy (threading_atomic_ref_count ref )
146+ {
147+ #if defined(__THREAD_SANITIZER__ )
148+ threading_mutex_destroy (& ref -> m );
149+ #else
150+ (void )ref ;
151+ #endif
152+ }
153+
95154#ifdef __cplusplus
96155}
97156#endif
0 commit comments