2015-08-03 18:52:32 +02:00
|
|
|
/*
|
2016-11-22 18:21:40 +01:00
|
|
|
* Copyright (C) 2016 TriaGnoSys GmbH
|
|
|
|
* 2013-15 Freie Universität Berlin
|
2015-08-03 18:52:32 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*
|
2016-11-22 18:21:40 +01:00
|
|
|
* @author Víctor Ariño <victor.arino@zii.aero>
|
2015-08-03 18:52:32 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
2020-11-23 14:00:54 +01:00
|
|
|
#include <limits.h>
|
|
|
|
|
2015-08-03 18:52:32 +02:00
|
|
|
#include "irq.h"
|
2016-11-22 18:21:40 +01:00
|
|
|
#include "assert.h"
|
2015-10-19 16:15:37 +02:00
|
|
|
#include "sema.h"
|
2021-01-15 15:57:46 +01:00
|
|
|
|
2020-10-22 11:35:22 +02:00
|
|
|
#define ENABLE_DEBUG 0
|
2015-08-03 18:52:32 +02:00
|
|
|
#include "debug.h"
|
|
|
|
|
2016-11-22 18:21:40 +01:00
|
|
|
void sema_create(sema_t *sema, unsigned int value)
|
2015-08-03 18:52:32 +02:00
|
|
|
{
|
2016-11-22 18:21:40 +01:00
|
|
|
assert(sema != NULL);
|
|
|
|
|
2015-10-19 16:25:06 +02:00
|
|
|
sema->value = value;
|
2016-11-22 18:21:40 +01:00
|
|
|
sema->state = SEMA_OK;
|
|
|
|
mutex_init(&sema->mutex);
|
|
|
|
if (value == 0) {
|
|
|
|
mutex_lock(&sema->mutex);
|
|
|
|
}
|
2015-08-03 18:52:32 +02:00
|
|
|
}
|
|
|
|
|
2016-11-22 18:21:40 +01:00
|
|
|
void sema_destroy(sema_t *sema)
|
2015-08-03 18:52:32 +02:00
|
|
|
{
|
2016-11-22 18:21:40 +01:00
|
|
|
assert(sema != NULL);
|
2015-12-01 19:11:45 +01:00
|
|
|
|
2016-11-22 18:21:40 +01:00
|
|
|
sema->state = SEMA_DESTROY;
|
|
|
|
mutex_unlock(&sema->mutex);
|
|
|
|
}
|
|
|
|
|
2022-03-01 10:12:34 +01:00
|
|
|
#if IS_USED(MODULE_SEMA_DEPRECATED)
|
|
|
|
int _sema_wait_ztimer64(sema_t *sema, int block, ztimer64_clock_t *clock, uint64_t us)
|
2015-08-03 18:52:32 +02:00
|
|
|
{
|
2016-11-22 18:21:40 +01:00
|
|
|
assert(sema != NULL);
|
2015-12-01 19:11:45 +01:00
|
|
|
|
2016-11-22 18:21:40 +01:00
|
|
|
if (sema->state != SEMA_OK) {
|
|
|
|
return -ECANCELED;
|
2015-08-03 18:52:32 +02:00
|
|
|
}
|
|
|
|
|
2017-03-06 12:02:46 +01:00
|
|
|
int did_block = block;
|
2016-11-22 18:21:40 +01:00
|
|
|
unsigned old = irq_disable();
|
2017-03-06 12:02:46 +01:00
|
|
|
while ((sema->value == 0) && block) {
|
2016-11-22 18:21:40 +01:00
|
|
|
irq_restore(old);
|
2017-03-06 12:02:46 +01:00
|
|
|
if (us == 0) {
|
|
|
|
mutex_lock(&sema->mutex);
|
|
|
|
}
|
|
|
|
else {
|
2022-03-01 10:12:34 +01:00
|
|
|
uint64_t start = ztimer64_now(clock);
|
|
|
|
block = !ztimer64_mutex_lock_timeout(clock, &sema->mutex, us);
|
|
|
|
uint64_t elapsed = ztimer64_now(clock) - start;
|
2017-03-06 12:02:46 +01:00
|
|
|
|
|
|
|
if (elapsed < us) {
|
|
|
|
us -= elapsed;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
block = 0;
|
|
|
|
}
|
|
|
|
}
|
2015-08-03 18:52:32 +02:00
|
|
|
|
2016-11-22 18:21:40 +01:00
|
|
|
if (sema->state != SEMA_OK) {
|
|
|
|
mutex_unlock(&sema->mutex);
|
|
|
|
return -ECANCELED;
|
2015-08-03 18:52:32 +02:00
|
|
|
}
|
|
|
|
|
2016-11-22 18:21:40 +01:00
|
|
|
old = irq_disable();
|
2015-08-03 18:52:32 +02:00
|
|
|
}
|
|
|
|
|
2017-03-06 12:02:46 +01:00
|
|
|
if (sema->value == 0) {
|
|
|
|
irq_restore(old);
|
|
|
|
return (did_block) ? -ETIMEDOUT : -EAGAIN;
|
|
|
|
}
|
|
|
|
|
2016-11-22 18:21:40 +01:00
|
|
|
unsigned int value = --sema->value;
|
|
|
|
irq_restore(old);
|
|
|
|
|
2017-03-06 12:02:46 +01:00
|
|
|
/* only unlock mutex if it was a blocking operation */
|
|
|
|
if (did_block && value > 0) {
|
2016-11-22 18:21:40 +01:00
|
|
|
mutex_unlock(&sema->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2015-08-03 18:52:32 +02:00
|
|
|
}
|
2021-01-15 15:57:46 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
int _sema_wait_ztimer(sema_t *sema, int block,
|
|
|
|
ztimer_clock_t *clock, uint32_t timeout)
|
|
|
|
{
|
|
|
|
assert(sema != NULL);
|
|
|
|
|
|
|
|
if (sema->state != SEMA_OK) {
|
|
|
|
return -ECANCELED;
|
|
|
|
}
|
|
|
|
|
|
|
|
int did_block = block;
|
|
|
|
unsigned old = irq_disable();
|
|
|
|
while ((sema->value == 0) && block) {
|
|
|
|
irq_restore(old);
|
|
|
|
if (timeout == 0) {
|
|
|
|
mutex_lock(&sema->mutex);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ztimer_now_t start = ztimer_now(clock);
|
|
|
|
block = !ztimer_mutex_lock_timeout(clock, &sema->mutex, timeout);
|
|
|
|
uint32_t elapsed = (uint32_t)(ztimer_now(clock) - start);
|
|
|
|
|
|
|
|
if (elapsed < timeout) {
|
|
|
|
timeout -= elapsed;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
block = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sema->state != SEMA_OK) {
|
|
|
|
mutex_unlock(&sema->mutex);
|
|
|
|
return -ECANCELED;
|
|
|
|
}
|
|
|
|
|
|
|
|
old = irq_disable();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sema->value == 0) {
|
|
|
|
irq_restore(old);
|
|
|
|
return (did_block) ? -ETIMEDOUT : -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int value = --sema->value;
|
|
|
|
irq_restore(old);
|
|
|
|
|
|
|
|
/* only unlock mutex if it was a blocking operation */
|
|
|
|
if (did_block && value > 0) {
|
|
|
|
mutex_unlock(&sema->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-08-03 18:52:32 +02:00
|
|
|
|
2015-10-19 16:25:06 +02:00
|
|
|
int sema_post(sema_t *sema)
|
2015-08-03 18:52:32 +02:00
|
|
|
{
|
2016-11-22 18:21:40 +01:00
|
|
|
assert(sema != NULL);
|
2015-12-01 19:11:45 +01:00
|
|
|
|
2016-11-22 18:21:40 +01:00
|
|
|
unsigned old = irq_disable();
|
|
|
|
if (sema->value == UINT_MAX) {
|
|
|
|
irq_restore(old);
|
2015-08-03 18:52:32 +02:00
|
|
|
return -EOVERFLOW;
|
|
|
|
}
|
2016-11-22 18:21:40 +01:00
|
|
|
|
|
|
|
unsigned value = sema->value++;
|
|
|
|
irq_restore(old);
|
|
|
|
|
|
|
|
if (value == 0) {
|
|
|
|
mutex_unlock(&sema->mutex);
|
2015-08-03 18:52:32 +02:00
|
|
|
}
|
|
|
|
|
2015-12-26 17:30:35 +01:00
|
|
|
return 0;
|
2015-08-03 18:52:32 +02:00
|
|
|
}
|
|
|
|
/** @} */
|