xref: /llvm-project/compiler-rt/test/tsan/Linux/thread_tryjoin.c (revision d0fb5d8b00ab4c9de7a610099b0f2e88bea6f917)
1 // RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2 #define _GNU_SOURCE
3 #include "../test.h"
4 #include <errno.h>
5 
6 int var;
7 
Thread(void * x)8 void *Thread(void *x) {
9   barrier_wait(&barrier);
10   var = 1;
11   return 0;
12 }
13 
check(int res)14 static void check(int res) {
15   if (res != EBUSY) {
16     fprintf(stderr, "Unexpected result of pthread_tryjoin_np: %d\n", res);
17     exit(1);
18   }
19 }
20 
main()21 int main() {
22   barrier_init(&barrier, 2);
23   pthread_t t;
24   pthread_create(&t, 0, Thread, 0);
25   check(pthread_tryjoin_np(t, 0));
26   barrier_wait(&barrier);
27   for (;;) {
28     int res = pthread_tryjoin_np(t, 0);
29     if (!res)
30       break;
31     check(res);
32     pthread_yield();
33   }
34   var = 2;
35   fprintf(stderr, "PASS\n");
36   return 0;
37 }
38 
39 // CHECK-NOT: WARNING: ThreadSanitizer: data race
40 // CHECK-NOT: WARNING: ThreadSanitizer: thread leak
41 // CHECK: PASS
42