1 /*
2 * Copyright (C) 2024 Intel Corporation
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6 #include <zephyr/kernel.h>
7 #include <kernel_internal.h>
8
9 /* We are not building thread.c when MULTITHREADING=n, so we
10 * need to provide a few stubs here.
11 */
k_is_in_isr(void)12 bool k_is_in_isr(void)
13 {
14 return arch_is_in_isr();
15 }
16
17 /* This is a fallback implementation of k_sleep() for when multi-threading is
18 * disabled. The main implementation is in sched.c.
19 */
z_impl_k_sleep(k_timeout_t timeout)20 int32_t z_impl_k_sleep(k_timeout_t timeout)
21 {
22 k_ticks_t ticks;
23 uint32_t ticks_to_wait;
24
25 __ASSERT(!arch_is_in_isr(), "");
26
27 SYS_PORT_TRACING_FUNC_ENTER(k_thread, sleep, timeout);
28
29 /* in case of K_FOREVER, we suspend */
30 if (K_TIMEOUT_EQ(timeout, K_FOREVER)) {
31 /* In Single Thread, just wait for an interrupt saving power */
32 k_cpu_idle();
33 SYS_PORT_TRACING_FUNC_EXIT(k_thread, sleep, timeout, (int32_t) K_TICKS_FOREVER);
34
35 return (int32_t) K_TICKS_FOREVER;
36 }
37
38 ticks = timeout.ticks;
39 if (Z_TICK_ABS(ticks) <= 0) {
40 /* ticks is delta timeout */
41 ticks_to_wait = ticks;
42 } else {
43 /* ticks is absolute timeout expiration */
44 uint32_t curr_ticks = sys_clock_tick_get_32();
45
46 if (Z_TICK_ABS(ticks) > curr_ticks) {
47 ticks_to_wait = Z_TICK_ABS(ticks) - curr_ticks;
48 } else {
49 ticks_to_wait = 0;
50 }
51 }
52 /* busy wait to be time coherent since subsystems may depend on it */
53 z_impl_k_busy_wait(k_ticks_to_us_ceil32(ticks_to_wait));
54
55 int32_t ret = k_ticks_to_ms_ceil64(0);
56
57 SYS_PORT_TRACING_FUNC_EXIT(k_thread, sleep, timeout, ret);
58
59 return ret;
60 }
61