GCC Code Coverage Report


Directory: src/
File: src/standalone/barrier.c
Date: 2026-11-18 16:12:57
Exec Total Coverage
Lines: 47 47 100.0%
Functions: 6 6 100.0%
Branches: 7 8 87.5%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2026 Tiger Data, Inc.
3 * Licensed under the PostgreSQL License. See LICENSE for details.
4 *
5 * barrier_standalone.c - Dynamic phase barrier over pthreads
6 *
7 * Standalone implementation of the PostgreSQL Barrier API used by the
8 * parallel build. See vs_barrier.h. PG builds use PostgreSQL's Barrier
9 * instead, so this file is compiled only for standalone.
10 */
11
12 #ifdef VS_STANDALONE
13
14 #include "standalone/barrier.h"
15
16 void
17 114 BarrierInit(Barrier *barrier, int participants)
18 {
19 114 pthread_mutex_init(&barrier->mutex, NULL);
20 114 pthread_cond_init(&barrier->cv, NULL);
21 114 barrier->participants = participants;
22 114 barrier->arrived = 0;
23 114 barrier->phase = 0;
24 114 }
25
26 bool
27 8520 BarrierArriveAndWait(Barrier *barrier, uint32_t wait_event_info)
28 {
29 bool elected;
30
31 (void)wait_event_info; /* no wait-event reporting in standalone */
32
33 8520 pthread_mutex_lock(&barrier->mutex);
34
35 8520 int my_phase = barrier->phase;
36
37
2/2
✓ Branch 0 taken 2976 times.
✓ Branch 1 taken 5544 times.
8520 if (++barrier->arrived == barrier->participants)
38 {
39 /* Last to arrive: complete the phase and release everyone. */
40 2976 barrier->arrived = 0;
41 2976 barrier->phase++;
42 2976 pthread_cond_broadcast(&barrier->cv);
43 2976 elected = true;
44 }
45 else
46 {
47 /* Wait until some other participant completes the phase. */
48
2/2
✓ Branch 0 taken 5544 times.
✓ Branch 1 taken 5544 times.
11088 while (barrier->phase == my_phase)
49 5544 pthread_cond_wait(&barrier->cv, &barrier->mutex);
50 5544 elected = false;
51 }
52
53 8520 pthread_mutex_unlock(&barrier->mutex);
54 8520 return elected;
55 }
56
57 int
58 268 BarrierAttach(Barrier *barrier)
59 {
60 int phase;
61
62 268 pthread_mutex_lock(&barrier->mutex);
63 268 barrier->participants++;
64 268 phase = barrier->phase;
65 268 pthread_mutex_unlock(&barrier->mutex);
66 268 return phase;
67 }
68
69 bool
70 270 BarrierDetach(Barrier *barrier)
71 {
72 270 bool released = false;
73
74 270 pthread_mutex_lock(&barrier->mutex);
75 270 barrier->participants--;
76
77 /*
78 * If the participants that remain have all already arrived, this detach
79 * completes the current phase — otherwise they would wait forever for a
80 * party member that has left.
81 */
82
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 268 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
270 if (barrier->arrived > 0 && barrier->arrived == barrier->participants)
83 {
84 2 barrier->arrived = 0;
85 2 barrier->phase++;
86 2 pthread_cond_broadcast(&barrier->cv);
87 2 released = true;
88 }
89
90 270 pthread_mutex_unlock(&barrier->mutex);
91 270 return released;
92 }
93
94 int
95 6 BarrierPhase(Barrier *barrier)
96 {
97 int phase;
98
99 6 pthread_mutex_lock(&barrier->mutex);
100 6 phase = barrier->phase;
101 6 pthread_mutex_unlock(&barrier->mutex);
102 6 return phase;
103 }
104
105 int
106 1605 BarrierParticipants(Barrier *barrier)
107 {
108 int n;
109
110 1605 pthread_mutex_lock(&barrier->mutex);
111 1605 n = barrier->participants;
112 1605 pthread_mutex_unlock(&barrier->mutex);
113 1605 return n;
114 }
115
116 #endif /* VS_STANDALONE */
117