Skip to content

Commit fa66688

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 fa66688

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

lib/posix/options/cond.c

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

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

11+
#include <stdbool.h>
12+
1013
#include <zephyr/init.h>
1114
#include <zephyr/kernel.h>
1215
#include <zephyr/logging/log.h>
@@ -15,11 +18,13 @@
1518

1619
LOG_MODULE_REGISTER(pthread_cond, CONFIG_PTHREAD_COND_LOG_LEVEL);
1720

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

2123
SYS_BITARRAY_DEFINE_STATIC(posix_cond_bitarray, CONFIG_MAX_PTHREAD_COND_COUNT);
2224

25+
BUILD_ASSERT(sizeof(struct posix_condattr) <= sizeof(pthread_condattr_t),
26+
"posix_condattr is too large");
27+
2328
/*
2429
* We reserve the MSB to mark a pthread_cond_t as initialized (from the
2530
* perspective of the application). With a linear space, this means that
@@ -28,7 +33,7 @@ SYS_BITARRAY_DEFINE_STATIC(posix_cond_bitarray, CONFIG_MAX_PTHREAD_COND_COUNT);
2833
BUILD_ASSERT(CONFIG_MAX_PTHREAD_COND_COUNT < PTHREAD_OBJ_MASK_INIT,
2934
"CONFIG_MAX_PTHREAD_COND_COUNT is too high");
3035

31-
static inline size_t posix_cond_to_offset(struct k_condvar *cv)
36+
static inline size_t posix_cond_to_offset(struct posix_cond *cv)
3237
{
3338
return cv - posix_cond_pool;
3439
}
@@ -38,7 +43,7 @@ static inline size_t to_posix_cond_idx(pthread_cond_t cond)
3843
return mark_pthread_obj_uninitialized(cond);
3944
}
4045

41-
static struct k_condvar *get_posix_cond(pthread_cond_t cond)
46+
static struct posix_cond *get_posix_cond(pthread_cond_t cond)
4247
{
4348
int actually_initialized;
4449
size_t bit = to_posix_cond_idx(cond);
@@ -64,10 +69,10 @@ static struct k_condvar *get_posix_cond(pthread_cond_t cond)
6469
return &posix_cond_pool[bit];
6570
}
6671

67-
static struct k_condvar *to_posix_cond(pthread_cond_t *cvar)
72+
static struct posix_cond *to_posix_cond(pthread_cond_t *cvar)
6873
{
6974
size_t bit;
70-
struct k_condvar *cv;
75+
struct posix_cond *cv;
7176

7277
if (*cvar != PTHREAD_COND_INITIALIZER) {
7378
return get_posix_cond(*cvar);
@@ -91,7 +96,7 @@ static int cond_wait(pthread_cond_t *cond, pthread_mutex_t *mu, k_timeout_t time
9196
{
9297
int ret;
9398
struct k_mutex *m;
94-
struct k_condvar *cv;
99+
struct posix_cond *cv;
95100

96101
m = to_posix_mutex(mu);
97102
cv = to_posix_cond(cond);
@@ -100,7 +105,7 @@ static int cond_wait(pthread_cond_t *cond, pthread_mutex_t *mu, k_timeout_t time
100105
}
101106

102107
LOG_DBG("Waiting on cond %p with timeout %llx", cv, timeout.ticks);
103-
ret = k_condvar_wait(cv, m, timeout);
108+
ret = k_condvar_wait(&cv->condvar, m, timeout);
104109
if (ret == -EAGAIN) {
105110
LOG_DBG("Timeout waiting on cond %p", cv);
106111
ret = ETIMEDOUT;
@@ -118,15 +123,15 @@ static int cond_wait(pthread_cond_t *cond, pthread_mutex_t *mu, k_timeout_t time
118123
int pthread_cond_signal(pthread_cond_t *cvar)
119124
{
120125
int ret;
121-
struct k_condvar *cv;
126+
struct posix_cond *cv;
122127

123128
cv = to_posix_cond(cvar);
124129
if (cv == NULL) {
125130
return EINVAL;
126131
}
127132

128133
LOG_DBG("Signaling cond %p", cv);
129-
ret = k_condvar_signal(cv);
134+
ret = k_condvar_signal(&cv->condvar);
130135
if (ret < 0) {
131136
LOG_DBG("k_condvar_signal() failed: %d", ret);
132137
return -ret;
@@ -140,15 +145,15 @@ int pthread_cond_signal(pthread_cond_t *cvar)
140145
int pthread_cond_broadcast(pthread_cond_t *cvar)
141146
{
142147
int ret;
143-
struct k_condvar *cv;
148+
struct posix_cond *cv;
144149

145150
cv = get_posix_cond(*cvar);
146151
if (cv == NULL) {
147152
return EINVAL;
148153
}
149154

150155
LOG_DBG("Broadcasting on cond %p", cv);
151-
ret = k_condvar_broadcast(cv);
156+
ret = k_condvar_broadcast(&cv->condvar);
152157
if (ret < 0) {
153158
LOG_DBG("k_condvar_broadcast() failed: %d", ret);
154159
return -ret;
@@ -171,12 +176,9 @@ int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *mut, const struc
171176

172177
int pthread_cond_init(pthread_cond_t *cvar, const pthread_condattr_t *att)
173178
{
174-
struct k_condvar *cv;
179+
struct posix_cond *cv;
175180

176-
ARG_UNUSED(att);
177181
*cvar = PTHREAD_COND_INITIALIZER;
178-
179-
/* calls k_condvar_init() */
180182
cv = to_posix_cond(cvar);
181183
if (cv == NULL) {
182184
return ENOMEM;
@@ -191,7 +193,7 @@ int pthread_cond_destroy(pthread_cond_t *cvar)
191193
{
192194
int err;
193195
size_t bit;
194-
struct k_condvar *cv;
196+
struct posix_cond *cv;
195197

196198
cv = get_posix_cond(*cvar);
197199
if (cv == NULL) {
@@ -216,7 +218,7 @@ static int pthread_cond_pool_init(void)
216218
size_t i;
217219

218220
for (i = 0; i < CONFIG_MAX_PTHREAD_COND_COUNT; ++i) {
219-
err = k_condvar_init(&posix_cond_pool[i]);
221+
err = k_condvar_init(&posix_cond_pool[i].condvar);
220222
__ASSERT_NO_MSG(err == 0);
221223
}
222224

@@ -227,33 +229,45 @@ int pthread_condattr_init(pthread_condattr_t *att)
227229
{
228230
__ASSERT_NO_MSG(att != NULL);
229231

230-
att->clock = CLOCK_MONOTONIC;
232+
struct posix_condattr *const attr = (struct posix_condattr *)att;
233+
234+
attr->clock = CLOCK_MONOTONIC;
231235

232236
return 0;
233237
}
234238

235239
int pthread_condattr_destroy(pthread_condattr_t *att)
236240
{
237-
ARG_UNUSED(att);
241+
struct posix_condattr *const attr = (struct posix_condattr *)att;
242+
243+
if (attr == NULL) {
244+
return EINVAL;
245+
}
246+
247+
*attr = (struct posix_condattr){0};
238248

239249
return 0;
240250
}
241251

242252
int pthread_condattr_getclock(const pthread_condattr_t *ZRESTRICT att,
243253
clockid_t *ZRESTRICT clock_id)
244254
{
245-
*clock_id = att->clock;
255+
struct posix_condattr *const attr = (struct posix_condattr *)att;
256+
257+
*clock_id = attr->clock;
246258

247259
return 0;
248260
}
249261

250262
int pthread_condattr_setclock(pthread_condattr_t *att, clockid_t clock_id)
251263
{
264+
struct posix_condattr *const attr = (struct posix_condattr *)att;
265+
252266
if (clock_id != CLOCK_REALTIME && clock_id != CLOCK_MONOTONIC) {
253267
return -EINVAL;
254268
}
255269

256-
att->clock = clock_id;
270+
attr->clock = clock_id;
257271

258272
return 0;
259273
}

0 commit comments

Comments
 (0)