diff --git a/makefiles/deprecated_modules.inc.mk b/makefiles/deprecated_modules.inc.mk index 6bdbc5c007..84f29e0a7c 100644 --- a/makefiles/deprecated_modules.inc.mk +++ b/makefiles/deprecated_modules.inc.mk @@ -2,3 +2,4 @@ # Keep this list ALPHABETICALLY SORTED!!!!111elven DEPRECATED_MODULES += event_thread_lowest DEPRECATED_MODULES += gnrc_netdev_default +DEPRECATED_MODULES += sema_deprecated diff --git a/makefiles/pseudomodules.inc.mk b/makefiles/pseudomodules.inc.mk index 2531e39681..29a2be0f82 100644 --- a/makefiles/pseudomodules.inc.mk +++ b/makefiles/pseudomodules.inc.mk @@ -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 diff --git a/sys/Makefile.dep b/sys/Makefile.dep index 82271037cc..619d64ee3e 100644 --- a/sys/Makefile.dep +++ b/sys/Makefile.dep @@ -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 diff --git a/sys/include/sema.h b/sys/include/sema.h index 77fd56666f..58488ede7f 100644 --- a/sys/include/sema.h +++ b/sys/include/sema.h @@ -28,9 +28,10 @@ #include #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. diff --git a/sys/sema/Kconfig b/sys/sema/Kconfig index c05c224fd1..038136f70c 100644 --- a/sys/sema/Kconfig +++ b/sys/sema/Kconfig @@ -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 diff --git a/sys/sema/sema.c b/sys/sema/sema.c index 37c5601e5f..95c89330df 100644 --- a/sys/sema/sema.c +++ b/sys/sema/sema.c @@ -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) { diff --git a/tests/sema/Makefile b/tests/sema/Makefile index 0d0af60982..ce88d28105 100644 --- a/tests/sema/Makefile +++ b/tests/sema/Makefile @@ -1,7 +1,5 @@ include ../Makefile.tests_common USEMODULE += sema -USEMODULE += xtimer -USEMODULE += ztimer_usec include $(RIOTBASE)/Makefile.include