GCC Code Coverage Report


Directory: src/
File: src/standalone/latch.c
Date: 2026-09-30 11:11:31
Exec Total Coverage
Lines: 40 42 95.2%
Functions: 5 5 100.0%
Branches: 12 18 66.7%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2026 Tiger Data, Inc.
3 * Licensed under the PostgreSQL License. See LICENSE for details.
4 *
5 * latch_standalone.c - Wait/wake latch over a condition variable
6 *
7 * Standalone implementation of the Latch API the posting drain loop uses (see
8 * vs_latch.h). PG builds use PostgreSQL's Latch instead, so this file is
9 * compiled only for standalone.
10 */
11
12 #ifdef VS_STANDALONE
13
14 #include <errno.h>
15 #include <time.h>
16
17 #include "standalone/latch.h"
18
19 __thread Latch *MyLatch = NULL;
20
21 void
22 206 InitLatch(Latch *latch)
23 {
24 206 pthread_mutex_init(&latch->mutex, NULL);
25 206 pthread_cond_init(&latch->cv, NULL);
26 206 latch->is_set = false;
27 206 }
28
29 void
30 200 vs_latch_attach_self(Latch *latch)
31 {
32 200 MyLatch = latch;
33 200 }
34
35 void
36 6 SetLatch(Latch *latch)
37 {
38 6 pthread_mutex_lock(&latch->mutex);
39 6 latch->is_set = true;
40 6 pthread_cond_broadcast(&latch->cv);
41 6 pthread_mutex_unlock(&latch->mutex);
42 6 }
43
44 void
45 76 ResetLatch(Latch *latch)
46 {
47 76 pthread_mutex_lock(&latch->mutex);
48 76 latch->is_set = false;
49 76 pthread_mutex_unlock(&latch->mutex);
50 76 }
51
52 int
53 82 WaitLatch(
54 Latch *latch,
55 int wakeEvents,
56 long timeout_ms,
57 uint32_t wait_event_info)
58 {
59 82 int result = 0;
60
61 (void)wait_event_info;
62
63 82 pthread_mutex_lock(&latch->mutex);
64
65
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 4 times.
82 if (!latch->is_set)
66 {
67
2/4
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 78 times.
✗ Branch 3 not taken.
78 if ((wakeEvents & WL_TIMEOUT) && timeout_ms >= 0)
68 78 {
69 struct timespec ts;
70 78 int rc = 0;
71
72 78 clock_gettime(CLOCK_REALTIME, &ts);
73 78 ts.tv_sec += timeout_ms / 1000;
74 78 ts.tv_nsec += (timeout_ms % 1000) * 1000000L;
75
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 76 times.
78 if (ts.tv_nsec >= 1000000000L)
76 {
77 2 ts.tv_sec++;
78 2 ts.tv_nsec -= 1000000000L;
79 }
80
81
3/4
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 78 times.
✓ Branch 3 taken 78 times.
156 while (!latch->is_set && rc != ETIMEDOUT)
82 78 rc = pthread_cond_timedwait(&latch->cv, &latch->mutex, &ts);
83
84
1/2
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
78 if (rc == ETIMEDOUT)
85 78 result |= WL_TIMEOUT;
86 }
87 else
88 {
89 ✗ while (!latch->is_set)
90 ✗ pthread_cond_wait(&latch->cv, &latch->mutex);
91 }
92 }
93
94
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 78 times.
82 if (latch->is_set)
95 4 result |= WL_LATCH_SET;
96
97 82 pthread_mutex_unlock(&latch->mutex);
98 82 return result;
99 }
100
101 #endif /* VS_STANDALONE */
102