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 _Atomic  int sem2;
9 
f(void * va)10 static void *f(void *va)
11 {
12   void **p = va;
13   if (*p) return *p;
14   sem1 = 1;
15   while (!__atomic_load_n(&sem2, __ATOMIC_ACQUIRE));
16   // GCC used to RTL-CSE this and the first load, causing 0 to be returned
17   return *p;
18 }
19 
main()20 int main()
21 {
22   void *p = 0;
23   pthread_t thr;
24   if (pthread_create(&thr, 0, f, &p))
25     return 2;
26   while (!sem1);
27   __atomic_thread_fence(__ATOMIC_ACQUIRE);
28   p = &p;
29   __atomic_store_n(&sem2, 1, __ATOMIC_RELEASE);
30   pthread_join(thr, &p);
31   return !p;
32 }
33