Skip to content

Commit 96466fe

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 b6a582a commit 96466fe

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
@@ -86,22 +86,28 @@ static struct posix_cond *to_posix_cond(pthread_cond_t *cvar)
8686
/* Record the associated posix_cond in mu and mark as initialized */
8787
*cvar = mark_pthread_obj_initialized(bit);
8888
cv = &posix_cond_pool[bit];
89+
(void)pthread_condattr_init((pthread_condattr_t *)&cv->attr);
8990

9091
return cv;
9192
}
9293

93-
static int cond_wait(pthread_cond_t *cond, pthread_mutex_t *mu, k_timeout_t timeout)
94+
static int cond_wait(pthread_cond_t *cond, pthread_mutex_t *mu, const struct timespec *abstime)
9495
{
9596
int ret;
9697
struct k_mutex *m;
9798
struct posix_cond *cv;
99+
k_timeout_t timeout = K_FOREVER;
98100

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

107+
if (abstime != NULL) {
108+
timeout = K_MSEC(timespec_to_clock_timeoutms(cv->attr.clock, abstime));
109+
}
110+
105111
LOG_DBG("Waiting on cond %p with timeout %llx", cv, timeout.ticks);
106112
ret = k_condvar_wait(&cv->condvar, m, timeout);
107113
if (ret == -EAGAIN) {
@@ -164,24 +170,39 @@ int pthread_cond_broadcast(pthread_cond_t *cvar)
164170

165171
int pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mut)
166172
{
167-
return cond_wait(cv, mut, K_FOREVER);
173+
return cond_wait(cv, mut, NULL);
168174
}
169175

170176
int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *mut, const struct timespec *abstime)
171177
{
172-
return cond_wait(cv, mut, K_MSEC(timespec_to_timeoutms(abstime)));
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);
173184
}
174185

175186
int pthread_cond_init(pthread_cond_t *cvar, const pthread_condattr_t *att)
176187
{
177188
struct posix_cond *cv;
189+
struct posix_condattr *attr = (struct posix_condattr *)attr;
178190

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

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+
185206
LOG_DBG("Initialized cond %p", cv);
186207

187208
return 0;

0 commit comments

Comments
 (0)