Commit e0173d9b authored by Brandon Allen's avatar Brandon Allen Committed by Benjamin Cabé
Browse files

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: default avatarBrandon Allen <brandon.allen@exacttechnology.com>
parent 64e5a31b
Loading
Loading
Loading
Loading
+8 −0
Changes for subsys/zbus/zbus.c: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -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;
+4 −4
Changes for tests/subsys/zbus/unittests/src/main.c: 4 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -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);