Skip to content

Commit e1b82d8

Browse files
ylnJulian Lettner
andauthored
Remove dlsym() lookup for TSan functions from common path (#36478)
* Move TSan function lookup out of the common path Move TSan function lookup via `dlsym()` out of the common path. The TSan runtime will now call `__tsan_on_initialize()` which we can use to initialize the TSan functions in the Swift runtime. This avoids paying the cost of `dlsym()` in the common, non-TSan case. Depends on: https://reviews.llvm.org/D98810 rdar://75493372 * Remove Windows code Thread Sanitizer is not supported on Windows. Co-authored-by: Julian Lettner <[email protected]>
1 parent 172df21 commit e1b82d8

File tree

1 file changed

+8
-23
lines changed

1 file changed

+8
-23
lines changed

stdlib/public/Concurrency/ThreadSanitizer.cpp

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,54 +10,39 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212
//
13-
// Thread Sanitizer support for the Swift Task runtime
13+
// Thread Sanitizer support for the Swift Task runtime.
1414
//
1515
//===----------------------------------------------------------------------===//
1616

1717
#include "TaskPrivate.h"
1818

19+
// Thread Sanitizer is not supported on Windows.
1920
#if defined(_WIN32)
20-
#define NOMINMAX
21-
#include <windows.h>
21+
void swift::_swift_tsan_acquire(void *addr) {}
22+
void swift::_swift_tsan_release(void *addr) {}
2223
#else
2324
#include <dlfcn.h>
24-
#endif
2525

2626
namespace {
2727
using TSanFunc = void(void *);
2828
TSanFunc *tsan_acquire, *tsan_release;
29-
30-
TSanFunc *loadSymbol(const char *name) {
31-
#if defined(_WIN32)
32-
return (TSanFunc *)GetProcAddress(GetModuleHandle(NULL), name);
33-
#else
34-
return (TSanFunc *)dlsym(RTLD_DEFAULT, name);
35-
#endif
36-
}
37-
38-
swift::swift_once_t initOnceToken;
39-
void initializeThreadSanitizer(void *unused) {
40-
tsan_acquire = loadSymbol("__tsan_acquire");
41-
tsan_release = loadSymbol("__tsan_release");
42-
}
4329
} // anonymous namespace
4430

4531
void swift::_swift_tsan_acquire(void *addr) {
46-
swift_once(&initOnceToken, initializeThreadSanitizer, nullptr);
4732
if (tsan_acquire) {
4833
tsan_acquire(addr);
4934
}
5035
}
5136

5237
void swift::_swift_tsan_release(void *addr) {
53-
swift_once(&initOnceToken, initializeThreadSanitizer, nullptr);
5438
if (tsan_release) {
5539
tsan_release(addr);
5640
}
5741
}
5842

5943
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(c)
60-
void swift_task_set_tsan_hooks(TSanFunc *acquire, TSanFunc *release) {
61-
tsan_acquire = acquire;
62-
tsan_release = release;
44+
void __tsan_on_initialize() {
45+
tsan_acquire = (TSanFunc *)dlsym(RTLD_DEFAULT, "__tsan_acquire");
46+
tsan_release = (TSanFunc *)dlsym(RTLD_DEFAULT, "__tsan_release");
6347
}
48+
#endif

0 commit comments

Comments
 (0)