Skip to content

Commit a2541d6

Browse files
committed
util: Replace all POSIX arch busy_waits with Z_SPIN_DELAY
A new Z_SPIN_DELAY() macro has been added which can be used to reduce a bit the amount of noise due to the POSIX arch need to break busy loops with k_busy_wait(). Use it. Signed-off-by: Alberto Escolar Piedras <[email protected]>
1 parent 6272ba2 commit a2541d6

File tree

10 files changed

+21
-89
lines changed

10 files changed

+21
-89
lines changed

include/zephyr/rtio/rtio.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -757,11 +757,8 @@ static inline int z_impl_rtio_submit(struct rtio *r, uint32_t wait_count)
757757
}
758758
#else
759759
while (rtio_spsc_consumable(r->cq) < wait_count) {
760-
#ifdef CONFIG_ARCH_POSIX
761-
k_busy_wait(10);
762-
#else
760+
Z_SPIN_DELAY(10);
763761
k_yield();
764-
#endif /* CONFIG_ARCH_POSIX */
765762
}
766763
#endif
767764

samples/subsys/logging/logger/src/main.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,7 @@ static void performance_showcase(void)
206206
start_timestamp = timestamp_get();
207207

208208
while (start_timestamp == timestamp_get()) {
209-
#if (CONFIG_ARCH_POSIX)
210-
k_busy_wait(100);
211-
#endif
209+
Z_SPIN_DELAY(100);
212210
}
213211

214212
start_timestamp = timestamp_get();
@@ -217,9 +215,7 @@ static void performance_showcase(void)
217215
LOG_INF("performance test - log message %d", cnt);
218216
cnt++;
219217
current_timestamp = timestamp_get();
220-
#if (CONFIG_ARCH_POSIX)
221-
k_busy_wait(100);
222-
#endif
218+
Z_SPIN_DELAY(100);
223219
} while (current_timestamp < (start_timestamp + window));
224220

225221
wait_on_log_flushed();

tests/kernel/common/src/clock.c

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,12 @@
66

77
#include <zephyr/ztest.h>
88

9-
#if defined(CONFIG_ARCH_POSIX)
109
#define ALIGN_MS_BOUNDARY \
1110
do { \
1211
uint32_t t = k_uptime_get_32(); \
1312
while (t == k_uptime_get_32()) \
14-
k_busy_wait(50); \
13+
Z_SPIN_DELAY(50); \
1514
} while (0)
16-
#else
17-
#define ALIGN_MS_BOUNDARY \
18-
do { \
19-
uint32_t t = k_uptime_get_32(); \
20-
while (t == k_uptime_get_32()) \
21-
; \
22-
} while (0)
23-
#endif
2415

2516
struct timer_data {
2617
int duration_count;
@@ -55,17 +46,13 @@ ZTEST_USER(clock, test_clock_uptime)
5546
/**TESTPOINT: uptime elapse*/
5647
t64 = k_uptime_get();
5748
while (k_uptime_get() < (t64 + 5)) {
58-
#if defined(CONFIG_ARCH_POSIX)
59-
k_busy_wait(50);
60-
#endif
49+
Z_SPIN_DELAY(50);
6150
}
6251

6352
/**TESTPOINT: uptime elapse lower 32-bit*/
6453
t32 = k_uptime_get_32();
6554
while (k_uptime_get_32() < (t32 + 5)) {
66-
#if defined(CONFIG_ARCH_POSIX)
67-
k_busy_wait(50);
68-
#endif
55+
Z_SPIN_DELAY(50);
6956
}
7057

7158
/**TESTPOINT: uptime straddled ms boundary*/
@@ -76,9 +63,7 @@ ZTEST_USER(clock, test_clock_uptime)
7663
/**TESTPOINT: uptime delta*/
7764
d64 = k_uptime_delta(&d64);
7865
while (k_uptime_delta(&d64) == 0) {
79-
#if defined(CONFIG_ARCH_POSIX)
80-
k_busy_wait(50);
81-
#endif
66+
Z_SPIN_DELAY(50);
8267
}
8368
}
8469

@@ -132,19 +117,15 @@ ZTEST(clock, test_clock_cycle_32)
132117
/*break if cycle counter wrap around*/
133118
while (k_cycle_get_32() > c32 &&
134119
k_cycle_get_32() < (c32 + k_ticks_to_cyc_floor32(1))) {
135-
#if defined(CONFIG_ARCH_POSIX)
136-
k_busy_wait(50);
137-
#endif
120+
Z_SPIN_DELAY(50);
138121
}
139122

140123
/**TESTPOINT: cycle/uptime cross check*/
141124
c0 = k_cycle_get_32();
142125
ALIGN_MS_BOUNDARY;
143126
t32 = k_uptime_get_32();
144127
while (t32 == k_uptime_get_32()) {
145-
#if defined(CONFIG_ARCH_POSIX)
146-
k_busy_wait(50);
147-
#endif
128+
Z_SPIN_DELAY(50);
148129
}
149130

150131
c1 = k_uptime_get_32();

tests/kernel/common/src/timeout_order.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ ZTEST(common_1cpu, test_timeout_order)
6666

6767
/* sync on tick */
6868
while (uptime == k_uptime_get_32()) {
69-
#if defined(CONFIG_ARCH_POSIX)
70-
k_busy_wait(50);
71-
#endif
69+
Z_SPIN_DELAY(50);
7270
}
7371

7472
for (ii = 0; ii < NUM_TIMEOUTS; ii++) {

tests/kernel/context/src/main.c

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,7 @@ static void _test_kernel_cpu_idle(int atomic)
254254
/* Align to a "ms boundary". */
255255
tms = k_uptime_get_32();
256256
while (tms == k_uptime_get_32()) {
257-
#if defined(CONFIG_ARCH_POSIX)
258-
k_busy_wait(50);
259-
#endif
257+
Z_SPIN_DELAY(50);
260258
}
261259

262260
tms = k_uptime_get_32();
@@ -396,16 +394,12 @@ static void _test_kernel_interrupts(disable_int_func disable_int,
396394
/* Align to a "tick boundary" */
397395
tick = sys_clock_tick_get_32();
398396
while (sys_clock_tick_get_32() == tick) {
399-
#if defined(CONFIG_ARCH_POSIX)
400-
k_busy_wait(1000);
401-
#endif
397+
Z_SPIN_DELAY(1000);
402398
}
403399

404400
tick++;
405401
while (sys_clock_tick_get_32() == tick) {
406-
#if defined(CONFIG_ARCH_POSIX)
407-
k_busy_wait(1000);
408-
#endif
402+
Z_SPIN_DELAY(1000);
409403
count++;
410404
}
411405

@@ -422,9 +416,7 @@ static void _test_kernel_interrupts(disable_int_func disable_int,
422416
tick = sys_clock_tick_get_32();
423417
for (i = 0; i < count; i++) {
424418
sys_clock_tick_get_32();
425-
#if defined(CONFIG_ARCH_POSIX)
426-
k_busy_wait(1000);
427-
#endif
419+
Z_SPIN_DELAY(1000);
428420
}
429421

430422
tick2 = sys_clock_tick_get_32();
@@ -446,9 +438,7 @@ static void _test_kernel_interrupts(disable_int_func disable_int,
446438
/* Now repeat with interrupts unlocked. */
447439
for (i = 0; i < count; i++) {
448440
sys_clock_tick_get_32();
449-
#if defined(CONFIG_ARCH_POSIX)
450-
k_busy_wait(1000);
451-
#endif
441+
Z_SPIN_DELAY(1000);
452442
}
453443

454444
tick2 = sys_clock_tick_get_32();

tests/kernel/sched/preempt/src/main.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,7 @@ void wakeup_src_thread(int id)
117117

118118
while (do_sleep && !(src_thread->base.thread_state & _THREAD_PENDING)) {
119119
/* spin, waiting on the sleep timeout */
120-
#if defined(CONFIG_ARCH_POSIX)
121-
/**
122-
* In the posix arch busy wait loops waiting for something to
123-
* happen need to halt the CPU due to the infinitely fast clock
124-
* assumption. (Or in plain English: otherwise you hang in this
125-
* loop. Because the posix arch emulates having 1 CPU by only
126-
* enabling 1 thread at a time. And because it assumes code
127-
* executes in 0 time: it always waits for the code to finish
128-
* and it letting the cpu sleep before letting time pass)
129-
*/
130-
k_busy_wait(50);
131-
#endif
120+
Z_SPIN_DELAY(50);
132121
}
133122

134123
/* We are lowest priority, SOMEONE must have run */

tests/kernel/sched/schedule_api/src/test_sched_timeslice_reset.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,7 @@ ZTEST(threads_scheduling, test_slice_reset)
177177
/* current thread (ztest native) consumed a half timeslice */
178178
t32 = k_cycle_get_32();
179179
while (k_cycle_get_32() - t32 < half_slice_cyc) {
180-
#if defined(CONFIG_ARCH_POSIX)
181-
k_busy_wait(50);
182-
#endif
180+
Z_SPIN_DELAY(50);
183181
}
184182

185183
/* relinquish CPU and wait for each thread to complete */

tests/kernel/sleep/src/main.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ static void align_to_tick_boundary(void)
8383
tick = k_uptime_get_32();
8484
while (k_uptime_get_32() == tick) {
8585
/* Busy wait to align to tick boundary */
86-
#if defined(CONFIG_ARCH_POSIX)
87-
k_busy_wait(50);
88-
#endif
86+
Z_SPIN_DELAY(50);
8987
}
9088

9189
}

tests/kernel/tickless/tickless_concept/src/main.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,13 @@ static struct k_thread tdata[NUM_THREAD];
2727
#define SLICE_SIZE_LIMIT k_ticks_to_ms_floor64((IDLE_THRESH >> 1) + 1)
2828

2929
/*align to millisecond boundary*/
30-
#if defined(CONFIG_ARCH_POSIX)
3130
#define ALIGN_MS_BOUNDARY() \
3231
do { \
3332
uint32_t t = k_uptime_get_32(); \
3433
while (t == k_uptime_get_32()) \
35-
k_busy_wait(50); \
34+
Z_SPIN_DELAY(50); \
3635
} while (0)
37-
#else
38-
#define ALIGN_MS_BOUNDARY() \
39-
do { \
40-
uint32_t t = k_uptime_get_32(); \
41-
while (t == k_uptime_get_32()) \
42-
; \
43-
} while (0)
44-
#endif
36+
4537
K_SEM_DEFINE(sema, 0, NUM_THREAD);
4638
static int64_t elapsed_slice;
4739

tests/kernel/workq/critical/src/main.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,7 @@ uint32_t critical_loop(const char *tag, uint32_t count)
100100
k_work_init(&work_item, critical_rtn);
101101
k_work_submit_to_queue(&offload_work_q, &work_item);
102102
count++;
103-
#if defined(CONFIG_ARCH_POSIX)
104-
k_busy_wait(50);
105-
/*
106-
* For the POSIX arch this loop and critical_rtn would otherwise
107-
* run in 0 time and therefore would never finish.
108-
* => We purposely waste 50us per loop
109-
*/
110-
#endif
103+
Z_SPIN_DELAY(50);
111104
}
112105
TC_PRINT("End %s at %u\n", tag, (uint32_t)now);
113106

0 commit comments

Comments
 (0)