xref: /openbsd-src/gnu/gcc/libgomp/testsuite/libgomp.c/critical-1.c (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert /* Trivial test of critical sections.  */
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 volatile int test = -1;
13*404b540aSrobert 
function(void * dummy)14*404b540aSrobert static void function(void *dummy)
15*404b540aSrobert {
16*404b540aSrobert   int iam = omp_get_thread_num ();
17*404b540aSrobert   int old;
18*404b540aSrobert 
19*404b540aSrobert   GOMP_critical_start ();
20*404b540aSrobert 
21*404b540aSrobert   old = __sync_lock_test_and_set (&test, iam);
22*404b540aSrobert   assert (old == -1);
23*404b540aSrobert 
24*404b540aSrobert   usleep (10);
25*404b540aSrobert   test = -1;
26*404b540aSrobert 
27*404b540aSrobert   GOMP_critical_end ();
28*404b540aSrobert }
29*404b540aSrobert 
main()30*404b540aSrobert int main()
31*404b540aSrobert {
32*404b540aSrobert   omp_set_dynamic (0);
33*404b540aSrobert 
34*404b540aSrobert   GOMP_parallel_start (function, NULL, 3);
35*404b540aSrobert   function (NULL);
36*404b540aSrobert   GOMP_parallel_end ();
37*404b540aSrobert 
38*404b540aSrobert   return 0;
39*404b540aSrobert }
40