lib: posix: Update to new timeout API

Mostly trivial search-and-replace, except for pthread_rwlock.c, where
we need spread timeout over 2 semaphore operations.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
This commit is contained in:
Paul Sokolovsky 2020-05-05 11:23:55 +03:00 committed by Carles Cufí
commit 0b634793cc
7 changed files with 37 additions and 36 deletions

View file

@ -12,7 +12,6 @@ config POSIX_MAX_FDS
config POSIX_API config POSIX_API
depends on !ARCH_POSIX depends on !ARCH_POSIX
bool "POSIX APIs" bool "POSIX APIs"
select LEGACY_TIMEOUT_API
help help
Enable mostly-standards-compliant implementations of Enable mostly-standards-compliant implementations of
various POSIX (IEEE 1003.1) APIs. various POSIX (IEEE 1003.1) APIs.

View file

@ -33,9 +33,9 @@ sys_slist_t mq_list = SYS_SLIST_STATIC_INIT(&mq_list);
s64_t timespec_to_timeoutms(const struct timespec *abstime); s64_t timespec_to_timeoutms(const struct timespec *abstime);
static mqueue_object *find_in_list(const char *name); static mqueue_object *find_in_list(const char *name);
static s32_t send_message(mqueue_desc *mqd, const char *msg_ptr, size_t msg_len, static s32_t send_message(mqueue_desc *mqd, const char *msg_ptr, size_t msg_len,
s32_t timeout); k_timeout_t timeout);
static int receive_message(mqueue_desc *mqd, char *msg_ptr, size_t msg_len, static int receive_message(mqueue_desc *mqd, char *msg_ptr, size_t msg_len,
s32_t timeout); k_timeout_t timeout);
static void remove_mq(mqueue_object *msg_queue); static void remove_mq(mqueue_object *msg_queue);
/** /**
@ -233,9 +233,8 @@ int mq_send(mqd_t mqdes, const char *msg_ptr, size_t msg_len,
unsigned int msg_prio) unsigned int msg_prio)
{ {
mqueue_desc *mqd = (mqueue_desc *)mqdes; mqueue_desc *mqd = (mqueue_desc *)mqdes;
s32_t timeout = K_FOREVER;
return send_message(mqd, msg_ptr, msg_len, timeout); return send_message(mqd, msg_ptr, msg_len, K_FOREVER);
} }
/** /**
@ -249,10 +248,9 @@ int mq_timedsend(mqd_t mqdes, const char *msg_ptr, size_t msg_len,
unsigned int msg_prio, const struct timespec *abstime) unsigned int msg_prio, const struct timespec *abstime)
{ {
mqueue_desc *mqd = (mqueue_desc *)mqdes; mqueue_desc *mqd = (mqueue_desc *)mqdes;
s32_t timeout; s32_t timeout = (s32_t) timespec_to_timeoutms(abstime);
timeout = (s32_t) timespec_to_timeoutms(abstime); return send_message(mqd, msg_ptr, msg_len, K_MSEC(timeout));
return send_message(mqd, msg_ptr, msg_len, timeout);
} }
/** /**
@ -266,10 +264,8 @@ int mq_receive(mqd_t mqdes, char *msg_ptr, size_t msg_len,
unsigned int *msg_prio) unsigned int *msg_prio)
{ {
mqueue_desc *mqd = (mqueue_desc *)mqdes; mqueue_desc *mqd = (mqueue_desc *)mqdes;
s32_t timeout = K_FOREVER;
return receive_message(mqd, msg_ptr, msg_len, timeout);
return receive_message(mqd, msg_ptr, msg_len, K_FOREVER);
} }
/** /**
@ -283,10 +279,9 @@ int mq_timedreceive(mqd_t mqdes, char *msg_ptr, size_t msg_len,
unsigned int *msg_prio, const struct timespec *abstime) unsigned int *msg_prio, const struct timespec *abstime)
{ {
mqueue_desc *mqd = (mqueue_desc *)mqdes; mqueue_desc *mqd = (mqueue_desc *)mqdes;
s32_t timeout = K_NO_WAIT; s32_t timeout = (s32_t) timespec_to_timeoutms(abstime);
timeout = (s32_t) timespec_to_timeoutms(abstime); return receive_message(mqd, msg_ptr, msg_len, K_MSEC(timeout));
return receive_message(mqd, msg_ptr, msg_len, timeout);
} }
/** /**
@ -366,7 +361,7 @@ static mqueue_object *find_in_list(const char *name)
} }
static s32_t send_message(mqueue_desc *mqd, const char *msg_ptr, size_t msg_len, static s32_t send_message(mqueue_desc *mqd, const char *msg_ptr, size_t msg_len,
s32_t timeout) k_timeout_t timeout)
{ {
s32_t ret = -1; s32_t ret = -1;
@ -385,7 +380,7 @@ static s32_t send_message(mqueue_desc *mqd, const char *msg_ptr, size_t msg_len,
} }
if (k_msgq_put(&mqd->mqueue->queue, (void *)msg_ptr, timeout) != 0) { if (k_msgq_put(&mqd->mqueue->queue, (void *)msg_ptr, timeout) != 0) {
errno = (timeout == K_NO_WAIT) ? EAGAIN : ETIMEDOUT; errno = K_TIMEOUT_EQ(timeout, K_NO_WAIT) ? EAGAIN : ETIMEDOUT;
return ret; return ret;
} }
@ -393,7 +388,7 @@ static s32_t send_message(mqueue_desc *mqd, const char *msg_ptr, size_t msg_len,
} }
static s32_t receive_message(mqueue_desc *mqd, char *msg_ptr, size_t msg_len, static s32_t receive_message(mqueue_desc *mqd, char *msg_ptr, size_t msg_len,
s32_t timeout) k_timeout_t timeout)
{ {
int ret = -1; int ret = -1;
@ -412,7 +407,7 @@ static s32_t receive_message(mqueue_desc *mqd, char *msg_ptr, size_t msg_len,
} }
if (k_msgq_get(&mqd->mqueue->queue, (void *)msg_ptr, timeout) != 0) { if (k_msgq_get(&mqd->mqueue->queue, (void *)msg_ptr, timeout) != 0) {
errno = (timeout != K_NO_WAIT) ? ETIMEDOUT : EAGAIN; errno = K_TIMEOUT_EQ(timeout, K_NO_WAIT) ? EAGAIN : ETIMEDOUT;
} else { } else {
ret = mqd->mqueue->queue.msg_size; ret = mqd->mqueue->queue.msg_size;
} }

View file

@ -24,7 +24,7 @@ static const pthread_attr_t init_pthread_attrs = {
.stack = NULL, .stack = NULL,
.stacksize = 0, .stacksize = 0,
.flags = PTHREAD_INIT_FLAGS, .flags = PTHREAD_INIT_FLAGS,
.delayedstart = K_NO_WAIT, .delayedstart = 0,
#if defined(CONFIG_PREEMPT_ENABLED) #if defined(CONFIG_PREEMPT_ENABLED)
.schedpolicy = SCHED_RR, .schedpolicy = SCHED_RR,
#else #else
@ -190,7 +190,7 @@ int pthread_create(pthread_t *newthread, const pthread_attr_t *attr,
(void *)arg, NULL, (void *)arg, NULL,
threadroutine, prio, threadroutine, prio,
(~K_ESSENTIAL & attr->flags), (~K_ESSENTIAL & attr->flags),
attr->delayedstart); K_MSEC(attr->delayedstart));
return 0; return 0;
} }

View file

@ -11,7 +11,8 @@
s64_t timespec_to_timeoutms(const struct timespec *abstime); s64_t timespec_to_timeoutms(const struct timespec *abstime);
static int cond_wait(pthread_cond_t *cv, pthread_mutex_t *mut, int timeout) static int cond_wait(pthread_cond_t *cv, pthread_mutex_t *mut,
k_timeout_t timeout)
{ {
__ASSERT(mut->lock_count == 1U, ""); __ASSERT(mut->lock_count == 1U, "");
@ -78,5 +79,5 @@ int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *mut,
const struct timespec *abstime) const struct timespec *abstime)
{ {
s32_t timeout = (s32_t)timespec_to_timeoutms(abstime); s32_t timeout = (s32_t)timespec_to_timeoutms(abstime);
return cond_wait(cv, mut, timeout); return cond_wait(cv, mut, K_MSEC(timeout));
} }

View file

@ -20,7 +20,7 @@ static const pthread_mutexattr_t def_attr = {
.type = PTHREAD_MUTEX_DEFAULT, .type = PTHREAD_MUTEX_DEFAULT,
}; };
static int acquire_mutex(pthread_mutex_t *m, int timeout) static int acquire_mutex(pthread_mutex_t *m, k_timeout_t timeout)
{ {
int rc = 0, key = irq_lock(); int rc = 0, key = irq_lock();
@ -45,7 +45,7 @@ static int acquire_mutex(pthread_mutex_t *m, int timeout)
return rc; return rc;
} }
if (timeout == K_NO_WAIT) { if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
irq_unlock(key); irq_unlock(key);
return EINVAL; return EINVAL;
} }
@ -78,7 +78,7 @@ int pthread_mutex_timedlock(pthread_mutex_t *m,
const struct timespec *abstime) const struct timespec *abstime)
{ {
s32_t timeout = (s32_t)timespec_to_timeoutms(abstime); s32_t timeout = (s32_t)timespec_to_timeoutms(abstime);
return acquire_mutex(m, timeout); return acquire_mutex(m, K_MSEC(timeout));
} }
/** /**

View file

@ -71,7 +71,7 @@ int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
return EINVAL; return EINVAL;
} }
return read_lock_acquire(rwlock, K_FOREVER); return read_lock_acquire(rwlock, SYS_FOREVER_MS);
} }
/** /**
@ -116,7 +116,7 @@ int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
return EINVAL; return EINVAL;
} }
return read_lock_acquire(rwlock, K_NO_WAIT); return read_lock_acquire(rwlock, 0);
} }
/** /**
@ -133,7 +133,7 @@ int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
return EINVAL; return EINVAL;
} }
return write_lock_acquire(rwlock, K_FOREVER); return write_lock_acquire(rwlock, SYS_FOREVER_MS);
} }
/** /**
@ -178,7 +178,7 @@ int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
return EINVAL; return EINVAL;
} }
return write_lock_acquire(rwlock, K_NO_WAIT); return write_lock_acquire(rwlock, 0);
} }
/** /**
@ -216,7 +216,7 @@ static u32_t read_lock_acquire(pthread_rwlock_t *rwlock, s32_t timeout)
{ {
u32_t ret = 0U; u32_t ret = 0U;
if (k_sem_take(&rwlock->wr_sem, timeout) == 0) { if (k_sem_take(&rwlock->wr_sem, SYS_TIMEOUT_MS(timeout)) == 0) {
k_sem_take(&rwlock->reader_active, K_NO_WAIT); k_sem_take(&rwlock->reader_active, K_NO_WAIT);
k_sem_take(&rwlock->rd_sem, K_NO_WAIT); k_sem_take(&rwlock->rd_sem, K_NO_WAIT);
k_sem_give(&rwlock->wr_sem); k_sem_give(&rwlock->wr_sem);
@ -231,17 +231,23 @@ static u32_t write_lock_acquire(pthread_rwlock_t *rwlock, s32_t timeout)
{ {
u32_t ret = 0U; u32_t ret = 0U;
s64_t elapsed_time, st_time = k_uptime_get(); s64_t elapsed_time, st_time = k_uptime_get();
k_timeout_t k_timeout;
k_timeout = SYS_TIMEOUT_MS(timeout);
/* waiting for release of write lock */ /* waiting for release of write lock */
if (k_sem_take(&rwlock->wr_sem, timeout) == 0) { if (k_sem_take(&rwlock->wr_sem, k_timeout) == 0) {
if (timeout > K_NO_WAIT) { /* update remaining timeout time for 2nd sem */
if (timeout != SYS_FOREVER_MS) {
elapsed_time = k_uptime_get() - st_time; elapsed_time = k_uptime_get() - st_time;
timeout = timeout <= elapsed_time ? K_NO_WAIT : timeout = timeout <= elapsed_time ? 0 :
timeout - elapsed_time; timeout - elapsed_time;
} }
k_timeout = SYS_TIMEOUT_MS(timeout);
/* waiting for reader to complete operation */ /* waiting for reader to complete operation */
if (k_sem_take(&rwlock->reader_active, timeout) == 0) { if (k_sem_take(&rwlock->reader_active, k_timeout) == 0) {
rwlock->wr_owner = k_current_get(); rwlock->wr_owner = k_current_get();
} else { } else {
k_sem_give(&rwlock->wr_sem); k_sem_give(&rwlock->wr_sem);

View file

@ -104,12 +104,12 @@ int sem_timedwait(sem_t *semaphore, struct timespec *abstime)
abstime_ms = (s64_t)_ts_to_ms(abstime); abstime_ms = (s64_t)_ts_to_ms(abstime);
if (abstime_ms <= current_ms) { if (abstime_ms <= current_ms) {
timeout = K_NO_WAIT; timeout = 0;
} else { } else {
timeout = (s32_t)(abstime_ms - current_ms); timeout = (s32_t)(abstime_ms - current_ms);
} }
if (k_sem_take(semaphore, timeout)) { if (k_sem_take(semaphore, K_MSEC(timeout))) {
errno = ETIMEDOUT; errno = ETIMEDOUT;
return -1; return -1;
} }