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