mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
Merge pull request #17719 from fjmolinas/pr_sys_posix_convert
sys/sema: add sema_ztimer64 to implement old api, deprecate sema
This commit is contained in:
commit
b29a658555
@ -2,3 +2,4 @@
|
||||
# Keep this list ALPHABETICALLY SORTED!!!!111elven
|
||||
DEPRECATED_MODULES += event_thread_lowest
|
||||
DEPRECATED_MODULES += gnrc_netdev_default
|
||||
DEPRECATED_MODULES += sema_deprecated
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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.
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -2,5 +2,6 @@ include ../Makefile.tests_common
|
||||
|
||||
USEMODULE += fmt
|
||||
USEMODULE += posix_semaphore
|
||||
USEMODULE += ztimer64_usec
|
||||
|
||||
include $(RIOTBASE)/Makefile.include
|
||||
|
@ -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) {
|
||||
|
@ -1,7 +1,6 @@
|
||||
include ../Makefile.tests_common
|
||||
|
||||
USEMODULE += sema
|
||||
USEMODULE += xtimer
|
||||
USEMODULE += ztimer_usec
|
||||
USEMODULE += sema_deprecated
|
||||
|
||||
include $(RIOTBASE)/Makefile.include
|
||||
|
Loading…
Reference in New Issue
Block a user