1 /* Copyright (C) 2005, 2008, 2009 Free Software Foundation, Inc. 2 Contributed by Richard Henderson <rth@redhat.com>. 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 default implementation of a barrier synchronization mechanism 26 for libgomp. This type is private to the library. Note that we rely on 27 being able to adjust the barrier count while threads are blocked, so the 28 POSIX pthread_barrier_t won't work. */ 29 30 #ifndef GOMP_BARRIER_H 31 #define GOMP_BARRIER_H 1 32 33 #include <pthread.h> 34 35 typedef struct 36 { 37 gomp_mutex_t mutex1; 38 #ifndef HAVE_SYNC_BUILTINS 39 gomp_mutex_t mutex2; 40 #endif 41 gomp_sem_t sem1; 42 gomp_sem_t sem2; 43 unsigned total; 44 unsigned arrived; 45 unsigned generation; 46 } gomp_barrier_t; 47 typedef unsigned int gomp_barrier_state_t; 48 49 extern void gomp_barrier_init (gomp_barrier_t *, unsigned); 50 extern void gomp_barrier_reinit (gomp_barrier_t *, unsigned); 51 extern void gomp_barrier_destroy (gomp_barrier_t *); 52 53 extern void gomp_barrier_wait (gomp_barrier_t *); 54 extern void gomp_barrier_wait_end (gomp_barrier_t *, gomp_barrier_state_t); 55 extern void gomp_team_barrier_wait (gomp_barrier_t *); 56 extern void gomp_team_barrier_wait_end (gomp_barrier_t *, 57 gomp_barrier_state_t); 58 extern void gomp_team_barrier_wake (gomp_barrier_t *, int); 59 60 static inline gomp_barrier_state_t 61 gomp_barrier_wait_start (gomp_barrier_t *bar) 62 { 63 unsigned int ret; 64 gomp_mutex_lock (&bar->mutex1); 65 ret = bar->generation & ~3; 66 ret += ++bar->arrived == bar->total; 67 return ret; 68 } 69 70 static inline bool 71 gomp_barrier_last_thread (gomp_barrier_state_t state) 72 { 73 return state & 1; 74 } 75 76 static inline void 77 gomp_barrier_wait_last (gomp_barrier_t *bar) 78 { 79 gomp_barrier_wait (bar); 80 } 81 82 /* All the inlines below must be called with team->task_lock 83 held. */ 84 85 static inline void 86 gomp_team_barrier_set_task_pending (gomp_barrier_t *bar) 87 { 88 bar->generation |= 1; 89 } 90 91 static inline void 92 gomp_team_barrier_clear_task_pending (gomp_barrier_t *bar) 93 { 94 bar->generation &= ~1; 95 } 96 97 static inline void 98 gomp_team_barrier_set_waiting_for_tasks (gomp_barrier_t *bar) 99 { 100 bar->generation |= 2; 101 } 102 103 static inline bool 104 gomp_team_barrier_waiting_for_tasks (gomp_barrier_t *bar) 105 { 106 return (bar->generation & 2) != 0; 107 } 108 109 static inline void 110 gomp_team_barrier_done (gomp_barrier_t *bar, gomp_barrier_state_t state) 111 { 112 bar->generation = (state & ~3) + 4; 113 } 114 115 #endif /* GOMP_BARRIER_H */ 116