xref: /openbsd-src/gnu/gcc/libgomp/testsuite/libgomp.c/single-1.c (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert /* Trivial test of single.  */
2*404b540aSrobert 
3*404b540aSrobert /* { dg-require-effective-target sync_int_long } */
4*404b540aSrobert 
5*404b540aSrobert #include <omp.h>
6*404b540aSrobert #include <sys/time.h>
7*404b540aSrobert #include <unistd.h>
8*404b540aSrobert #include <assert.h>
9*404b540aSrobert #include "libgomp_g.h"
10*404b540aSrobert 
11*404b540aSrobert 
12*404b540aSrobert static int test;
13*404b540aSrobert 
f_nocopy(void * dummy)14*404b540aSrobert static void f_nocopy (void *dummy)
15*404b540aSrobert {
16*404b540aSrobert   if (GOMP_single_start ())
17*404b540aSrobert     {
18*404b540aSrobert       int iam = omp_get_thread_num ();
19*404b540aSrobert       int old = __sync_lock_test_and_set (&test, iam);
20*404b540aSrobert       assert (old == -1);
21*404b540aSrobert     }
22*404b540aSrobert }
23*404b540aSrobert 
f_copy(void * dummy)24*404b540aSrobert static void f_copy (void *dummy)
25*404b540aSrobert {
26*404b540aSrobert   int *x = GOMP_single_copy_start ();
27*404b540aSrobert   if (x == NULL)
28*404b540aSrobert     {
29*404b540aSrobert       int iam = omp_get_thread_num ();
30*404b540aSrobert       int old = __sync_lock_test_and_set (&test, iam);
31*404b540aSrobert       assert (old == -1);
32*404b540aSrobert       GOMP_single_copy_end (&test);
33*404b540aSrobert     }
34*404b540aSrobert   else
35*404b540aSrobert     assert (x == &test);
36*404b540aSrobert }
37*404b540aSrobert 
main()38*404b540aSrobert int main()
39*404b540aSrobert {
40*404b540aSrobert   omp_set_dynamic (0);
41*404b540aSrobert 
42*404b540aSrobert   test = -1;
43*404b540aSrobert   GOMP_parallel_start (f_nocopy, NULL, 3);
44*404b540aSrobert   f_nocopy (NULL);
45*404b540aSrobert   GOMP_parallel_end ();
46*404b540aSrobert 
47*404b540aSrobert   test = -1;
48*404b540aSrobert   GOMP_parallel_start (f_copy, NULL, 3);
49*404b540aSrobert   f_copy (NULL);
50*404b540aSrobert   GOMP_parallel_end ();
51*404b540aSrobert 
52*404b540aSrobert   return 0;
53*404b540aSrobert }
54