| 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 | 90 | BarrierInit(Barrier *barrier, int participants) | |
| 18 | { | ||
| 19 | 90 | pthread_mutex_init(&barrier->mutex, NULL); | |
| 20 | 90 | pthread_cond_init(&barrier->cv, NULL); | |
| 21 | 90 | barrier->participants = participants; | |
| 22 | 90 | barrier->arrived = 0; | |
| 23 | 90 | barrier->phase = 0; | |
| 24 | 90 | } | |
| 25 | |||
| 26 | bool | ||
| 27 | 7212 | 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 | 7212 | pthread_mutex_lock(&barrier->mutex); | |
| 34 | |||
| 35 | 7212 | int my_phase = barrier->phase; | |
| 36 | |||
| 37 |
2/2✓ Branch 0 taken 2400 times.
✓ Branch 1 taken 4812 times.
|
7212 | if (++barrier->arrived == barrier->participants) |
| 38 | { | ||
| 39 | /* Last to arrive: complete the phase and release everyone. */ | ||
| 40 | 2400 | barrier->arrived = 0; | |
| 41 | 2400 | barrier->phase++; | |
| 42 | 2400 | pthread_cond_broadcast(&barrier->cv); | |
| 43 | 2400 | elected = true; | |
| 44 | } | ||
| 45 | else | ||
| 46 | { | ||
| 47 | /* Wait until some other participant completes the phase. */ | ||
| 48 |
2/2✓ Branch 0 taken 4812 times.
✓ Branch 1 taken 4812 times.
|
9624 | while (barrier->phase == my_phase) |
| 49 | 4812 | pthread_cond_wait(&barrier->cv, &barrier->mutex); | |
| 50 | 4812 | elected = false; | |
| 51 | } | ||
| 52 | |||
| 53 | 7212 | pthread_mutex_unlock(&barrier->mutex); | |
| 54 | 7212 | return elected; | |
| 55 | } | ||
| 56 | |||
| 57 | int | ||
| 58 | 208 | BarrierAttach(Barrier *barrier) | |
| 59 | { | ||
| 60 | int phase; | ||
| 61 | |||
| 62 | 208 | pthread_mutex_lock(&barrier->mutex); | |
| 63 | 208 | barrier->participants++; | |
| 64 | 208 | phase = barrier->phase; | |
| 65 | 208 | pthread_mutex_unlock(&barrier->mutex); | |
| 66 | 208 | return phase; | |
| 67 | } | ||
| 68 | |||
| 69 | bool | ||
| 70 | 210 | BarrierDetach(Barrier *barrier) | |
| 71 | { | ||
| 72 | 210 | bool released = false; | |
| 73 | |||
| 74 | 210 | pthread_mutex_lock(&barrier->mutex); | |
| 75 | 210 | 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 208 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
210 | 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 | 210 | pthread_mutex_unlock(&barrier->mutex); | |
| 91 | 210 | 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 | 2108 | BarrierParticipants(Barrier *barrier) | |
| 107 | { | ||
| 108 | int n; | ||
| 109 | |||
| 110 | 2108 | pthread_mutex_lock(&barrier->mutex); | |
| 111 | 2108 | n = barrier->participants; | |
| 112 | 2108 | pthread_mutex_unlock(&barrier->mutex); | |
| 113 | 2108 | return n; | |
| 114 | } | ||
| 115 | |||
| 116 | #endif /* VS_STANDALONE */ | ||
| 117 |