Skip to content

Commit 0535413

Browse files
committed
posix: cond: use struct posix_cond and struct posix_condattr internally
Use struct posix_cond and struct posix_condattr internally. Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
1 parent a73da15 commit 0535413

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

lib/posix/options/cond.c

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* SPDX-License-Identifier: Apache-2.0
66
*/
77

8+
#include "posix_clock.h"
89
#include "posix_internal.h"
910

1011
#include <zephyr/init.h>
@@ -15,11 +16,13 @@
1516

1617
LOG_MODULE_REGISTER(pthread_cond, CONFIG_PTHREAD_COND_LOG_LEVEL);
1718

18-
__pinned_bss
19-
static struct k_condvar posix_cond_pool[CONFIG_MAX_PTHREAD_COND_COUNT];
19+
__pinned_bss static struct posix_cond posix_cond_pool[CONFIG_MAX_PTHREAD_COND_COUNT];
2020

2121
SYS_BITARRAY_DEFINE_STATIC(posix_cond_bitarray, CONFIG_MAX_PTHREAD_COND_COUNT);
2222

23+
BUILD_ASSERT(sizeof(struct posix_condattr) <= sizeof(pthread_condattr_t),
24+
"posix_condattr is too large");
25+
2326
/*
2427
* We reserve the MSB to mark a pthread_cond_t as initialized (from the
2528
* perspective of the application). With a linear space, this means that
@@ -28,7 +31,7 @@ SYS_BITARRAY_DEFINE_STATIC(posix_cond_bitarray, CONFIG_MAX_PTHREAD_COND_COUNT);
2831
BUILD_ASSERT(CONFIG_MAX_PTHREAD_COND_COUNT < PTHREAD_OBJ_MASK_INIT,
2932
"CONFIG_MAX_PTHREAD_COND_COUNT is too high");
3033

31-
static inline size_t posix_cond_to_offset(struct k_condvar *cv)
34+
static inline size_t posix_cond_to_offset(struct posix_cond *cv)
3235
{
3336
return cv - posix_cond_pool;
3437
}
@@ -38,7 +41,7 @@ static inline size_t to_posix_cond_idx(pthread_cond_t cond)
3841
return mark_pthread_obj_uninitialized(cond);
3942
}
4043

41-
static struct k_condvar *get_posix_cond(pthread_cond_t cond)
44+
static struct posix_cond *get_posix_cond(pthread_cond_t cond)
4245
{
4346
int actually_initialized;
4447
size_t bit = to_posix_cond_idx(cond);
@@ -64,10 +67,10 @@ static struct k_condvar *get_posix_cond(pthread_cond_t cond)
6467
return &posix_cond_pool[bit];
6568
}
6669

67-
static struct k_condvar *to_posix_cond(pthread_cond_t *cvar)
70+
static struct posix_cond *to_posix_cond(pthread_cond_t *cvar)
6871
{
6972
size_t bit;
70-
struct k_condvar *cv;
73+
struct posix_cond *cv;
7174

7275
if (*cvar != PTHREAD_COND_INITIALIZER) {
7376
return get_posix_cond(*cvar);
@@ -91,7 +94,7 @@ static int cond_wait(pthread_cond_t *cond, pthread_mutex_t *mu, k_timeout_t time
9194
{
9295
int ret;
9396
struct k_mutex *m;
94-
struct k_condvar *cv;
97+
struct posix_cond *cv;
9598

9699
m = to_posix_mutex(mu);
97100
cv = to_posix_cond(cond);
@@ -100,7 +103,7 @@ static int cond_wait(pthread_cond_t *cond, pthread_mutex_t *mu, k_timeout_t time
100103
}
101104

102105
LOG_DBG("Waiting on cond %p with timeout %llx", cv, timeout.ticks);
103-
ret = k_condvar_wait(cv, m, timeout);
106+
ret = k_condvar_wait(&cv->condvar, m, timeout);
104107
if (ret == -EAGAIN) {
105108
LOG_DBG("Timeout waiting on cond %p", cv);
106109
ret = ETIMEDOUT;
@@ -118,15 +121,15 @@ static int cond_wait(pthread_cond_t *cond, pthread_mutex_t *mu, k_timeout_t time
118121
int pthread_cond_signal(pthread_cond_t *cvar)
119122
{
120123
int ret;
121-
struct k_condvar *cv;
124+
struct posix_cond *cv;
122125

123126
cv = to_posix_cond(cvar);
124127
if (cv == NULL) {
125128
return EINVAL;
126129
}
127130

128131
LOG_DBG("Signaling cond %p", cv);
129-
ret = k_condvar_signal(cv);
132+
ret = k_condvar_signal(&cv->condvar);
130133
if (ret < 0) {
131134
LOG_DBG("k_condvar_signal() failed: %d", ret);
132135
return -ret;
@@ -140,15 +143,15 @@ int pthread_cond_signal(pthread_cond_t *cvar)
140143
int pthread_cond_broadcast(pthread_cond_t *cvar)
141144
{
142145
int ret;
143-
struct k_condvar *cv;
146+
struct posix_cond *cv;
144147

145148
cv = get_posix_cond(*cvar);
146149
if (cv == NULL) {
147150
return EINVAL;
148151
}
149152

150153
LOG_DBG("Broadcasting on cond %p", cv);
151-
ret = k_condvar_broadcast(cv);
154+
ret = k_condvar_broadcast(&cv->condvar);
152155
if (ret < 0) {
153156
LOG_DBG("k_condvar_broadcast() failed: %d", ret);
154157
return -ret;
@@ -171,12 +174,9 @@ int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *mut, const struc
171174

172175
int pthread_cond_init(pthread_cond_t *cvar, const pthread_condattr_t *att)
173176
{
174-
struct k_condvar *cv;
177+
struct posix_cond *cv;
175178

176-
ARG_UNUSED(att);
177179
*cvar = PTHREAD_COND_INITIALIZER;
178-
179-
/* calls k_condvar_init() */
180180
cv = to_posix_cond(cvar);
181181
if (cv == NULL) {
182182
return ENOMEM;
@@ -191,7 +191,7 @@ int pthread_cond_destroy(pthread_cond_t *cvar)
191191
{
192192
int err;
193193
size_t bit;
194-
struct k_condvar *cv;
194+
struct posix_cond *cv;
195195

196196
cv = get_posix_cond(*cvar);
197197
if (cv == NULL) {
@@ -216,7 +216,7 @@ static int pthread_cond_pool_init(void)
216216
size_t i;
217217

218218
for (i = 0; i < CONFIG_MAX_PTHREAD_COND_COUNT; ++i) {
219-
err = k_condvar_init(&posix_cond_pool[i]);
219+
err = k_condvar_init(&posix_cond_pool[i].condvar);
220220
__ASSERT_NO_MSG(err == 0);
221221
}
222222

@@ -227,33 +227,45 @@ int pthread_condattr_init(pthread_condattr_t *att)
227227
{
228228
__ASSERT_NO_MSG(att != NULL);
229229

230-
att->clock = CLOCK_MONOTONIC;
230+
struct posix_condattr *const attr = (struct posix_condattr *)att;
231+
232+
attr->clock = CLOCK_MONOTONIC;
231233

232234
return 0;
233235
}
234236

235237
int pthread_condattr_destroy(pthread_condattr_t *att)
236238
{
237-
ARG_UNUSED(att);
239+
struct posix_condattr *const attr = (struct posix_condattr *)att;
240+
241+
if (attr == NULL) {
242+
return EINVAL;
243+
}
244+
245+
*attr = (struct posix_condattr){0};
238246

239247
return 0;
240248
}
241249

242250
int pthread_condattr_getclock(const pthread_condattr_t *ZRESTRICT att,
243251
clockid_t *ZRESTRICT clock_id)
244252
{
245-
*clock_id = att->clock;
253+
struct posix_condattr *const attr = (struct posix_condattr *)att;
254+
255+
*clock_id = attr->clock;
246256

247257
return 0;
248258
}
249259

250260
int pthread_condattr_setclock(pthread_condattr_t *att, clockid_t clock_id)
251261
{
262+
struct posix_condattr *const attr = (struct posix_condattr *)att;
263+
252264
if (clock_id != CLOCK_REALTIME && clock_id != CLOCK_MONOTONIC) {
253265
return -EINVAL;
254266
}
255267

256-
att->clock = clock_id;
268+
attr->clock = clock_id;
257269

258270
return 0;
259271
}

0 commit comments

Comments
 (0)