Skip to content

Commit

Permalink
kernel: spinlock: add assertion to protect K_SPINLOCK
Browse files Browse the repository at this point in the history
Adds an assertion for the GCC toolchain to check whether the K_SPINLOCK
block was left without unlocking the spinlock, e.g. by calling break,
return or goto.

Signed-off-by: Florian Grandel <fgrandel@code-for-humans.de>
  • Loading branch information
fgrandel committed Jul 6, 2023
1 parent 7db768b commit 1b05865
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion include/zephyr/spinlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,17 @@ static ALWAYS_INLINE void k_spin_release(struct k_spinlock *l)
#endif
}

#if defined(CONFIG_SPIN_VALIDATE) && defined(__GNUC__)
static ALWAYS_INLINE void z_spin_onexit(k_spinlock_key_t *k)
{
__ASSERT(k->key, "K_SPINLOCK exited with goto, break or return, "
"use K_SPINLOCK_BREAK instead.");
}
#define K_SPINLOCK_ONEXIT __attribute__((__cleanup__(z_spin_onexit)))
#else
#define K_SPINLOCK_ONEXIT
#endif

/**
* INTERNAL_HIDDEN @endcond
*/
Expand Down Expand Up @@ -286,7 +297,7 @@ static ALWAYS_INLINE void k_spin_release(struct k_spinlock *l)
* @param lck Spinlock used to guard the enclosed code block.
*/
#define K_SPINLOCK(lck) \
for (k_spinlock_key_t __i = {}, __key = k_spin_lock(lck); !__i.key; \
for (k_spinlock_key_t __i K_SPINLOCK_ONEXIT = {}, __key = k_spin_lock(lck); !__i.key; \
k_spin_unlock(lck, __key), __i.key = 1)

/** @} */
Expand Down

0 comments on commit 1b05865

Please sign in to comment.