Skip to content

Commit 6499c5d

Browse files
authored
[compiler-rt][builtins] Add opt-in pthread_mutex_t locks to libatomic (#95326)
When an uninstrumented libatomic is used with a TSan instrumented memcpy, TSan may report a data race in circumstances where writes are arguably safe. This occurs because __atomic_compare_exchange won't be instrumented in an uninstrumented libatomic, so TSan doesn't know that the subsequent memcpy is race-free. On the other hand, pthread_mutex_(un)lock will be intercepted by TSan, meaning an uninstrumented libatomic will not report this false-positive. pthread_mutexes also may try a number of different strategies to acquire the lock, which may bound the amount of time a thread has to wait for a lock during contention. While pthread_mutex_lock has a larger overhead (due to the function call and some dispatching), a dispatch to libatomic already predicates a lack of performance guarantees.
1 parent a239343 commit 6499c5d

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

compiler-rt/lib/builtins/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,14 @@ if(NOT FUCHSIA AND NOT COMPILER_RT_BAREMETAL_BUILD)
237237
)
238238
endif()
239239

240+
option(COMPILER_RT_LIBATOMIC_USE_PTHREAD
241+
"Whether libatomic should use pthreads if available."
242+
Off)
243+
244+
if(COMPILER_RT_LIBATOMIC_USE_PTHREAD)
245+
add_compile_definitions(_LIBATOMIC_USE_PTHREAD)
246+
endif()
247+
240248
if(COMPILER_RT_HAS_ATOMIC_KEYWORD AND NOT COMPILER_RT_EXCLUDE_ATOMIC_BUILTIN)
241249
set(GENERIC_SOURCES
242250
${GENERIC_SOURCES}

compiler-rt/lib/builtins/atomic.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//
1313
// 1) This code must work with C programs that do not link to anything
1414
// (including pthreads) and so it should not depend on any pthread
15-
// functions.
15+
// functions. If the user wishes to opt into using pthreads, they may do so.
1616
// 2) Atomic operations, rather than explicit mutexes, are most commonly used
1717
// on code where contended operations are rate.
1818
//
@@ -56,7 +56,17 @@ static const long SPINLOCK_MASK = SPINLOCK_COUNT - 1;
5656
// defined. Each platform should define the Lock type, and corresponding
5757
// lock() and unlock() functions.
5858
////////////////////////////////////////////////////////////////////////////////
59-
#if defined(__FreeBSD__) || defined(__DragonFly__)
59+
#if defined(_LIBATOMIC_USE_PTHREAD)
60+
#include <pthread.h>
61+
typedef pthread_mutex_t Lock;
62+
/// Unlock a lock. This is a release operation.
63+
__inline static void unlock(Lock *l) { pthread_mutex_unlock(l); }
64+
/// Locks a lock.
65+
__inline static void lock(Lock *l) { pthread_mutex_lock(l); }
66+
/// locks for atomic operations
67+
static Lock locks[SPINLOCK_COUNT];
68+
69+
#elif defined(__FreeBSD__) || defined(__DragonFly__)
6070
#include <errno.h>
6171
// clang-format off
6272
#include <sys/types.h>

0 commit comments

Comments
 (0)