4
* This file implements the "barrier" synchronization construct.
6
* A barrier causes threads to wait until a set of threads has
7
* all "reached" the barrier. The number of threads required is
8
* set when the barrier is initialized, and cannot be changed
9
* except by reinitializing.
11
* The barrier_init() and barrier_destroy() functions,
12
* respectively, allow you to initialize and destroy the
15
* The barrier_wait() function allows a thread to wait for a
16
* barrier to be completed. One thread (the one that happens to
17
* arrive last) will return from barrier_wait() with the status
18
* -1 on success -- others will return with 0. The special
19
* status makes it easy for the calling code to cause one thread
20
* to do something in a serial region before entering another
21
* parallel section of code.
28
* Initialize a barrier for use.
30
int barrier_init (barrier_t *barrier, int count)
34
barrier->threshold = barrier->counter = count;
36
status = pthread_mutex_init (&barrier->mutex, NULL);
39
status = pthread_cond_init (&barrier->cv, NULL);
41
pthread_mutex_destroy (&barrier->mutex);
44
barrier->valid = BARRIER_VALID;
49
* Destroy a barrier when done using it.
51
int barrier_destroy (barrier_t *barrier)
55
if (barrier->valid != BARRIER_VALID)
58
status = pthread_mutex_lock (&barrier->mutex);
63
* Check whether any threads are known to be waiting; report
66
if (barrier->counter != barrier->threshold) {
67
pthread_mutex_unlock (&barrier->mutex);
72
status = pthread_mutex_unlock (&barrier->mutex);
77
* If unable to destroy either 1003.1c synchronization
78
* object, return the error status.
80
status = pthread_mutex_destroy (&barrier->mutex);
81
status2 = pthread_cond_destroy (&barrier->cv);
82
return (status == 0 ? status : status2);
86
* Wait for all members of a barrier to reach the barrier. When
87
* the count (of remaining members) reaches 0, broadcast to wake
88
* all threads waiting.
90
int barrier_wait (barrier_t *barrier)
92
int status, cancel, tmp, cycle;
94
if (barrier->valid != BARRIER_VALID)
97
status = pthread_mutex_lock (&barrier->mutex);
101
cycle = barrier->cycle; /* Remember which cycle we're on */
103
if (--barrier->counter == 0) {
104
barrier->cycle = !barrier->cycle;
105
barrier->counter = barrier->threshold;
106
status = pthread_cond_broadcast (&barrier->cv);
108
* The last thread into the barrier will return status
109
* -1 rather than 0, so that it can be used to perform
110
* some special serial code following the barrier.
116
* Wait with cancellation disabled, because barrier_wait
117
* should not be a cancellation point.
119
pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &cancel);
122
* Wait until the barrier's cycle changes, which means
123
* that it has been broadcast, and we don't want to wait
126
while (cycle == barrier->cycle) {
127
status = pthread_cond_wait (
128
&barrier->cv, &barrier->mutex);
129
if (status != 0) break;
132
pthread_setcancelstate (cancel, &tmp);
135
* Ignore an error in unlocking. It shouldn't happen, and
136
* reporting it here would be misleading -- the barrier wait
137
* completed, after all, whereas returning, for example,
138
* EINVAL would imply the wait had failed. The next attempt
139
* to use the barrier *will* return an error, or hang, due
140
* to whatever happened to the mutex.
142
pthread_mutex_unlock (&barrier->mutex);
143
return status; /* error, -1 for waker, or 0 */