Fix wording in comment
[pgsql.git] / src / port / pthread_barrier_wait.c
blob835dbf1c7afb57ce7d9475f8eadfdad1cfbc9af9
1 /*-------------------------------------------------------------------------
3 * pthread_barrier_wait.c
4 * Implementation of pthread_barrier_t support for platforms lacking it.
6 * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
8 * IDENTIFICATION
9 * src/port/pthread_barrier_wait.c
11 *-------------------------------------------------------------------------
14 #include "c.h"
16 #include "port/pg_pthread.h"
18 int
19 pthread_barrier_init(pthread_barrier_t *barrier, const void *attr, int count)
21 int error;
23 barrier->sense = false;
24 barrier->count = count;
25 barrier->arrived = 0;
26 if ((error = pthread_cond_init(&barrier->cond, NULL)) != 0)
27 return error;
28 if ((error = pthread_mutex_init(&barrier->mutex, NULL)) != 0)
30 pthread_cond_destroy(&barrier->cond);
31 return error;
34 return 0;
37 int
38 pthread_barrier_wait(pthread_barrier_t *barrier)
40 bool initial_sense;
42 pthread_mutex_lock(&barrier->mutex);
44 /* We have arrived at the barrier. */
45 barrier->arrived++;
46 Assert(barrier->arrived <= barrier->count);
48 /* If we were the last to arrive, release the others and return. */
49 if (barrier->arrived == barrier->count)
51 barrier->arrived = 0;
52 barrier->sense = !barrier->sense;
53 pthread_mutex_unlock(&barrier->mutex);
54 pthread_cond_broadcast(&barrier->cond);
56 return PTHREAD_BARRIER_SERIAL_THREAD;
59 /* Wait for someone else to flip the sense. */
60 initial_sense = barrier->sense;
63 pthread_cond_wait(&barrier->cond, &barrier->mutex);
64 } while (barrier->sense == initial_sense);
66 pthread_mutex_unlock(&barrier->mutex);
68 return 0;
71 int
72 pthread_barrier_destroy(pthread_barrier_t *barrier)
74 pthread_cond_destroy(&barrier->cond);
75 pthread_mutex_destroy(&barrier->mutex);
76 return 0;