pthread: cond: fix pthread_cond_wait always returning ETIMEDOUT

It was noted that `pthread_cond_wait()` would always return
ETIMEDOUT, even when successful (and no timeout should ever
occur with `K_FOREVER`).

The z_sched_wake() / z_sched_wake_all() / z_sched_wait() API
are used here with a swap return value of 0 to indicate
success.

Fixes #41284

Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
This commit is contained in:
Christopher Friedt 2022-02-02 13:35:09 -05:00 committed by Anas Nashif
commit 07c00fff25

View file

@ -24,7 +24,7 @@ static int cond_wait(pthread_cond_t *cv, pthread_mutex_t *mut,
mut->lock_count = 0U;
mut->owner = NULL;
_ready_one_thread(&mut->wait_q);
ret = z_pend_curr(&z_pthread_spinlock, key, &cv->wait_q, timeout);
ret = z_sched_wait(&z_pthread_spinlock, key, &cv->wait_q, timeout, NULL);
/* FIXME: this extra lock (and the potential context switch it
* can cause) could be optimized out. At the point of the
@ -39,37 +39,15 @@ static int cond_wait(pthread_cond_t *cv, pthread_mutex_t *mut,
return ret == -EAGAIN ? ETIMEDOUT : ret;
}
/* This implements a "fair" scheduling policy: at the end of a POSIX
* thread call that might result in a change of the current maximum
* priority thread, we always check and context switch if needed.
* Note that there is significant dispute in the community over the
* "right" way to do this and different systems do it differently by
* default. Zephyr is an RTOS, so we choose latency over
* throughput. See here for a good discussion of the broad issue:
*
* https://blog.mozilla.org/nfroyd/2017/03/29/on-mutex-performance-part-1/
*/
int pthread_cond_signal(pthread_cond_t *cv)
{
k_spinlock_key_t key = k_spin_lock(&z_pthread_spinlock);
_ready_one_thread(&cv->wait_q);
z_reschedule(&z_pthread_spinlock, key);
z_sched_wake(&cv->wait_q, 0, NULL);
return 0;
}
int pthread_cond_broadcast(pthread_cond_t *cv)
{
k_spinlock_key_t key = k_spin_lock(&z_pthread_spinlock);
while (z_waitq_head(&cv->wait_q)) {
_ready_one_thread(&cv->wait_q);
}
z_reschedule(&z_pthread_spinlock, key);
z_sched_wake_all(&cv->wait_q, 0, NULL);
return 0;
}