Skip to content

Commit f37ecf5

Browse files
committed
util: Provide macro to cause a minor delay in busy loops in POSIX ARCH
In the POSIX ARCH, code takes zero time to execute, so busy wait loops become infinite loops, unless we force the loop to take a bit of time. Signed-off-by: Alberto Escolar Piedras <[email protected]>
1 parent 1f178ca commit f37ecf5

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

include/zephyr/sys/util.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,24 @@ char *utf8_lcpy(char *dst, const char *src, size_t n);
578578
/** @brief Number of Hz in @p x MHz */
579579
#define MHZ(x) (KHZ(x) * 1000)
580580

581+
/**
582+
* @brief For the POSIX architecture add a minimal delay in a busy wait loop.
583+
* For other architectures this is a no-op.
584+
*
585+
* In the POSIX ARCH, code takes zero simulated time to execute,
586+
* so busy wait loops become infinite loops, unless we
587+
* force the loop to take a bit of time.
588+
* Include this macro in all busy wait/spin loops
589+
* so they will also work when building for the POSIX architecture.
590+
*
591+
* @param t Time in microseconds we will busy wait
592+
*/
593+
#if defined(CONFIG_ARCH_POSIX)
594+
#define Z_SPIN_DELAY(t) k_busy_wait(t)
595+
#else
596+
#define Z_SPIN_DELAY(t)
597+
#endif
598+
581599
/**
582600
* @brief Wait for an expression to return true with a timeout
583601
*

0 commit comments

Comments
 (0)