1 /*
2 * Copyright (c) 2017 Intel Corporation
3 * Copyright (c) 2023 Meta
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include "posix_internal.h"
9
10 #include <zephyr/init.h>
11 #include <zephyr/kernel.h>
12 #include <zephyr/logging/log.h>
13 #include <zephyr/posix/pthread.h>
14 #include <zephyr/sys/bitarray.h>
15
16 LOG_MODULE_REGISTER(pthread_cond, CONFIG_PTHREAD_COND_LOG_LEVEL);
17
18 int64_t timespec_to_timeoutms(const struct timespec *abstime);
19
20 static struct k_condvar posix_cond_pool[CONFIG_MAX_PTHREAD_COND_COUNT];
21 SYS_BITARRAY_DEFINE_STATIC(posix_cond_bitarray, CONFIG_MAX_PTHREAD_COND_COUNT);
22
23 /*
24 * We reserve the MSB to mark a pthread_cond_t as initialized (from the
25 * perspective of the application). With a linear space, this means that
26 * the theoretical pthread_cond_t range is [0,2147483647].
27 */
28 BUILD_ASSERT(CONFIG_MAX_PTHREAD_COND_COUNT < PTHREAD_OBJ_MASK_INIT,
29 "CONFIG_MAX_PTHREAD_COND_COUNT is too high");
30
posix_cond_to_offset(struct k_condvar * cv)31 static inline size_t posix_cond_to_offset(struct k_condvar *cv)
32 {
33 return cv - posix_cond_pool;
34 }
35
to_posix_cond_idx(pthread_cond_t cond)36 static inline size_t to_posix_cond_idx(pthread_cond_t cond)
37 {
38 return mark_pthread_obj_uninitialized(cond);
39 }
40
get_posix_cond(pthread_cond_t cond)41 static struct k_condvar *get_posix_cond(pthread_cond_t cond)
42 {
43 int actually_initialized;
44 size_t bit = to_posix_cond_idx(cond);
45
46 /* if the provided cond does not claim to be initialized, its invalid */
47 if (!is_pthread_obj_initialized(cond)) {
48 LOG_DBG("Cond is uninitialized (%x)", cond);
49 return NULL;
50 }
51
52 /* Mask off the MSB to get the actual bit index */
53 if (sys_bitarray_test_bit(&posix_cond_bitarray, bit, &actually_initialized) < 0) {
54 LOG_DBG("Cond is invalid (%x)", cond);
55 return NULL;
56 }
57
58 if (actually_initialized == 0) {
59 /* The cond claims to be initialized but is actually not */
60 LOG_DBG("Cond claims to be initialized (%x)", cond);
61 return NULL;
62 }
63
64 return &posix_cond_pool[bit];
65 }
66
to_posix_cond(pthread_cond_t * cvar)67 static struct k_condvar *to_posix_cond(pthread_cond_t *cvar)
68 {
69 size_t bit;
70 struct k_condvar *cv;
71
72 if (*cvar != PTHREAD_COND_INITIALIZER) {
73 return get_posix_cond(*cvar);
74 }
75
76 /* Try and automatically associate a posix_cond */
77 if (sys_bitarray_alloc(&posix_cond_bitarray, 1, &bit) < 0) {
78 /* No conds left to allocate */
79 LOG_DBG("Unable to allocate pthread_cond_t");
80 return NULL;
81 }
82
83 /* Record the associated posix_cond in mu and mark as initialized */
84 *cvar = mark_pthread_obj_initialized(bit);
85 cv = &posix_cond_pool[bit];
86
87 return cv;
88 }
89
cond_wait(pthread_cond_t * cond,pthread_mutex_t * mu,k_timeout_t timeout)90 static int cond_wait(pthread_cond_t *cond, pthread_mutex_t *mu, k_timeout_t timeout)
91 {
92 int ret;
93 struct k_mutex *m;
94 struct k_condvar *cv;
95
96 m = to_posix_mutex(mu);
97 cv = to_posix_cond(cond);
98 if (cv == NULL || m == NULL) {
99 return EINVAL;
100 }
101
102 LOG_DBG("Waiting on cond %p with timeout %llx", cv, timeout.ticks);
103 ret = k_condvar_wait(cv, m, timeout);
104 if (ret == -EAGAIN) {
105 LOG_DBG("Timeout waiting on cond %p", cv);
106 ret = ETIMEDOUT;
107 } else if (ret < 0) {
108 LOG_DBG("k_condvar_wait() failed: %d", ret);
109 ret = -ret;
110 } else {
111 __ASSERT_NO_MSG(ret == 0);
112 LOG_DBG("Cond %p received signal", cv);
113 }
114
115 return ret;
116 }
117
pthread_cond_signal(pthread_cond_t * cvar)118 int pthread_cond_signal(pthread_cond_t *cvar)
119 {
120 int ret;
121 struct k_condvar *cv;
122
123 cv = to_posix_cond(cvar);
124 if (cv == NULL) {
125 return EINVAL;
126 }
127
128 LOG_DBG("Signaling cond %p", cv);
129 ret = k_condvar_signal(cv);
130 if (ret < 0) {
131 LOG_DBG("k_condvar_signal() failed: %d", ret);
132 return -ret;
133 }
134
135 __ASSERT_NO_MSG(ret == 0);
136
137 return 0;
138 }
139
pthread_cond_broadcast(pthread_cond_t * cvar)140 int pthread_cond_broadcast(pthread_cond_t *cvar)
141 {
142 int ret;
143 struct k_condvar *cv;
144
145 cv = get_posix_cond(*cvar);
146 if (cv == NULL) {
147 return EINVAL;
148 }
149
150 LOG_DBG("Broadcasting on cond %p", cv);
151 ret = k_condvar_broadcast(cv);
152 if (ret < 0) {
153 LOG_DBG("k_condvar_broadcast() failed: %d", ret);
154 return -ret;
155 }
156
157 __ASSERT_NO_MSG(ret >= 0);
158
159 return 0;
160 }
161
pthread_cond_wait(pthread_cond_t * cv,pthread_mutex_t * mut)162 int pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mut)
163 {
164 return cond_wait(cv, mut, K_FOREVER);
165 }
166
pthread_cond_timedwait(pthread_cond_t * cv,pthread_mutex_t * mut,const struct timespec * abstime)167 int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *mut, const struct timespec *abstime)
168 {
169 return cond_wait(cv, mut, K_MSEC((int32_t)timespec_to_timeoutms(abstime)));
170 }
171
pthread_cond_init(pthread_cond_t * cvar,const pthread_condattr_t * att)172 int pthread_cond_init(pthread_cond_t *cvar, const pthread_condattr_t *att)
173 {
174 struct k_condvar *cv;
175
176 ARG_UNUSED(att);
177 *cvar = PTHREAD_COND_INITIALIZER;
178
179 /* calls k_condvar_init() */
180 cv = to_posix_cond(cvar);
181 if (cv == NULL) {
182 return ENOMEM;
183 }
184
185 LOG_DBG("Initialized cond %p", cv);
186
187 return 0;
188 }
189
pthread_cond_destroy(pthread_cond_t * cvar)190 int pthread_cond_destroy(pthread_cond_t *cvar)
191 {
192 int err;
193 size_t bit;
194 struct k_condvar *cv;
195
196 cv = get_posix_cond(*cvar);
197 if (cv == NULL) {
198 return EINVAL;
199 }
200
201 bit = posix_cond_to_offset(cv);
202 err = sys_bitarray_free(&posix_cond_bitarray, 1, bit);
203 __ASSERT_NO_MSG(err == 0);
204
205 *cvar = -1;
206
207 LOG_DBG("Destroyed cond %p", cv);
208
209 return 0;
210 }
211
pthread_cond_pool_init(void)212 static int pthread_cond_pool_init(void)
213 {
214 int err;
215 size_t i;
216
217 for (i = 0; i < CONFIG_MAX_PTHREAD_COND_COUNT; ++i) {
218 err = k_condvar_init(&posix_cond_pool[i]);
219 __ASSERT_NO_MSG(err == 0);
220 }
221
222 return 0;
223 }
224
pthread_condattr_init(pthread_condattr_t * att)225 int pthread_condattr_init(pthread_condattr_t *att)
226 {
227 __ASSERT_NO_MSG(att != NULL);
228
229 att->clock = CLOCK_MONOTONIC;
230
231 return 0;
232 }
233
pthread_condattr_destroy(pthread_condattr_t * att)234 int pthread_condattr_destroy(pthread_condattr_t *att)
235 {
236 ARG_UNUSED(att);
237
238 return 0;
239 }
240
pthread_condattr_getclock(const pthread_condattr_t * ZRESTRICT att,clockid_t * ZRESTRICT clock_id)241 int pthread_condattr_getclock(const pthread_condattr_t *ZRESTRICT att,
242 clockid_t *ZRESTRICT clock_id)
243 {
244 *clock_id = att->clock;
245
246 return 0;
247 }
248
pthread_condattr_setclock(pthread_condattr_t * att,clockid_t clock_id)249 int pthread_condattr_setclock(pthread_condattr_t *att, clockid_t clock_id)
250 {
251 if (clock_id != CLOCK_REALTIME && clock_id != CLOCK_MONOTONIC) {
252 return -EINVAL;
253 }
254
255 att->clock = clock_id;
256
257 return 0;
258 }
259 SYS_INIT(pthread_cond_pool_init, PRE_KERNEL_1, 0);
260