Skip to content

Commit

Permalink
sys/net/gnrc_sock: use ztimer_mbox_get_timeout()
Browse files Browse the repository at this point in the history
  • Loading branch information
maribu committed Nov 13, 2023
1 parent 034f020 commit 963d3b3
Showing 1 changed file with 25 additions and 62 deletions.
87 changes: 25 additions & 62 deletions sys/net/gnrc/sock/gnrc_sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
#if IS_USED(MODULE_ZTIMER_USEC)
#include "ztimer.h"
#endif
#if IS_USED(MODULE_XTIMER)
#include "xtimer.h"
#endif

#include "sock_types.h"
#include "gnrc_sock_internal.h"
Expand All @@ -41,22 +38,8 @@ extern gnrc_pktsnip_t *gnrc_pktbuf_fuzzptr;
gnrc_pktsnip_t *gnrc_sock_prevpkt = NULL;
#endif

#if IS_USED(MODULE_XTIMER) || IS_USED(MODULE_ZTIMER_USEC)
#define _TIMEOUT_MAGIC (0xF38A0B63U)
#define _TIMEOUT_MSG_TYPE (0x8474)

static void _callback_put(void *arg)
{
msg_t timeout_msg = { .sender_pid = KERNEL_PID_UNDEF,
.type = _TIMEOUT_MSG_TYPE,
.content = { .value = _TIMEOUT_MAGIC } };
gnrc_sock_reg_t *reg = arg;

/* should be safe, because otherwise if mbox were filled this callback is
* senseless */
mbox_try_put(&reg->mbox, &timeout_msg);
}
#endif
#define TIMEOUT_SUPPORTED \
(IS_USED(MODULE_ZTIMER_USEC) && IS_USED(MODULE_CORE_THREAD_FLAGS) && IS_USED(MODULE_CORE_MBOX))

#ifdef SOCK_HAS_ASYNC
static void _netapi_cb(uint16_t cmd, gnrc_pktsnip_t *pkt, void *ctx)
Expand Down Expand Up @@ -119,60 +102,40 @@ ssize_t gnrc_sock_recv(gnrc_sock_reg_t *reg, gnrc_pktsnip_t **pkt_out,
if (mbox_size(&reg->mbox) != GNRC_SOCK_MBOX_SIZE) {
return -EINVAL;
}
#if IS_USED(MODULE_ZTIMER_USEC)
ztimer_t timeout_timer = { .base = { .next = NULL } };
if ((timeout != SOCK_NO_TIMEOUT) && (timeout != 0)) {
timeout_timer.callback = _callback_put;
timeout_timer.arg = reg;
ztimer_set(ZTIMER_USEC, &timeout_timer, timeout);
}
#elif IS_USED(MODULE_XTIMER)
xtimer_t timeout_timer = { .callback = NULL };

/* xtimer_spin would make this never receive anything.
* Avoid that by setting the minimal not spinning timeout. */
if (timeout < XTIMER_BACKOFF && timeout > 0) {
timeout = XTIMER_BACKOFF;
}

if ((timeout != SOCK_NO_TIMEOUT) && (timeout != 0)) {
timeout_timer.callback = _callback_put;
timeout_timer.arg = reg;
xtimer_set(&timeout_timer, timeout);
}
#endif
if (timeout != 0) {
#if defined(DEVELHELP) && IS_ACTIVE(SOCK_HAS_ASYNC)
if (reg->async_cb.generic) {
/* this warning is a false positive when sock_*_recv() was not called from
* the asynchronous handler */
LOG_WARNING("gnrc_sock: timeout != 0 within the asynchronous callback lead "
"to unexpected delays within the asynchronous handler.\n");
}
#endif
mbox_get(&reg->mbox, &msg);
if ((timeout != 0) && (reg->async_cb.generic)) {
/* this warning is a false positive when sock_*_recv() was not called from
* the asynchronous handler */
LOG_WARNING("gnrc_sock: timeout != 0 within the asynchronous callback lead "
"to unexpected delays within the asynchronous handler.\n");
}
else {
#endif

switch (timeout) {
case 0: /* non-blocking without timeout */
if (!mbox_try_get(&reg->mbox, &msg)) {
return -EAGAIN;
}
}
#if IS_USED(MODULE_ZTIMER_USEC)
ztimer_remove(ZTIMER_USEC, &timeout_timer);
#elif IS_USED(MODULE_XTIMER)
xtimer_remove(&timeout_timer);
break;
case SOCK_NO_TIMEOUT: /* blocking without timeout */
mbox_get(&reg->mbox, &msg);
break;
default: /* non-blocking with timeout */
#if TIMEOUT_SUPPORTED
if (ztimer_mbox_get_timeout(ZTIMER_USEC, &reg->mbox, &msg, timeout)) {
return -ETIMEDOUT;
}
#else
return -ENOTSUP;
#endif
break;
}

switch (msg.type) {
case GNRC_NETAPI_MSG_TYPE_RCV:
pkt = msg.content.ptr;
break;
#if IS_USED(MODULE_XTIMER) || IS_USED(MODULE_ZTIMER_USEC)
case _TIMEOUT_MSG_TYPE:
if (msg.content.value == _TIMEOUT_MAGIC) {
return -ETIMEDOUT;
}
#endif
/* Falls Through. */
default:
return -EINVAL;
}
Expand Down

0 comments on commit 963d3b3

Please sign in to comment.