GCC Code Coverage Report


Directory: src/
File: src/standalone/latch.c
Date: 2026-10-23 09:17:32
Exec Total Coverage
Lines: 38 42 90.5%
Functions: 5 5 100.0%
Branches: 11 18 61.1%

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 214 InitLatch(Latch *latch)
23 {
24 214 pthread_mutex_init(&latch->mutex, NULL);
25 214 pthread_cond_init(&latch->cv, NULL);
26 214 latch->is_set = false;
27 214 }
28
29 void
30 208 vs_latch_attach_self(Latch *latch)
31 {
32 208 MyLatch = latch;
33 208 }
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 80 ResetLatch(Latch *latch)
46 {
47 80 pthread_mutex_lock(&latch->mutex);
48 80 latch->is_set = false;
49 80 pthread_mutex_unlock(&latch->mutex);
50 80 }
51
52 int
53 86 WaitLatch(
54 Latch *latch,
55 int wakeEvents,
56 long timeout_ms,
57 uint32_t wait_event_info)
58 {
59 86 int result = 0;
60
61 (void)wait_event_info;
62
63 86 pthread_mutex_lock(&latch->mutex);
64
65
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 4 times.
86 if (!latch->is_set)
66 {
67
2/4
✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 82 times.
✗ Branch 3 not taken.
82 if ((wakeEvents & WL_TIMEOUT) && timeout_ms >= 0)
68 82 {
69 struct timespec ts;
70 82 int rc = 0;
71
72 82 clock_gettime(CLOCK_REALTIME, &ts);
73 82 ts.tv_sec += timeout_ms / 1000;
74 82 ts.tv_nsec += (timeout_ms % 1000) * 1000000L;
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82 times.
82 if (ts.tv_nsec >= 1000000000L)
76 {
77 ✗ ts.tv_sec++;
78 ✗ ts.tv_nsec -= 1000000000L;
79 }
80
81
3/4
✓ Branch 0 taken 164 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 82 times.
✓ Branch 3 taken 82 times.
164 while (!latch->is_set && rc != ETIMEDOUT)
82 82 rc = pthread_cond_timedwait(&latch->cv, &latch->mutex, &ts);
83
84
1/2
✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
82 if (rc == ETIMEDOUT)
85 82 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 82 times.
86 if (latch->is_set)
95 4 result |= WL_LATCH_SET;
96
97 86 pthread_mutex_unlock(&latch->mutex);
98 86 return result;
99 }
100
101 #endif /* VS_STANDALONE */
102