aboutsummaryrefslogtreecommitdiff
path: root/test/tsan/test.h
diff options
context:
space:
mode:
Diffstat (limited to 'test/tsan/test.h')
-rw-r--r--test/tsan/test.h31
1 files changed, 31 insertions, 0 deletions
diff --git a/test/tsan/test.h b/test/tsan/test.h
new file mode 100644
index 000000000000..4496e56cda87
--- /dev/null
+++ b/test/tsan/test.h
@@ -0,0 +1,31 @@
+#include <pthread.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <dlfcn.h>
+#include <stddef.h>
+
+// TSan-invisible barrier.
+// Tests use it to establish necessary execution order in a way that does not
+// interfere with tsan (does not establish synchronization between threads).
+__typeof(pthread_barrier_wait) *barrier_wait;
+
+void barrier_init(pthread_barrier_t *barrier, unsigned count) {
+ if (barrier_wait == 0) {
+ void *h = dlopen("libpthread.so.0", RTLD_LAZY);
+ if (h == 0) {
+ fprintf(stderr, "failed to dlopen libpthread.so.0, exiting\n");
+ exit(1);
+ }
+ barrier_wait = (__typeof(barrier_wait))dlsym(h, "pthread_barrier_wait");
+ if (barrier_wait == 0) {
+ fprintf(stderr, "failed to resolve pthread_barrier_wait, exiting\n");
+ exit(1);
+ }
+ }
+ pthread_barrier_init(barrier, 0, count);
+}
+
+// Default instance of the barrier, but a test can declare more manually.
+pthread_barrier_t barrier;
+