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 e814d30b7d..f513554cd5 100644 --- a/sys/Makefile.dep +++ b/sys/Makefile.dep @@ -255,7 +255,7 @@ ifneq (,$(filter posix_select,$(USEMODULE))) endif USEMODULE += core_thread_flags USEMODULE += posix_headers - USEMODULE += xtimer + USEMODULE += ztimer64_usec endif ifneq (,$(filter picolibc,$(USEMODULE))) @@ -325,12 +325,8 @@ ifneq (,$(filter shell_commands,$(USEMODULE))) endif ifneq (,$(filter posix_semaphore,$(USEMODULE))) - USEMODULE += sema - USEMODULE += xtimer - ifneq (,$(filter ztimer_xtimer_compat,$(USEMODULE))) - # requires sema_timed that requires 64bit - USEMODULE += ztimer64_xtimer_compat - endif + USEMODULE += sema_deprecated + USEMODULE += ztimer64_usec USEMODULE += posix_headers endif @@ -348,6 +344,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 @@ -393,7 +399,7 @@ ifneq (,$(filter netstats_neighbor_%, $(USEMODULE))) endif ifneq (,$(filter pthread,$(USEMODULE))) - USEMODULE += xtimer + USEMODULE += ztimer64_usec USEMODULE += timex ifneq (,$(filter ztimer_xtimer_compat,$(USEMODULE))) # requires 64bit ftimestamps 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/posix/pthread/pthread_cond.c b/sys/posix/pthread/pthread_cond.c index a9ad54b14c..d5f9daa18c 100644 --- a/sys/posix/pthread/pthread_cond.c +++ b/sys/posix/pthread/pthread_cond.c @@ -22,7 +22,8 @@ #include "pthread_cond.h" #include "thread.h" -#include "xtimer.h" +#include "ztimer64.h" +#include "timex.h" #include "sched.h" #include "irq.h" #include "debug.h" @@ -126,17 +127,17 @@ int pthread_cond_timedwait(pthread_cond_t *cond, mutex_t *mutex, const struct ti return EINVAL; } - uint64_t now = xtimer_now_usec64(); + uint64_t now = ztimer64_now(ZTIMER64_USEC); uint64_t then = ((uint64_t)abstime->tv_sec * US_PER_SEC) + (abstime->tv_nsec / NS_PER_US); int ret = 0; if (then > now) { - xtimer_t timer; + ztimer64_t timer; priority_queue_node_t n; _init_cond_wait(cond, &n); - xtimer_set_wakeup64(&timer, (then - now), thread_getpid()); + ztimer64_set_wakeup(ZTIMER64_USEC, &timer, (then - now), thread_getpid()); mutex_unlock_and_sleep(mutex); @@ -148,7 +149,7 @@ int pthread_cond_timedwait(pthread_cond_t *cond, mutex_t *mutex, const struct ti irq_restore(old_state); ret = ETIMEDOUT; } - xtimer_remove(&timer); + ztimer64_remove(ZTIMER64_USEC, &timer); } else { mutex_unlock(mutex); diff --git a/sys/posix/pthread/pthread_rwlock.c b/sys/posix/pthread/pthread_rwlock.c index 200b412079..7ed1db6bfe 100644 --- a/sys/posix/pthread/pthread_rwlock.c +++ b/sys/posix/pthread/pthread_rwlock.c @@ -33,7 +33,8 @@ #include "pthread.h" #include "sched.h" -#include "xtimer.h" +#include "ztimer64.h" +#include "timex.h" #include "thread.h" @@ -187,7 +188,7 @@ static int pthread_rwlock_timedlock(pthread_rwlock_t *rwlock, int incr_when_held, const struct timespec *abstime) { - uint64_t now = xtimer_now_usec64(); + uint64_t now = ztimer64_now(ZTIMER64_USEC); uint64_t then = ((uint64_t)abstime->tv_sec * US_PER_SEC) + (abstime->tv_nsec / NS_PER_US); @@ -195,11 +196,11 @@ static int pthread_rwlock_timedlock(pthread_rwlock_t *rwlock, return ETIMEDOUT; } else { - xtimer_t timer; - xtimer_set_wakeup64(&timer, (then - now), thread_getpid()); + ztimer64_t timer; + ztimer64_set_wakeup(ZTIMER64_USEC, &timer, (then - now), thread_getpid()); int result = pthread_rwlock_lock(rwlock, is_blocked, is_writer, incr_when_held, true); if (result != ETIMEDOUT) { - xtimer_remove(&timer); + ztimer64_remove(ZTIMER64_USEC, &timer); } return result; diff --git a/sys/posix/select/posix_select.c b/sys/posix/select/posix_select.c index b8d56fa8dc..1ada0d7f29 100644 --- a/sys/posix/select/posix_select.c +++ b/sys/posix/select/posix_select.c @@ -18,7 +18,8 @@ #include "thread_flags.h" #include "vfs.h" -#include "xtimer.h" +#include "ztimer64.h" +#include "timex.h" #if IS_USED(MODULE_POSIX_SOCKETS) extern bool posix_socket_is(int fd); @@ -44,7 +45,7 @@ static inline void posix_socket_select(int fd) } #endif /* IS_USED(MODULE_POSIX_SOCKETS) */ -static int _set_timeout(xtimer_t *timeout_timer, struct timeval *timeout, +static int _set_timeout(ztimer64_t *timeout_timer, struct timeval *timeout, uint32_t offset, bool *wait) { if (timeout != NULL) { @@ -62,7 +63,7 @@ static int _set_timeout(xtimer_t *timeout_timer, struct timeval *timeout, return -1; } else { - xtimer_set_timeout_flag(timeout_timer, (uint32_t)t); + ztimer64_set_timeout_flag(ZTIMER64_USEC, timeout_timer, (uint32_t)t); } } return 0; @@ -71,9 +72,9 @@ static int _set_timeout(xtimer_t *timeout_timer, struct timeval *timeout, int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout) { - uint32_t start_time = xtimer_now_usec(); + uint32_t start_time = ztimer64_now(ZTIMER64_USEC); fd_set ret_readfds; - xtimer_t timeout_timer; + ztimer64_t timeout_timer; int fds_set = 0; bool wait = true; @@ -112,7 +113,7 @@ int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, } while (wait) { if (_set_timeout(&timeout_timer, timeout, - xtimer_now_usec() - start_time, &wait) < 0) { + ztimer64_now(ZTIMER64_USEC) - start_time, &wait) < 0) { return -1; } if (!wait) { @@ -136,7 +137,7 @@ int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, errno = EINTR; return -1; } - xtimer_remove(&timeout_timer); + ztimer64_remove(ZTIMER64_USEC, &timeout_timer); } *readfds = ret_readfds; return fds_set; diff --git a/sys/posix/semaphore/posix_semaphore.c b/sys/posix/semaphore/posix_semaphore.c index b5df5bc118..75d67640f0 100644 --- a/sys/posix/semaphore/posix_semaphore.c +++ b/sys/posix/semaphore/posix_semaphore.c @@ -26,7 +26,7 @@ #include "sema.h" #include "timex.h" #include "thread.h" -#include "xtimer.h" +#include "ztimer64.h" #define ENABLE_DEBUG 0 #include "debug.h" @@ -37,7 +37,7 @@ int sem_timedwait(sem_t *sem, const struct timespec *abstime) { uint64_t timeout = (((uint64_t)abstime->tv_sec) * US_PER_SEC) + (abstime->tv_nsec / NS_PER_US); - uint64_t now = xtimer_now_usec64(); + uint64_t now = ztimer64_now(ZTIMER64_USEC); if (now > timeout) { errno = ETIMEDOUT; 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/posix_semaphore/Makefile b/tests/posix_semaphore/Makefile index 921a3c876c..1761e297ce 100644 --- a/tests/posix_semaphore/Makefile +++ b/tests/posix_semaphore/Makefile @@ -2,5 +2,6 @@ include ../Makefile.tests_common USEMODULE += fmt USEMODULE += posix_semaphore +USEMODULE += ztimer64_usec include $(RIOTBASE)/Makefile.include diff --git a/tests/posix_semaphore/main.c b/tests/posix_semaphore/main.c index 2a8da0622f..fc3a175e84 100644 --- a/tests/posix_semaphore/main.c +++ b/tests/posix_semaphore/main.c @@ -30,7 +30,7 @@ #include "msg.h" #include "timex.h" #include "thread.h" -#include "xtimer.h" +#include "ztimer64.h" #define SEMAPHORE_MSG_QUEUE_SIZE (8) #define SEMAPHORE_TEST_THREADS (5) @@ -263,12 +263,12 @@ void test4(void) puts("first: wait 1 sec for s1"); - start = xtimer_now_usec64(); + start = ztimer64_now(ZTIMER64_USEC); abs.tv_sec = (time_t)((start / US_PER_SEC) + 1); abs.tv_nsec = (long)((start % US_PER_SEC) * 1000); int ret = sem_timedwait(&s1, &abs); - elapsed = xtimer_now_usec64() - start; + elapsed = ztimer64_now(ZTIMER64_USEC) - start; if (ret != 0) { if (errno != ETIMEDOUT) { diff --git a/tests/sema/Makefile b/tests/sema/Makefile index 0d0af60982..9f6f91ade1 100644 --- a/tests/sema/Makefile +++ b/tests/sema/Makefile @@ -1,7 +1,6 @@ include ../Makefile.tests_common USEMODULE += sema -USEMODULE += xtimer -USEMODULE += ztimer_usec +USEMODULE += sema_deprecated include $(RIOTBASE)/Makefile.include