1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

sys/sema: use sema_ztimer64 to implement old sema api

This PR removes the old xtimer based implementation for sema. Since
this implementation used 64bit timeout, backweard compatibility is
kept by having `sema_wait_timed` be implemented by `ztimer64_usec`
which is enabled by selecting `sema_deprecated`

With this 64bit `sema` api is now deprecated.
This commit is contained in:
Francisco Molina 2022-03-01 10:12:34 +01:00
parent 888b2aa098
commit 2e2daae5d0
7 changed files with 55 additions and 53 deletions

View File

@ -2,3 +2,4 @@
# Keep this list ALPHABETICALLY SORTED!!!!111elven
DEPRECATED_MODULES += event_thread_lowest
DEPRECATED_MODULES += gnrc_netdev_default
DEPRECATED_MODULES += sema_deprecated

View File

@ -186,6 +186,7 @@ PSEUDOMODULES += saul_pwm
PSEUDOMODULES += scanf_float
PSEUDOMODULES += sched_cb
PSEUDOMODULES += sched_runq_callback
PSEUDOMODULES += sema_deprecated
PSEUDOMODULES += semtech_loramac_rx
PSEUDOMODULES += senml_cbor
PSEUDOMODULES += senml_phydat

View File

@ -339,6 +339,16 @@ ifneq (,$(filter sema_inv,$(USEMODULE)))
USEMODULE += atomic_utils
endif
ifneq (,$(filter sema,$(USEMODULE)))
USEMODULE += ztimer
endif
ifneq (,$(filter sema_deprecated,$(USEMODULE)))
USEMODULE += sema
USEMODULE += ztimer64
USEMODULE += ztimer64_usec
endif
ifneq (,$(filter telnet,$(USEMODULE)))
USEMODULE += pipe
USEMODULE += sock_tcp

View File

@ -28,9 +28,10 @@
#include <stdint.h>
#include "mutex.h"
#if IS_USED(MODULE_ZTIMER)
#include "ztimer.h"
#if IS_USED(MODULE_SEMA_DEPRECATED)
#include "ztimer64.h"
#endif
#ifdef __cplusplus
@ -112,30 +113,6 @@ static inline unsigned sema_get_value(const sema_t *sema)
{
return sema->value;
}
#if IS_USED(MODULE_XTIMER) || !IS_USED(MODULE_ZTIMER)
/**
* @brief Wait for a semaphore, blocking or non-blocking.
*
* @details For commit purposes you should probably use sema_wait(),
* sema_wait_timed() and sema_try_wait() instead.
*
* @pre `(sema != NULL)`
*
* @param[in] sema A semaphore.
* @param[in] block if true, block until semaphore is available.
* @param[in] timeout if blocking, time in microseconds until the semaphore
* times out. 0 waits forever.
*
* @return 0 on success
* @return -ETIMEDOUT, if the semaphore times out.
* @return -ECANCELED, if the semaphore was destroyed.
* @return -EAGAIN, if the semaphore is not posted (only if block = 0)
*/
int _sema_wait_xtimer(sema_t *sema, int block, uint64_t timeout);
#endif
#if IS_USED(MODULE_ZTIMER)
/**
* @brief Wait for a semaphore, blocking or non-blocking.
*
@ -157,6 +134,30 @@ int _sema_wait_xtimer(sema_t *sema, int block, uint64_t timeout);
*/
int _sema_wait_ztimer(sema_t *sema, int block,
ztimer_clock_t *clock, uint32_t timeout);
#if IS_USED(MODULE_SEMA_DEPRECATED)
/**
* @brief Wait for a semaphore, blocking or non-blocking.
*
* @internal only
* @details For commit purposes you should probably use sema_wait(),
* sema_wait_timed_ztimer64() and sema_try_wait() instead.
*
* @pre `(sema != NULL)`
*
* @param[in] sema A semaphore.
* @param[in] block if true, block until semaphore is available.
* @param[in] clock ztimer64 clock
* @param[in] timeout if blocking, ticks of @p clock until the semaphore
* times out. 0 waits forever.
*
* @return 0 on success
* @return -ETIMEDOUT, if the semaphore times out.
* @return -ECANCELED, if the semaphore was destroyed.
* @return -EAGAIN, if the semaphore is not posted (only if block = 0)
*/
int _sema_wait_ztimer64(sema_t *sema, int block,
ztimer64_clock_t *clock, uint64_t timeout);
#endif
/**
@ -171,11 +172,7 @@ int _sema_wait_ztimer(sema_t *sema, int block,
*/
static inline int sema_wait(sema_t *sema)
{
#if IS_USED(MODULE_ZTIMER)
return _sema_wait_ztimer(sema, 1, NULL, 0);
#else
return _sema_wait_xtimer(sema, 1, 0);
#endif
}
/**
@ -191,16 +188,14 @@ static inline int sema_wait(sema_t *sema)
*/
static inline int sema_try_wait(sema_t *sema)
{
#if IS_USED(MODULE_ZTIMER)
return _sema_wait_ztimer(sema, 0, NULL, 0);
#else
return _sema_wait_xtimer(sema, 0, 0);
#endif
}
#if IS_USED(MODULE_XTIMER) || defined(DOXYGEN)
#if IS_USED(MODULE_SEMA_DEPRECATED) || defined(DOXYGEN)
/**
* @brief Wait for a semaphore being posted.
* @brief Wait for a semaphore being posted with a 64bit timeout
*
* @deprecated Will be removed after release 2021.07
*
* @pre `(sema != NULL)`
*
@ -215,11 +210,10 @@ static inline int sema_try_wait(sema_t *sema)
*/
static inline int sema_wait_timed(sema_t *sema, uint64_t timeout)
{
return _sema_wait_xtimer(sema, (timeout != 0), timeout);
return _sema_wait_ztimer64(sema, (timeout != 0), ZTIMER64_USEC, timeout);
}
#endif
#if IS_USED(MODULE_ZTIMER) || defined(DOXYGEN)
/**
* @brief Wait for a semaphore being posted, using ztimer as backend
*
@ -242,7 +236,6 @@ static inline int sema_wait_timed_ztimer(sema_t *sema,
{
return _sema_wait_ztimer(sema, (timeout != 0), clock, timeout);
}
#endif
/**
* @brief Signal semaphore.

View File

@ -8,4 +8,9 @@
config MODULE_SEMA
bool "Semaphore"
depends on TEST_KCONFIG
depends on MODULE_XTIMER || MODULE_ZTIMER
select MODULE_ZTIMER
config MODULE_SEMA_DEPRECATED
bool "Semaphore compatible with 64bit timeouts, deprecated"
select MODULE_SEMA
select ZTIMER64_USEC

View File

@ -22,10 +22,6 @@
#include "assert.h"
#include "sema.h"
#if IS_USED(MODULE_XTIMER)
#include "xtimer.h"
#endif
#define ENABLE_DEBUG 0
#include "debug.h"
@ -49,8 +45,8 @@ void sema_destroy(sema_t *sema)
mutex_unlock(&sema->mutex);
}
#if IS_USED(MODULE_XTIMER)
int _sema_wait_xtimer(sema_t *sema, int block, uint64_t us)
#if IS_USED(MODULE_SEMA_DEPRECATED)
int _sema_wait_ztimer64(sema_t *sema, int block, ztimer64_clock_t *clock, uint64_t us)
{
assert(sema != NULL);
@ -66,9 +62,9 @@ int _sema_wait_xtimer(sema_t *sema, int block, uint64_t us)
mutex_lock(&sema->mutex);
}
else {
uint64_t start = xtimer_now_usec64();
block = !xtimer_mutex_lock_timeout(&sema->mutex, us);
uint64_t elapsed = xtimer_now_usec64() - start;
uint64_t start = ztimer64_now(clock);
block = !ztimer64_mutex_lock_timeout(clock, &sema->mutex, us);
uint64_t elapsed = ztimer64_now(clock) - start;
if (elapsed < us) {
us -= elapsed;
@ -103,7 +99,6 @@ int _sema_wait_xtimer(sema_t *sema, int block, uint64_t us)
}
#endif
#if IS_USED(MODULE_ZTIMER)
int _sema_wait_ztimer(sema_t *sema, int block,
ztimer_clock_t *clock, uint32_t timeout)
{
@ -156,7 +151,6 @@ int _sema_wait_ztimer(sema_t *sema, int block,
return 0;
}
#endif
int sema_post(sema_t *sema)
{

View File

@ -1,7 +1,5 @@
include ../Makefile.tests_common
USEMODULE += sema
USEMODULE += xtimer
USEMODULE += ztimer_usec
include $(RIOTBASE)/Makefile.include