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 a Linux specific implementation of a barrier synchronization 26 mechanism for libgomp. This type is private to the library. This 27 implementation uses atomic instructions and the futex syscall. */ 28 29 #ifndef GOMP_BARRIER_H 30 #define GOMP_BARRIER_H 1 31 32 #include "mutex.h" 33 34 typedef struct 35 { 36 /* Make sure total/generation is in a mostly read cacheline, while 37 awaited in a separate cacheline. */ 38 unsigned total __attribute__((aligned (64))); 39 unsigned generation; 40 unsigned awaited __attribute__((aligned (64))); 41 } gomp_barrier_t; 42 typedef unsigned int gomp_barrier_state_t; 43 44 static inline void gomp_barrier_init (gomp_barrier_t *bar, unsigned count) 45 { 46 bar->total = count; 47 bar->awaited = count; 48 bar->generation = 0; 49 } 50 51 static inline void gomp_barrier_reinit (gomp_barrier_t *bar, unsigned count) 52 { 53 __sync_fetch_and_add (&bar->awaited, count - bar->total); 54 bar->total = count; 55 } 56 57 static inline void gomp_barrier_destroy (gomp_barrier_t *bar) 58 { 59 } 60 61 extern void gomp_barrier_wait (gomp_barrier_t *); 62 extern void gomp_barrier_wait_last (gomp_barrier_t *); 63 extern void gomp_barrier_wait_end (gomp_barrier_t *, gomp_barrier_state_t); 64 extern void gomp_team_barrier_wait (gomp_barrier_t *); 65 extern void gomp_team_barrier_wait_end (gomp_barrier_t *, 66 gomp_barrier_state_t); 67 extern void gomp_team_barrier_wake (gomp_barrier_t *, int); 68 69 static inline gomp_barrier_state_t 70 gomp_barrier_wait_start (gomp_barrier_t *bar) 71 { 72 unsigned int ret = bar->generation & ~3; 73 /* Do we need any barrier here or is __sync_add_and_fetch acting 74 as the needed LoadLoad barrier already? */ 75 ret += __sync_add_and_fetch (&bar->awaited, -1) == 0; 76 return ret; 77 } 78 79 static inline bool 80 gomp_barrier_last_thread (gomp_barrier_state_t state) 81 { 82 return state & 1; 83 } 84 85 /* All the inlines below must be called with team->task_lock 86 held. */ 87 88 static inline void 89 gomp_team_barrier_set_task_pending (gomp_barrier_t *bar) 90 { 91 bar->generation |= 1; 92 } 93 94 static inline void 95 gomp_team_barrier_clear_task_pending (gomp_barrier_t *bar) 96 { 97 bar->generation &= ~1; 98 } 99 100 static inline void 101 gomp_team_barrier_set_waiting_for_tasks (gomp_barrier_t *bar) 102 { 103 bar->generation |= 2; 104 } 105 106 static inline bool 107 gomp_team_barrier_waiting_for_tasks (gomp_barrier_t *bar) 108 { 109 return (bar->generation & 2) != 0; 110 } 111 112 static inline void 113 gomp_team_barrier_done (gomp_barrier_t *bar, gomp_barrier_state_t state) 114 { 115 bar->generation = (state & ~3) + 4; 116 } 117 118 #endif /* GOMP_BARRIER_H */ 119