Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * pthread_barrier_wait.c
4 : * Implementation of pthread_barrier_t support for platforms lacking it.
5 : *
6 : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : *
8 : * IDENTIFICATION
9 : * src/port/pthread_barrier_wait.c
10 : *
11 : *-------------------------------------------------------------------------
12 : */
13 :
14 : #include "c.h"
15 :
16 : #include "port/pg_pthread.h"
17 :
18 : int
19 0 : pthread_barrier_init(pthread_barrier_t *barrier, const void *attr, int count)
20 : {
21 0 : int error;
22 :
23 0 : barrier->sense = false;
24 0 : barrier->count = count;
25 0 : barrier->arrived = 0;
26 0 : if ((error = pthread_cond_init(&barrier->cond, NULL)) != 0)
27 0 : return error;
28 0 : if ((error = pthread_mutex_init(&barrier->mutex, NULL)) != 0)
29 : {
30 0 : pthread_cond_destroy(&barrier->cond);
31 0 : return error;
32 : }
33 :
34 0 : return 0;
35 0 : }
36 :
37 : int
38 0 : pthread_barrier_wait(pthread_barrier_t *barrier)
39 : {
40 0 : bool initial_sense;
41 :
42 0 : pthread_mutex_lock(&barrier->mutex);
43 :
44 : /* We have arrived at the barrier. */
45 0 : barrier->arrived++;
46 0 : Assert(barrier->arrived <= barrier->count);
47 :
48 : /* If we were the last to arrive, release the others and return. */
49 0 : if (barrier->arrived == barrier->count)
50 : {
51 0 : barrier->arrived = 0;
52 0 : barrier->sense = !barrier->sense;
53 0 : pthread_mutex_unlock(&barrier->mutex);
54 0 : pthread_cond_broadcast(&barrier->cond);
55 :
56 0 : return PTHREAD_BARRIER_SERIAL_THREAD;
57 : }
58 :
59 : /* Wait for someone else to flip the sense. */
60 0 : initial_sense = barrier->sense;
61 0 : do
62 : {
63 0 : pthread_cond_wait(&barrier->cond, &barrier->mutex);
64 0 : } while (barrier->sense == initial_sense);
65 :
66 0 : pthread_mutex_unlock(&barrier->mutex);
67 :
68 0 : return 0;
69 0 : }
70 :
71 : int
72 0 : pthread_barrier_destroy(pthread_barrier_t *barrier)
73 : {
74 0 : pthread_cond_destroy(&barrier->cond);
75 0 : pthread_mutex_destroy(&barrier->mutex);
76 0 : return 0;
77 : }
|