Debian Codegraph @

gcc/testsuite/gcc.dg/atomic/pr80640.c

raw
1 /* { dg-do run } */
2 /* { dg-options "-pthread" } */
3 /* { dg-require-effective-target pthread } */
4
5 #include <pthread.h>
6
7 static volatile int sem1;
8 static volatile int sem2;
9
10 static void *f(void *va)
11 {
12 void **p = va;
13 if (*p) return *p;
14 sem1 = 1;
15 while (!sem2)
16 sched_yield ();
17 __atomic_thread_fence(__ATOMIC_ACQUIRE);
18 // GCC used to RTL-CSE this and the first load, causing 0 to be returned
19 return *p;
20 }
21
22 int main()
23 {
24 void *p = 0;
25 pthread_t thr;
26 if (pthread_create(&thr, 0, f, &p))
27 return 2;
28 while (!sem1)
29 sched_yield ();
30 __atomic_thread_fence(__ATOMIC_ACQUIRE);
31 p = &p;
32 __atomic_thread_fence(__ATOMIC_RELEASE);
33 sem2 = 1;
34 pthread_join(thr, &p);
35 return !p;
36 }