(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
atomic/
pr80640-2.c
       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  
      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      sched_yield ();
      17    // GCC used to RTL-CSE this and the first load, causing 0 to be returned
      18    return *p;
      19  }
      20  
      21  int main()
      22  {
      23    void *p = 0;
      24    pthread_t thr;
      25    if (pthread_create(&thr, 0, f, &p))
      26      return 2;
      27    while (!sem1)
      28      sched_yield ();
      29    __atomic_thread_fence(__ATOMIC_ACQUIRE);
      30    p = &p;
      31    __atomic_store_n(&sem2, 1, __ATOMIC_RELEASE);
      32    pthread_join(thr, &p);
      33    return !p;
      34  }