@@ -844,16 +844,26 @@ TSAN_INTERCEPTOR(int, posix_memalign, void **memptr, uptr align, uptr sz) {
844
844
}
845
845
#endif
846
846
847
+ // Both __cxa_guard_acquire and pthread_once 0-initialize
848
+ // the object initially. pthread_once does not have any
849
+ // other ABI requirements. __cxa_guard_acquire assumes
850
+ // that any non-0 value in the first byte means that
851
+ // initialization is completed. Contents of the remaining
852
+ // bytes are up to us.
853
+ constexpr u32 kGuardInit = 0 ;
854
+ constexpr u32 kGuardDone = 1 ;
855
+ constexpr u32 kGuardRunning = 1 << 16 ;
856
+
847
857
static int guard_acquire (ThreadState *thr, uptr pc, atomic_uint32_t *g) {
848
858
OnPotentiallyBlockingRegionBegin ();
849
859
auto on_exit = at_scope_exit (&OnPotentiallyBlockingRegionEnd);
850
860
for (;;) {
851
861
u32 cmp = atomic_load (g, memory_order_acquire);
852
- if (cmp == 0 ) {
853
- if (atomic_compare_exchange_strong (g, &cmp, 1 << 16 ,
862
+ if (cmp == kGuardInit ) {
863
+ if (atomic_compare_exchange_strong (g, &cmp, kGuardRunning ,
854
864
memory_order_relaxed))
855
865
return 1 ;
856
- } else if (cmp == 1 ) {
866
+ } else if (cmp == kGuardDone ) {
857
867
if (!thr->in_ignored_lib )
858
868
Acquire (thr, pc, (uptr)g);
859
869
return 0 ;
@@ -866,7 +876,7 @@ static int guard_acquire(ThreadState *thr, uptr pc, atomic_uint32_t *g) {
866
876
static void guard_release (ThreadState *thr, uptr pc, atomic_uint32_t *g) {
867
877
if (!thr->in_ignored_lib )
868
878
Release (thr, pc, (uptr)g);
869
- atomic_store (g, 1 , memory_order_release);
879
+ atomic_store (g, kGuardDone , memory_order_release);
870
880
}
871
881
872
882
// __cxa_guard_acquire and friends need to be intercepted in a special way -
@@ -899,7 +909,7 @@ STDCXX_INTERCEPTOR(void, __cxa_guard_release, atomic_uint32_t *g) {
899
909
900
910
STDCXX_INTERCEPTOR (void , __cxa_guard_abort, atomic_uint32_t *g) {
901
911
SCOPED_INTERCEPTOR_RAW (__cxa_guard_abort, g);
902
- atomic_store (g, 0 , memory_order_relaxed);
912
+ atomic_store (g, kGuardInit , memory_order_relaxed);
903
913
}
904
914
905
915
namespace __tsan {
0 commit comments