zbus: assert when inside an ISR and time out is not zero

Currently various zbus functions silently change the timeout
to zero  when inside an ISR. If a developer is not aware
of this it could lead to unexpected behaviour or a
publish/read failing.

Also in the zbus docs it states to only use a timeout of
zero when inside a ISR multiple times.

Signed-off-by: Brandon Allen <brandon.allen@exacttechnology.com>
This commit is contained in:
Brandon Allen 2025-01-17 14:21:53 -05:00 committed by Benjamin Cabé
commit e0173d9b8e
2 changed files with 12 additions and 4 deletions

View file

@ -380,6 +380,8 @@ int zbus_chan_pub(const struct zbus_channel *chan, const void *msg, k_timeout_t
_ZBUS_ASSERT(chan != NULL, "chan is required");
_ZBUS_ASSERT(msg != NULL, "msg is required");
_ZBUS_ASSERT(k_is_in_isr() ? K_TIMEOUT_EQ(timeout, K_NO_WAIT) : true,
"inside an ISR, the timeout must be K_NO_WAIT");
if (k_is_in_isr()) {
timeout = K_NO_WAIT;
@ -416,6 +418,8 @@ int zbus_chan_read(const struct zbus_channel *chan, void *msg, k_timeout_t timeo
{
_ZBUS_ASSERT(chan != NULL, "chan is required");
_ZBUS_ASSERT(msg != NULL, "msg is required");
_ZBUS_ASSERT(k_is_in_isr() ? K_TIMEOUT_EQ(timeout, K_NO_WAIT) : true,
"inside an ISR, the timeout must be K_NO_WAIT");
if (k_is_in_isr()) {
timeout = K_NO_WAIT;
@ -438,6 +442,8 @@ int zbus_chan_notify(const struct zbus_channel *chan, k_timeout_t timeout)
int err;
_ZBUS_ASSERT(chan != NULL, "chan is required");
_ZBUS_ASSERT(k_is_in_isr() ? K_TIMEOUT_EQ(timeout, K_NO_WAIT) : true,
"inside an ISR, the timeout must be K_NO_WAIT");
if (k_is_in_isr()) {
timeout = K_NO_WAIT;
@ -462,6 +468,8 @@ int zbus_chan_notify(const struct zbus_channel *chan, k_timeout_t timeout)
int zbus_chan_claim(const struct zbus_channel *chan, k_timeout_t timeout)
{
_ZBUS_ASSERT(chan != NULL, "chan is required");
_ZBUS_ASSERT(k_is_in_isr() ? K_TIMEOUT_EQ(timeout, K_NO_WAIT) : true,
"inside an ISR, the timeout must be K_NO_WAIT");
if (k_is_in_isr()) {
timeout = K_NO_WAIT;

View file

@ -336,14 +336,14 @@ ZTEST(basic, test_specification_based__zbus_chan)
/* Trying to call the zbus functions in a ISR context. None must work */
ISR_OP(PUB_ISR, 0);
ISR_OP(PUB_ISR_INVAL, 0);
ISR_OP(PUB_ISR_INVAL, -EFAULT);
ISR_OP(READ_ISR, 0);
ISR_OP(READ_ISR_INVAL, 0);
ISR_OP(READ_ISR_INVAL, -EFAULT);
ISR_OP(NOTIFY_ISR, 0);
ISR_OP(NOTIFY_ISR_INVAL, 0);
ISR_OP(NOTIFY_ISR_INVAL, -EFAULT);
ISR_OP(CLAIM_ISR, 0);
ISR_OP(FINISH_ISR, 0);
ISR_OP(CLAIM_ISR_INVAL, 0);
ISR_OP(CLAIM_ISR_INVAL, -EFAULT);
ISR_OP(FINISH_ISR, 0);
ISR_OP(FINISH_ISR_INVAL, -EFAULT);
ISR_OP(ADD_OBS_ISR, -EFAULT);