xref: /dflybsd-src/contrib/gcc-4.7/libgomp/config/posix/bar.h (revision 94b98a915ba84699b2a2adab9146fbc2cf617459)
1*629ff9f7SJohn Marino /* Copyright (C) 2005, 2008, 2009 Free Software Foundation, Inc.
2*629ff9f7SJohn Marino    Contributed by Richard Henderson <rth@redhat.com>.
3*629ff9f7SJohn Marino 
4*629ff9f7SJohn Marino    This file is part of the GNU OpenMP Library (libgomp).
5*629ff9f7SJohn Marino 
6*629ff9f7SJohn Marino    Libgomp is free software; you can redistribute it and/or modify it
7*629ff9f7SJohn Marino    under the terms of the GNU General Public License as published by
8*629ff9f7SJohn Marino    the Free Software Foundation; either version 3, or (at your option)
9*629ff9f7SJohn Marino    any later version.
10*629ff9f7SJohn Marino 
11*629ff9f7SJohn Marino    Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
12*629ff9f7SJohn Marino    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13*629ff9f7SJohn Marino    FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14*629ff9f7SJohn Marino    more details.
15*629ff9f7SJohn Marino 
16*629ff9f7SJohn Marino    Under Section 7 of GPL version 3, you are granted additional
17*629ff9f7SJohn Marino    permissions described in the GCC Runtime Library Exception, version
18*629ff9f7SJohn Marino    3.1, as published by the Free Software Foundation.
19*629ff9f7SJohn Marino 
20*629ff9f7SJohn Marino    You should have received a copy of the GNU General Public License and
21*629ff9f7SJohn Marino    a copy of the GCC Runtime Library Exception along with this program;
22*629ff9f7SJohn Marino    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23*629ff9f7SJohn Marino    <http://www.gnu.org/licenses/>.  */
24*629ff9f7SJohn Marino 
25*629ff9f7SJohn Marino /* This is the default implementation of a barrier synchronization mechanism
26*629ff9f7SJohn Marino    for libgomp.  This type is private to the library.  Note that we rely on
27*629ff9f7SJohn Marino    being able to adjust the barrier count while threads are blocked, so the
28*629ff9f7SJohn Marino    POSIX pthread_barrier_t won't work.  */
29*629ff9f7SJohn Marino 
30*629ff9f7SJohn Marino #ifndef GOMP_BARRIER_H
31*629ff9f7SJohn Marino #define GOMP_BARRIER_H 1
32*629ff9f7SJohn Marino 
33*629ff9f7SJohn Marino #include <pthread.h>
34*629ff9f7SJohn Marino 
35*629ff9f7SJohn Marino typedef struct
36*629ff9f7SJohn Marino {
37*629ff9f7SJohn Marino   gomp_mutex_t mutex1;
38*629ff9f7SJohn Marino #ifndef HAVE_SYNC_BUILTINS
39*629ff9f7SJohn Marino   gomp_mutex_t mutex2;
40*629ff9f7SJohn Marino #endif
41*629ff9f7SJohn Marino   gomp_sem_t sem1;
42*629ff9f7SJohn Marino   gomp_sem_t sem2;
43*629ff9f7SJohn Marino   unsigned total;
44*629ff9f7SJohn Marino   unsigned arrived;
45*629ff9f7SJohn Marino   unsigned generation;
46*629ff9f7SJohn Marino } gomp_barrier_t;
47*629ff9f7SJohn Marino typedef unsigned int gomp_barrier_state_t;
48*629ff9f7SJohn Marino 
49*629ff9f7SJohn Marino extern void gomp_barrier_init (gomp_barrier_t *, unsigned);
50*629ff9f7SJohn Marino extern void gomp_barrier_reinit (gomp_barrier_t *, unsigned);
51*629ff9f7SJohn Marino extern void gomp_barrier_destroy (gomp_barrier_t *);
52*629ff9f7SJohn Marino 
53*629ff9f7SJohn Marino extern void gomp_barrier_wait (gomp_barrier_t *);
54*629ff9f7SJohn Marino extern void gomp_barrier_wait_end (gomp_barrier_t *, gomp_barrier_state_t);
55*629ff9f7SJohn Marino extern void gomp_team_barrier_wait (gomp_barrier_t *);
56*629ff9f7SJohn Marino extern void gomp_team_barrier_wait_end (gomp_barrier_t *,
57*629ff9f7SJohn Marino 					gomp_barrier_state_t);
58*629ff9f7SJohn Marino extern void gomp_team_barrier_wake (gomp_barrier_t *, int);
59*629ff9f7SJohn Marino 
60*629ff9f7SJohn Marino static inline gomp_barrier_state_t
gomp_barrier_wait_start(gomp_barrier_t * bar)61*629ff9f7SJohn Marino gomp_barrier_wait_start (gomp_barrier_t *bar)
62*629ff9f7SJohn Marino {
63*629ff9f7SJohn Marino   unsigned int ret;
64*629ff9f7SJohn Marino   gomp_mutex_lock (&bar->mutex1);
65*629ff9f7SJohn Marino   ret = bar->generation & ~3;
66*629ff9f7SJohn Marino   ret += ++bar->arrived == bar->total;
67*629ff9f7SJohn Marino   return ret;
68*629ff9f7SJohn Marino }
69*629ff9f7SJohn Marino 
70*629ff9f7SJohn Marino static inline bool
gomp_barrier_last_thread(gomp_barrier_state_t state)71*629ff9f7SJohn Marino gomp_barrier_last_thread (gomp_barrier_state_t state)
72*629ff9f7SJohn Marino {
73*629ff9f7SJohn Marino   return state & 1;
74*629ff9f7SJohn Marino }
75*629ff9f7SJohn Marino 
76*629ff9f7SJohn Marino static inline void
gomp_barrier_wait_last(gomp_barrier_t * bar)77*629ff9f7SJohn Marino gomp_barrier_wait_last (gomp_barrier_t *bar)
78*629ff9f7SJohn Marino {
79*629ff9f7SJohn Marino   gomp_barrier_wait (bar);
80*629ff9f7SJohn Marino }
81*629ff9f7SJohn Marino 
82*629ff9f7SJohn Marino /* All the inlines below must be called with team->task_lock
83*629ff9f7SJohn Marino    held.  */
84*629ff9f7SJohn Marino 
85*629ff9f7SJohn Marino static inline void
gomp_team_barrier_set_task_pending(gomp_barrier_t * bar)86*629ff9f7SJohn Marino gomp_team_barrier_set_task_pending (gomp_barrier_t *bar)
87*629ff9f7SJohn Marino {
88*629ff9f7SJohn Marino   bar->generation |= 1;
89*629ff9f7SJohn Marino }
90*629ff9f7SJohn Marino 
91*629ff9f7SJohn Marino static inline void
gomp_team_barrier_clear_task_pending(gomp_barrier_t * bar)92*629ff9f7SJohn Marino gomp_team_barrier_clear_task_pending (gomp_barrier_t *bar)
93*629ff9f7SJohn Marino {
94*629ff9f7SJohn Marino   bar->generation &= ~1;
95*629ff9f7SJohn Marino }
96*629ff9f7SJohn Marino 
97*629ff9f7SJohn Marino static inline void
gomp_team_barrier_set_waiting_for_tasks(gomp_barrier_t * bar)98*629ff9f7SJohn Marino gomp_team_barrier_set_waiting_for_tasks (gomp_barrier_t *bar)
99*629ff9f7SJohn Marino {
100*629ff9f7SJohn Marino   bar->generation |= 2;
101*629ff9f7SJohn Marino }
102*629ff9f7SJohn Marino 
103*629ff9f7SJohn Marino static inline bool
gomp_team_barrier_waiting_for_tasks(gomp_barrier_t * bar)104*629ff9f7SJohn Marino gomp_team_barrier_waiting_for_tasks (gomp_barrier_t *bar)
105*629ff9f7SJohn Marino {
106*629ff9f7SJohn Marino   return (bar->generation & 2) != 0;
107*629ff9f7SJohn Marino }
108*629ff9f7SJohn Marino 
109*629ff9f7SJohn Marino static inline void
gomp_team_barrier_done(gomp_barrier_t * bar,gomp_barrier_state_t state)110*629ff9f7SJohn Marino gomp_team_barrier_done (gomp_barrier_t *bar, gomp_barrier_state_t state)
111*629ff9f7SJohn Marino {
112*629ff9f7SJohn Marino   bar->generation = (state & ~3) + 4;
113*629ff9f7SJohn Marino }
114*629ff9f7SJohn Marino 
115*629ff9f7SJohn Marino #endif /* GOMP_BARRIER_H */
116