1 /* Copyright (C) 2005-2021 Free Software Foundation, Inc.
2    Contributed by Sebastian Huber <sebastian.huber@embedded-brains.de>.
3 
4    This file is part of the GNU OpenMP Library (libgomp).
5 
6    Libgomp is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3, or (at your option)
9    any later version.
10 
11    Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
12    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13    FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14    more details.
15 
16    Under Section 7 of GPL version 3, you are granted additional
17    permissions described in the GCC Runtime Library Exception, version
18    3.1, as published by the Free Software Foundation.
19 
20    You should have received a copy of the GNU General Public License and
21    a copy of the GCC Runtime Library Exception along with this program;
22    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23    <http://www.gnu.org/licenses/>.  */
24 
25 /* This is the RTEMS implementation of a barrier synchronization
26    mechanism for libgomp.  It is identical to the Linux implementation, except
27    that the futex API is slightly different.  This type is private to the
28    library.  */
29 
30 #include "libgomp.h"
31 #include "bar.h"
32 #include <limits.h>
33 
34 static gomp_barrier_t *
generation_to_barrier(int * addr)35 generation_to_barrier (int *addr)
36 {
37   return (gomp_barrier_t *)
38 	 ((char *) addr - __builtin_offsetof (gomp_barrier_t, generation));
39 }
40 
41 static void
futex_wait(int * addr,int val)42 futex_wait (int *addr, int val)
43 {
44   gomp_barrier_t *bar = generation_to_barrier (addr);
45   _Futex_Wait (&bar->futex, addr, val);
46 }
47 
48 static void
futex_wake(int * addr,int count)49 futex_wake (int *addr, int count)
50 {
51   gomp_barrier_t *bar = generation_to_barrier (addr);
52   _Futex_Wake (&bar->futex, count);
53 }
54 
55 static int
do_spin(int * addr,int val)56 do_spin (int *addr, int val)
57 {
58   unsigned long long i, count = gomp_spin_count_var;
59 
60   if (__builtin_expect (gomp_managed_threads > gomp_available_cpus, 0))
61     count = gomp_throttled_spin_count_var;
62   for (i = 0; i < count; i++)
63     if (__builtin_expect (__atomic_load_n (addr, MEMMODEL_RELAXED) != val, 0))
64       return 0;
65   return 1;
66 }
67 
68 static void
do_wait(int * addr,int val)69 do_wait (int *addr, int val)
70 {
71   if (do_spin (addr, val))
72     futex_wait (addr, val);
73 }
74 
75 #define GOMP_WAIT_H 1
76 #include "../linux/bar.c"
77