aboutsummaryrefslogtreecommitdiff
path: root/libgomp/testsuite/libgomp.c-c++-common/task-detach-13.c
blob: 9622fd8251f0cbb6fc81feff8461ad1f6f55118e (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* { dg-do run { target *-*-linux* *-*-gnu* *-*-freebsd* } } */
/* { dg-timeout 10 } */

/* Test that omp_fulfill_event works when called from an external
   non-OpenMP thread.  */

#include <omp.h>
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>

int finished = 0;
int event_pending = 0;
omp_event_handle_t detach_event;

void *
fulfill_thread (void *)
{
  while (!__atomic_load_n (&finished, __ATOMIC_RELAXED))
    {
      if (__atomic_load_n (&event_pending, __ATOMIC_ACQUIRE))
	{
	  omp_fulfill_event (detach_event);
	  __atomic_store_n (&event_pending, 0, __ATOMIC_RELEASE);
	}

      sleep(1);
    }

  return 0;
}

int
main (void)
{
  pthread_t thr;
  int dep;
  pthread_create (&thr, NULL, fulfill_thread, 0);

  #pragma omp parallel
    #pragma omp single
    {
      omp_event_handle_t ev;

      #pragma omp task depend (out: dep) detach (ev)
      {
	detach_event = ev;
	__atomic_store_n (&event_pending, 1, __ATOMIC_RELEASE);
      }

      #pragma omp task depend (in: dep)
      {
	__atomic_store_n (&finished, 1, __ATOMIC_RELAXED);
      }
    }

  pthread_join (thr, 0);
  return 0;
}