PR target/83368
[official-gcc.git] / gcc / testsuite / c-c++-common / tsan / race_on_mutex.c
blobdef1d47de7bc1bb2d10084eb13b68d690c91a1d5
1 /* { dg-shouldfail "tsan" } */
2 /* { dg-additional-options "-ldl" } */
4 #include <pthread.h>
5 #include "tsan_barrier.h"
7 static pthread_barrier_t barrier;
8 pthread_mutex_t Mtx;
9 int Global;
11 void *Thread1(void *x) {
12 pthread_mutex_init(&Mtx, 0);
13 pthread_mutex_lock(&Mtx);
14 Global = 42;
15 pthread_mutex_unlock(&Mtx);
16 barrier_wait(&barrier);
17 return NULL;
20 void *Thread2(void *x) {
21 barrier_wait(&barrier);
22 pthread_mutex_lock(&Mtx);
23 Global = 43;
24 pthread_mutex_unlock(&Mtx);
25 return NULL;
28 int main() {
29 barrier_init(&barrier, 2);
30 pthread_t t[2];
31 pthread_create(&t[0], NULL, Thread1, NULL);
32 pthread_create(&t[1], NULL, Thread2, NULL);
33 pthread_join(t[0], NULL);
34 pthread_join(t[1], NULL);
35 pthread_mutex_destroy(&Mtx);
36 return 0;
39 /* { dg-output "WARNING: ThreadSanitizer: data race.*(\n|\r\n|\r)" } */
40 /* { dg-output " Atomic read of size \[0-9]\+ at .* by thread T2:(\n|\r\n|\r)" } */
41 /* { dg-output " #0 pthread_mutex_lock.*" } */
42 /* { dg-output " #1 Thread2.* .*(race_on_mutex.c:22|\\?{2}:0) (.*)" } */
43 /* { dg-output " Previous write of size \[0-9]\+ at .* by thread T1:(\n|\r\n|\r)" } */
44 /* { dg-output "( #0 \[^\n\r\]*(\n|\r\n|\r))?" } */
45 /* { dg-output " #\[01\] (__)?pthread_mutex_init \[^\n\r\]* (.)*" } */
46 /* { dg-output " #\[12\] Thread1.* .*(race_on_mutex.c:12|\\?{2}:0) .*" } */