1 /*
2  * Copyright (c) 2015 Wind River Systems, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_KERNEL_INCLUDE_TIMEOUT_Q_H_
8 #define ZEPHYR_KERNEL_INCLUDE_TIMEOUT_Q_H_
9 
10 /**
11  * @file
12  * @brief timeout queue for threads on kernel objects
13  */
14 
15 #include <zephyr/kernel.h>
16 
17 #include <stdbool.h>
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 #ifdef CONFIG_SYS_CLOCK_EXISTS
24 
25 /* Value written to dticks when timeout is aborted. */
26 #define TIMEOUT_DTICKS_ABORTED (IS_ENABLED(CONFIG_TIMEOUT_64BIT) ? INT64_MIN : INT32_MIN)
27 
z_init_timeout(struct _timeout * to)28 static inline void z_init_timeout(struct _timeout *to)
29 {
30 	sys_dnode_init(&to->node);
31 }
32 
33 /* Adds the timeout to the queue.
34  *
35  * @return Absolute tick value when timeout will expire.
36  */
37 k_ticks_t z_add_timeout(struct _timeout *to, _timeout_func_t fn, k_timeout_t timeout);
38 
39 int z_abort_timeout(struct _timeout *to);
40 
z_is_inactive_timeout(const struct _timeout * to)41 static inline bool z_is_inactive_timeout(const struct _timeout *to)
42 {
43 	return !sys_dnode_is_linked(&to->node);
44 }
45 
z_is_aborted_timeout(const struct _timeout * to)46 static inline bool z_is_aborted_timeout(const struct _timeout *to)
47 {
48 	/* When timeout is aborted then dticks is set to special value. */
49 	return to->dticks == TIMEOUT_DTICKS_ABORTED;
50 }
51 
z_init_thread_timeout(struct _thread_base * thread_base)52 static inline void z_init_thread_timeout(struct _thread_base *thread_base)
53 {
54 	z_init_timeout(&thread_base->timeout);
55 }
56 
57 extern void z_thread_timeout(struct _timeout *timeout);
58 
z_add_thread_timeout(struct k_thread * thread,k_timeout_t ticks)59 static inline k_ticks_t z_add_thread_timeout(struct k_thread *thread, k_timeout_t ticks)
60 {
61 	return z_add_timeout(&thread->base.timeout, z_thread_timeout, ticks);
62 }
63 
z_abort_thread_timeout(struct k_thread * thread)64 static inline void z_abort_thread_timeout(struct k_thread *thread)
65 {
66 	z_abort_timeout(&thread->base.timeout);
67 }
68 
z_is_aborted_thread_timeout(struct k_thread * thread)69 static inline bool z_is_aborted_thread_timeout(struct k_thread *thread)
70 {
71 
72 	return z_is_aborted_timeout(&thread->base.timeout);
73 }
74 
75 int32_t z_get_next_timeout_expiry(void);
76 
77 k_ticks_t z_timeout_remaining(const struct _timeout *timeout);
78 
79 #else
80 
81 /* Stubs when !CONFIG_SYS_CLOCK_EXISTS */
82 #define z_init_thread_timeout(thread_base) do {} while (false)
83 #define z_abort_thread_timeout(to) do {} while (false)
84 #define z_is_aborted_thread_timeout(to) false
85 #define z_is_inactive_timeout(to) 1
86 #define z_is_aborted_timeout(to) false
87 #define z_get_next_timeout_expiry() ((int32_t) K_TICKS_FOREVER)
88 #define z_set_timeout_expiry(ticks, is_idle) do {} while (false)
89 
90 static inline k_ticks_t z_add_thread_timeout(struct k_thread *thread, k_timeout_t ticks)
91 {
92 	ARG_UNUSED(thread);
93 	ARG_UNUSED(ticks);
94 	return 0;
95 }
96 
97 #endif /* CONFIG_SYS_CLOCK_EXISTS */
98 
99 #ifdef __cplusplus
100 }
101 #endif
102 
103 #endif /* ZEPHYR_KERNEL_INCLUDE_TIMEOUT_Q_H_ */
104