aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/atomic/pr80640-2.c
blob: e33dcc386ab6f66ed13e09097183091d03e1117d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* { dg-do run } */
/* { dg-options "-pthread" } */
/* { dg-require-effective-target pthread } */

#include <pthread.h>

static volatile int sem1;
static _Atomic  int sem2;

static void *f(void *va)
{
  void **p = va;
  if (*p) return *p;
  sem1 = 1;
  while (!__atomic_load_n(&sem2, __ATOMIC_ACQUIRE))
    sched_yield ();
  // GCC used to RTL-CSE this and the first load, causing 0 to be returned
  return *p;
}

int main()
{
  void *p = 0;
  pthread_t thr;
  if (pthread_create(&thr, 0, f, &p))
    return 2;
  while (!sem1)
    sched_yield ();
  __atomic_thread_fence(__ATOMIC_ACQUIRE);
  p = &p;
  __atomic_store_n(&sem2, 1, __ATOMIC_RELEASE);
  pthread_join(thr, &p);
  return !p;
}