2014-02-18 12:17:06 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Hamburg University of Applied Sciences (HAW)
|
|
|
|
*
|
2014-07-31 19:45:27 +02:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
2014-02-18 12:17:06 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup sys
|
|
|
|
* @{
|
|
|
|
*
|
2015-05-22 07:34:41 +02:00
|
|
|
* @file
|
2014-02-18 12:17:06 +01:00
|
|
|
* @brief Condition variable implementation
|
|
|
|
*
|
|
|
|
* @author Martin Landsmann <martin.landsmann@haw-hamburg.de>
|
|
|
|
* @author René Kijewski <rene.kijewski@fu-berlin.de>
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
2017-06-21 11:21:24 +02:00
|
|
|
#include <errno.h>
|
2014-02-18 12:17:06 +01:00
|
|
|
|
|
|
|
#include "pthread_cond.h"
|
|
|
|
#include "thread.h"
|
2022-03-08 09:18:06 +01:00
|
|
|
#include "ztimer64.h"
|
|
|
|
#include "timex.h"
|
2014-02-18 12:17:06 +01:00
|
|
|
#include "sched.h"
|
|
|
|
#include "irq.h"
|
|
|
|
#include "debug.h"
|
|
|
|
|
2016-04-07 21:31:00 +02:00
|
|
|
int pthread_cond_condattr_destroy(pthread_condattr_t *attr)
|
2014-02-18 12:17:06 +01:00
|
|
|
{
|
|
|
|
if (attr != NULL) {
|
|
|
|
DEBUG("pthread_cond_condattr_destroy: currently attributes are not supported.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-07 21:31:00 +02:00
|
|
|
int pthread_cond_condattr_init(pthread_condattr_t *attr)
|
2014-02-18 12:17:06 +01:00
|
|
|
{
|
|
|
|
if (attr != NULL) {
|
|
|
|
DEBUG("pthread_cond_condattr_init: currently attributes are not supported.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_condattr_getpshared(const pthread_condattr_t *attr, int *pshared)
|
|
|
|
{
|
|
|
|
(void)attr;
|
|
|
|
(void)pshared;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared)
|
|
|
|
{
|
|
|
|
(void)attr;
|
|
|
|
(void)pshared;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_condattr_getclock(const pthread_condattr_t *attr, clockid_t *clock_id)
|
|
|
|
{
|
|
|
|
(void)attr;
|
|
|
|
(void)clock_id;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clock_id)
|
|
|
|
{
|
|
|
|
(void)attr;
|
|
|
|
(void)clock_id;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-07 21:31:00 +02:00
|
|
|
int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr)
|
2014-02-18 12:17:06 +01:00
|
|
|
{
|
|
|
|
if (attr != NULL) {
|
|
|
|
DEBUG("pthread_cond_init: currently attributes are not supported.\n");
|
|
|
|
}
|
|
|
|
|
2014-05-07 00:41:21 +02:00
|
|
|
cond->queue.first = NULL;
|
2014-02-18 12:17:06 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-07 21:31:00 +02:00
|
|
|
int pthread_cond_destroy(pthread_cond_t *cond)
|
2014-02-18 12:17:06 +01:00
|
|
|
{
|
|
|
|
pthread_cond_init(cond, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-21 11:21:24 +02:00
|
|
|
void _init_cond_wait(pthread_cond_t *cond, priority_queue_node_t *n)
|
2014-02-18 12:17:06 +01:00
|
|
|
{
|
2020-08-23 21:25:54 +02:00
|
|
|
n->priority = thread_get_active()->priority;
|
|
|
|
n->data = thread_getpid();
|
2017-06-21 11:21:24 +02:00
|
|
|
n->next = NULL;
|
2014-02-18 12:17:06 +01:00
|
|
|
|
|
|
|
/* the signaling thread may not hold the mutex, the queue is not thread safe */
|
2016-03-19 09:25:47 +01:00
|
|
|
unsigned old_state = irq_disable();
|
2017-06-21 11:21:24 +02:00
|
|
|
priority_queue_add(&(cond->queue), n);
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(old_state);
|
2017-06-21 11:21:24 +02:00
|
|
|
}
|
2014-02-18 12:17:06 +01:00
|
|
|
|
2017-06-21 11:21:24 +02:00
|
|
|
int pthread_cond_wait(pthread_cond_t *cond, mutex_t *mutex)
|
|
|
|
{
|
|
|
|
if (cond == NULL) {
|
|
|
|
return EINVAL;
|
2014-02-18 12:17:06 +01:00
|
|
|
}
|
2017-06-21 11:21:24 +02:00
|
|
|
priority_queue_node_t n;
|
|
|
|
_init_cond_wait(cond, &n);
|
|
|
|
|
|
|
|
mutex_unlock_and_sleep(mutex);
|
2014-02-18 12:17:06 +01:00
|
|
|
|
|
|
|
mutex_lock(mutex);
|
2017-06-21 11:21:24 +02:00
|
|
|
|
2014-02-18 12:17:06 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-07 21:31:00 +02:00
|
|
|
int pthread_cond_timedwait(pthread_cond_t *cond, mutex_t *mutex, const struct timespec *abstime)
|
2014-02-18 12:17:06 +01:00
|
|
|
{
|
make: fix various compile errors with Wextra
pkg, nordic_softdevice_ble: disable CFLAGS to omit compiler error
sys, pm_layered: fix casting nonscalar to the same type
cpu, stm32_common: fix type-limits, remove always true assert
cpu, stm32f4: fix pointer arithmetic in periph/i2c
drivers, at86rf2xx: fix type-limits where condition always true
saul, gpio: fix if no gpio configured for saul
cpu, saml21: add frequency check to periph/timer
driver, cc110x: fix unused param and type-limts errors
boards, wsn430-common: fix old-style-declaration
make: fix old style definition
drivers, sdcard_spi: fix old style typedef
driver, at30tse: remove unnecessary check
driver, nrf24: fix type-limit
driver, pn532: change buffer from char to uint8_t
tests/driver_sdcard: fix type limits
boards, feather-m0: add missing field inits
driver, tcs37727: fix type limits
pkg, emb6: disable some compiler warnings
tests/emb6: disable some compiler warings
pkg, openthread: fix sign compare and unused params
tests/trickle: fix struct init
tests/pthread_cooperation: fix type limits
board, mips-malta: remove feature periph_uart
shell: fix var size for netif command
gnrc, netif: fix sign-compare
gnrc, nib: fix sign-compare
shell: fix output in netif command
posix: fix type-limits in pthread_cond
2017-10-31 11:52:18 +01:00
|
|
|
if (cond == NULL) {
|
2017-06-21 11:21:24 +02:00
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-03-08 09:18:06 +01:00
|
|
|
uint64_t now = ztimer64_now(ZTIMER64_USEC);
|
2017-06-21 11:24:22 +02:00
|
|
|
uint64_t then = ((uint64_t)abstime->tv_sec * US_PER_SEC) +
|
|
|
|
(abstime->tv_nsec / NS_PER_US);
|
2014-02-18 12:17:06 +01:00
|
|
|
|
2017-06-21 11:21:24 +02:00
|
|
|
int ret = 0;
|
|
|
|
if (then > now) {
|
2022-03-08 09:18:06 +01:00
|
|
|
ztimer64_t timer;
|
2017-06-21 11:21:24 +02:00
|
|
|
priority_queue_node_t n;
|
|
|
|
|
|
|
|
_init_cond_wait(cond, &n);
|
2022-03-08 09:18:06 +01:00
|
|
|
ztimer64_set_wakeup(ZTIMER64_USEC, &timer, (then - now), thread_getpid());
|
2017-06-21 11:21:24 +02:00
|
|
|
|
|
|
|
mutex_unlock_and_sleep(mutex);
|
|
|
|
|
|
|
|
if (n.data != -1u) {
|
|
|
|
/* on signaling n.data is set to -1u */
|
|
|
|
/* if it isn't set, then the wakeup is either spurious or a timer wakeup */
|
|
|
|
unsigned old_state = irq_disable();
|
|
|
|
priority_queue_remove(&(cond->queue), &n);
|
|
|
|
irq_restore(old_state);
|
|
|
|
ret = ETIMEDOUT;
|
|
|
|
}
|
2022-03-08 09:18:06 +01:00
|
|
|
ztimer64_remove(ZTIMER64_USEC, &timer);
|
2017-06-21 11:21:24 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
mutex_unlock(mutex);
|
|
|
|
ret = ETIMEDOUT;
|
|
|
|
}
|
|
|
|
mutex_lock(mutex);
|
|
|
|
return ret;
|
2014-02-18 12:17:06 +01:00
|
|
|
}
|
|
|
|
|
2016-04-07 21:31:00 +02:00
|
|
|
int pthread_cond_signal(pthread_cond_t *cond)
|
2014-02-18 12:17:06 +01:00
|
|
|
{
|
2016-03-19 09:25:47 +01:00
|
|
|
unsigned old_state = irq_disable();
|
2014-02-18 12:17:06 +01:00
|
|
|
|
2014-07-29 09:21:11 +02:00
|
|
|
priority_queue_node_t *head = priority_queue_remove_head(&(cond->queue));
|
2014-02-18 12:17:06 +01:00
|
|
|
int other_prio = -1;
|
|
|
|
if (head != NULL) {
|
2020-08-23 21:25:54 +02:00
|
|
|
thread_t *other_thread = thread_get(head->data);
|
2014-02-18 12:17:06 +01:00
|
|
|
if (other_thread) {
|
|
|
|
other_prio = other_thread->priority;
|
|
|
|
sched_set_status(other_thread, STATUS_PENDING);
|
|
|
|
}
|
|
|
|
head->data = -1u;
|
|
|
|
}
|
|
|
|
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(old_state);
|
2014-02-18 12:17:06 +01:00
|
|
|
|
|
|
|
if (other_prio >= 0) {
|
2014-04-25 18:20:42 +02:00
|
|
|
sched_switch(other_prio);
|
2014-02-18 12:17:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int max_prio(int a, int b)
|
|
|
|
{
|
|
|
|
return (a < 0) ? b : ((a < b) ? a : b);
|
|
|
|
}
|
|
|
|
|
2016-04-07 21:31:00 +02:00
|
|
|
int pthread_cond_broadcast(pthread_cond_t *cond)
|
2014-02-18 12:17:06 +01:00
|
|
|
{
|
2016-03-19 09:25:47 +01:00
|
|
|
unsigned old_state = irq_disable();
|
2014-02-18 12:17:06 +01:00
|
|
|
|
|
|
|
int other_prio = -1;
|
|
|
|
|
|
|
|
while (1) {
|
2014-07-29 09:21:11 +02:00
|
|
|
priority_queue_node_t *head = priority_queue_remove_head(&(cond->queue));
|
2014-02-18 12:17:06 +01:00
|
|
|
if (head == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-08-23 21:25:54 +02:00
|
|
|
thread_t *other_thread = thread_get(head->data);
|
2014-02-18 12:17:06 +01:00
|
|
|
if (other_thread) {
|
|
|
|
other_prio = max_prio(other_prio, other_thread->priority);
|
|
|
|
sched_set_status(other_thread, STATUS_PENDING);
|
|
|
|
}
|
|
|
|
head->data = -1u;
|
|
|
|
}
|
|
|
|
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(old_state);
|
2014-02-18 12:17:06 +01:00
|
|
|
|
|
|
|
if (other_prio >= 0) {
|
2014-04-25 18:20:42 +02:00
|
|
|
sched_switch(other_prio);
|
2014-02-18 12:17:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|