Skip to content

Commit 4c43076

Browse files
committed
posix: cond: use clock specified via pthread_condattr_t
Use the clock specified via pthread_condattr_t in pthread_cond_timedwait(). Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
1 parent ccecd17 commit 4c43076

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

lib/posix/options/cond.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,22 +88,33 @@ static struct posix_cond *to_posix_cond(pthread_cond_t *cvar)
8888
/* Record the associated posix_cond in mu and mark as initialized */
8989
*cvar = mark_pthread_obj_initialized(bit);
9090
cv = &posix_cond_pool[bit];
91+
(void)pthread_condattr_init((pthread_condattr_t *)&cv->attr);
9192

9293
return cv;
9394
}
9495

95-
static int cond_wait(pthread_cond_t *cond, pthread_mutex_t *mu, k_timeout_t timeout)
96+
static int cond_wait(pthread_cond_t *cond, pthread_mutex_t *mu, const struct timespec *abstime)
9697
{
9798
int ret;
9899
struct k_mutex *m;
99100
struct posix_cond *cv;
101+
k_timeout_t timeout = K_FOREVER;
100102

101103
m = to_posix_mutex(mu);
102104
cv = to_posix_cond(cond);
103105
if (cv == NULL || m == NULL) {
104106
return EINVAL;
105107
}
106108

109+
if (abstime != NULL) {
110+
if (!timespec_is_valid(abstime)) {
111+
LOG_DBG("%s is invalid", "abstime");
112+
return EINVAL;
113+
}
114+
115+
timeout = K_MSEC(timespec_to_clock_timeoutms(cv->attr.clock, abstime));
116+
}
117+
107118
LOG_DBG("Waiting on cond %p with timeout %llx", cv, timeout.ticks);
108119
ret = k_condvar_wait(&cv->condvar, m, timeout);
109120
if (ret == -EAGAIN) {
@@ -166,24 +177,34 @@ int pthread_cond_broadcast(pthread_cond_t *cvar)
166177

167178
int pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mut)
168179
{
169-
return cond_wait(cv, mut, K_FOREVER);
180+
return cond_wait(cv, mut, NULL);
170181
}
171182

172183
int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *mut, const struct timespec *abstime)
173184
{
174-
return cond_wait(cv, mut, K_MSEC(timespec_to_timeoutms(abstime)));
185+
return cond_wait(cv, mut, abstime);
175186
}
176187

177188
int pthread_cond_init(pthread_cond_t *cvar, const pthread_condattr_t *att)
178189
{
179190
struct posix_cond *cv;
191+
struct posix_condattr *attr = (struct posix_condattr *)attr;
180192

181193
*cvar = PTHREAD_COND_INITIALIZER;
182194
cv = to_posix_cond(cvar);
183195
if (cv == NULL) {
184196
return ENOMEM;
185197
}
186198

199+
if (attr != NULL) {
200+
if (!attr->initialized) {
201+
return EINVAL;
202+
}
203+
204+
(void)pthread_condattr_destroy((pthread_condattr_t *)&cv->attr);
205+
cv->attr = *attr;
206+
}
207+
187208
LOG_DBG("Initialized cond %p", cv);
188209

189210
return 0;

0 commit comments

Comments
 (0)