Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, Wind River Systems, Inc. |
| 3 | * |
David B. Kinder | ac74d8b | 2017-01-18 17:01:01 -0800 | [diff] [blame] | 4 | * SPDX-License-Identifier: Apache-2.0 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * @file |
| 9 | * |
| 10 | * @brief Public kernel APIs. |
| 11 | */ |
| 12 | |
Flavio Ceolin | 67ca176 | 2018-09-14 10:43:44 -0700 | [diff] [blame] | 13 | #ifndef ZEPHYR_INCLUDE_KERNEL_H_ |
| 14 | #define ZEPHYR_INCLUDE_KERNEL_H_ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 15 | |
Benjamin Walsh | dfa7ce5 | 2017-01-22 17:06:05 -0500 | [diff] [blame] | 16 | #if !defined(_ASMLANGUAGE) |
Ioannis Glaropoulos | 92b8a41 | 2018-06-20 17:30:48 +0200 | [diff] [blame] | 17 | #include <kernel_includes.h> |
Kumar Gala | 8777ff1 | 2018-07-25 20:24:34 -0500 | [diff] [blame] | 18 | #include <errno.h> |
Flavio Ceolin | 6fdc56d | 2018-09-18 12:32:27 -0700 | [diff] [blame] | 19 | #include <stdbool.h> |
Stephanos Ioannidis | 33fbe00 | 2019-09-09 21:26:59 +0900 | [diff] [blame] | 20 | #include <toolchain.h> |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 21 | |
| 22 | #ifdef __cplusplus |
| 23 | extern "C" { |
| 24 | #endif |
| 25 | |
Anas Nashif | bbb157d | 2017-01-15 08:46:31 -0500 | [diff] [blame] | 26 | /** |
| 27 | * @brief Kernel APIs |
| 28 | * @defgroup kernel_apis Kernel APIs |
| 29 | * @{ |
| 30 | * @} |
| 31 | */ |
| 32 | |
Anas Nashif | 61f4b24 | 2016-11-18 10:53:59 -0500 | [diff] [blame] | 33 | #ifdef CONFIG_KERNEL_DEBUG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 34 | #define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__) |
| 35 | #else |
| 36 | #define K_DEBUG(fmt, ...) |
| 37 | #endif |
| 38 | |
Benjamin Walsh | 2f28041 | 2017-01-14 19:23:46 -0500 | [diff] [blame] | 39 | #if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED) |
| 40 | #define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES) |
| 41 | #define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1) |
| 42 | #elif defined(CONFIG_COOP_ENABLED) |
| 43 | #define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1) |
| 44 | #define _NUM_PREEMPT_PRIO (0) |
| 45 | #elif defined(CONFIG_PREEMPT_ENABLED) |
| 46 | #define _NUM_COOP_PRIO (0) |
| 47 | #define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1) |
| 48 | #else |
| 49 | #error "invalid configuration" |
| 50 | #endif |
| 51 | |
| 52 | #define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x))) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 53 | #define K_PRIO_PREEMPT(x) (x) |
| 54 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 55 | #define K_ANY NULL |
| 56 | #define K_END NULL |
| 57 | |
Benjamin Walsh | edb3570 | 2017-01-14 18:47:22 -0500 | [diff] [blame] | 58 | #if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 59 | #define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES) |
Benjamin Walsh | edb3570 | 2017-01-14 18:47:22 -0500 | [diff] [blame] | 60 | #elif defined(CONFIG_COOP_ENABLED) |
| 61 | #define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1) |
| 62 | #elif defined(CONFIG_PREEMPT_ENABLED) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 63 | #define K_HIGHEST_THREAD_PRIO 0 |
Benjamin Walsh | edb3570 | 2017-01-14 18:47:22 -0500 | [diff] [blame] | 64 | #else |
| 65 | #error "invalid configuration" |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 66 | #endif |
| 67 | |
Benjamin Walsh | 7fa3cd7 | 2017-01-14 18:49:11 -0500 | [diff] [blame] | 68 | #ifdef CONFIG_PREEMPT_ENABLED |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 69 | #define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES |
| 70 | #else |
| 71 | #define K_LOWEST_THREAD_PRIO -1 |
| 72 | #endif |
| 73 | |
Benjamin Walsh | fab8d92 | 2016-11-08 15:36:36 -0500 | [diff] [blame] | 74 | #define K_IDLE_PRIO K_LOWEST_THREAD_PRIO |
| 75 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 76 | #define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO) |
| 77 | #define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1) |
| 78 | |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 79 | #ifdef CONFIG_OBJECT_TRACING |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 80 | #define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next; |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 81 | #define _OBJECT_TRACING_LINKED_FLAG u8_t __linked; |
| 82 | #define _OBJECT_TRACING_INIT \ |
| 83 | .__next = NULL, \ |
| 84 | .__linked = 0, |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 85 | #else |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 86 | #define _OBJECT_TRACING_INIT |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 87 | #define _OBJECT_TRACING_NEXT_PTR(type) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 88 | #define _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 89 | #endif |
| 90 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 91 | #ifdef CONFIG_POLL |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 92 | #define _POLL_EVENT_OBJ_INIT(obj) \ |
| 93 | .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), |
| 94 | #define _POLL_EVENT sys_dlist_t poll_events |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 95 | #else |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 96 | #define _POLL_EVENT_OBJ_INIT(obj) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 97 | #define _POLL_EVENT |
| 98 | #endif |
| 99 | |
Benjamin Walsh | f6ca7de | 2016-11-08 10:36:50 -0500 | [diff] [blame] | 100 | struct k_thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 101 | struct k_mutex; |
| 102 | struct k_sem; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 103 | struct k_msgq; |
| 104 | struct k_mbox; |
| 105 | struct k_pipe; |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 106 | struct k_queue; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 107 | struct k_fifo; |
| 108 | struct k_lifo; |
| 109 | struct k_stack; |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 110 | struct k_mem_slab; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 111 | struct k_mem_pool; |
| 112 | struct k_timer; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 113 | struct k_poll_event; |
| 114 | struct k_poll_signal; |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 115 | struct k_mem_domain; |
| 116 | struct k_mem_partition; |
Wentong Wu | 5611e92 | 2019-06-20 23:51:27 +0800 | [diff] [blame] | 117 | struct k_futex; |
Andrew Boie | f2734ab | 2020-03-11 06:37:42 -0700 | [diff] [blame] | 118 | struct z_futex_data; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 119 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 120 | /** |
| 121 | * @brief Kernel Object Types |
| 122 | * |
| 123 | * This enumeration needs to be kept in sync with the lists of kernel objects |
Andrew Boie | 5bd891d | 2017-09-27 12:59:28 -0700 | [diff] [blame] | 124 | * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str() |
| 125 | * function in kernel/userspace.c |
| 126 | */ |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 127 | enum k_objects { |
Andrew Boie | 7e3d3d7 | 2017-10-10 09:31:32 -0700 | [diff] [blame] | 128 | K_OBJ_ANY, |
| 129 | |
Leandro Pereira | c200367 | 2018-04-04 13:50:32 -0700 | [diff] [blame] | 130 | /** @cond |
| 131 | * Doxygen should ignore this build-time generated include file |
| 132 | * when genrating API documentation. Enumeration values are |
| 133 | * generated during build by gen_kobject_list.py. It includes |
| 134 | * basic kernel objects (e.g. pipes and mutexes) and driver types. |
| 135 | */ |
| 136 | #include <kobj-types-enum.h> |
| 137 | /** @endcond |
| 138 | */ |
Andrew Boie | 5bd891d | 2017-09-27 12:59:28 -0700 | [diff] [blame] | 139 | |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 140 | K_OBJ_LAST |
| 141 | }; |
Anas Nashif | 4bcb294 | 2019-01-23 23:06:29 -0500 | [diff] [blame] | 142 | /** |
| 143 | * @defgroup usermode_apis User Mode APIs |
| 144 | * @ingroup kernel_apis |
| 145 | * @{ |
| 146 | */ |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 147 | |
| 148 | #ifdef CONFIG_USERSPACE |
Andrew Boie | 28be793 | 2020-03-11 10:56:19 -0700 | [diff] [blame] | 149 | #ifdef CONFIG_GEN_PRIV_STACKS |
| 150 | /* Metadata struct for K_OBJ_THREAD_STACK_ELEMENT */ |
| 151 | struct z_stack_data { |
| 152 | /* Size of the entire stack object, including reserved areas */ |
| 153 | size_t size; |
| 154 | |
| 155 | /* Stack buffer for privilege mode elevations */ |
| 156 | u8_t *priv; |
| 157 | }; |
| 158 | #endif /* CONFIG_GEN_PRIV_STACKS */ |
| 159 | |
Andrew Boie | f2734ab | 2020-03-11 06:37:42 -0700 | [diff] [blame] | 160 | /* Object extra data. Only some objects use this, determined by object type */ |
| 161 | union z_object_data { |
| 162 | /* Backing mutex for K_OBJ_SYS_MUTEX */ |
| 163 | struct k_mutex *mutex; |
| 164 | |
| 165 | /* Numerical thread ID for K_OBJ_THREAD */ |
| 166 | unsigned int thread_id; |
| 167 | |
Andrew Boie | 28be793 | 2020-03-11 10:56:19 -0700 | [diff] [blame] | 168 | #ifdef CONFIG_GEN_PRIV_STACKS |
| 169 | /* Metadata for K_OBJ_THREAD_STACK_ELEMENT */ |
| 170 | struct z_stack_data *stack_data; |
| 171 | #else |
Andrew Boie | 4bad34e | 2020-03-11 06:56:58 -0700 | [diff] [blame] | 172 | /* Stack buffer size for K_OBJ_THREAD_STACK_ELEMENT */ |
Andrew Boie | f2734ab | 2020-03-11 06:37:42 -0700 | [diff] [blame] | 173 | size_t stack_size; |
Andrew Boie | 28be793 | 2020-03-11 10:56:19 -0700 | [diff] [blame] | 174 | #endif /* CONFIG_GEN_PRIV_STACKS */ |
Andrew Boie | f2734ab | 2020-03-11 06:37:42 -0700 | [diff] [blame] | 175 | |
| 176 | /* Futex wait queue and spinlock for K_OBJ_FUTEX */ |
| 177 | struct z_futex_data *futex_data; |
| 178 | |
| 179 | /* All other objects */ |
| 180 | int unused; |
| 181 | }; |
| 182 | |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 183 | /* Table generated by gperf, these objects are retrieved via |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 184 | * z_object_find() */ |
Andrew Boie | 2dc2ecf | 2020-03-11 07:13:07 -0700 | [diff] [blame] | 185 | struct z_object { |
Andrew Boie | 22553a7 | 2019-11-19 18:27:42 -0800 | [diff] [blame] | 186 | void *name; |
Andrew Boie | a811af3 | 2017-10-14 13:50:26 -0700 | [diff] [blame] | 187 | u8_t perms[CONFIG_MAX_THREAD_BYTES]; |
| 188 | u8_t type; |
| 189 | u8_t flags; |
Andrew Boie | f2734ab | 2020-03-11 06:37:42 -0700 | [diff] [blame] | 190 | union z_object_data data; |
Andrew Boie | df55524 | 2018-05-25 07:28:54 -0700 | [diff] [blame] | 191 | } __packed __aligned(4); |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 192 | |
Andrew Boie | 2f3a89f | 2020-03-11 07:00:55 -0700 | [diff] [blame] | 193 | struct z_object_assignment { |
Andrew Boie | 877f82e | 2017-10-17 11:20:22 -0700 | [diff] [blame] | 194 | struct k_thread *thread; |
| 195 | void * const *objects; |
| 196 | }; |
| 197 | |
| 198 | /** |
| 199 | * @brief Grant a static thread access to a list of kernel objects |
| 200 | * |
| 201 | * For threads declared with K_THREAD_DEFINE(), grant the thread access to |
| 202 | * a set of kernel objects. These objects do not need to be in an initialized |
| 203 | * state. The permissions will be granted when the threads are initialized |
| 204 | * in the early boot sequence. |
| 205 | * |
| 206 | * All arguments beyond the first must be pointers to kernel objects. |
| 207 | * |
| 208 | * @param name_ Name of the thread, as passed to K_THREAD_DEFINE() |
| 209 | */ |
| 210 | #define K_THREAD_ACCESS_GRANT(name_, ...) \ |
| 211 | static void * const _CONCAT(_object_list_, name_)[] = \ |
| 212 | { __VA_ARGS__, NULL }; \ |
Andrew Boie | 2f3a89f | 2020-03-11 07:00:55 -0700 | [diff] [blame] | 213 | static const Z_STRUCT_SECTION_ITERABLE(z_object_assignment, \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 214 | _CONCAT(_object_access_, name_)) = \ |
Andrew Boie | 877f82e | 2017-10-17 11:20:22 -0700 | [diff] [blame] | 215 | { (&_k_thread_obj_ ## name_), \ |
| 216 | (_CONCAT(_object_list_, name_)) } |
| 217 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 218 | /** Object initialized */ |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 219 | #define K_OBJ_FLAG_INITIALIZED BIT(0) |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 220 | /** Object is Public */ |
Andrew Boie | 04caa67 | 2017-10-13 13:57:07 -0700 | [diff] [blame] | 221 | #define K_OBJ_FLAG_PUBLIC BIT(1) |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 222 | /** Object allocated */ |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 223 | #define K_OBJ_FLAG_ALLOC BIT(2) |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 224 | /** Driver Object */ |
Andrew Boie | 7875707 | 2019-07-23 13:29:30 -0700 | [diff] [blame] | 225 | #define K_OBJ_FLAG_DRIVER BIT(3) |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 226 | |
| 227 | /** |
| 228 | * Lookup a kernel object and init its metadata if it exists |
| 229 | * |
| 230 | * Calling this on an object will make it usable from userspace. |
| 231 | * Intended to be called as the last statement in kernel object init |
| 232 | * functions. |
| 233 | * |
Anas Nashif | 50e3acd | 2018-12-08 13:26:18 -0500 | [diff] [blame] | 234 | * @param obj Address of the kernel object |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 235 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 236 | void z_object_init(void *obj); |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 237 | #else |
Andrew Boie | c3d4e65 | 2019-06-28 14:19:16 -0700 | [diff] [blame] | 238 | /* LCOV_EXCL_START */ |
Andrew Boie | 877f82e | 2017-10-17 11:20:22 -0700 | [diff] [blame] | 239 | #define K_THREAD_ACCESS_GRANT(thread, ...) |
| 240 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 241 | /** |
| 242 | * @internal |
| 243 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 244 | static inline void z_object_init(void *obj) |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 245 | { |
| 246 | ARG_UNUSED(obj); |
| 247 | } |
| 248 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 249 | /** |
| 250 | * @internal |
| 251 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 252 | static inline void z_impl_k_object_access_grant(void *object, |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 253 | struct k_thread *thread) |
| 254 | { |
| 255 | ARG_UNUSED(object); |
| 256 | ARG_UNUSED(thread); |
| 257 | } |
| 258 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 259 | /** |
| 260 | * @internal |
| 261 | */ |
Andrew Boie | e9cfc54 | 2018-04-13 13:15:28 -0700 | [diff] [blame] | 262 | static inline void k_object_access_revoke(void *object, |
| 263 | struct k_thread *thread) |
Andrew Boie | a89bf01 | 2017-10-09 14:47:55 -0700 | [diff] [blame] | 264 | { |
| 265 | ARG_UNUSED(object); |
| 266 | ARG_UNUSED(thread); |
| 267 | } |
| 268 | |
Andrew Boie | e9cfc54 | 2018-04-13 13:15:28 -0700 | [diff] [blame] | 269 | /** |
| 270 | * @internal |
| 271 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 272 | static inline void z_impl_k_object_release(void *object) |
Andrew Boie | e9cfc54 | 2018-04-13 13:15:28 -0700 | [diff] [blame] | 273 | { |
| 274 | ARG_UNUSED(object); |
| 275 | } |
| 276 | |
Andrew Boie | 41bab6e | 2017-10-14 14:42:23 -0700 | [diff] [blame] | 277 | static inline void k_object_access_all_grant(void *object) |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 278 | { |
| 279 | ARG_UNUSED(object); |
| 280 | } |
Andrew Boie | c3d4e65 | 2019-06-28 14:19:16 -0700 | [diff] [blame] | 281 | /* LCOV_EXCL_STOP */ |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 282 | #endif /* !CONFIG_USERSPACE */ |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 283 | |
| 284 | /** |
Marti Bolivar | 67db616 | 2019-08-27 19:12:51 -0600 | [diff] [blame] | 285 | * Grant a thread access to a kernel object |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 286 | * |
| 287 | * The thread will be granted access to the object if the caller is from |
| 288 | * supervisor mode, or the caller is from user mode AND has permissions |
Andrew Boie | a89bf01 | 2017-10-09 14:47:55 -0700 | [diff] [blame] | 289 | * on both the object and the thread whose access is being granted. |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 290 | * |
| 291 | * @param object Address of kernel object |
| 292 | * @param thread Thread to grant access to the object |
| 293 | */ |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 294 | __syscall void k_object_access_grant(void *object, struct k_thread *thread); |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 295 | |
Andrew Boie | a89bf01 | 2017-10-09 14:47:55 -0700 | [diff] [blame] | 296 | /** |
Marti Bolivar | 67db616 | 2019-08-27 19:12:51 -0600 | [diff] [blame] | 297 | * Revoke a thread's access to a kernel object |
Andrew Boie | a89bf01 | 2017-10-09 14:47:55 -0700 | [diff] [blame] | 298 | * |
| 299 | * The thread will lose access to the object if the caller is from |
| 300 | * supervisor mode, or the caller is from user mode AND has permissions |
| 301 | * on both the object and the thread whose access is being revoked. |
| 302 | * |
| 303 | * @param object Address of kernel object |
| 304 | * @param thread Thread to remove access to the object |
| 305 | */ |
Andrew Boie | e9cfc54 | 2018-04-13 13:15:28 -0700 | [diff] [blame] | 306 | void k_object_access_revoke(void *object, struct k_thread *thread); |
| 307 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 308 | /** |
| 309 | * @brief Release an object |
| 310 | * |
| 311 | * Allows user threads to drop their own permission on an object |
| 312 | * Their permissions are automatically cleared when a thread terminates. |
| 313 | * |
| 314 | * @param object The object to be released |
| 315 | * |
| 316 | */ |
Andrew Boie | e9cfc54 | 2018-04-13 13:15:28 -0700 | [diff] [blame] | 317 | __syscall void k_object_release(void *object); |
Andrew Boie | 3b5ae80 | 2017-10-04 12:10:32 -0700 | [diff] [blame] | 318 | |
| 319 | /** |
Marti Bolivar | 67db616 | 2019-08-27 19:12:51 -0600 | [diff] [blame] | 320 | * Grant all present and future threads access to an object |
Andrew Boie | 3b5ae80 | 2017-10-04 12:10:32 -0700 | [diff] [blame] | 321 | * |
| 322 | * If the caller is from supervisor mode, or the caller is from user mode and |
| 323 | * have sufficient permissions on the object, then that object will have |
| 324 | * permissions granted to it for *all* current and future threads running in |
| 325 | * the system, effectively becoming a public kernel object. |
| 326 | * |
| 327 | * Use of this API should be avoided on systems that are running untrusted code |
| 328 | * as it is possible for such code to derive the addresses of kernel objects |
| 329 | * and perform unwanted operations on them. |
| 330 | * |
Andrew Boie | 04caa67 | 2017-10-13 13:57:07 -0700 | [diff] [blame] | 331 | * It is not possible to revoke permissions on public objects; once public, |
| 332 | * any thread may use it. |
| 333 | * |
Andrew Boie | 3b5ae80 | 2017-10-04 12:10:32 -0700 | [diff] [blame] | 334 | * @param object Address of kernel object |
| 335 | */ |
Andrew Boie | 41bab6e | 2017-10-14 14:42:23 -0700 | [diff] [blame] | 336 | void k_object_access_all_grant(void *object); |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 337 | |
Andrew Boie | 31bdfc0 | 2017-11-08 16:38:03 -0800 | [diff] [blame] | 338 | /** |
| 339 | * Allocate a kernel object of a designated type |
| 340 | * |
| 341 | * This will instantiate at runtime a kernel object of the specified type, |
| 342 | * returning a pointer to it. The object will be returned in an uninitialized |
| 343 | * state, with the calling thread being granted permission on it. The memory |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 344 | * for the object will be allocated out of the calling thread's resource pool. |
Andrew Boie | 31bdfc0 | 2017-11-08 16:38:03 -0800 | [diff] [blame] | 345 | * |
| 346 | * Currently, allocation of thread stacks is not supported. |
| 347 | * |
| 348 | * @param otype Requested kernel object type |
| 349 | * @return A pointer to the allocated kernel object, or NULL if memory wasn't |
| 350 | * available |
| 351 | */ |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 352 | __syscall void *k_object_alloc(enum k_objects otype); |
Andrew Boie | 31bdfc0 | 2017-11-08 16:38:03 -0800 | [diff] [blame] | 353 | |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 354 | #ifdef CONFIG_DYNAMIC_OBJECTS |
Andrew Boie | 31bdfc0 | 2017-11-08 16:38:03 -0800 | [diff] [blame] | 355 | /** |
| 356 | * Free a kernel object previously allocated with k_object_alloc() |
| 357 | * |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 358 | * This will return memory for a kernel object back to resource pool it was |
| 359 | * allocated from. Care must be exercised that the object will not be used |
| 360 | * during or after when this call is made. |
Andrew Boie | 31bdfc0 | 2017-11-08 16:38:03 -0800 | [diff] [blame] | 361 | * |
| 362 | * @param obj Pointer to the kernel object memory address. |
| 363 | */ |
| 364 | void k_object_free(void *obj); |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 365 | #else |
Andrew Boie | c3d4e65 | 2019-06-28 14:19:16 -0700 | [diff] [blame] | 366 | /* LCOV_EXCL_START */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 367 | static inline void *z_impl_k_object_alloc(enum k_objects otype) |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 368 | { |
Kumar Gala | 85699f7 | 2018-05-17 09:26:03 -0500 | [diff] [blame] | 369 | ARG_UNUSED(otype); |
| 370 | |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 371 | return NULL; |
| 372 | } |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 373 | /** |
| 374 | * @brief Free an object |
| 375 | * |
| 376 | * @param obj |
| 377 | */ |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 378 | static inline void k_obj_free(void *obj) |
| 379 | { |
| 380 | ARG_UNUSED(obj); |
| 381 | } |
Andrew Boie | c3d4e65 | 2019-06-28 14:19:16 -0700 | [diff] [blame] | 382 | /* LCOV_EXCL_STOP */ |
Andrew Boie | 31bdfc0 | 2017-11-08 16:38:03 -0800 | [diff] [blame] | 383 | #endif /* CONFIG_DYNAMIC_OBJECTS */ |
| 384 | |
Anas Nashif | 4bcb294 | 2019-01-23 23:06:29 -0500 | [diff] [blame] | 385 | /** @} */ |
| 386 | |
Andrew Boie | bca15da | 2017-10-15 14:17:48 -0700 | [diff] [blame] | 387 | |
Andrew Boie | 1e06ffc | 2017-09-11 09:30:04 -0700 | [diff] [blame] | 388 | /** |
| 389 | * @typedef k_thread_entry_t |
| 390 | * @brief Thread entry point function type. |
| 391 | * |
| 392 | * A thread's entry point function is invoked when the thread starts executing. |
| 393 | * Up to 3 argument values can be passed to the function. |
| 394 | * |
| 395 | * The thread terminates execution permanently if the entry point function |
| 396 | * returns. The thread is responsible for releasing any shared resources |
| 397 | * it may own (such as mutexes and dynamically allocated memory), prior to |
| 398 | * returning. |
| 399 | * |
| 400 | * @param p1 First argument. |
| 401 | * @param p2 Second argument. |
| 402 | * @param p3 Third argument. |
| 403 | * |
| 404 | * @return N/A |
| 405 | */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 406 | |
| 407 | #ifdef CONFIG_THREAD_MONITOR |
| 408 | struct __thread_entry { |
Andrew Boie | 1e06ffc | 2017-09-11 09:30:04 -0700 | [diff] [blame] | 409 | k_thread_entry_t pEntry; |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 410 | void *parameter1; |
| 411 | void *parameter2; |
| 412 | void *parameter3; |
| 413 | }; |
| 414 | #endif |
| 415 | |
| 416 | /* can be used for creating 'dummy' threads, e.g. for pending on objects */ |
| 417 | struct _thread_base { |
| 418 | |
| 419 | /* this thread's entry in a ready/wait queue */ |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 420 | union { |
Peter A. Bigot | 82ad0d2 | 2019-01-03 23:49:28 -0600 | [diff] [blame] | 421 | sys_dnode_t qnode_dlist; |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 422 | struct rbnode qnode_rb; |
| 423 | }; |
| 424 | |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 425 | /* wait queue on which the thread is pended (needed only for |
| 426 | * trees, not dumb lists) |
| 427 | */ |
| 428 | _wait_q_t *pended_on; |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 429 | |
| 430 | /* user facing 'thread options'; values defined in include/kernel.h */ |
| 431 | u8_t user_options; |
| 432 | |
| 433 | /* thread state */ |
| 434 | u8_t thread_state; |
| 435 | |
| 436 | /* |
| 437 | * scheduler lock count and thread priority |
| 438 | * |
| 439 | * These two fields control the preemptibility of a thread. |
| 440 | * |
| 441 | * When the scheduler is locked, sched_locked is decremented, which |
| 442 | * means that the scheduler is locked for values from 0xff to 0x01. A |
| 443 | * thread is coop if its prio is negative, thus 0x80 to 0xff when |
| 444 | * looked at the value as unsigned. |
| 445 | * |
| 446 | * By putting them end-to-end, this means that a thread is |
| 447 | * non-preemptible if the bundled value is greater than or equal to |
| 448 | * 0x0080. |
| 449 | */ |
| 450 | union { |
| 451 | struct { |
| 452 | #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 453 | u8_t sched_locked; |
| 454 | s8_t prio; |
| 455 | #else /* LITTLE and PDP */ |
| 456 | s8_t prio; |
| 457 | u8_t sched_locked; |
| 458 | #endif |
| 459 | }; |
| 460 | u16_t preempt; |
| 461 | }; |
| 462 | |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 463 | #ifdef CONFIG_SCHED_DEADLINE |
| 464 | int prio_deadline; |
| 465 | #endif |
| 466 | |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 467 | u32_t order_key; |
| 468 | |
Andy Ross | 2724fd1 | 2018-01-29 14:55:20 -0800 | [diff] [blame] | 469 | #ifdef CONFIG_SMP |
| 470 | /* True for the per-CPU idle threads */ |
| 471 | u8_t is_idle; |
| 472 | |
Andy Ross | 2724fd1 | 2018-01-29 14:55:20 -0800 | [diff] [blame] | 473 | /* CPU index on which thread was last run */ |
| 474 | u8_t cpu; |
Andy Ross | 15c4007 | 2018-04-12 12:50:05 -0700 | [diff] [blame] | 475 | |
| 476 | /* Recursive count of irq_lock() calls */ |
| 477 | u8_t global_lock_count; |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 478 | |
| 479 | #endif |
| 480 | |
| 481 | #ifdef CONFIG_SCHED_CPU_MASK |
| 482 | /* "May run on" bits for each CPU */ |
| 483 | u8_t cpu_mask; |
Andy Ross | 2724fd1 | 2018-01-29 14:55:20 -0800 | [diff] [blame] | 484 | #endif |
| 485 | |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 486 | /* data returned by APIs */ |
| 487 | void *swap_data; |
| 488 | |
| 489 | #ifdef CONFIG_SYS_CLOCK_EXISTS |
| 490 | /* this thread's entry in a timeout queue */ |
| 491 | struct _timeout timeout; |
| 492 | #endif |
Andrew Boie | 322816e | 2020-02-20 16:33:06 -0800 | [diff] [blame] | 493 | |
| 494 | _wait_q_t join_waiters; |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 495 | }; |
| 496 | |
| 497 | typedef struct _thread_base _thread_base_t; |
| 498 | |
| 499 | #if defined(CONFIG_THREAD_STACK_INFO) |
| 500 | /* Contains the stack information of a thread */ |
| 501 | struct _thread_stack_info { |
Andrew Boie | 4e5c093 | 2019-04-04 12:05:28 -0700 | [diff] [blame] | 502 | /* Stack start - Represents the start address of the thread-writable |
| 503 | * stack area. |
Andrew Boie | b85ac3e | 2018-06-01 12:15:13 -0700 | [diff] [blame] | 504 | */ |
Nicolas Pitre | 58d839b | 2019-05-21 11:32:21 -0400 | [diff] [blame] | 505 | uintptr_t start; |
Andrew Boie | b85ac3e | 2018-06-01 12:15:13 -0700 | [diff] [blame] | 506 | |
| 507 | /* Stack Size - Thread writable stack buffer size. Represents |
| 508 | * the size of the actual area, starting from the start member, |
| 509 | * that should be writable by the thread |
| 510 | */ |
Andrew Boie | 528233e | 2019-12-11 14:54:15 -0800 | [diff] [blame] | 511 | size_t size; |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 512 | }; |
Andrew Boie | 41c68ec | 2017-05-11 15:38:20 -0700 | [diff] [blame] | 513 | |
| 514 | typedef struct _thread_stack_info _thread_stack_info_t; |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 515 | #endif /* CONFIG_THREAD_STACK_INFO */ |
| 516 | |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 517 | #if defined(CONFIG_USERSPACE) |
| 518 | struct _mem_domain_info { |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 519 | /** memory domain queue node */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 520 | sys_dnode_t mem_domain_q_node; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 521 | /** memory domain of the thread */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 522 | struct k_mem_domain *mem_domain; |
| 523 | }; |
| 524 | |
| 525 | #endif /* CONFIG_USERSPACE */ |
| 526 | |
Daniel Leung | fc18243 | 2018-08-16 15:42:28 -0700 | [diff] [blame] | 527 | #ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA |
| 528 | struct _thread_userspace_local_data { |
| 529 | int errno_var; |
| 530 | }; |
| 531 | #endif |
| 532 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 533 | /** |
| 534 | * @ingroup thread_apis |
| 535 | * Thread Structure |
| 536 | */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 537 | struct k_thread { |
| 538 | |
| 539 | struct _thread_base base; |
| 540 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 541 | /** defined by the architecture, but all archs need these */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 542 | struct _callee_saved callee_saved; |
| 543 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 544 | /** static thread init data */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 545 | void *init_data; |
| 546 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 547 | /** |
| 548 | * abort function |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 549 | * */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 550 | void (*fn_abort)(void); |
| 551 | |
| 552 | #if defined(CONFIG_THREAD_MONITOR) |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 553 | /** thread entry and parameters description */ |
Andrew Boie | 2dd91ec | 2018-06-06 08:45:01 -0700 | [diff] [blame] | 554 | struct __thread_entry entry; |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 555 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 556 | /** next item in list of all threads */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 557 | struct k_thread *next_thread; |
| 558 | #endif |
| 559 | |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 560 | #if defined(CONFIG_THREAD_NAME) |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 561 | /** Thread name */ |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 562 | char name[CONFIG_THREAD_MAX_NAME_LEN]; |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 563 | #endif |
| 564 | |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 565 | #ifdef CONFIG_THREAD_CUSTOM_DATA |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 566 | /** crude thread-local storage */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 567 | void *custom_data; |
| 568 | #endif |
| 569 | |
Daniel Leung | fc18243 | 2018-08-16 15:42:28 -0700 | [diff] [blame] | 570 | #ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA |
| 571 | struct _thread_userspace_local_data *userspace_local_data; |
| 572 | #endif |
| 573 | |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 574 | #ifdef CONFIG_ERRNO |
Daniel Leung | fc18243 | 2018-08-16 15:42:28 -0700 | [diff] [blame] | 575 | #ifndef CONFIG_USERSPACE |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 576 | /** per-thread errno variable */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 577 | int errno_var; |
| 578 | #endif |
Andrew Boie | 7f4d006 | 2018-07-19 11:09:33 -0700 | [diff] [blame] | 579 | #endif |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 580 | |
| 581 | #if defined(CONFIG_THREAD_STACK_INFO) |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 582 | /** Stack Info */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 583 | struct _thread_stack_info stack_info; |
| 584 | #endif /* CONFIG_THREAD_STACK_INFO */ |
| 585 | |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 586 | #if defined(CONFIG_USERSPACE) |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 587 | /** memory domain info of the thread */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 588 | struct _mem_domain_info mem_domain_info; |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 589 | /** Base address of thread stack */ |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 590 | k_thread_stack_t *stack_obj; |
Andy Ross | 7353c7f | 2020-02-06 13:39:03 -0800 | [diff] [blame] | 591 | /** current syscall frame pointer */ |
| 592 | void *syscall_frame; |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 593 | #endif /* CONFIG_USERSPACE */ |
| 594 | |
Andy Ross | 7353c7f | 2020-02-06 13:39:03 -0800 | [diff] [blame] | 595 | |
Andy Ross | 042d8ec | 2017-12-09 08:37:20 -0800 | [diff] [blame] | 596 | #if defined(CONFIG_USE_SWITCH) |
| 597 | /* When using __switch() a few previously arch-specific items |
| 598 | * become part of the core OS |
| 599 | */ |
| 600 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 601 | /** z_swap() return value */ |
Andy Ross | 042d8ec | 2017-12-09 08:37:20 -0800 | [diff] [blame] | 602 | int swap_retval; |
| 603 | |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 604 | /** Context handle returned via arch_switch() */ |
Andy Ross | 042d8ec | 2017-12-09 08:37:20 -0800 | [diff] [blame] | 605 | void *switch_handle; |
| 606 | #endif |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 607 | /** resource pool */ |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 608 | struct k_mem_pool *resource_pool; |
Andy Ross | 042d8ec | 2017-12-09 08:37:20 -0800 | [diff] [blame] | 609 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 610 | /** arch-specifics: must always be at the end */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 611 | struct _thread_arch arch; |
| 612 | }; |
| 613 | |
| 614 | typedef struct k_thread _thread_t; |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 615 | typedef struct k_thread *k_tid_t; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 616 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 617 | enum execution_context_types { |
| 618 | K_ISR = 0, |
| 619 | K_COOP_THREAD, |
| 620 | K_PREEMPT_THREAD, |
| 621 | }; |
| 622 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 623 | /** |
Anas Nashif | 4bcb294 | 2019-01-23 23:06:29 -0500 | [diff] [blame] | 624 | * @addtogroup thread_apis |
Carles Cufi | cb0cf9f | 2017-01-10 10:57:38 +0100 | [diff] [blame] | 625 | * @{ |
| 626 | */ |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 627 | |
Ramakrishna Pallala | 110b8e4 | 2018-04-27 12:55:43 +0530 | [diff] [blame] | 628 | typedef void (*k_thread_user_cb_t)(const struct k_thread *thread, |
| 629 | void *user_data); |
Carles Cufi | cb0cf9f | 2017-01-10 10:57:38 +0100 | [diff] [blame] | 630 | |
| 631 | /** |
Ramakrishna Pallala | 110b8e4 | 2018-04-27 12:55:43 +0530 | [diff] [blame] | 632 | * @brief Iterate over all the threads in the system. |
| 633 | * |
| 634 | * This routine iterates over all the threads in the system and |
| 635 | * calls the user_cb function for each thread. |
| 636 | * |
| 637 | * @param user_cb Pointer to the user callback function. |
| 638 | * @param user_data Pointer to user data. |
| 639 | * |
| 640 | * @note CONFIG_THREAD_MONITOR must be set for this function |
Radoslaw Koppel | 2c529ce | 2019-11-27 14:20:37 +0100 | [diff] [blame] | 641 | * to be effective. |
| 642 | * @note This API uses @ref k_spin_lock to protect the _kernel.threads |
| 643 | * list which means creation of new threads and terminations of existing |
| 644 | * threads are blocked until this API returns. |
Ramakrishna Pallala | 110b8e4 | 2018-04-27 12:55:43 +0530 | [diff] [blame] | 645 | * |
| 646 | * @return N/A |
| 647 | */ |
| 648 | extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data); |
| 649 | |
Radoslaw Koppel | 2c529ce | 2019-11-27 14:20:37 +0100 | [diff] [blame] | 650 | /** |
| 651 | * @brief Iterate over all the threads in the system without locking. |
| 652 | * |
| 653 | * This routine works exactly the same like @ref k_thread_foreach |
| 654 | * but unlocks interrupts when user_cb is executed. |
| 655 | * |
| 656 | * @param user_cb Pointer to the user callback function. |
| 657 | * @param user_data Pointer to user data. |
| 658 | * |
| 659 | * @note CONFIG_THREAD_MONITOR must be set for this function |
| 660 | * to be effective. |
| 661 | * @note This API uses @ref k_spin_lock only when accessing the _kernel.threads |
| 662 | * queue elements. It unlocks it during user callback function processing. |
| 663 | * If a new task is created when this @c foreach function is in progress, |
| 664 | * the added new task would not be included in the enumeration. |
| 665 | * If a task is aborted during this enumeration, there would be a race here |
| 666 | * and there is a possibility that this aborted task would be included in the |
| 667 | * enumeration. |
| 668 | * @note If the task is aborted and the memory occupied by its @c k_thread |
| 669 | * structure is reused when this @c k_thread_foreach_unlocked is in progress |
| 670 | * it might even lead to the system behave unstable. |
| 671 | * This function may never return, as it would follow some @c next task |
| 672 | * pointers treating given pointer as a pointer to the k_thread structure |
| 673 | * while it is something different right now. |
| 674 | * Do not reuse the memory that was occupied by k_thread structure of aborted |
| 675 | * task if it was aborted after this function was called in any context. |
| 676 | */ |
| 677 | extern void k_thread_foreach_unlocked( |
| 678 | k_thread_user_cb_t user_cb, void *user_data); |
| 679 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 680 | /** @} */ |
Carles Cufi | cb0cf9f | 2017-01-10 10:57:38 +0100 | [diff] [blame] | 681 | |
| 682 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 683 | * @defgroup thread_apis Thread APIs |
| 684 | * @ingroup kernel_apis |
| 685 | * @{ |
| 686 | */ |
| 687 | |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 688 | #endif /* !_ASMLANGUAGE */ |
| 689 | |
| 690 | |
| 691 | /* |
| 692 | * Thread user options. May be needed by assembly code. Common part uses low |
| 693 | * bits, arch-specific use high bits. |
| 694 | */ |
| 695 | |
Anas Nashif | a541e93 | 2018-05-24 11:19:16 -0500 | [diff] [blame] | 696 | /** |
| 697 | * @brief system thread that must not abort |
Anas Nashif | a541e93 | 2018-05-24 11:19:16 -0500 | [diff] [blame] | 698 | * */ |
Flavio Ceolin | 8aec087 | 2018-08-15 11:52:00 -0700 | [diff] [blame] | 699 | #define K_ESSENTIAL (BIT(0)) |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 700 | |
Stephanos Ioannidis | aaf9320 | 2020-05-03 18:03:19 +0900 | [diff] [blame^] | 701 | #if defined(CONFIG_FPU_SHARING) |
Anas Nashif | a541e93 | 2018-05-24 11:19:16 -0500 | [diff] [blame] | 702 | /** |
| 703 | * @brief thread uses floating point registers |
| 704 | */ |
Flavio Ceolin | 8aec087 | 2018-08-15 11:52:00 -0700 | [diff] [blame] | 705 | #define K_FP_REGS (BIT(1)) |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 706 | #endif |
| 707 | |
Anas Nashif | a541e93 | 2018-05-24 11:19:16 -0500 | [diff] [blame] | 708 | /** |
| 709 | * @brief user mode thread |
| 710 | * |
| 711 | * This thread has dropped from supervisor mode to user mode and consequently |
Andrew Boie | 5cfa5dc | 2017-08-30 14:17:44 -0700 | [diff] [blame] | 712 | * has additional restrictions |
| 713 | */ |
Flavio Ceolin | 8aec087 | 2018-08-15 11:52:00 -0700 | [diff] [blame] | 714 | #define K_USER (BIT(2)) |
Andrew Boie | 5cfa5dc | 2017-08-30 14:17:44 -0700 | [diff] [blame] | 715 | |
Anas Nashif | a541e93 | 2018-05-24 11:19:16 -0500 | [diff] [blame] | 716 | /** |
| 717 | * @brief Inherit Permissions |
| 718 | * |
| 719 | * @details |
| 720 | * Indicates that the thread being created should inherit all kernel object |
Andrew Boie | 47f8fd1 | 2017-10-05 11:11:02 -0700 | [diff] [blame] | 721 | * permissions from the thread that created it. No effect if CONFIG_USERSPACE |
| 722 | * is not enabled. |
| 723 | */ |
Flavio Ceolin | 8aec087 | 2018-08-15 11:52:00 -0700 | [diff] [blame] | 724 | #define K_INHERIT_PERMS (BIT(3)) |
Andrew Boie | 47f8fd1 | 2017-10-05 11:11:02 -0700 | [diff] [blame] | 725 | |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 726 | #ifdef CONFIG_X86 |
| 727 | /* x86 Bitmask definitions for threads user options */ |
| 728 | |
Stephanos Ioannidis | aaf9320 | 2020-05-03 18:03:19 +0900 | [diff] [blame^] | 729 | #if defined(CONFIG_FPU_SHARING) && defined(CONFIG_SSE) |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 730 | /* thread uses SSEx (and also FP) registers */ |
Flavio Ceolin | 8aec087 | 2018-08-15 11:52:00 -0700 | [diff] [blame] | 731 | #define K_SSE_REGS (BIT(7)) |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 732 | #endif |
| 733 | #endif |
| 734 | |
| 735 | /* end - thread options */ |
| 736 | |
| 737 | #if !defined(_ASMLANGUAGE) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 738 | /** |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 739 | * @brief Create a thread. |
| 740 | * |
| 741 | * This routine initializes a thread, then schedules it for execution. |
| 742 | * |
| 743 | * The new thread may be scheduled for immediate execution or a delayed start. |
| 744 | * If the newly spawned thread does not have a delayed start the kernel |
| 745 | * scheduler may preempt the current thread to allow the new thread to |
| 746 | * execute. |
| 747 | * |
| 748 | * Thread options are architecture-specific, and can include K_ESSENTIAL, |
| 749 | * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating |
| 750 | * them using "|" (the logical OR operator). |
| 751 | * |
| 752 | * Historically, users often would use the beginning of the stack memory region |
| 753 | * to store the struct k_thread data, although corruption will occur if the |
| 754 | * stack overflows this region and stack protection features may not detect this |
| 755 | * situation. |
| 756 | * |
| 757 | * @param new_thread Pointer to uninitialized struct k_thread |
| 758 | * @param stack Pointer to the stack space. |
| 759 | * @param stack_size Stack size in bytes. |
| 760 | * @param entry Thread entry function. |
| 761 | * @param p1 1st entry point parameter. |
| 762 | * @param p2 2nd entry point parameter. |
| 763 | * @param p3 3rd entry point parameter. |
| 764 | * @param prio Thread priority. |
| 765 | * @param options Thread options. |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 766 | * @param delay Scheduling delay, or K_NO_WAIT (for no delay). |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 767 | * |
| 768 | * @return ID of new thread. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 769 | * |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 770 | */ |
Andrew Boie | 662c345 | 2017-10-02 10:51:18 -0700 | [diff] [blame] | 771 | __syscall k_tid_t k_thread_create(struct k_thread *new_thread, |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 772 | k_thread_stack_t *stack, |
Andrew Boie | 662c345 | 2017-10-02 10:51:18 -0700 | [diff] [blame] | 773 | size_t stack_size, |
| 774 | k_thread_entry_t entry, |
| 775 | void *p1, void *p2, void *p3, |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 776 | int prio, u32_t options, k_timeout_t delay); |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 777 | |
Andrew Boie | 3f091b5 | 2017-08-30 14:34:14 -0700 | [diff] [blame] | 778 | /** |
| 779 | * @brief Drop a thread's privileges permanently to user mode |
| 780 | * |
| 781 | * @param entry Function to start executing from |
| 782 | * @param p1 1st entry point parameter |
| 783 | * @param p2 2nd entry point parameter |
| 784 | * @param p3 3rd entry point parameter |
| 785 | */ |
| 786 | extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry, |
| 787 | void *p1, void *p2, |
| 788 | void *p3); |
Andrew Boie | 3f091b5 | 2017-08-30 14:34:14 -0700 | [diff] [blame] | 789 | |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 790 | /** |
Adithya Baglody | 392219e | 2019-01-02 14:40:39 +0530 | [diff] [blame] | 791 | * @brief Grant a thread access to a set of kernel objects |
Andrew Boie | e12857a | 2017-10-17 11:38:26 -0700 | [diff] [blame] | 792 | * |
| 793 | * This is a convenience function. For the provided thread, grant access to |
| 794 | * the remaining arguments, which must be pointers to kernel objects. |
Andrew Boie | e12857a | 2017-10-17 11:38:26 -0700 | [diff] [blame] | 795 | * |
| 796 | * The thread object must be initialized (i.e. running). The objects don't |
| 797 | * need to be. |
Adithya Baglody | 392219e | 2019-01-02 14:40:39 +0530 | [diff] [blame] | 798 | * Note that NULL shouldn't be passed as an argument. |
Andrew Boie | e12857a | 2017-10-17 11:38:26 -0700 | [diff] [blame] | 799 | * |
| 800 | * @param thread Thread to grant access to objects |
Adithya Baglody | 392219e | 2019-01-02 14:40:39 +0530 | [diff] [blame] | 801 | * @param ... list of kernel object pointers |
Andrew Boie | e12857a | 2017-10-17 11:38:26 -0700 | [diff] [blame] | 802 | */ |
Adithya Baglody | 392219e | 2019-01-02 14:40:39 +0530 | [diff] [blame] | 803 | #define k_thread_access_grant(thread, ...) \ |
| 804 | FOR_EACH_FIXED_ARG(k_object_access_grant, thread, __VA_ARGS__) |
Andrew Boie | e12857a | 2017-10-17 11:38:26 -0700 | [diff] [blame] | 805 | |
| 806 | /** |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 807 | * @brief Assign a resource memory pool to a thread |
| 808 | * |
| 809 | * By default, threads have no resource pool assigned unless their parent |
| 810 | * thread has a resource pool, in which case it is inherited. Multiple |
| 811 | * threads may be assigned to the same memory pool. |
| 812 | * |
| 813 | * Changing a thread's resource pool will not migrate allocations from the |
| 814 | * previous pool. |
| 815 | * |
Jukka Rissanen | fdf1848 | 2020-05-01 12:37:51 +0300 | [diff] [blame] | 816 | * @param thread Target thread to assign a memory pool for resource requests. |
| 817 | * @param pool Memory pool to use for resources, |
| 818 | * or NULL if the thread should no longer have a memory pool. |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 819 | */ |
| 820 | static inline void k_thread_resource_pool_assign(struct k_thread *thread, |
| 821 | struct k_mem_pool *pool) |
| 822 | { |
| 823 | thread->resource_pool = pool; |
| 824 | } |
| 825 | |
Andrew Boie | efc5fe0 | 2020-02-05 10:41:58 -0800 | [diff] [blame] | 826 | #if defined(CONFIG_INIT_STACKS) && defined(CONFIG_THREAD_STACK_INFO) |
| 827 | /** |
| 828 | * @brief Obtain stack usage information for the specified thread |
| 829 | * |
| 830 | * User threads will need to have permission on the target thread object. |
| 831 | * |
| 832 | * Some hardware may prevent inspection of a stack buffer currently in use. |
| 833 | * If this API is called from supervisor mode, on the currently running thread, |
| 834 | * on a platform which selects CONFIG_NO_UNUSED_STACK_INSPECTION, an error |
| 835 | * will be generated. |
| 836 | * |
| 837 | * @param thread Thread to inspect stack information |
| 838 | * @param unused_ptr Output parameter, filled in with the unused stack space |
| 839 | * of the target thread in bytes. |
| 840 | * @return 0 on success |
| 841 | * @return -EBADF Bad thread object (user mode only) |
| 842 | * @return -EPERM No permissions on thread object (user mode only) |
| 843 | * #return -ENOTSUP Forbidden by hardware policy |
| 844 | * @return -EINVAL Thread is uninitialized or exited (user mode only) |
| 845 | * @return -EFAULT Bad memory address for unused_ptr (user mode only) |
| 846 | */ |
| 847 | __syscall int k_thread_stack_space_get(const struct k_thread *thread, |
| 848 | size_t *unused_ptr); |
| 849 | #endif |
| 850 | |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 851 | #if (CONFIG_HEAP_MEM_POOL_SIZE > 0) |
| 852 | /** |
| 853 | * @brief Assign the system heap as a thread's resource pool |
| 854 | * |
| 855 | * Similar to k_thread_resource_pool_assign(), but the thread will use |
| 856 | * the kernel heap to draw memory. |
| 857 | * |
| 858 | * Use with caution, as a malicious thread could perform DoS attacks on the |
| 859 | * kernel heap. |
| 860 | * |
| 861 | * @param thread Target thread to assign the system heap for resource requests |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 862 | * |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 863 | */ |
| 864 | void k_thread_system_pool_assign(struct k_thread *thread); |
| 865 | #endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */ |
| 866 | |
| 867 | /** |
Andrew Boie | 322816e | 2020-02-20 16:33:06 -0800 | [diff] [blame] | 868 | * @brief Sleep until a thread exits |
| 869 | * |
| 870 | * The caller will be put to sleep until the target thread exits, either due |
| 871 | * to being aborted, self-exiting, or taking a fatal error. This API returns |
| 872 | * immediately if the thread isn't running. |
| 873 | * |
| 874 | * This API may only be called from ISRs with a K_NO_WAIT timeout. |
| 875 | * |
| 876 | * @param thread Thread to wait to exit |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 877 | * @param timeout upper bound time to wait for the thread to exit. |
Andrew Boie | 322816e | 2020-02-20 16:33:06 -0800 | [diff] [blame] | 878 | * @retval 0 success, target thread has exited or wasn't running |
| 879 | * @retval -EBUSY returned without waiting |
| 880 | * @retval -EAGAIN waiting period timed out |
| 881 | * @retval -EDEADLK target thread is joining on the caller, or target thread |
| 882 | * is the caller |
| 883 | */ |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 884 | __syscall int k_thread_join(struct k_thread *thread, k_timeout_t timeout); |
| 885 | |
| 886 | /** |
| 887 | * @brief Put the current thread to sleep. |
| 888 | * |
| 889 | * This routine puts the current thread to sleep for @a duration, |
| 890 | * specified as a k_timeout_t object. |
| 891 | * |
| 892 | * @param timeout Desired duration of sleep. |
| 893 | * |
| 894 | * @return Zero if the requested time has elapsed or the number of milliseconds |
| 895 | * left to sleep, if thread was woken up by \ref k_wakeup call. |
| 896 | */ |
| 897 | __syscall s32_t k_sleep(k_timeout_t timeout); |
Andrew Boie | 322816e | 2020-02-20 16:33:06 -0800 | [diff] [blame] | 898 | |
| 899 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 900 | * @brief Put the current thread to sleep. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 901 | * |
Charles E. Youse | a567831 | 2019-05-09 16:46:46 -0700 | [diff] [blame] | 902 | * This routine puts the current thread to sleep for @a duration milliseconds. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 903 | * |
Charles E. Youse | a567831 | 2019-05-09 16:46:46 -0700 | [diff] [blame] | 904 | * @param ms Number of milliseconds to sleep. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 905 | * |
Piotr Zięcik | 7700eb2 | 2018-10-25 17:45:08 +0200 | [diff] [blame] | 906 | * @return Zero if the requested time has elapsed or the number of milliseconds |
| 907 | * left to sleep, if thread was woken up by \ref k_wakeup call. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 908 | */ |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 909 | static inline s32_t k_msleep(s32_t ms) |
| 910 | { |
| 911 | return k_sleep(Z_TIMEOUT_MS(ms)); |
| 912 | } |
Charles E. Youse | a567831 | 2019-05-09 16:46:46 -0700 | [diff] [blame] | 913 | |
| 914 | /** |
| 915 | * @brief Put the current thread to sleep with microsecond resolution. |
| 916 | * |
| 917 | * This function is unlikely to work as expected without kernel tuning. |
| 918 | * In particular, because the lower bound on the duration of a sleep is |
| 919 | * the duration of a tick, CONFIG_SYS_CLOCK_TICKS_PER_SEC must be adjusted |
| 920 | * to achieve the resolution desired. The implications of doing this must |
| 921 | * be understood before attempting to use k_usleep(). Use with caution. |
| 922 | * |
| 923 | * @param us Number of microseconds to sleep. |
| 924 | * |
| 925 | * @return Zero if the requested time has elapsed or the number of microseconds |
| 926 | * left to sleep, if thread was woken up by \ref k_wakeup call. |
| 927 | */ |
| 928 | __syscall s32_t k_usleep(s32_t us); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 929 | |
| 930 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 931 | * @brief Cause the current thread to busy wait. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 932 | * |
| 933 | * This routine causes the current thread to execute a "do nothing" loop for |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 934 | * @a usec_to_wait microseconds. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 935 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 936 | * @return N/A |
| 937 | */ |
Andrew Boie | 42cfd4f | 2018-11-14 14:29:24 -0800 | [diff] [blame] | 938 | __syscall void k_busy_wait(u32_t usec_to_wait); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 939 | |
| 940 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 941 | * @brief Yield the current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 942 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 943 | * This routine causes the current thread to yield execution to another |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 944 | * thread of the same or higher priority. If there are no other ready threads |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 945 | * of the same or higher priority, the routine returns immediately. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 946 | * |
| 947 | * @return N/A |
| 948 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 949 | __syscall void k_yield(void); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 950 | |
| 951 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 952 | * @brief Wake up a sleeping thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 953 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 954 | * This routine prematurely wakes up @a thread from sleeping. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 955 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 956 | * If @a thread is not currently sleeping, the routine has no effect. |
| 957 | * |
| 958 | * @param thread ID of thread to wake. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 959 | * |
| 960 | * @return N/A |
| 961 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 962 | __syscall void k_wakeup(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 963 | |
| 964 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 965 | * @brief Get thread ID of the current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 966 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 967 | * @return ID of current thread. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 968 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 969 | */ |
Andrew Boie | 76c04a2 | 2017-09-27 14:45:10 -0700 | [diff] [blame] | 970 | __syscall k_tid_t k_current_get(void); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 971 | |
| 972 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 973 | * @brief Abort a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 974 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 975 | * This routine permanently stops execution of @a thread. The thread is taken |
| 976 | * off all kernel queues it is part of (i.e. the ready queue, the timeout |
| 977 | * queue, or a kernel object wait queue). However, any kernel resources the |
| 978 | * thread might currently own (such as mutexes or memory blocks) are not |
| 979 | * released. It is the responsibility of the caller of this routine to ensure |
| 980 | * all necessary cleanup is performed. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 981 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 982 | * @param thread ID of thread to abort. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 983 | * |
| 984 | * @return N/A |
| 985 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 986 | __syscall void k_thread_abort(k_tid_t thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 987 | |
Andrew Boie | 7d627c5 | 2017-08-30 11:01:56 -0700 | [diff] [blame] | 988 | |
| 989 | /** |
| 990 | * @brief Start an inactive thread |
| 991 | * |
| 992 | * If a thread was created with K_FOREVER in the delay parameter, it will |
| 993 | * not be added to the scheduling queue until this function is called |
| 994 | * on it. |
| 995 | * |
| 996 | * @param thread thread to start |
| 997 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 998 | __syscall void k_thread_start(k_tid_t thread); |
Andrew Boie | 7d627c5 | 2017-08-30 11:01:56 -0700 | [diff] [blame] | 999 | |
Andy Ross | 5a5d3da | 2020-03-09 13:59:15 -0700 | [diff] [blame] | 1000 | extern k_ticks_t z_timeout_expires(struct _timeout *timeout); |
| 1001 | extern k_ticks_t z_timeout_remaining(struct _timeout *timeout); |
| 1002 | |
| 1003 | #ifdef CONFIG_SYS_CLOCK_EXISTS |
| 1004 | |
| 1005 | /** |
Andy Ross | e39bf29 | 2020-03-19 10:30:33 -0700 | [diff] [blame] | 1006 | * @brief Get time when a thread wakes up, in system ticks |
Andy Ross | 5a5d3da | 2020-03-09 13:59:15 -0700 | [diff] [blame] | 1007 | * |
| 1008 | * This routine computes the system uptime when a waiting thread next |
| 1009 | * executes, in units of system ticks. If the thread is not waiting, |
| 1010 | * it returns current system time. |
| 1011 | */ |
| 1012 | __syscall k_ticks_t k_thread_timeout_expires_ticks(struct k_thread *t); |
| 1013 | |
| 1014 | static inline k_ticks_t z_impl_k_thread_timeout_expires_ticks( |
| 1015 | struct k_thread *t) |
| 1016 | { |
| 1017 | return z_timeout_expires(&t->base.timeout); |
| 1018 | } |
| 1019 | |
| 1020 | /** |
Andy Ross | e39bf29 | 2020-03-19 10:30:33 -0700 | [diff] [blame] | 1021 | * @brief Get time remaining before a thread wakes up, in system ticks |
Andy Ross | 5a5d3da | 2020-03-09 13:59:15 -0700 | [diff] [blame] | 1022 | * |
| 1023 | * This routine computes the time remaining before a waiting thread |
| 1024 | * next executes, in units of system ticks. If the thread is not |
| 1025 | * waiting, it returns zero. |
| 1026 | */ |
| 1027 | __syscall k_ticks_t k_thread_timeout_remaining_ticks(struct k_thread *t); |
| 1028 | |
| 1029 | static inline k_ticks_t z_impl_k_thread_timeout_remaining_ticks( |
| 1030 | struct k_thread *t) |
| 1031 | { |
| 1032 | return z_timeout_remaining(&t->base.timeout); |
| 1033 | } |
| 1034 | |
| 1035 | #endif /* CONFIG_SYS_CLOCK_EXISTS */ |
| 1036 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1037 | /** |
| 1038 | * @cond INTERNAL_HIDDEN |
| 1039 | */ |
| 1040 | |
Benjamin Walsh | d211a52 | 2016-12-06 11:44:01 -0500 | [diff] [blame] | 1041 | /* timeout has timed out and is not on _timeout_q anymore */ |
| 1042 | #define _EXPIRED (-2) |
| 1043 | |
Peter Mitsis | a04c0d7 | 2016-09-28 19:26:00 -0400 | [diff] [blame] | 1044 | struct _static_thread_data { |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 1045 | struct k_thread *init_thread; |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 1046 | k_thread_stack_t *init_stack; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1047 | unsigned int init_stack_size; |
Andrew Boie | 1e06ffc | 2017-09-11 09:30:04 -0700 | [diff] [blame] | 1048 | k_thread_entry_t init_entry; |
Allan Stephens | 7c5bffa | 2016-10-26 10:01:28 -0500 | [diff] [blame] | 1049 | void *init_p1; |
| 1050 | void *init_p2; |
| 1051 | void *init_p3; |
| 1052 | int init_prio; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 1053 | u32_t init_options; |
| 1054 | s32_t init_delay; |
Allan Stephens | 7c5bffa | 2016-10-26 10:01:28 -0500 | [diff] [blame] | 1055 | void (*init_abort)(void); |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 1056 | const char *init_name; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1057 | }; |
| 1058 | |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 1059 | #define Z_THREAD_INITIALIZER(thread, stack, stack_size, \ |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 1060 | entry, p1, p2, p3, \ |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 1061 | prio, options, delay, abort, tname) \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 1062 | { \ |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 1063 | .init_thread = (thread), \ |
| 1064 | .init_stack = (stack), \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 1065 | .init_stack_size = (stack_size), \ |
Andrew Boie | 1e06ffc | 2017-09-11 09:30:04 -0700 | [diff] [blame] | 1066 | .init_entry = (k_thread_entry_t)entry, \ |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 1067 | .init_p1 = (void *)p1, \ |
| 1068 | .init_p2 = (void *)p2, \ |
| 1069 | .init_p3 = (void *)p3, \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 1070 | .init_prio = (prio), \ |
| 1071 | .init_options = (options), \ |
| 1072 | .init_delay = (delay), \ |
| 1073 | .init_abort = (abort), \ |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 1074 | .init_name = STRINGIFY(tname), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1075 | } |
| 1076 | |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 1077 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1078 | * INTERNAL_HIDDEN @endcond |
| 1079 | */ |
| 1080 | |
| 1081 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1082 | * @brief Statically define and initialize a thread. |
| 1083 | * |
| 1084 | * The thread may be scheduled for immediate execution or a delayed start. |
| 1085 | * |
| 1086 | * Thread options are architecture-specific, and can include K_ESSENTIAL, |
| 1087 | * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating |
| 1088 | * them using "|" (the logical OR operator). |
| 1089 | * |
| 1090 | * The ID of the thread can be accessed using: |
| 1091 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 1092 | * @code extern const k_tid_t <name>; @endcode |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1093 | * |
| 1094 | * @param name Name of the thread. |
| 1095 | * @param stack_size Stack size in bytes. |
| 1096 | * @param entry Thread entry function. |
| 1097 | * @param p1 1st entry point parameter. |
| 1098 | * @param p2 2nd entry point parameter. |
| 1099 | * @param p3 3rd entry point parameter. |
| 1100 | * @param prio Thread priority. |
| 1101 | * @param options Thread options. |
Peter Bigot | 73c387c | 2020-04-20 08:55:20 -0500 | [diff] [blame] | 1102 | * @param delay Scheduling delay (in milliseconds), zero for no delay. |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 1103 | * |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1104 | * |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 1105 | * @internal It has been observed that the x86 compiler by default aligns |
| 1106 | * these _static_thread_data structures to 32-byte boundaries, thereby |
| 1107 | * wasting space. To work around this, force a 4-byte alignment. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1108 | * |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 1109 | */ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 1110 | #define K_THREAD_DEFINE(name, stack_size, \ |
| 1111 | entry, p1, p2, p3, \ |
| 1112 | prio, options, delay) \ |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 1113 | K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 1114 | struct k_thread _k_thread_obj_##name; \ |
| 1115 | Z_STRUCT_SECTION_ITERABLE(_static_thread_data, _k_thread_data_##name) =\ |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 1116 | Z_THREAD_INITIALIZER(&_k_thread_obj_##name, \ |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 1117 | _k_thread_stack_##name, stack_size, \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 1118 | entry, p1, p2, p3, prio, options, delay, \ |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 1119 | NULL, name); \ |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 1120 | const k_tid_t name = (k_tid_t)&_k_thread_obj_##name |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1121 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1122 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1123 | * @brief Get a thread's priority. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1124 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1125 | * This routine gets the priority of @a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1126 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1127 | * @param thread ID of thread whose priority is needed. |
| 1128 | * |
| 1129 | * @return Priority of @a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1130 | */ |
Andrew Boie | 76c04a2 | 2017-09-27 14:45:10 -0700 | [diff] [blame] | 1131 | __syscall int k_thread_priority_get(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1132 | |
| 1133 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1134 | * @brief Set a thread's priority. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1135 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1136 | * This routine immediately changes the priority of @a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1137 | * |
| 1138 | * Rescheduling can occur immediately depending on the priority @a thread is |
| 1139 | * set to: |
| 1140 | * |
| 1141 | * - If its priority is raised above the priority of the caller of this |
| 1142 | * function, and the caller is preemptible, @a thread will be scheduled in. |
| 1143 | * |
| 1144 | * - If the caller operates on itself, it lowers its priority below that of |
| 1145 | * other threads in the system, and the caller is preemptible, the thread of |
| 1146 | * highest priority will be scheduled in. |
| 1147 | * |
| 1148 | * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to |
| 1149 | * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the |
| 1150 | * highest priority. |
| 1151 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1152 | * @param thread ID of thread whose priority is to be set. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1153 | * @param prio New priority. |
| 1154 | * |
| 1155 | * @warning Changing the priority of a thread currently involved in mutex |
| 1156 | * priority inheritance may result in undefined behavior. |
| 1157 | * |
| 1158 | * @return N/A |
| 1159 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1160 | __syscall void k_thread_priority_set(k_tid_t thread, int prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1161 | |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 1162 | |
| 1163 | #ifdef CONFIG_SCHED_DEADLINE |
| 1164 | /** |
| 1165 | * @brief Set deadline expiration time for scheduler |
| 1166 | * |
| 1167 | * This sets the "deadline" expiration as a time delta from the |
| 1168 | * current time, in the same units used by k_cycle_get_32(). The |
| 1169 | * scheduler (when deadline scheduling is enabled) will choose the |
| 1170 | * next expiring thread when selecting between threads at the same |
| 1171 | * static priority. Threads at different priorities will be scheduled |
| 1172 | * according to their static priority. |
| 1173 | * |
| 1174 | * @note Deadlines that are negative (i.e. in the past) are still seen |
| 1175 | * as higher priority than others, even if the thread has "finished" |
| 1176 | * its work. If you don't want it scheduled anymore, you have to |
| 1177 | * reset the deadline into the future, block/pend the thread, or |
| 1178 | * modify its priority with k_thread_priority_set(). |
| 1179 | * |
| 1180 | * @note Despite the API naming, the scheduler makes no guarantees the |
| 1181 | * the thread WILL be scheduled within that deadline, nor does it take |
| 1182 | * extra metadata (like e.g. the "runtime" and "period" parameters in |
| 1183 | * Linux sched_setattr()) that allows the kernel to validate the |
| 1184 | * scheduling for achievability. Such features could be implemented |
| 1185 | * above this call, which is simply input to the priority selection |
| 1186 | * logic. |
| 1187 | * |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1188 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1189 | * @rst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1190 | * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project |
| 1191 | * configuration. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1192 | * @endrst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1193 | * |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 1194 | * @param thread A thread on which to set the deadline |
| 1195 | * @param deadline A time delta, in cycle units |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1196 | * |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 1197 | */ |
| 1198 | __syscall void k_thread_deadline_set(k_tid_t thread, int deadline); |
| 1199 | #endif |
| 1200 | |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1201 | #ifdef CONFIG_SCHED_CPU_MASK |
| 1202 | /** |
| 1203 | * @brief Sets all CPU enable masks to zero |
| 1204 | * |
| 1205 | * After this returns, the thread will no longer be schedulable on any |
| 1206 | * CPUs. The thread must not be currently runnable. |
| 1207 | * |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1208 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1209 | * @rst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1210 | * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project |
| 1211 | * configuration. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1212 | * @endrst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1213 | * |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1214 | * @param thread Thread to operate upon |
| 1215 | * @return Zero on success, otherwise error code |
| 1216 | */ |
| 1217 | int k_thread_cpu_mask_clear(k_tid_t thread); |
| 1218 | |
| 1219 | /** |
| 1220 | * @brief Sets all CPU enable masks to one |
| 1221 | * |
| 1222 | * After this returns, the thread will be schedulable on any CPU. The |
| 1223 | * thread must not be currently runnable. |
| 1224 | * |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1225 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1226 | * @rst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1227 | * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project |
| 1228 | * configuration. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1229 | * @endrst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1230 | * |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1231 | * @param thread Thread to operate upon |
| 1232 | * @return Zero on success, otherwise error code |
| 1233 | */ |
| 1234 | int k_thread_cpu_mask_enable_all(k_tid_t thread); |
| 1235 | |
| 1236 | /** |
| 1237 | * @brief Enable thread to run on specified CPU |
| 1238 | * |
| 1239 | * The thread must not be currently runnable. |
| 1240 | * |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1241 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1242 | * @rst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1243 | * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project |
| 1244 | * configuration. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1245 | * @endrst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1246 | * |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1247 | * @param thread Thread to operate upon |
| 1248 | * @param cpu CPU index |
| 1249 | * @return Zero on success, otherwise error code |
| 1250 | */ |
| 1251 | int k_thread_cpu_mask_enable(k_tid_t thread, int cpu); |
| 1252 | |
| 1253 | /** |
| 1254 | * @brief Prevent thread to run on specified CPU |
| 1255 | * |
| 1256 | * The thread must not be currently runnable. |
| 1257 | * |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1258 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1259 | * @rst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1260 | * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project |
| 1261 | * configuration. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1262 | * @endrst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1263 | * |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1264 | * @param thread Thread to operate upon |
| 1265 | * @param cpu CPU index |
| 1266 | * @return Zero on success, otherwise error code |
| 1267 | */ |
| 1268 | int k_thread_cpu_mask_disable(k_tid_t thread, int cpu); |
| 1269 | #endif |
| 1270 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1271 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1272 | * @brief Suspend a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1273 | * |
Andy Ross | 50d0942 | 2019-11-19 11:20:07 -0800 | [diff] [blame] | 1274 | * This routine prevents the kernel scheduler from making @a thread |
| 1275 | * the current thread. All other internal operations on @a thread are |
| 1276 | * still performed; for example, kernel objects it is waiting on are |
| 1277 | * still handed to it. Note that any existing timeouts |
| 1278 | * (e.g. k_sleep(), or a timeout argument to k_sem_take() et. al.) |
| 1279 | * will be canceled. On resume, the thread will begin running |
| 1280 | * immediately and return from the blocked call. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1281 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1282 | * If @a thread is already suspended, the routine has no effect. |
| 1283 | * |
| 1284 | * @param thread ID of thread to suspend. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1285 | * |
| 1286 | * @return N/A |
| 1287 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1288 | __syscall void k_thread_suspend(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1289 | |
| 1290 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1291 | * @brief Resume a suspended thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1292 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1293 | * This routine allows the kernel scheduler to make @a thread the current |
| 1294 | * thread, when it is next eligible for that role. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1295 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1296 | * If @a thread is not currently suspended, the routine has no effect. |
| 1297 | * |
| 1298 | * @param thread ID of thread to resume. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1299 | * |
| 1300 | * @return N/A |
| 1301 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1302 | __syscall void k_thread_resume(k_tid_t thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1303 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1304 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1305 | * @brief Set time-slicing period and scope. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1306 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1307 | * This routine specifies how the scheduler will perform time slicing of |
| 1308 | * preemptible threads. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1309 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1310 | * To enable time slicing, @a slice must be non-zero. The scheduler |
| 1311 | * ensures that no thread runs for more than the specified time limit |
| 1312 | * before other threads of that priority are given a chance to execute. |
| 1313 | * Any thread whose priority is higher than @a prio is exempted, and may |
David B. Kinder | 8b986d7 | 2017-04-18 15:56:26 -0700 | [diff] [blame] | 1314 | * execute as long as desired without being preempted due to time slicing. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1315 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1316 | * Time slicing only limits the maximum amount of time a thread may continuously |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1317 | * execute. Once the scheduler selects a thread for execution, there is no |
| 1318 | * minimum guaranteed time the thread will execute before threads of greater or |
| 1319 | * equal priority are scheduled. |
| 1320 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1321 | * When the current thread is the only one of that priority eligible |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1322 | * for execution, this routine has no effect; the thread is immediately |
| 1323 | * rescheduled after the slice period expires. |
| 1324 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1325 | * To disable timeslicing, set both @a slice and @a prio to zero. |
| 1326 | * |
| 1327 | * @param slice Maximum time slice length (in milliseconds). |
| 1328 | * @param prio Highest thread priority level eligible for time slicing. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1329 | * |
| 1330 | * @return N/A |
| 1331 | */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 1332 | extern void k_sched_time_slice_set(s32_t slice, int prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1333 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 1334 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1335 | |
| 1336 | /** |
| 1337 | * @addtogroup isr_apis |
| 1338 | * @{ |
| 1339 | */ |
| 1340 | |
| 1341 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1342 | * @brief Determine if code is running at interrupt level. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1343 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1344 | * This routine allows the caller to customize its actions, depending on |
| 1345 | * whether it is a thread or an ISR. |
| 1346 | * |
| 1347 | * @note Can be called by ISRs. |
| 1348 | * |
Flavio Ceolin | 6a4a86e | 2018-12-17 12:40:22 -0800 | [diff] [blame] | 1349 | * @return false if invoked by a thread. |
| 1350 | * @return true if invoked by an ISR. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1351 | */ |
Flavio Ceolin | 6a4a86e | 2018-12-17 12:40:22 -0800 | [diff] [blame] | 1352 | extern bool k_is_in_isr(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1353 | |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1354 | /** |
| 1355 | * @brief Determine if code is running in a preemptible thread. |
| 1356 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1357 | * This routine allows the caller to customize its actions, depending on |
| 1358 | * whether it can be preempted by another thread. The routine returns a 'true' |
| 1359 | * value if all of the following conditions are met: |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1360 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1361 | * - The code is running in a thread, not at ISR. |
| 1362 | * - The thread's priority is in the preemptible range. |
| 1363 | * - The thread has not locked the scheduler. |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1364 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1365 | * @note Can be called by ISRs. |
| 1366 | * |
| 1367 | * @return 0 if invoked by an ISR or by a cooperative thread. |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1368 | * @return Non-zero if invoked by a preemptible thread. |
| 1369 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1370 | __syscall int k_is_preempt_thread(void); |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1371 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1372 | /** |
Peter Bigot | 74ef395 | 2019-12-23 11:48:43 -0600 | [diff] [blame] | 1373 | * @brief Test whether startup is in the before-main-task phase. |
| 1374 | * |
| 1375 | * This routine allows the caller to customize its actions, depending on |
| 1376 | * whether it being invoked before the kernel is fully active. |
| 1377 | * |
| 1378 | * @note Can be called by ISRs. |
| 1379 | * |
| 1380 | * @return true if invoked before post-kernel initialization |
| 1381 | * @return false if invoked during/after post-kernel initialization |
| 1382 | */ |
| 1383 | static inline bool k_is_pre_kernel(void) |
| 1384 | { |
| 1385 | extern bool z_sys_post_kernel; /* in init.c */ |
| 1386 | |
| 1387 | return !z_sys_post_kernel; |
| 1388 | } |
| 1389 | |
| 1390 | /** |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 1391 | * @} |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1392 | */ |
| 1393 | |
| 1394 | /** |
| 1395 | * @addtogroup thread_apis |
| 1396 | * @{ |
| 1397 | */ |
| 1398 | |
| 1399 | /** |
| 1400 | * @brief Lock the scheduler. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1401 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1402 | * This routine prevents the current thread from being preempted by another |
| 1403 | * thread by instructing the scheduler to treat it as a cooperative thread. |
| 1404 | * If the thread subsequently performs an operation that makes it unready, |
| 1405 | * it will be context switched out in the normal manner. When the thread |
| 1406 | * again becomes the current thread, its non-preemptible status is maintained. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1407 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1408 | * This routine can be called recursively. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1409 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1410 | * @note k_sched_lock() and k_sched_unlock() should normally be used |
| 1411 | * when the operation being performed can be safely interrupted by ISRs. |
| 1412 | * However, if the amount of processing involved is very small, better |
| 1413 | * performance may be obtained by using irq_lock() and irq_unlock(). |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1414 | * |
| 1415 | * @return N/A |
| 1416 | */ |
| 1417 | extern void k_sched_lock(void); |
| 1418 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1419 | /** |
| 1420 | * @brief Unlock the scheduler. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1421 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1422 | * This routine reverses the effect of a previous call to k_sched_lock(). |
| 1423 | * A thread must call the routine once for each time it called k_sched_lock() |
| 1424 | * before the thread becomes preemptible. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1425 | * |
| 1426 | * @return N/A |
| 1427 | */ |
| 1428 | extern void k_sched_unlock(void); |
| 1429 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1430 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1431 | * @brief Set current thread's custom data. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1432 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1433 | * This routine sets the custom data for the current thread to @ value. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1434 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1435 | * Custom data is not used by the kernel itself, and is freely available |
| 1436 | * for a thread to use as it sees fit. It can be used as a framework |
| 1437 | * upon which to build thread-local storage. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1438 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1439 | * @param value New custom data value. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1440 | * |
| 1441 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1442 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1443 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1444 | __syscall void k_thread_custom_data_set(void *value); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1445 | |
| 1446 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1447 | * @brief Get current thread's custom data. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1448 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1449 | * This routine returns the custom data for the current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1450 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1451 | * @return Current custom data value. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1452 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1453 | __syscall void *k_thread_custom_data_get(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1454 | |
| 1455 | /** |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 1456 | * @brief Set current thread name |
| 1457 | * |
| 1458 | * Set the name of the thread to be used when THREAD_MONITOR is enabled for |
| 1459 | * tracing and debugging. |
| 1460 | * |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 1461 | * @param thread_id Thread to set name, or NULL to set the current thread |
| 1462 | * @param value Name string |
| 1463 | * @retval 0 on success |
| 1464 | * @retval -EFAULT Memory access error with supplied string |
| 1465 | * @retval -ENOSYS Thread name configuration option not enabled |
| 1466 | * @retval -EINVAL Thread name too long |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 1467 | */ |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 1468 | __syscall int k_thread_name_set(k_tid_t thread_id, const char *value); |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 1469 | |
| 1470 | /** |
| 1471 | * @brief Get thread name |
| 1472 | * |
| 1473 | * Get the name of a thread |
| 1474 | * |
| 1475 | * @param thread_id Thread ID |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 1476 | * @retval Thread name, or NULL if configuration not enabled |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 1477 | */ |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 1478 | const char *k_thread_name_get(k_tid_t thread_id); |
| 1479 | |
| 1480 | /** |
| 1481 | * @brief Copy the thread name into a supplied buffer |
| 1482 | * |
| 1483 | * @param thread_id Thread to obtain name information |
| 1484 | * @param buf Destination buffer |
David B. Kinder | 73896c0 | 2019-10-28 16:27:57 -0700 | [diff] [blame] | 1485 | * @param size Destination buffer size |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 1486 | * @retval -ENOSPC Destination buffer too small |
| 1487 | * @retval -EFAULT Memory access error |
| 1488 | * @retval -ENOSYS Thread name feature not enabled |
| 1489 | * @retval 0 Success |
| 1490 | */ |
| 1491 | __syscall int k_thread_name_copy(k_tid_t thread_id, char *buf, |
| 1492 | size_t size); |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 1493 | |
| 1494 | /** |
Pavlo Hamov | 8076c80 | 2019-07-31 12:43:54 +0300 | [diff] [blame] | 1495 | * @brief Get thread state string |
| 1496 | * |
| 1497 | * Get the human friendly thread state string |
| 1498 | * |
| 1499 | * @param thread_id Thread ID |
| 1500 | * @retval Thread state string, empty if no state flag is set |
| 1501 | */ |
| 1502 | const char *k_thread_state_str(k_tid_t thread_id); |
| 1503 | |
| 1504 | /** |
Andy Ross | cfe6203 | 2018-09-29 07:34:55 -0700 | [diff] [blame] | 1505 | * @} |
| 1506 | */ |
| 1507 | |
| 1508 | /** |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1509 | * @addtogroup clock_apis |
| 1510 | * @{ |
| 1511 | */ |
| 1512 | |
| 1513 | /** |
| 1514 | * @brief Generate null timeout delay. |
| 1515 | * |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1516 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1517 | * not to wait if the requested operation cannot be performed immediately. |
| 1518 | * |
| 1519 | * @return Timeout delay value. |
| 1520 | */ |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 1521 | #define K_NO_WAIT Z_TIMEOUT_NO_WAIT |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1522 | |
| 1523 | /** |
Andy Ross | e1bc595 | 2020-03-09 12:19:54 -0700 | [diff] [blame] | 1524 | * @brief Generate timeout delay from nanoseconds. |
| 1525 | * |
| 1526 | * This macro generates a timeout delay that instructs a kernel API to |
| 1527 | * wait up to @a t nanoseconds to perform the requested operation. |
| 1528 | * Note that timer precision is limited to the tick rate, not the |
| 1529 | * requested value. |
| 1530 | * |
Andy Ross | e39bf29 | 2020-03-19 10:30:33 -0700 | [diff] [blame] | 1531 | * @param t Duration in nanoseconds. |
Andy Ross | e1bc595 | 2020-03-09 12:19:54 -0700 | [diff] [blame] | 1532 | * |
| 1533 | * @return Timeout delay value. |
| 1534 | */ |
| 1535 | #define K_NSEC(t) Z_TIMEOUT_NS(t) |
| 1536 | |
| 1537 | /** |
| 1538 | * @brief Generate timeout delay from microseconds. |
| 1539 | * |
| 1540 | * This macro generates a timeout delay that instructs a kernel API |
| 1541 | * to wait up to @a t microseconds to perform the requested operation. |
| 1542 | * Note that timer precision is limited to the tick rate, not the |
| 1543 | * requested value. |
| 1544 | * |
Andy Ross | e39bf29 | 2020-03-19 10:30:33 -0700 | [diff] [blame] | 1545 | * @param t Duration in microseconds. |
Andy Ross | e1bc595 | 2020-03-09 12:19:54 -0700 | [diff] [blame] | 1546 | * |
| 1547 | * @return Timeout delay value. |
| 1548 | */ |
| 1549 | #define K_USEC(t) Z_TIMEOUT_US(t) |
| 1550 | |
| 1551 | /** |
| 1552 | * @brief Generate timeout delay from cycles. |
| 1553 | * |
| 1554 | * This macro generates a timeout delay that instructs a kernel API |
| 1555 | * to wait up to @a t cycles to perform the requested operation. |
| 1556 | * |
Andy Ross | e39bf29 | 2020-03-19 10:30:33 -0700 | [diff] [blame] | 1557 | * @param t Duration in cycles. |
Andy Ross | e1bc595 | 2020-03-09 12:19:54 -0700 | [diff] [blame] | 1558 | * |
| 1559 | * @return Timeout delay value. |
| 1560 | */ |
| 1561 | #define K_CYC(t) Z_TIMEOUT_CYC(t) |
| 1562 | |
| 1563 | /** |
| 1564 | * @brief Generate timeout delay from system ticks. |
| 1565 | * |
| 1566 | * This macro generates a timeout delay that instructs a kernel API |
| 1567 | * to wait up to @a t ticks to perform the requested operation. |
| 1568 | * |
Andy Ross | e39bf29 | 2020-03-19 10:30:33 -0700 | [diff] [blame] | 1569 | * @param t Duration in system ticks. |
Andy Ross | e1bc595 | 2020-03-09 12:19:54 -0700 | [diff] [blame] | 1570 | * |
| 1571 | * @return Timeout delay value. |
| 1572 | */ |
| 1573 | #define K_TICKS(t) Z_TIMEOUT_TICKS(t) |
| 1574 | |
| 1575 | /** |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1576 | * @brief Generate timeout delay from milliseconds. |
| 1577 | * |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1578 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1579 | * to wait up to @a ms milliseconds to perform the requested operation. |
| 1580 | * |
| 1581 | * @param ms Duration in milliseconds. |
| 1582 | * |
| 1583 | * @return Timeout delay value. |
| 1584 | */ |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 1585 | #define K_MSEC(ms) Z_TIMEOUT_MS(ms) |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1586 | |
| 1587 | /** |
| 1588 | * @brief Generate timeout delay from seconds. |
| 1589 | * |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1590 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1591 | * to wait up to @a s seconds to perform the requested operation. |
| 1592 | * |
| 1593 | * @param s Duration in seconds. |
| 1594 | * |
| 1595 | * @return Timeout delay value. |
| 1596 | */ |
Johan Hedberg | 1447169 | 2016-11-13 10:52:15 +0200 | [diff] [blame] | 1597 | #define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC) |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1598 | |
| 1599 | /** |
| 1600 | * @brief Generate timeout delay from minutes. |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1601 | |
| 1602 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1603 | * to wait up to @a m minutes to perform the requested operation. |
| 1604 | * |
| 1605 | * @param m Duration in minutes. |
| 1606 | * |
| 1607 | * @return Timeout delay value. |
| 1608 | */ |
Johan Hedberg | 1447169 | 2016-11-13 10:52:15 +0200 | [diff] [blame] | 1609 | #define K_MINUTES(m) K_SECONDS((m) * 60) |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1610 | |
| 1611 | /** |
| 1612 | * @brief Generate timeout delay from hours. |
| 1613 | * |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1614 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1615 | * to wait up to @a h hours to perform the requested operation. |
| 1616 | * |
| 1617 | * @param h Duration in hours. |
| 1618 | * |
| 1619 | * @return Timeout delay value. |
| 1620 | */ |
Johan Hedberg | 1447169 | 2016-11-13 10:52:15 +0200 | [diff] [blame] | 1621 | #define K_HOURS(h) K_MINUTES((h) * 60) |
| 1622 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1623 | /** |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1624 | * @brief Generate infinite timeout delay. |
| 1625 | * |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1626 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1627 | * to wait as long as necessary to perform the requested operation. |
| 1628 | * |
| 1629 | * @return Timeout delay value. |
| 1630 | */ |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 1631 | #define K_FOREVER Z_FOREVER |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1632 | |
Andy Ross | e1bc595 | 2020-03-09 12:19:54 -0700 | [diff] [blame] | 1633 | #ifdef CONFIG_TIMEOUT_64BIT |
| 1634 | |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1635 | /** |
Andy Ross | e39bf29 | 2020-03-19 10:30:33 -0700 | [diff] [blame] | 1636 | * @brief Generates an absolute/uptime timeout value from system ticks |
Andy Ross | 4c7b77a | 2020-03-09 09:35:35 -0700 | [diff] [blame] | 1637 | * |
| 1638 | * This macro generates a timeout delay that represents an expiration |
Andy Ross | e39bf29 | 2020-03-19 10:30:33 -0700 | [diff] [blame] | 1639 | * at the absolute uptime value specified, in system ticks. That is, the |
Andy Ross | 4c7b77a | 2020-03-09 09:35:35 -0700 | [diff] [blame] | 1640 | * timeout will expire immediately after the system uptime reaches the |
| 1641 | * specified tick count. |
| 1642 | * |
| 1643 | * @param t Tick uptime value |
| 1644 | * @return Timeout delay value |
| 1645 | */ |
| 1646 | #define K_TIMEOUT_ABS_TICKS(t) Z_TIMEOUT_TICKS(Z_TICK_ABS(MAX(t, 0))) |
| 1647 | |
| 1648 | /** |
Andy Ross | e39bf29 | 2020-03-19 10:30:33 -0700 | [diff] [blame] | 1649 | * @brief Generates an absolute/uptime timeout value from milliseconds |
Andy Ross | 4c7b77a | 2020-03-09 09:35:35 -0700 | [diff] [blame] | 1650 | * |
| 1651 | * This macro generates a timeout delay that represents an expiration |
| 1652 | * at the absolute uptime value specified, in milliseconds. That is, |
| 1653 | * the timeout will expire immediately after the system uptime reaches |
| 1654 | * the specified tick count. |
| 1655 | * |
| 1656 | * @param t Millisecond uptime value |
| 1657 | * @return Timeout delay value |
| 1658 | */ |
| 1659 | #define K_TIMEOUT_ABS_MS(t) K_TIMEOUT_ABS_TICKS(k_ms_to_ticks_ceil64(t)) |
| 1660 | |
| 1661 | /** |
Andy Ross | e39bf29 | 2020-03-19 10:30:33 -0700 | [diff] [blame] | 1662 | * @brief Generates an absolute/uptime timeout value from microseconds |
Andy Ross | e1bc595 | 2020-03-09 12:19:54 -0700 | [diff] [blame] | 1663 | * |
| 1664 | * This macro generates a timeout delay that represents an expiration |
| 1665 | * at the absolute uptime value specified, in microseconds. That is, |
| 1666 | * the timeout will expire immediately after the system uptime reaches |
| 1667 | * the specified time. Note that timer precision is limited by the |
| 1668 | * system tick rate and not the requested timeout value. |
| 1669 | * |
| 1670 | * @param t Microsecond uptime value |
| 1671 | * @return Timeout delay value |
| 1672 | */ |
| 1673 | #define K_TIMEOUT_ABS_US(t) K_TIMEOUT_ABS_TICKS(k_us_to_ticks_ceil64(t)) |
| 1674 | |
| 1675 | /** |
Andy Ross | e39bf29 | 2020-03-19 10:30:33 -0700 | [diff] [blame] | 1676 | * @brief Generates an absolute/uptime timeout value from nanoseconds |
Andy Ross | e1bc595 | 2020-03-09 12:19:54 -0700 | [diff] [blame] | 1677 | * |
| 1678 | * This macro generates a timeout delay that represents an expiration |
| 1679 | * at the absolute uptime value specified, in nanoseconds. That is, |
| 1680 | * the timeout will expire immediately after the system uptime reaches |
| 1681 | * the specified time. Note that timer precision is limited by the |
| 1682 | * system tick rate and not the requested timeout value. |
| 1683 | * |
| 1684 | * @param t Nanosecond uptime value |
| 1685 | * @return Timeout delay value |
| 1686 | */ |
| 1687 | #define K_TIMEOUT_ABS_NS(t) K_TIMEOUT_ABS_TICKS(k_ns_to_ticks_ceil64(t)) |
| 1688 | |
| 1689 | /** |
Andy Ross | e39bf29 | 2020-03-19 10:30:33 -0700 | [diff] [blame] | 1690 | * @brief Generates an absolute/uptime timeout value from system cycles |
Andy Ross | e1bc595 | 2020-03-09 12:19:54 -0700 | [diff] [blame] | 1691 | * |
| 1692 | * This macro generates a timeout delay that represents an expiration |
| 1693 | * at the absolute uptime value specified, in cycles. That is, the |
| 1694 | * timeout will expire immediately after the system uptime reaches the |
| 1695 | * specified time. Note that timer precision is limited by the system |
| 1696 | * tick rate and not the requested timeout value. |
| 1697 | * |
| 1698 | * @param t Cycle uptime value |
| 1699 | * @return Timeout delay value |
| 1700 | */ |
| 1701 | #define K_TIMEOUT_ABS_CYC(t) K_TIMEOUT_ABS_TICKS(k_cyc_to_ticks_ceil64(t)) |
| 1702 | |
| 1703 | #endif |
| 1704 | |
| 1705 | /** |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 1706 | * @} |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1707 | */ |
| 1708 | |
| 1709 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1710 | * @cond INTERNAL_HIDDEN |
| 1711 | */ |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 1712 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1713 | struct k_timer { |
| 1714 | /* |
| 1715 | * _timeout structure must be first here if we want to use |
| 1716 | * dynamic timer allocation. timeout.node is used in the double-linked |
| 1717 | * list of free timers |
| 1718 | */ |
| 1719 | struct _timeout timeout; |
| 1720 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1721 | /* wait queue for the (single) thread waiting on this timer */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1722 | _wait_q_t wait_q; |
| 1723 | |
| 1724 | /* runs in ISR context */ |
Flavio Ceolin | 4b35dd2 | 2018-11-16 19:06:59 -0800 | [diff] [blame] | 1725 | void (*expiry_fn)(struct k_timer *timer); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1726 | |
| 1727 | /* runs in the context of the thread that calls k_timer_stop() */ |
Flavio Ceolin | 4b35dd2 | 2018-11-16 19:06:59 -0800 | [diff] [blame] | 1728 | void (*stop_fn)(struct k_timer *timer); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1729 | |
| 1730 | /* timer period */ |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 1731 | k_timeout_t period; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1732 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1733 | /* timer status */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 1734 | u32_t status; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1735 | |
Benjamin Walsh | e4e98f9 | 2017-01-12 19:38:53 -0500 | [diff] [blame] | 1736 | /* user-specific data, also used to support legacy features */ |
| 1737 | void *user_data; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1738 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 1739 | _OBJECT_TRACING_NEXT_PTR(k_timer) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 1740 | _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1741 | }; |
| 1742 | |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 1743 | #define Z_TIMER_INITIALIZER(obj, expiry, stop) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1744 | { \ |
Krzysztof Chruscinski | be06327 | 2019-02-13 11:19:54 +0100 | [diff] [blame] | 1745 | .timeout = { \ |
| 1746 | .node = {},\ |
| 1747 | .dticks = 0, \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1748 | .fn = z_timer_expiration_handler \ |
Krzysztof Chruscinski | be06327 | 2019-02-13 11:19:54 +0100 | [diff] [blame] | 1749 | }, \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1750 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 1751 | .expiry_fn = expiry, \ |
| 1752 | .stop_fn = stop, \ |
| 1753 | .status = 0, \ |
Benjamin Walsh | e4e98f9 | 2017-01-12 19:38:53 -0500 | [diff] [blame] | 1754 | .user_data = 0, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 1755 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1756 | } |
| 1757 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 1758 | #define K_TIMER_INITIALIZER __DEPRECATED_MACRO Z_TIMER_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 1759 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1760 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1761 | * INTERNAL_HIDDEN @endcond |
| 1762 | */ |
| 1763 | |
| 1764 | /** |
| 1765 | * @defgroup timer_apis Timer APIs |
| 1766 | * @ingroup kernel_apis |
| 1767 | * @{ |
| 1768 | */ |
| 1769 | |
| 1770 | /** |
Allan Stephens | 5eceb85 | 2016-11-16 10:16:30 -0500 | [diff] [blame] | 1771 | * @typedef k_timer_expiry_t |
| 1772 | * @brief Timer expiry function type. |
| 1773 | * |
| 1774 | * A timer's expiry function is executed by the system clock interrupt handler |
| 1775 | * each time the timer expires. The expiry function is optional, and is only |
| 1776 | * invoked if the timer has been initialized with one. |
| 1777 | * |
| 1778 | * @param timer Address of timer. |
| 1779 | * |
| 1780 | * @return N/A |
| 1781 | */ |
| 1782 | typedef void (*k_timer_expiry_t)(struct k_timer *timer); |
| 1783 | |
| 1784 | /** |
| 1785 | * @typedef k_timer_stop_t |
| 1786 | * @brief Timer stop function type. |
| 1787 | * |
| 1788 | * A timer's stop function is executed if the timer is stopped prematurely. |
| 1789 | * The function runs in the context of the thread that stops the timer. |
| 1790 | * The stop function is optional, and is only invoked if the timer has been |
| 1791 | * initialized with one. |
| 1792 | * |
| 1793 | * @param timer Address of timer. |
| 1794 | * |
| 1795 | * @return N/A |
| 1796 | */ |
| 1797 | typedef void (*k_timer_stop_t)(struct k_timer *timer); |
| 1798 | |
| 1799 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1800 | * @brief Statically define and initialize a timer. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1801 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1802 | * The timer can be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1803 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 1804 | * @code extern struct k_timer <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1805 | * |
| 1806 | * @param name Name of the timer variable. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1807 | * @param expiry_fn Function to invoke each time the timer expires. |
| 1808 | * @param stop_fn Function to invoke if the timer is stopped while running. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1809 | */ |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 1810 | #define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 1811 | Z_STRUCT_SECTION_ITERABLE(k_timer, name) = \ |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 1812 | Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1813 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1814 | /** |
| 1815 | * @brief Initialize a timer. |
| 1816 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1817 | * This routine initializes a timer, prior to its first use. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1818 | * |
| 1819 | * @param timer Address of timer. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1820 | * @param expiry_fn Function to invoke each time the timer expires. |
| 1821 | * @param stop_fn Function to invoke if the timer is stopped while running. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1822 | * |
| 1823 | * @return N/A |
| 1824 | */ |
| 1825 | extern void k_timer_init(struct k_timer *timer, |
Allan Stephens | 5eceb85 | 2016-11-16 10:16:30 -0500 | [diff] [blame] | 1826 | k_timer_expiry_t expiry_fn, |
| 1827 | k_timer_stop_t stop_fn); |
Andy Ross | 8d8b2ac | 2016-09-23 10:08:54 -0700 | [diff] [blame] | 1828 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1829 | /** |
| 1830 | * @brief Start a timer. |
| 1831 | * |
| 1832 | * This routine starts a timer, and resets its status to zero. The timer |
| 1833 | * begins counting down using the specified duration and period values. |
| 1834 | * |
| 1835 | * Attempting to start a timer that is already running is permitted. |
| 1836 | * The timer's status is reset to zero and the timer begins counting down |
| 1837 | * using the new duration and period values. |
| 1838 | * |
| 1839 | * @param timer Address of timer. |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 1840 | * @param duration Initial timer duration. |
| 1841 | * @param period Timer period. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1842 | * |
| 1843 | * @return N/A |
| 1844 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1845 | __syscall void k_timer_start(struct k_timer *timer, |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 1846 | k_timeout_t duration, k_timeout_t period); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1847 | |
| 1848 | /** |
| 1849 | * @brief Stop a timer. |
| 1850 | * |
| 1851 | * This routine stops a running timer prematurely. The timer's stop function, |
| 1852 | * if one exists, is invoked by the caller. |
| 1853 | * |
| 1854 | * Attempting to stop a timer that is not running is permitted, but has no |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1855 | * effect on the timer. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1856 | * |
Anas Nashif | 4fb12ae | 2017-02-01 20:06:55 -0500 | [diff] [blame] | 1857 | * @note Can be called by ISRs. The stop handler has to be callable from ISRs |
| 1858 | * if @a k_timer_stop is to be called from ISRs. |
| 1859 | * |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1860 | * @param timer Address of timer. |
| 1861 | * |
| 1862 | * @return N/A |
| 1863 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1864 | __syscall void k_timer_stop(struct k_timer *timer); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1865 | |
| 1866 | /** |
| 1867 | * @brief Read timer status. |
| 1868 | * |
| 1869 | * This routine reads the timer's status, which indicates the number of times |
| 1870 | * it has expired since its status was last read. |
| 1871 | * |
| 1872 | * Calling this routine resets the timer's status to zero. |
| 1873 | * |
| 1874 | * @param timer Address of timer. |
| 1875 | * |
| 1876 | * @return Timer status. |
| 1877 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1878 | __syscall u32_t k_timer_status_get(struct k_timer *timer); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1879 | |
| 1880 | /** |
| 1881 | * @brief Synchronize thread to timer expiration. |
| 1882 | * |
| 1883 | * This routine blocks the calling thread until the timer's status is non-zero |
| 1884 | * (indicating that it has expired at least once since it was last examined) |
| 1885 | * or the timer is stopped. If the timer status is already non-zero, |
| 1886 | * or the timer is already stopped, the caller continues without waiting. |
| 1887 | * |
| 1888 | * Calling this routine resets the timer's status to zero. |
| 1889 | * |
| 1890 | * This routine must not be used by interrupt handlers, since they are not |
| 1891 | * allowed to block. |
| 1892 | * |
| 1893 | * @param timer Address of timer. |
| 1894 | * |
| 1895 | * @return Timer status. |
| 1896 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1897 | __syscall u32_t k_timer_status_sync(struct k_timer *timer); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1898 | |
Andy Ross | 5a5d3da | 2020-03-09 13:59:15 -0700 | [diff] [blame] | 1899 | #ifdef CONFIG_SYS_CLOCK_EXISTS |
| 1900 | |
| 1901 | /** |
Andy Ross | e39bf29 | 2020-03-19 10:30:33 -0700 | [diff] [blame] | 1902 | * @brief Get next expiration time of a timer, in system ticks |
Andy Ross | 5a5d3da | 2020-03-09 13:59:15 -0700 | [diff] [blame] | 1903 | * |
| 1904 | * This routine returns the future system uptime reached at the next |
| 1905 | * time of expiration of the timer, in units of system ticks. If the |
| 1906 | * timer is not running, current system time is returned. |
| 1907 | * |
| 1908 | * @param timer The timer object |
| 1909 | * @return Uptime of expiration, in ticks |
| 1910 | */ |
| 1911 | __syscall k_ticks_t k_timer_expires_ticks(struct k_timer *timer); |
| 1912 | |
| 1913 | static inline k_ticks_t z_impl_k_timer_expires_ticks(struct k_timer *timer) |
| 1914 | { |
| 1915 | return z_timeout_expires(&timer->timeout); |
| 1916 | } |
| 1917 | |
| 1918 | /** |
Andy Ross | e39bf29 | 2020-03-19 10:30:33 -0700 | [diff] [blame] | 1919 | * @brief Get time remaining before a timer next expires, in system ticks |
Andy Ross | 5a5d3da | 2020-03-09 13:59:15 -0700 | [diff] [blame] | 1920 | * |
| 1921 | * This routine computes the time remaining before a running timer |
| 1922 | * next expires, in units of system ticks. If the timer is not |
| 1923 | * running, it returns zero. |
| 1924 | */ |
| 1925 | __syscall k_ticks_t k_timer_remaining_ticks(struct k_timer *timer); |
| 1926 | |
| 1927 | static inline k_ticks_t z_impl_k_timer_remaining_ticks(struct k_timer *timer) |
| 1928 | { |
| 1929 | return z_timeout_remaining(&timer->timeout); |
| 1930 | } |
Andy Ross | 52e444b | 2018-09-28 09:06:37 -0700 | [diff] [blame] | 1931 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1932 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1933 | * @brief Get time remaining before a timer next expires. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1934 | * |
| 1935 | * This routine computes the (approximate) time remaining before a running |
| 1936 | * timer next expires. If the timer is not running, it returns zero. |
| 1937 | * |
| 1938 | * @param timer Address of timer. |
| 1939 | * |
| 1940 | * @return Remaining time (in milliseconds). |
| 1941 | */ |
Andy Ross | 5a5d3da | 2020-03-09 13:59:15 -0700 | [diff] [blame] | 1942 | static inline u32_t k_timer_remaining_get(struct k_timer *timer) |
Johan Hedberg | f99ad3f | 2016-12-09 10:39:49 +0200 | [diff] [blame] | 1943 | { |
Andy Ross | 5a5d3da | 2020-03-09 13:59:15 -0700 | [diff] [blame] | 1944 | return k_ticks_to_ms_floor32(k_timer_remaining_ticks(timer)); |
Johan Hedberg | f99ad3f | 2016-12-09 10:39:49 +0200 | [diff] [blame] | 1945 | } |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1946 | |
Andy Ross | 5a5d3da | 2020-03-09 13:59:15 -0700 | [diff] [blame] | 1947 | #endif /* CONFIG_SYS_CLOCK_EXISTS */ |
| 1948 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1949 | /** |
Benjamin Walsh | e4e98f9 | 2017-01-12 19:38:53 -0500 | [diff] [blame] | 1950 | * @brief Associate user-specific data with a timer. |
| 1951 | * |
| 1952 | * This routine records the @a user_data with the @a timer, to be retrieved |
| 1953 | * later. |
| 1954 | * |
| 1955 | * It can be used e.g. in a timer handler shared across multiple subsystems to |
| 1956 | * retrieve data specific to the subsystem this timer is associated with. |
| 1957 | * |
| 1958 | * @param timer Address of timer. |
| 1959 | * @param user_data User data to associate with the timer. |
| 1960 | * |
| 1961 | * @return N/A |
| 1962 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1963 | __syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data); |
| 1964 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 1965 | /** |
| 1966 | * @internal |
| 1967 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1968 | static inline void z_impl_k_timer_user_data_set(struct k_timer *timer, |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1969 | void *user_data) |
Benjamin Walsh | e4e98f9 | 2017-01-12 19:38:53 -0500 | [diff] [blame] | 1970 | { |
| 1971 | timer->user_data = user_data; |
| 1972 | } |
| 1973 | |
| 1974 | /** |
| 1975 | * @brief Retrieve the user-specific data from a timer. |
| 1976 | * |
| 1977 | * @param timer Address of timer. |
| 1978 | * |
| 1979 | * @return The user data. |
| 1980 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1981 | __syscall void *k_timer_user_data_get(struct k_timer *timer); |
| 1982 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1983 | static inline void *z_impl_k_timer_user_data_get(struct k_timer *timer) |
Benjamin Walsh | e4e98f9 | 2017-01-12 19:38:53 -0500 | [diff] [blame] | 1984 | { |
| 1985 | return timer->user_data; |
| 1986 | } |
| 1987 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 1988 | /** @} */ |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1989 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1990 | /** |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1991 | * @addtogroup clock_apis |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1992 | * @{ |
| 1993 | */ |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1994 | |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1995 | /** |
Andy Ross | e39bf29 | 2020-03-19 10:30:33 -0700 | [diff] [blame] | 1996 | * @brief Get system uptime, in system ticks. |
Andy Ross | 914205c | 2020-03-10 15:26:38 -0700 | [diff] [blame] | 1997 | * |
| 1998 | * This routine returns the elapsed time since the system booted, in |
| 1999 | * ticks (c.f. :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC`), which is the |
| 2000 | * fundamental unit of resolution of kernel timekeeping. |
| 2001 | * |
| 2002 | * @return Current uptime in ticks. |
| 2003 | */ |
| 2004 | __syscall s64_t k_uptime_ticks(void); |
| 2005 | |
| 2006 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2007 | * @brief Get system uptime. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 2008 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2009 | * This routine returns the elapsed time since the system booted, |
| 2010 | * in milliseconds. |
| 2011 | * |
David B. Kinder | 00c41ea | 2019-06-10 11:13:33 -0700 | [diff] [blame] | 2012 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 2013 | * @rst |
David B. Kinder | 00c41ea | 2019-06-10 11:13:33 -0700 | [diff] [blame] | 2014 | * While this function returns time in milliseconds, it does |
| 2015 | * not mean it has millisecond resolution. The actual resolution depends on |
Andy Ross | 669730f | 2019-06-11 11:18:20 -0700 | [diff] [blame] | 2016 | * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 2017 | * @endrst |
Paul Sokolovsky | 65d51fd | 2019-02-04 22:44:50 +0300 | [diff] [blame] | 2018 | * |
| 2019 | * @return Current uptime in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 2020 | */ |
Andy Ross | 914205c | 2020-03-10 15:26:38 -0700 | [diff] [blame] | 2021 | static inline s64_t k_uptime_get(void) |
| 2022 | { |
| 2023 | return k_ticks_to_ms_floor64(k_uptime_ticks()); |
| 2024 | } |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 2025 | |
Ramesh Thomas | 89ffd44 | 2017-02-05 19:37:19 -0800 | [diff] [blame] | 2026 | /** |
| 2027 | * @brief Enable clock always on in tickless kernel |
| 2028 | * |
Andy Ross | 1db9f18 | 2019-06-25 10:09:45 -0700 | [diff] [blame] | 2029 | * Deprecated. This does nothing (it was always just a hint). This |
| 2030 | * functionality has been migrated to the SYSTEM_CLOCK_SLOPPY_IDLE |
| 2031 | * kconfig. |
Ramesh Thomas | 89ffd44 | 2017-02-05 19:37:19 -0800 | [diff] [blame] | 2032 | * |
| 2033 | * @retval prev_status Previous status of always on flag |
| 2034 | */ |
Andy Ross | 1db9f18 | 2019-06-25 10:09:45 -0700 | [diff] [blame] | 2035 | /* LCOV_EXCL_START */ |
| 2036 | __deprecated static inline int k_enable_sys_clock_always_on(void) |
| 2037 | { |
| 2038 | __ASSERT(IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE), |
| 2039 | "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead"); |
| 2040 | |
| 2041 | return !IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE); |
| 2042 | } |
| 2043 | /* LCOV_EXCL_STOP */ |
Ramesh Thomas | 89ffd44 | 2017-02-05 19:37:19 -0800 | [diff] [blame] | 2044 | |
| 2045 | /** |
| 2046 | * @brief Disable clock always on in tickless kernel |
| 2047 | * |
Andy Ross | 1db9f18 | 2019-06-25 10:09:45 -0700 | [diff] [blame] | 2048 | * Deprecated. This does nothing (it was always just a hint). This |
| 2049 | * functionality has been migrated to the SYS_CLOCK_SLOPPY_IDLE |
| 2050 | * kconfig. |
Ramesh Thomas | 89ffd44 | 2017-02-05 19:37:19 -0800 | [diff] [blame] | 2051 | */ |
Andy Ross | 1db9f18 | 2019-06-25 10:09:45 -0700 | [diff] [blame] | 2052 | /* LCOV_EXCL_START */ |
| 2053 | __deprecated static inline void k_disable_sys_clock_always_on(void) |
| 2054 | { |
| 2055 | __ASSERT(!IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE), |
| 2056 | "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead"); |
| 2057 | } |
| 2058 | /* LCOV_EXCL_STOP */ |
Ramesh Thomas | 89ffd44 | 2017-02-05 19:37:19 -0800 | [diff] [blame] | 2059 | |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 2060 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2061 | * @brief Get system uptime (32-bit version). |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 2062 | * |
Peter Bigot | a6067a3 | 2019-08-28 08:19:26 -0500 | [diff] [blame] | 2063 | * This routine returns the lower 32 bits of the system uptime in |
| 2064 | * milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 2065 | * |
Peter Bigot | a6067a3 | 2019-08-28 08:19:26 -0500 | [diff] [blame] | 2066 | * Because correct conversion requires full precision of the system |
| 2067 | * clock there is no benefit to using this over k_uptime_get() unless |
| 2068 | * you know the application will never run long enough for the system |
| 2069 | * clock to approach 2^32 ticks. Calls to this function may involve |
| 2070 | * interrupt blocking and 64-bit math. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 2071 | * |
David B. Kinder | 00c41ea | 2019-06-10 11:13:33 -0700 | [diff] [blame] | 2072 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 2073 | * @rst |
David B. Kinder | 00c41ea | 2019-06-10 11:13:33 -0700 | [diff] [blame] | 2074 | * While this function returns time in milliseconds, it does |
| 2075 | * not mean it has millisecond resolution. The actual resolution depends on |
Andy Ross | 669730f | 2019-06-11 11:18:20 -0700 | [diff] [blame] | 2076 | * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 2077 | * @endrst |
Paul Sokolovsky | 65d51fd | 2019-02-04 22:44:50 +0300 | [diff] [blame] | 2078 | * |
Peter Bigot | a6067a3 | 2019-08-28 08:19:26 -0500 | [diff] [blame] | 2079 | * @return The low 32 bits of the current uptime, in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 2080 | */ |
Peter Bigot | a6067a3 | 2019-08-28 08:19:26 -0500 | [diff] [blame] | 2081 | static inline u32_t k_uptime_get_32(void) |
| 2082 | { |
| 2083 | return (u32_t)k_uptime_get(); |
| 2084 | } |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 2085 | |
| 2086 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2087 | * @brief Get elapsed time. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 2088 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2089 | * This routine computes the elapsed time between the current system uptime |
| 2090 | * and an earlier reference time, in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 2091 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2092 | * @param reftime Pointer to a reference time, which is updated to the current |
| 2093 | * uptime upon return. |
| 2094 | * |
| 2095 | * @return Elapsed time. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 2096 | */ |
Andy Ross | 987c0e5 | 2018-09-27 16:50:00 -0700 | [diff] [blame] | 2097 | static inline s64_t k_uptime_delta(s64_t *reftime) |
| 2098 | { |
| 2099 | s64_t uptime, delta; |
| 2100 | |
| 2101 | uptime = k_uptime_get(); |
| 2102 | delta = uptime - *reftime; |
| 2103 | *reftime = uptime; |
| 2104 | |
| 2105 | return delta; |
| 2106 | } |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 2107 | |
| 2108 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2109 | * @brief Get elapsed time (32-bit version). |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 2110 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2111 | * This routine computes the elapsed time between the current system uptime |
| 2112 | * and an earlier reference time, in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 2113 | * |
Peter Bigot | 42f9d14 | 2020-03-20 08:39:31 -0500 | [diff] [blame] | 2114 | * This is a wrapper around k_uptime_delta(). |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 2115 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2116 | * @param reftime Pointer to a reference time, which is updated to the current |
| 2117 | * uptime upon return. |
| 2118 | * |
| 2119 | * @return Elapsed time. |
Peter Bigot | 42f9d14 | 2020-03-20 08:39:31 -0500 | [diff] [blame] | 2120 | * |
| 2121 | * @deprecated in 2.3 release, replace with k_uptime_delta() |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 2122 | */ |
Peter Bigot | 42f9d14 | 2020-03-20 08:39:31 -0500 | [diff] [blame] | 2123 | __deprecated static inline u32_t k_uptime_delta_32(s64_t *reftime) |
Andy Ross | 987c0e5 | 2018-09-27 16:50:00 -0700 | [diff] [blame] | 2124 | { |
| 2125 | return (u32_t)k_uptime_delta(reftime); |
| 2126 | } |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 2127 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2128 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2129 | * @brief Read the hardware clock. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2130 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2131 | * This routine returns the current time, as measured by the system's hardware |
| 2132 | * clock. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2133 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2134 | * @return Current hardware clock up-counter (in cycles). |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2135 | */ |
Andrew Boie | 979b17f | 2019-10-03 15:20:41 -0700 | [diff] [blame] | 2136 | static inline u32_t k_cycle_get_32(void) |
| 2137 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 2138 | return arch_k_cycle_get_32(); |
Andrew Boie | 979b17f | 2019-10-03 15:20:41 -0700 | [diff] [blame] | 2139 | } |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2140 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2141 | /** |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 2142 | * @} |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2143 | */ |
| 2144 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2145 | /** |
| 2146 | * @cond INTERNAL_HIDDEN |
| 2147 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2148 | |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2149 | struct k_queue { |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2150 | sys_sflist_t data_q; |
Andy Ross | 603ea42 | 2018-07-25 13:01:54 -0700 | [diff] [blame] | 2151 | struct k_spinlock lock; |
Luiz Augusto von Dentz | 84db641 | 2017-07-13 12:43:59 +0300 | [diff] [blame] | 2152 | union { |
| 2153 | _wait_q_t wait_q; |
| 2154 | |
| 2155 | _POLL_EVENT; |
| 2156 | }; |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2157 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 2158 | _OBJECT_TRACING_NEXT_PTR(k_queue) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 2159 | _OBJECT_TRACING_LINKED_FLAG |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2160 | }; |
| 2161 | |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 2162 | #define Z_QUEUE_INITIALIZER(obj) \ |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2163 | { \ |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2164 | .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \ |
Stephanos Ioannidis | f628dcd | 2019-09-11 18:09:49 +0900 | [diff] [blame] | 2165 | .lock = { }, \ |
| 2166 | { \ |
| 2167 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
| 2168 | _POLL_EVENT_OBJ_INIT(obj) \ |
| 2169 | }, \ |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2170 | _OBJECT_TRACING_INIT \ |
| 2171 | } |
| 2172 | |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 2173 | #define K_QUEUE_INITIALIZER __DEPRECATED_MACRO Z_QUEUE_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2174 | |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2175 | extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free); |
| 2176 | |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2177 | /** |
| 2178 | * INTERNAL_HIDDEN @endcond |
| 2179 | */ |
| 2180 | |
| 2181 | /** |
| 2182 | * @defgroup queue_apis Queue APIs |
| 2183 | * @ingroup kernel_apis |
| 2184 | * @{ |
| 2185 | */ |
| 2186 | |
| 2187 | /** |
| 2188 | * @brief Initialize a queue. |
| 2189 | * |
| 2190 | * This routine initializes a queue object, prior to its first use. |
| 2191 | * |
| 2192 | * @param queue Address of the queue. |
| 2193 | * |
| 2194 | * @return N/A |
| 2195 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2196 | __syscall void k_queue_init(struct k_queue *queue); |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2197 | |
| 2198 | /** |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 2199 | * @brief Cancel waiting on a queue. |
| 2200 | * |
| 2201 | * This routine causes first thread pending on @a queue, if any, to |
| 2202 | * return from k_queue_get() call with NULL value (as if timeout expired). |
Paul Sokolovsky | 45c0b20 | 2018-08-21 23:29:11 +0300 | [diff] [blame] | 2203 | * If the queue is being waited on by k_poll(), it will return with |
| 2204 | * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent |
| 2205 | * k_queue_get() will return NULL). |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 2206 | * |
| 2207 | * @note Can be called by ISRs. |
| 2208 | * |
| 2209 | * @param queue Address of the queue. |
| 2210 | * |
| 2211 | * @return N/A |
| 2212 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2213 | __syscall void k_queue_cancel_wait(struct k_queue *queue); |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 2214 | |
| 2215 | /** |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2216 | * @brief Append an element to the end of a queue. |
| 2217 | * |
| 2218 | * This routine appends a data item to @a queue. A queue data item must be |
Nicolas Pitre | 659fa0d | 2019-05-21 22:13:01 -0400 | [diff] [blame] | 2219 | * aligned on a word boundary, and the first word of the item is reserved |
| 2220 | * for the kernel's use. |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2221 | * |
| 2222 | * @note Can be called by ISRs. |
| 2223 | * |
| 2224 | * @param queue Address of the queue. |
| 2225 | * @param data Address of the data item. |
| 2226 | * |
| 2227 | * @return N/A |
| 2228 | */ |
| 2229 | extern void k_queue_append(struct k_queue *queue, void *data); |
| 2230 | |
| 2231 | /** |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2232 | * @brief Append an element to a queue. |
| 2233 | * |
Andrew Boie | ac3dcc1 | 2019-04-01 12:28:03 -0700 | [diff] [blame] | 2234 | * This routine appends a data item to @a queue. There is an implicit memory |
| 2235 | * allocation to create an additional temporary bookkeeping data structure from |
| 2236 | * the calling thread's resource pool, which is automatically freed when the |
| 2237 | * item is removed. The data itself is not copied. |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2238 | * |
| 2239 | * @note Can be called by ISRs. |
| 2240 | * |
| 2241 | * @param queue Address of the queue. |
| 2242 | * @param data Address of the data item. |
| 2243 | * |
| 2244 | * @retval 0 on success |
| 2245 | * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool |
| 2246 | */ |
Adithya Baglody | 2a78b8d | 2018-10-25 12:09:04 +0530 | [diff] [blame] | 2247 | __syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data); |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2248 | |
| 2249 | /** |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2250 | * @brief Prepend an element to a queue. |
| 2251 | * |
| 2252 | * This routine prepends a data item to @a queue. A queue data item must be |
Nicolas Pitre | 659fa0d | 2019-05-21 22:13:01 -0400 | [diff] [blame] | 2253 | * aligned on a word boundary, and the first word of the item is reserved |
| 2254 | * for the kernel's use. |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2255 | * |
| 2256 | * @note Can be called by ISRs. |
| 2257 | * |
| 2258 | * @param queue Address of the queue. |
| 2259 | * @param data Address of the data item. |
| 2260 | * |
| 2261 | * @return N/A |
| 2262 | */ |
| 2263 | extern void k_queue_prepend(struct k_queue *queue, void *data); |
| 2264 | |
| 2265 | /** |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2266 | * @brief Prepend an element to a queue. |
| 2267 | * |
Andrew Boie | ac3dcc1 | 2019-04-01 12:28:03 -0700 | [diff] [blame] | 2268 | * This routine prepends a data item to @a queue. There is an implicit memory |
| 2269 | * allocation to create an additional temporary bookkeeping data structure from |
| 2270 | * the calling thread's resource pool, which is automatically freed when the |
| 2271 | * item is removed. The data itself is not copied. |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2272 | * |
| 2273 | * @note Can be called by ISRs. |
| 2274 | * |
| 2275 | * @param queue Address of the queue. |
| 2276 | * @param data Address of the data item. |
| 2277 | * |
| 2278 | * @retval 0 on success |
| 2279 | * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool |
| 2280 | */ |
Adithya Baglody | 2a78b8d | 2018-10-25 12:09:04 +0530 | [diff] [blame] | 2281 | __syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data); |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2282 | |
| 2283 | /** |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2284 | * @brief Inserts an element to a queue. |
| 2285 | * |
| 2286 | * This routine inserts a data item to @a queue after previous item. A queue |
Nicolas Pitre | 659fa0d | 2019-05-21 22:13:01 -0400 | [diff] [blame] | 2287 | * data item must be aligned on a word boundary, and the first word of |
| 2288 | * the item is reserved for the kernel's use. |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2289 | * |
| 2290 | * @note Can be called by ISRs. |
| 2291 | * |
| 2292 | * @param queue Address of the queue. |
| 2293 | * @param prev Address of the previous data item. |
| 2294 | * @param data Address of the data item. |
| 2295 | * |
| 2296 | * @return N/A |
| 2297 | */ |
| 2298 | extern void k_queue_insert(struct k_queue *queue, void *prev, void *data); |
| 2299 | |
| 2300 | /** |
| 2301 | * @brief Atomically append a list of elements to a queue. |
| 2302 | * |
| 2303 | * This routine adds a list of data items to @a queue in one operation. |
Nicolas Pitre | 659fa0d | 2019-05-21 22:13:01 -0400 | [diff] [blame] | 2304 | * The data items must be in a singly-linked list, with the first word |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2305 | * in each data item pointing to the next data item; the list must be |
| 2306 | * NULL-terminated. |
| 2307 | * |
| 2308 | * @note Can be called by ISRs. |
| 2309 | * |
| 2310 | * @param queue Address of the queue. |
| 2311 | * @param head Pointer to first node in singly-linked list. |
| 2312 | * @param tail Pointer to last node in singly-linked list. |
| 2313 | * |
Anas Nashif | 756d8b0 | 2019-06-16 09:53:55 -0400 | [diff] [blame] | 2314 | * @retval 0 on success |
| 2315 | * @retval -EINVAL on invalid supplied data |
| 2316 | * |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2317 | */ |
Anas Nashif | 756d8b0 | 2019-06-16 09:53:55 -0400 | [diff] [blame] | 2318 | extern int k_queue_append_list(struct k_queue *queue, void *head, void *tail); |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2319 | |
| 2320 | /** |
| 2321 | * @brief Atomically add a list of elements to a queue. |
| 2322 | * |
| 2323 | * This routine adds a list of data items to @a queue in one operation. |
| 2324 | * The data items must be in a singly-linked list implemented using a |
| 2325 | * sys_slist_t object. Upon completion, the original list is empty. |
| 2326 | * |
| 2327 | * @note Can be called by ISRs. |
| 2328 | * |
| 2329 | * @param queue Address of the queue. |
| 2330 | * @param list Pointer to sys_slist_t object. |
| 2331 | * |
Anas Nashif | 756d8b0 | 2019-06-16 09:53:55 -0400 | [diff] [blame] | 2332 | * @retval 0 on success |
| 2333 | * @retval -EINVAL on invalid data |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2334 | */ |
Anas Nashif | 756d8b0 | 2019-06-16 09:53:55 -0400 | [diff] [blame] | 2335 | extern int k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list); |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2336 | |
| 2337 | /** |
| 2338 | * @brief Get an element from a queue. |
| 2339 | * |
Nicolas Pitre | 659fa0d | 2019-05-21 22:13:01 -0400 | [diff] [blame] | 2340 | * This routine removes first data item from @a queue. The first word of the |
| 2341 | * data item is reserved for the kernel's use. |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2342 | * |
| 2343 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 2344 | * |
| 2345 | * @param queue Address of the queue. |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 2346 | * @param timeout Non-negative waiting period to obtain a data item |
| 2347 | * or one of the special values K_NO_WAIT and |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 2348 | * K_FOREVER. |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2349 | * |
| 2350 | * @return Address of the data item if successful; NULL if returned |
| 2351 | * without waiting, or waiting period timed out. |
| 2352 | */ |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 2353 | __syscall void *k_queue_get(struct k_queue *queue, k_timeout_t timeout); |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2354 | |
| 2355 | /** |
Luiz Augusto von Dentz | 50b9377 | 2017-07-03 16:52:45 +0300 | [diff] [blame] | 2356 | * @brief Remove an element from a queue. |
| 2357 | * |
Nicolas Pitre | 659fa0d | 2019-05-21 22:13:01 -0400 | [diff] [blame] | 2358 | * This routine removes data item from @a queue. The first word of the |
| 2359 | * data item is reserved for the kernel's use. Removing elements from k_queue |
Luiz Augusto von Dentz | 50b9377 | 2017-07-03 16:52:45 +0300 | [diff] [blame] | 2360 | * rely on sys_slist_find_and_remove which is not a constant time operation. |
| 2361 | * |
| 2362 | * @note Can be called by ISRs |
| 2363 | * |
| 2364 | * @param queue Address of the queue. |
| 2365 | * @param data Address of the data item. |
| 2366 | * |
| 2367 | * @return true if data item was removed |
| 2368 | */ |
| 2369 | static inline bool k_queue_remove(struct k_queue *queue, void *data) |
| 2370 | { |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2371 | return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data); |
Luiz Augusto von Dentz | 50b9377 | 2017-07-03 16:52:45 +0300 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | /** |
Dhananjay Gundapu Jayakrishnan | 24bfa40 | 2018-08-22 12:33:00 +0200 | [diff] [blame] | 2375 | * @brief Append an element to a queue only if it's not present already. |
| 2376 | * |
Nicolas Pitre | 659fa0d | 2019-05-21 22:13:01 -0400 | [diff] [blame] | 2377 | * This routine appends data item to @a queue. The first word of the data |
| 2378 | * item is reserved for the kernel's use. Appending elements to k_queue |
Dhananjay Gundapu Jayakrishnan | 24bfa40 | 2018-08-22 12:33:00 +0200 | [diff] [blame] | 2379 | * relies on sys_slist_is_node_in_list which is not a constant time operation. |
| 2380 | * |
| 2381 | * @note Can be called by ISRs |
| 2382 | * |
| 2383 | * @param queue Address of the queue. |
| 2384 | * @param data Address of the data item. |
| 2385 | * |
| 2386 | * @return true if data item was added, false if not |
| 2387 | */ |
| 2388 | static inline bool k_queue_unique_append(struct k_queue *queue, void *data) |
| 2389 | { |
| 2390 | sys_sfnode_t *test; |
| 2391 | |
| 2392 | SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) { |
| 2393 | if (test == (sys_sfnode_t *) data) { |
| 2394 | return false; |
| 2395 | } |
| 2396 | } |
| 2397 | |
| 2398 | k_queue_append(queue, data); |
| 2399 | return true; |
| 2400 | } |
| 2401 | |
| 2402 | /** |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2403 | * @brief Query a queue to see if it has data available. |
| 2404 | * |
| 2405 | * Note that the data might be already gone by the time this function returns |
| 2406 | * if other threads are also trying to read from the queue. |
| 2407 | * |
| 2408 | * @note Can be called by ISRs. |
| 2409 | * |
| 2410 | * @param queue Address of the queue. |
| 2411 | * |
| 2412 | * @return Non-zero if the queue is empty. |
| 2413 | * @return 0 if data is available. |
| 2414 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2415 | __syscall int k_queue_is_empty(struct k_queue *queue); |
| 2416 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2417 | static inline int z_impl_k_queue_is_empty(struct k_queue *queue) |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2418 | { |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2419 | return (int)sys_sflist_is_empty(&queue->data_q); |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | /** |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2423 | * @brief Peek element at the head of queue. |
| 2424 | * |
| 2425 | * Return element from the head of queue without removing it. |
| 2426 | * |
| 2427 | * @param queue Address of the queue. |
| 2428 | * |
| 2429 | * @return Head element, or NULL if queue is empty. |
| 2430 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2431 | __syscall void *k_queue_peek_head(struct k_queue *queue); |
| 2432 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2433 | static inline void *z_impl_k_queue_peek_head(struct k_queue *queue) |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2434 | { |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2435 | return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false); |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2436 | } |
| 2437 | |
| 2438 | /** |
| 2439 | * @brief Peek element at the tail of queue. |
| 2440 | * |
| 2441 | * Return element from the tail of queue without removing it. |
| 2442 | * |
| 2443 | * @param queue Address of the queue. |
| 2444 | * |
| 2445 | * @return Tail element, or NULL if queue is empty. |
| 2446 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2447 | __syscall void *k_queue_peek_tail(struct k_queue *queue); |
| 2448 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2449 | static inline void *z_impl_k_queue_peek_tail(struct k_queue *queue) |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2450 | { |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2451 | return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false); |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2452 | } |
| 2453 | |
| 2454 | /** |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2455 | * @brief Statically define and initialize a queue. |
| 2456 | * |
| 2457 | * The queue can be accessed outside the module where it is defined using: |
| 2458 | * |
| 2459 | * @code extern struct k_queue <name>; @endcode |
| 2460 | * |
| 2461 | * @param name Name of the queue. |
| 2462 | */ |
| 2463 | #define K_QUEUE_DEFINE(name) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 2464 | Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \ |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 2465 | Z_QUEUE_INITIALIZER(name) |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2466 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 2467 | /** @} */ |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2468 | |
Wentong Wu | 5611e92 | 2019-06-20 23:51:27 +0800 | [diff] [blame] | 2469 | #ifdef CONFIG_USERSPACE |
| 2470 | /** |
| 2471 | * @brief futex structure |
| 2472 | * |
| 2473 | * A k_futex is a lightweight mutual exclusion primitive designed |
| 2474 | * to minimize kernel involvement. Uncontended operation relies |
| 2475 | * only on atomic access to shared memory. k_futex are tracked as |
| 2476 | * kernel objects and can live in user memory so any access bypass |
| 2477 | * the kernel object permission management mechanism. |
| 2478 | */ |
| 2479 | struct k_futex { |
| 2480 | atomic_t val; |
| 2481 | }; |
| 2482 | |
| 2483 | /** |
| 2484 | * @brief futex kernel data structure |
| 2485 | * |
| 2486 | * z_futex_data are the helper data structure for k_futex to complete |
| 2487 | * futex contended operation on kernel side, structure z_futex_data |
| 2488 | * of every futex object is invisible in user mode. |
| 2489 | */ |
| 2490 | struct z_futex_data { |
| 2491 | _wait_q_t wait_q; |
| 2492 | struct k_spinlock lock; |
| 2493 | }; |
| 2494 | |
| 2495 | #define Z_FUTEX_DATA_INITIALIZER(obj) \ |
| 2496 | { \ |
| 2497 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q) \ |
| 2498 | } |
| 2499 | |
| 2500 | /** |
| 2501 | * @defgroup futex_apis FUTEX APIs |
| 2502 | * @ingroup kernel_apis |
| 2503 | * @{ |
| 2504 | */ |
| 2505 | |
| 2506 | /** |
Wentong Wu | 5611e92 | 2019-06-20 23:51:27 +0800 | [diff] [blame] | 2507 | * @brief Pend the current thread on a futex |
| 2508 | * |
| 2509 | * Tests that the supplied futex contains the expected value, and if so, |
| 2510 | * goes to sleep until some other thread calls k_futex_wake() on it. |
| 2511 | * |
| 2512 | * @param futex Address of the futex. |
| 2513 | * @param expected Expected value of the futex, if it is different the caller |
| 2514 | * will not wait on it. |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 2515 | * @param timeout Non-negative waiting period on the futex, or |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 2516 | * one of the special values K_NO_WAIT or K_FOREVER. |
Wentong Wu | 5611e92 | 2019-06-20 23:51:27 +0800 | [diff] [blame] | 2517 | * @retval -EACCES Caller does not have read access to futex address. |
| 2518 | * @retval -EAGAIN If the futex value did not match the expected parameter. |
| 2519 | * @retval -EINVAL Futex parameter address not recognized by the kernel. |
| 2520 | * @retval -ETIMEDOUT Thread woke up due to timeout and not a futex wakeup. |
| 2521 | * @retval 0 if the caller went to sleep and was woken up. The caller |
| 2522 | * should check the futex's value on wakeup to determine if it needs |
| 2523 | * to block again. |
| 2524 | */ |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 2525 | __syscall int k_futex_wait(struct k_futex *futex, int expected, |
| 2526 | k_timeout_t timeout); |
Wentong Wu | 5611e92 | 2019-06-20 23:51:27 +0800 | [diff] [blame] | 2527 | |
| 2528 | /** |
| 2529 | * @brief Wake one/all threads pending on a futex |
| 2530 | * |
| 2531 | * Wake up the highest priority thread pending on the supplied futex, or |
| 2532 | * wakeup all the threads pending on the supplied futex, and the behavior |
| 2533 | * depends on wake_all. |
| 2534 | * |
| 2535 | * @param futex Futex to wake up pending threads. |
| 2536 | * @param wake_all If true, wake up all pending threads; If false, |
| 2537 | * wakeup the highest priority thread. |
| 2538 | * @retval -EACCES Caller does not have access to the futex address. |
| 2539 | * @retval -EINVAL Futex parameter address not recognized by the kernel. |
| 2540 | * @retval Number of threads that were woken up. |
| 2541 | */ |
| 2542 | __syscall int k_futex_wake(struct k_futex *futex, bool wake_all); |
| 2543 | |
| 2544 | /** @} */ |
| 2545 | #endif |
| 2546 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2547 | struct k_fifo { |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2548 | struct k_queue _queue; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2549 | }; |
| 2550 | |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2551 | /** |
| 2552 | * @cond INTERNAL_HIDDEN |
| 2553 | */ |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 2554 | #define Z_FIFO_INITIALIZER(obj) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2555 | { \ |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 2556 | ._queue = Z_QUEUE_INITIALIZER(obj._queue) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2557 | } |
| 2558 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 2559 | #define K_FIFO_INITIALIZER __DEPRECATED_MACRO Z_FIFO_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2560 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2561 | /** |
| 2562 | * INTERNAL_HIDDEN @endcond |
| 2563 | */ |
| 2564 | |
| 2565 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2566 | * @defgroup fifo_apis FIFO APIs |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2567 | * @ingroup kernel_apis |
| 2568 | * @{ |
| 2569 | */ |
| 2570 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2571 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2572 | * @brief Initialize a FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2573 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2574 | * This routine initializes a FIFO queue, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2575 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2576 | * @param fifo Address of the FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2577 | * |
| 2578 | * @return N/A |
| 2579 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2580 | #define k_fifo_init(fifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2581 | k_queue_init(&(fifo)->_queue) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2582 | |
| 2583 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2584 | * @brief Cancel waiting on a FIFO queue. |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 2585 | * |
| 2586 | * This routine causes first thread pending on @a fifo, if any, to |
| 2587 | * return from k_fifo_get() call with NULL value (as if timeout |
| 2588 | * expired). |
| 2589 | * |
| 2590 | * @note Can be called by ISRs. |
| 2591 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2592 | * @param fifo Address of the FIFO queue. |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 2593 | * |
| 2594 | * @return N/A |
| 2595 | */ |
| 2596 | #define k_fifo_cancel_wait(fifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2597 | k_queue_cancel_wait(&(fifo)->_queue) |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 2598 | |
| 2599 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2600 | * @brief Add an element to a FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2601 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2602 | * This routine adds a data item to @a fifo. A FIFO data item must be |
Nicolas Pitre | 659fa0d | 2019-05-21 22:13:01 -0400 | [diff] [blame] | 2603 | * aligned on a word boundary, and the first word of the item is reserved |
| 2604 | * for the kernel's use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2605 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2606 | * @note Can be called by ISRs. |
| 2607 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2608 | * @param fifo Address of the FIFO. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2609 | * @param data Address of the data item. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2610 | * |
| 2611 | * @return N/A |
| 2612 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2613 | #define k_fifo_put(fifo, data) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2614 | k_queue_append(&(fifo)->_queue, data) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2615 | |
| 2616 | /** |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2617 | * @brief Add an element to a FIFO queue. |
| 2618 | * |
Andrew Boie | ac3dcc1 | 2019-04-01 12:28:03 -0700 | [diff] [blame] | 2619 | * This routine adds a data item to @a fifo. There is an implicit memory |
| 2620 | * allocation to create an additional temporary bookkeeping data structure from |
| 2621 | * the calling thread's resource pool, which is automatically freed when the |
| 2622 | * item is removed. The data itself is not copied. |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2623 | * |
| 2624 | * @note Can be called by ISRs. |
| 2625 | * |
| 2626 | * @param fifo Address of the FIFO. |
| 2627 | * @param data Address of the data item. |
| 2628 | * |
| 2629 | * @retval 0 on success |
| 2630 | * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool |
| 2631 | */ |
| 2632 | #define k_fifo_alloc_put(fifo, data) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2633 | k_queue_alloc_append(&(fifo)->_queue, data) |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2634 | |
| 2635 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2636 | * @brief Atomically add a list of elements to a FIFO. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2637 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2638 | * This routine adds a list of data items to @a fifo in one operation. |
Nicolas Pitre | 659fa0d | 2019-05-21 22:13:01 -0400 | [diff] [blame] | 2639 | * The data items must be in a singly-linked list, with the first word of |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2640 | * each data item pointing to the next data item; the list must be |
| 2641 | * NULL-terminated. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2642 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2643 | * @note Can be called by ISRs. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2644 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2645 | * @param fifo Address of the FIFO queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2646 | * @param head Pointer to first node in singly-linked list. |
| 2647 | * @param tail Pointer to last node in singly-linked list. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2648 | * |
| 2649 | * @return N/A |
| 2650 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2651 | #define k_fifo_put_list(fifo, head, tail) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2652 | k_queue_append_list(&(fifo)->_queue, head, tail) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2653 | |
| 2654 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2655 | * @brief Atomically add a list of elements to a FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2656 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2657 | * This routine adds a list of data items to @a fifo in one operation. |
| 2658 | * The data items must be in a singly-linked list implemented using a |
| 2659 | * sys_slist_t object. Upon completion, the sys_slist_t object is invalid |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2660 | * and must be re-initialized via sys_slist_init(). |
| 2661 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2662 | * @note Can be called by ISRs. |
| 2663 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2664 | * @param fifo Address of the FIFO queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2665 | * @param list Pointer to sys_slist_t object. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2666 | * |
| 2667 | * @return N/A |
| 2668 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2669 | #define k_fifo_put_slist(fifo, list) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2670 | k_queue_merge_slist(&(fifo)->_queue, list) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2671 | |
| 2672 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2673 | * @brief Get an element from a FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2674 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2675 | * This routine removes a data item from @a fifo in a "first in, first out" |
Nicolas Pitre | 659fa0d | 2019-05-21 22:13:01 -0400 | [diff] [blame] | 2676 | * manner. The first word of the data item is reserved for the kernel's use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2677 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2678 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 2679 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2680 | * @param fifo Address of the FIFO queue. |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 2681 | * @param timeout Waiting period to obtain a data item, |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2682 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 2683 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2684 | * @return Address of the data item if successful; NULL if returned |
| 2685 | * without waiting, or waiting period timed out. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2686 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2687 | #define k_fifo_get(fifo, timeout) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2688 | k_queue_get(&(fifo)->_queue, timeout) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2689 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2690 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2691 | * @brief Query a FIFO queue to see if it has data available. |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2692 | * |
| 2693 | * Note that the data might be already gone by the time this function returns |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2694 | * if other threads is also trying to read from the FIFO. |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2695 | * |
| 2696 | * @note Can be called by ISRs. |
| 2697 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2698 | * @param fifo Address of the FIFO queue. |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2699 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2700 | * @return Non-zero if the FIFO queue is empty. |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2701 | * @return 0 if data is available. |
| 2702 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2703 | #define k_fifo_is_empty(fifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2704 | k_queue_is_empty(&(fifo)->_queue) |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2705 | |
| 2706 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2707 | * @brief Peek element at the head of a FIFO queue. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2708 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2709 | * Return element from the head of FIFO queue without removing it. A usecase |
Ramakrishna Pallala | 92489ea | 2018-03-29 22:44:23 +0530 | [diff] [blame] | 2710 | * for this is if elements of the FIFO object are themselves containers. Then |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2711 | * on each iteration of processing, a head container will be peeked, |
| 2712 | * and some data processed out of it, and only if the container is empty, |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2713 | * it will be completely remove from the FIFO queue. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2714 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2715 | * @param fifo Address of the FIFO queue. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2716 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2717 | * @return Head element, or NULL if the FIFO queue is empty. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2718 | */ |
| 2719 | #define k_fifo_peek_head(fifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2720 | k_queue_peek_head(&(fifo)->_queue) |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2721 | |
| 2722 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2723 | * @brief Peek element at the tail of FIFO queue. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2724 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2725 | * Return element from the tail of FIFO queue (without removing it). A usecase |
| 2726 | * for this is if elements of the FIFO queue are themselves containers. Then |
| 2727 | * it may be useful to add more data to the last container in a FIFO queue. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2728 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2729 | * @param fifo Address of the FIFO queue. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2730 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2731 | * @return Tail element, or NULL if a FIFO queue is empty. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2732 | */ |
| 2733 | #define k_fifo_peek_tail(fifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2734 | k_queue_peek_tail(&(fifo)->_queue) |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2735 | |
| 2736 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2737 | * @brief Statically define and initialize a FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2738 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2739 | * The FIFO queue can be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2740 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 2741 | * @code extern struct k_fifo <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2742 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2743 | * @param name Name of the FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2744 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2745 | #define K_FIFO_DEFINE(name) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 2746 | Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \ |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 2747 | Z_FIFO_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2748 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 2749 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2750 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2751 | struct k_lifo { |
Luiz Augusto von Dentz | 0dc4dd4 | 2017-02-21 15:49:52 +0200 | [diff] [blame] | 2752 | struct k_queue _queue; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2753 | }; |
| 2754 | |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2755 | /** |
| 2756 | * @cond INTERNAL_HIDDEN |
| 2757 | */ |
| 2758 | |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 2759 | #define Z_LIFO_INITIALIZER(obj) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2760 | { \ |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 2761 | ._queue = Z_QUEUE_INITIALIZER(obj._queue) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2762 | } |
| 2763 | |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 2764 | #define K_LIFO_INITIALIZER __DEPRECATED_MACRO Z_LIFO_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2765 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2766 | /** |
| 2767 | * INTERNAL_HIDDEN @endcond |
| 2768 | */ |
| 2769 | |
| 2770 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2771 | * @defgroup lifo_apis LIFO APIs |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2772 | * @ingroup kernel_apis |
| 2773 | * @{ |
| 2774 | */ |
| 2775 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2776 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2777 | * @brief Initialize a LIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2778 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2779 | * This routine initializes a LIFO queue object, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2780 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2781 | * @param lifo Address of the LIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2782 | * |
| 2783 | * @return N/A |
| 2784 | */ |
Luiz Augusto von Dentz | 0dc4dd4 | 2017-02-21 15:49:52 +0200 | [diff] [blame] | 2785 | #define k_lifo_init(lifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2786 | k_queue_init(&(lifo)->_queue) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2787 | |
| 2788 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2789 | * @brief Add an element to a LIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2790 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2791 | * This routine adds a data item to @a lifo. A LIFO queue data item must be |
Nicolas Pitre | 659fa0d | 2019-05-21 22:13:01 -0400 | [diff] [blame] | 2792 | * aligned on a word boundary, and the first word of the item is |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2793 | * reserved for the kernel's use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2794 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2795 | * @note Can be called by ISRs. |
| 2796 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2797 | * @param lifo Address of the LIFO queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2798 | * @param data Address of the data item. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2799 | * |
| 2800 | * @return N/A |
| 2801 | */ |
Luiz Augusto von Dentz | 0dc4dd4 | 2017-02-21 15:49:52 +0200 | [diff] [blame] | 2802 | #define k_lifo_put(lifo, data) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2803 | k_queue_prepend(&(lifo)->_queue, data) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2804 | |
| 2805 | /** |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2806 | * @brief Add an element to a LIFO queue. |
| 2807 | * |
Andrew Boie | ac3dcc1 | 2019-04-01 12:28:03 -0700 | [diff] [blame] | 2808 | * This routine adds a data item to @a lifo. There is an implicit memory |
| 2809 | * allocation to create an additional temporary bookkeeping data structure from |
| 2810 | * the calling thread's resource pool, which is automatically freed when the |
| 2811 | * item is removed. The data itself is not copied. |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2812 | * |
| 2813 | * @note Can be called by ISRs. |
| 2814 | * |
| 2815 | * @param lifo Address of the LIFO. |
| 2816 | * @param data Address of the data item. |
| 2817 | * |
| 2818 | * @retval 0 on success |
| 2819 | * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool |
| 2820 | */ |
| 2821 | #define k_lifo_alloc_put(lifo, data) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2822 | k_queue_alloc_prepend(&(lifo)->_queue, data) |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2823 | |
| 2824 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2825 | * @brief Get an element from a LIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2826 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2827 | * This routine removes a data item from @a lifo in a "last in, first out" |
Nicolas Pitre | 659fa0d | 2019-05-21 22:13:01 -0400 | [diff] [blame] | 2828 | * manner. The first word of the data item is reserved for the kernel's use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2829 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2830 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 2831 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2832 | * @param lifo Address of the LIFO queue. |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 2833 | * @param timeout Waiting period to obtain a data item, |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2834 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 2835 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2836 | * @return Address of the data item if successful; NULL if returned |
| 2837 | * without waiting, or waiting period timed out. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2838 | */ |
Luiz Augusto von Dentz | 0dc4dd4 | 2017-02-21 15:49:52 +0200 | [diff] [blame] | 2839 | #define k_lifo_get(lifo, timeout) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2840 | k_queue_get(&(lifo)->_queue, timeout) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2841 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2842 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2843 | * @brief Statically define and initialize a LIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2844 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2845 | * The LIFO queue can be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2846 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 2847 | * @code extern struct k_lifo <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2848 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2849 | * @param name Name of the fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2850 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2851 | #define K_LIFO_DEFINE(name) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 2852 | Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \ |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 2853 | Z_LIFO_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2854 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 2855 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2856 | |
| 2857 | /** |
| 2858 | * @cond INTERNAL_HIDDEN |
| 2859 | */ |
Adithya Baglody | 28080d3 | 2018-10-15 11:48:51 +0530 | [diff] [blame] | 2860 | #define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2861 | |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2862 | typedef uintptr_t stack_data_t; |
| 2863 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2864 | struct k_stack { |
| 2865 | _wait_q_t wait_q; |
Andy Ross | f0933d0 | 2018-07-26 10:23:02 -0700 | [diff] [blame] | 2866 | struct k_spinlock lock; |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2867 | stack_data_t *base, *next, *top; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2868 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 2869 | _OBJECT_TRACING_NEXT_PTR(k_stack) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 2870 | _OBJECT_TRACING_LINKED_FLAG |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2871 | u8_t flags; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2872 | }; |
| 2873 | |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 2874 | #define Z_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2875 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2876 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2877 | .base = stack_buffer, \ |
| 2878 | .next = stack_buffer, \ |
| 2879 | .top = stack_buffer + stack_num_entries, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 2880 | _OBJECT_TRACING_INIT \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2881 | } |
| 2882 | |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 2883 | #define K_STACK_INITIALIZER __DEPRECATED_MACRO Z_STACK_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2884 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2885 | /** |
| 2886 | * INTERNAL_HIDDEN @endcond |
| 2887 | */ |
| 2888 | |
| 2889 | /** |
| 2890 | * @defgroup stack_apis Stack APIs |
| 2891 | * @ingroup kernel_apis |
| 2892 | * @{ |
| 2893 | */ |
| 2894 | |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2895 | /** |
| 2896 | * @brief Initialize a stack. |
| 2897 | * |
| 2898 | * This routine initializes a stack object, prior to its first use. |
| 2899 | * |
| 2900 | * @param stack Address of the stack. |
| 2901 | * @param buffer Address of array used to hold stacked values. |
| 2902 | * @param num_entries Maximum number of values that can be stacked. |
| 2903 | * |
| 2904 | * @return N/A |
| 2905 | */ |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2906 | void k_stack_init(struct k_stack *stack, |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2907 | stack_data_t *buffer, u32_t num_entries); |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2908 | |
| 2909 | |
| 2910 | /** |
| 2911 | * @brief Initialize a stack. |
| 2912 | * |
| 2913 | * This routine initializes a stack object, prior to its first use. Internal |
| 2914 | * buffers will be allocated from the calling thread's resource pool. |
| 2915 | * This memory will be released if k_stack_cleanup() is called, or |
| 2916 | * userspace is enabled and the stack object loses all references to it. |
| 2917 | * |
| 2918 | * @param stack Address of the stack. |
| 2919 | * @param num_entries Maximum number of values that can be stacked. |
| 2920 | * |
| 2921 | * @return -ENOMEM if memory couldn't be allocated |
| 2922 | */ |
| 2923 | |
Adithya Baglody | 28080d3 | 2018-10-15 11:48:51 +0530 | [diff] [blame] | 2924 | __syscall s32_t k_stack_alloc_init(struct k_stack *stack, |
| 2925 | u32_t num_entries); |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2926 | |
| 2927 | /** |
| 2928 | * @brief Release a stack's allocated buffer |
| 2929 | * |
| 2930 | * If a stack object was given a dynamically allocated buffer via |
| 2931 | * k_stack_alloc_init(), this will free it. This function does nothing |
| 2932 | * if the buffer wasn't dynamically allocated. |
| 2933 | * |
| 2934 | * @param stack Address of the stack. |
Anas Nashif | 1ed67d1 | 2019-06-16 08:58:10 -0400 | [diff] [blame] | 2935 | * @retval 0 on success |
| 2936 | * @retval -EAGAIN when object is still in use |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2937 | */ |
Anas Nashif | 1ed67d1 | 2019-06-16 08:58:10 -0400 | [diff] [blame] | 2938 | int k_stack_cleanup(struct k_stack *stack); |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2939 | |
| 2940 | /** |
| 2941 | * @brief Push an element onto a stack. |
| 2942 | * |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2943 | * This routine adds a stack_data_t value @a data to @a stack. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2944 | * |
| 2945 | * @note Can be called by ISRs. |
| 2946 | * |
| 2947 | * @param stack Address of the stack. |
| 2948 | * @param data Value to push onto the stack. |
| 2949 | * |
Anas Nashif | 1ed67d1 | 2019-06-16 08:58:10 -0400 | [diff] [blame] | 2950 | * @retval 0 on success |
| 2951 | * @retval -ENOMEM if stack is full |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2952 | */ |
Anas Nashif | 1ed67d1 | 2019-06-16 08:58:10 -0400 | [diff] [blame] | 2953 | __syscall int k_stack_push(struct k_stack *stack, stack_data_t data); |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2954 | |
| 2955 | /** |
| 2956 | * @brief Pop an element from a stack. |
| 2957 | * |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2958 | * This routine removes a stack_data_t value from @a stack in a "last in, |
| 2959 | * first out" manner and stores the value in @a data. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2960 | * |
| 2961 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 2962 | * |
| 2963 | * @param stack Address of the stack. |
| 2964 | * @param data Address of area to hold the value popped from the stack. |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 2965 | * @param timeout Waiting period to obtain a value, |
| 2966 | * or one of the special values K_NO_WAIT and |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 2967 | * K_FOREVER. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2968 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2969 | * @retval 0 Element popped from stack. |
| 2970 | * @retval -EBUSY Returned without waiting. |
| 2971 | * @retval -EAGAIN Waiting period timed out. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2972 | */ |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 2973 | __syscall int k_stack_pop(struct k_stack *stack, stack_data_t *data, |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 2974 | k_timeout_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2975 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2976 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2977 | * @brief Statically define and initialize a stack |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2978 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2979 | * The stack can be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2980 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 2981 | * @code extern struct k_stack <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2982 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2983 | * @param name Name of the stack. |
| 2984 | * @param stack_num_entries Maximum number of values that can be stacked. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2985 | */ |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 2986 | #define K_STACK_DEFINE(name, stack_num_entries) \ |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2987 | stack_data_t __noinit \ |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 2988 | _k_stack_buf_##name[stack_num_entries]; \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 2989 | Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \ |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 2990 | Z_STACK_INITIALIZER(name, _k_stack_buf_##name, \ |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 2991 | stack_num_entries) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2992 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 2993 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2994 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2995 | struct k_work; |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 2996 | struct k_work_poll; |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2997 | |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 2998 | /* private, used by k_poll and k_work_poll */ |
Piotr Zięcik | 1c4177d | 2019-08-27 12:19:26 +0200 | [diff] [blame] | 2999 | typedef int (*_poller_cb_t)(struct k_poll_event *event, u32_t state); |
| 3000 | struct _poller { |
| 3001 | volatile bool is_polling; |
| 3002 | struct k_thread *thread; |
| 3003 | _poller_cb_t cb; |
| 3004 | }; |
| 3005 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3006 | /** |
Anas Nashif | 29f37f0 | 2019-01-21 14:30:35 -0500 | [diff] [blame] | 3007 | * @addtogroup thread_apis |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3008 | * @{ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3009 | */ |
| 3010 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3011 | /** |
| 3012 | * @typedef k_work_handler_t |
| 3013 | * @brief Work item handler function type. |
| 3014 | * |
| 3015 | * A work item's handler function is executed by a workqueue's thread |
| 3016 | * when the work item is processed by the workqueue. |
| 3017 | * |
| 3018 | * @param work Address of the work item. |
| 3019 | * |
| 3020 | * @return N/A |
| 3021 | */ |
| 3022 | typedef void (*k_work_handler_t)(struct k_work *work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3023 | |
| 3024 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3025 | * @cond INTERNAL_HIDDEN |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3026 | */ |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3027 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3028 | struct k_work_q { |
Luiz Augusto von Dentz | adb581b | 2017-07-03 19:09:44 +0300 | [diff] [blame] | 3029 | struct k_queue queue; |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 3030 | struct k_thread thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3031 | }; |
| 3032 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3033 | enum { |
Iván Briano | 9c7b5ea | 2016-10-04 18:11:05 -0300 | [diff] [blame] | 3034 | K_WORK_STATE_PENDING, /* Work item pending state */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3035 | }; |
| 3036 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3037 | struct k_work { |
Luiz Augusto von Dentz | adb581b | 2017-07-03 19:09:44 +0300 | [diff] [blame] | 3038 | void *_reserved; /* Used by k_queue implementation. */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3039 | k_work_handler_t handler; |
| 3040 | atomic_t flags[1]; |
| 3041 | }; |
| 3042 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3043 | struct k_delayed_work { |
| 3044 | struct k_work work; |
| 3045 | struct _timeout timeout; |
| 3046 | struct k_work_q *work_q; |
| 3047 | }; |
| 3048 | |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 3049 | struct k_work_poll { |
| 3050 | struct k_work work; |
| 3051 | struct _poller poller; |
| 3052 | struct k_poll_event *events; |
| 3053 | int num_events; |
| 3054 | k_work_handler_t real_handler; |
| 3055 | struct _timeout timeout; |
| 3056 | int poll_result; |
| 3057 | }; |
| 3058 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3059 | extern struct k_work_q k_sys_work_q; |
| 3060 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3061 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3062 | * INTERNAL_HIDDEN @endcond |
| 3063 | */ |
| 3064 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3065 | #define Z_WORK_INITIALIZER(work_handler) \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3066 | { \ |
| 3067 | ._reserved = NULL, \ |
| 3068 | .handler = work_handler, \ |
| 3069 | .flags = { 0 } \ |
| 3070 | } |
| 3071 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 3072 | #define K_WORK_INITIALIZER __DEPRECATED_MACRO Z_WORK_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3073 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3074 | /** |
| 3075 | * @brief Initialize a statically-defined work item. |
| 3076 | * |
| 3077 | * This macro can be used to initialize a statically-defined workqueue work |
| 3078 | * item, prior to its first use. For example, |
| 3079 | * |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3080 | * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3081 | * |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3082 | * @param work Symbol name for work item object |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3083 | * @param work_handler Function to invoke each time work item is processed. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3084 | */ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3085 | #define K_WORK_DEFINE(work, work_handler) \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3086 | struct k_work work = Z_WORK_INITIALIZER(work_handler) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3087 | |
| 3088 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3089 | * @brief Initialize a work item. |
| 3090 | * |
| 3091 | * This routine initializes a workqueue work item, prior to its first use. |
| 3092 | * |
| 3093 | * @param work Address of work item. |
| 3094 | * @param handler Function to invoke each time work item is processed. |
| 3095 | * |
| 3096 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3097 | */ |
| 3098 | static inline void k_work_init(struct k_work *work, k_work_handler_t handler) |
| 3099 | { |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3100 | *work = (struct k_work)Z_WORK_INITIALIZER(handler); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3101 | } |
| 3102 | |
| 3103 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3104 | * @brief Submit a work item. |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 3105 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3106 | * This routine submits work item @a work to be processed by workqueue |
| 3107 | * @a work_q. If the work item is already pending in the workqueue's queue |
| 3108 | * as a result of an earlier submission, this routine has no effect on the |
| 3109 | * work item. If the work item has already been processed, or is currently |
| 3110 | * being processed, its work is considered complete and the work item can be |
| 3111 | * resubmitted. |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 3112 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3113 | * @warning |
| 3114 | * A submitted work item must not be modified until it has been processed |
| 3115 | * by the workqueue. |
| 3116 | * |
| 3117 | * @note Can be called by ISRs. |
| 3118 | * |
| 3119 | * @param work_q Address of workqueue. |
| 3120 | * @param work Address of work item. |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 3121 | * |
| 3122 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3123 | */ |
| 3124 | static inline void k_work_submit_to_queue(struct k_work_q *work_q, |
| 3125 | struct k_work *work) |
| 3126 | { |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 3127 | if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) { |
Luiz Augusto von Dentz | c1fa82b | 2017-07-03 19:24:10 +0300 | [diff] [blame] | 3128 | k_queue_append(&work_q->queue, work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3129 | } |
| 3130 | } |
| 3131 | |
| 3132 | /** |
Andrew Boie | 2b1d54e | 2018-11-12 14:25:19 -0800 | [diff] [blame] | 3133 | * @brief Submit a work item to a user mode workqueue |
| 3134 | * |
David B. Kinder | 06d7835 | 2018-12-17 14:32:40 -0800 | [diff] [blame] | 3135 | * Submits a work item to a workqueue that runs in user mode. A temporary |
Andrew Boie | 2b1d54e | 2018-11-12 14:25:19 -0800 | [diff] [blame] | 3136 | * memory allocation is made from the caller's resource pool which is freed |
| 3137 | * once the worker thread consumes the k_work item. The workqueue |
| 3138 | * thread must have memory access to the k_work item being submitted. The caller |
| 3139 | * must have permission granted on the work_q parameter's queue object. |
| 3140 | * |
| 3141 | * Otherwise this works the same as k_work_submit_to_queue(). |
| 3142 | * |
| 3143 | * @note Can be called by ISRs. |
| 3144 | * |
| 3145 | * @param work_q Address of workqueue. |
| 3146 | * @param work Address of work item. |
| 3147 | * |
| 3148 | * @retval -EBUSY if the work item was already in some workqueue |
| 3149 | * @retval -ENOMEM if no memory for thread resource pool allocation |
| 3150 | * @retval 0 Success |
Andrew Boie | 2b1d54e | 2018-11-12 14:25:19 -0800 | [diff] [blame] | 3151 | */ |
| 3152 | static inline int k_work_submit_to_user_queue(struct k_work_q *work_q, |
| 3153 | struct k_work *work) |
| 3154 | { |
| 3155 | int ret = -EBUSY; |
| 3156 | |
| 3157 | if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) { |
| 3158 | ret = k_queue_alloc_append(&work_q->queue, work); |
| 3159 | |
| 3160 | /* Couldn't insert into the queue. Clear the pending bit |
| 3161 | * so the work item can be submitted again |
| 3162 | */ |
Flavio Ceolin | 76b3518 | 2018-12-16 12:48:29 -0800 | [diff] [blame] | 3163 | if (ret != 0) { |
Andrew Boie | 2b1d54e | 2018-11-12 14:25:19 -0800 | [diff] [blame] | 3164 | atomic_clear_bit(work->flags, K_WORK_STATE_PENDING); |
| 3165 | } |
| 3166 | } |
| 3167 | |
| 3168 | return ret; |
| 3169 | } |
| 3170 | |
| 3171 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3172 | * @brief Check if a work item is pending. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3173 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3174 | * This routine indicates if work item @a work is pending in a workqueue's |
| 3175 | * queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3176 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3177 | * @note Can be called by ISRs. |
| 3178 | * |
| 3179 | * @param work Address of work item. |
| 3180 | * |
Flavio Ceolin | 82ef4f8 | 2018-11-21 18:12:34 -0800 | [diff] [blame] | 3181 | * @return true if work item is pending, or false if it is not pending. |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 3182 | */ |
Flavio Ceolin | 82ef4f8 | 2018-11-21 18:12:34 -0800 | [diff] [blame] | 3183 | static inline bool k_work_pending(struct k_work *work) |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 3184 | { |
Iván Briano | 9c7b5ea | 2016-10-04 18:11:05 -0300 | [diff] [blame] | 3185 | return atomic_test_bit(work->flags, K_WORK_STATE_PENDING); |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 3186 | } |
| 3187 | |
| 3188 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3189 | * @brief Start a workqueue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3190 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3191 | * This routine starts workqueue @a work_q. The workqueue spawns its work |
| 3192 | * processing thread, which runs forever. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3193 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3194 | * @param work_q Address of workqueue. |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 3195 | * @param stack Pointer to work queue thread's stack space, as defined by |
| 3196 | * K_THREAD_STACK_DEFINE() |
| 3197 | * @param stack_size Size of the work queue thread's stack (in bytes), which |
| 3198 | * should either be the same constant passed to |
| 3199 | * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF(). |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3200 | * @param prio Priority of the work queue's thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3201 | * |
| 3202 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3203 | */ |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 3204 | extern void k_work_q_start(struct k_work_q *work_q, |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 3205 | k_thread_stack_t *stack, |
Benjamin Walsh | 669360d | 2016-11-14 16:46:14 -0500 | [diff] [blame] | 3206 | size_t stack_size, int prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3207 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3208 | /** |
Andrew Boie | 2b1d54e | 2018-11-12 14:25:19 -0800 | [diff] [blame] | 3209 | * @brief Start a workqueue in user mode |
| 3210 | * |
| 3211 | * This works identically to k_work_q_start() except it is callable from user |
| 3212 | * mode, and the worker thread created will run in user mode. |
| 3213 | * The caller must have permissions granted on both the work_q parameter's |
| 3214 | * thread and queue objects, and the same restrictions on priority apply as |
| 3215 | * k_thread_create(). |
| 3216 | * |
| 3217 | * @param work_q Address of workqueue. |
| 3218 | * @param stack Pointer to work queue thread's stack space, as defined by |
| 3219 | * K_THREAD_STACK_DEFINE() |
| 3220 | * @param stack_size Size of the work queue thread's stack (in bytes), which |
| 3221 | * should either be the same constant passed to |
| 3222 | * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF(). |
| 3223 | * @param prio Priority of the work queue's thread. |
| 3224 | * |
| 3225 | * @return N/A |
Andrew Boie | 2b1d54e | 2018-11-12 14:25:19 -0800 | [diff] [blame] | 3226 | */ |
| 3227 | extern void k_work_q_user_start(struct k_work_q *work_q, |
| 3228 | k_thread_stack_t *stack, |
| 3229 | size_t stack_size, int prio); |
| 3230 | |
| 3231 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3232 | * @brief Initialize a delayed work item. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3233 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3234 | * This routine initializes a workqueue delayed work item, prior to |
| 3235 | * its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3236 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3237 | * @param work Address of delayed work item. |
| 3238 | * @param handler Function to invoke each time work item is processed. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3239 | * |
| 3240 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3241 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 3242 | extern void k_delayed_work_init(struct k_delayed_work *work, |
| 3243 | k_work_handler_t handler); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3244 | |
| 3245 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3246 | * @brief Submit a delayed work item. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3247 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3248 | * This routine schedules work item @a work to be processed by workqueue |
| 3249 | * @a work_q after a delay of @a delay milliseconds. The routine initiates |
David B. Kinder | 8b986d7 | 2017-04-18 15:56:26 -0700 | [diff] [blame] | 3250 | * an asynchronous countdown for the work item and then returns to the caller. |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3251 | * Only when the countdown completes is the work item actually submitted to |
| 3252 | * the workqueue and becomes pending. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3253 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3254 | * Submitting a previously submitted delayed work item that is still |
Andy Ross | 03c1d28 | 2018-02-13 12:13:25 -0800 | [diff] [blame] | 3255 | * counting down cancels the existing submission and restarts the |
| 3256 | * countdown using the new delay. Note that this behavior is |
| 3257 | * inherently subject to race conditions with the pre-existing |
| 3258 | * timeouts and work queue, so care must be taken to synchronize such |
| 3259 | * resubmissions externally. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3260 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3261 | * @warning |
| 3262 | * A delayed work item must not be modified until it has been processed |
| 3263 | * by the workqueue. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3264 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3265 | * @note Can be called by ISRs. |
| 3266 | * |
| 3267 | * @param work_q Address of workqueue. |
| 3268 | * @param work Address of delayed work item. |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 3269 | * @param delay Delay before submitting the work item |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3270 | * |
| 3271 | * @retval 0 Work item countdown started. |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3272 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 3273 | * @retval -EADDRINUSE Work item is pending on a different workqueue. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3274 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 3275 | extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q, |
| 3276 | struct k_delayed_work *work, |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 3277 | k_timeout_t delay); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3278 | |
| 3279 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3280 | * @brief Cancel a delayed work item. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3281 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3282 | * This routine cancels the submission of delayed work item @a work. |
David B. Kinder | 8b986d7 | 2017-04-18 15:56:26 -0700 | [diff] [blame] | 3283 | * A delayed work item can only be canceled while its countdown is still |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3284 | * underway. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3285 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3286 | * @note Can be called by ISRs. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3287 | * |
Andy Ross | d7ae2a8 | 2019-03-08 08:51:13 -0800 | [diff] [blame] | 3288 | * @note The result of calling this on a k_delayed_work item that has |
| 3289 | * not been submitted (i.e. before the return of the |
| 3290 | * k_delayed_work_submit_to_queue() call) is undefined. |
| 3291 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3292 | * @param work Address of delayed work item. |
| 3293 | * |
David B. Kinder | 8b986d7 | 2017-04-18 15:56:26 -0700 | [diff] [blame] | 3294 | * @retval 0 Work item countdown canceled. |
Luiz Augusto von Dentz | 038d727 | 2020-02-13 11:57:38 -0800 | [diff] [blame] | 3295 | * @retval -EINVAL Work item is being processed. |
| 3296 | * @retval -EALREADY Work item has already been completed. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3297 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 3298 | extern int k_delayed_work_cancel(struct k_delayed_work *work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3299 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3300 | /** |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3301 | * @brief Submit a work item to the system workqueue. |
| 3302 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3303 | * This routine submits work item @a work to be processed by the system |
| 3304 | * workqueue. If the work item is already pending in the workqueue's queue |
| 3305 | * as a result of an earlier submission, this routine has no effect on the |
| 3306 | * work item. If the work item has already been processed, or is currently |
| 3307 | * being processed, its work is considered complete and the work item can be |
| 3308 | * resubmitted. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3309 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3310 | * @warning |
| 3311 | * Work items submitted to the system workqueue should avoid using handlers |
| 3312 | * that block or yield since this may prevent the system workqueue from |
| 3313 | * processing other work items in a timely manner. |
| 3314 | * |
| 3315 | * @note Can be called by ISRs. |
| 3316 | * |
| 3317 | * @param work Address of work item. |
| 3318 | * |
| 3319 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3320 | */ |
| 3321 | static inline void k_work_submit(struct k_work *work) |
| 3322 | { |
| 3323 | k_work_submit_to_queue(&k_sys_work_q, work); |
| 3324 | } |
| 3325 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3326 | /** |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3327 | * @brief Submit a delayed work item to the system workqueue. |
| 3328 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3329 | * This routine schedules work item @a work to be processed by the system |
| 3330 | * workqueue after a delay of @a delay milliseconds. The routine initiates |
David B. Kinder | 8b986d7 | 2017-04-18 15:56:26 -0700 | [diff] [blame] | 3331 | * an asynchronous countdown for the work item and then returns to the caller. |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3332 | * Only when the countdown completes is the work item actually submitted to |
| 3333 | * the workqueue and becomes pending. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3334 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3335 | * Submitting a previously submitted delayed work item that is still |
| 3336 | * counting down cancels the existing submission and restarts the countdown |
| 3337 | * using the new delay. If the work item is currently pending on the |
| 3338 | * workqueue's queue because the countdown has completed it is too late to |
| 3339 | * resubmit the item, and resubmission fails without impacting the work item. |
| 3340 | * If the work item has already been processed, or is currently being processed, |
| 3341 | * its work is considered complete and the work item can be resubmitted. |
| 3342 | * |
| 3343 | * @warning |
| 3344 | * Work items submitted to the system workqueue should avoid using handlers |
| 3345 | * that block or yield since this may prevent the system workqueue from |
| 3346 | * processing other work items in a timely manner. |
| 3347 | * |
| 3348 | * @note Can be called by ISRs. |
| 3349 | * |
| 3350 | * @param work Address of delayed work item. |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 3351 | * @param delay Delay before submitting the work item |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3352 | * |
| 3353 | * @retval 0 Work item countdown started. |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3354 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 3355 | * @retval -EADDRINUSE Work item is pending on a different workqueue. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3356 | */ |
| 3357 | static inline int k_delayed_work_submit(struct k_delayed_work *work, |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 3358 | k_timeout_t delay) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3359 | { |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 3360 | return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3361 | } |
| 3362 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3363 | /** |
Andy Ross | 5a5d3da | 2020-03-09 13:59:15 -0700 | [diff] [blame] | 3364 | * @brief Get time when a delayed work will be scheduled |
| 3365 | * |
| 3366 | * This routine computes the system uptime when a delayed work gets |
| 3367 | * executed. If the delayed work is not waiting to be scheduled, it |
| 3368 | * returns current system time. |
| 3369 | * |
| 3370 | * @param work Delayed work item. |
| 3371 | * |
| 3372 | * @return Uptime of execution (in ticks). |
Andy Ross | 5a5d3da | 2020-03-09 13:59:15 -0700 | [diff] [blame] | 3373 | */ |
| 3374 | static inline k_ticks_t k_delayed_work_expires_ticks( |
| 3375 | struct k_delayed_work *work) |
| 3376 | { |
| 3377 | return z_timeout_expires(&work->timeout); |
| 3378 | } |
| 3379 | |
| 3380 | /** |
Andy Ross | e39bf29 | 2020-03-19 10:30:33 -0700 | [diff] [blame] | 3381 | * @brief Get time remaining before a delayed work gets scheduled, in |
| 3382 | * system ticks |
Andy Ross | 5a5d3da | 2020-03-09 13:59:15 -0700 | [diff] [blame] | 3383 | * |
| 3384 | * This routine computes the time remaining before a delayed work gets |
| 3385 | * executed. If the delayed work is not waiting to be scheduled, it |
| 3386 | * returns zero. |
| 3387 | * |
| 3388 | * @param work Delayed work item. |
| 3389 | * |
| 3390 | * @return Remaining time (in ticks). |
Andy Ross | 5a5d3da | 2020-03-09 13:59:15 -0700 | [diff] [blame] | 3391 | */ |
| 3392 | static inline k_ticks_t k_delayed_work_remaining_ticks( |
| 3393 | struct k_delayed_work *work) |
| 3394 | { |
| 3395 | return z_timeout_remaining(&work->timeout); |
| 3396 | } |
| 3397 | |
| 3398 | /** |
Johan Hedberg | c8201b2 | 2016-12-09 10:42:22 +0200 | [diff] [blame] | 3399 | * @brief Get time remaining before a delayed work gets scheduled. |
| 3400 | * |
| 3401 | * This routine computes the (approximate) time remaining before a |
| 3402 | * delayed work gets executed. If the delayed work is not waiting to be |
Paul Sokolovsky | e25df54 | 2017-12-28 15:40:21 +0200 | [diff] [blame] | 3403 | * scheduled, it returns zero. |
Johan Hedberg | c8201b2 | 2016-12-09 10:42:22 +0200 | [diff] [blame] | 3404 | * |
| 3405 | * @param work Delayed work item. |
| 3406 | * |
| 3407 | * @return Remaining time (in milliseconds). |
| 3408 | */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3409 | static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work) |
Johan Hedberg | c8201b2 | 2016-12-09 10:42:22 +0200 | [diff] [blame] | 3410 | { |
Andy Ross | 5a5d3da | 2020-03-09 13:59:15 -0700 | [diff] [blame] | 3411 | return k_ticks_to_ms_floor32(z_timeout_remaining(&work->timeout)); |
Johan Hedberg | c8201b2 | 2016-12-09 10:42:22 +0200 | [diff] [blame] | 3412 | } |
| 3413 | |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 3414 | /** |
| 3415 | * @brief Initialize a triggered work item. |
| 3416 | * |
| 3417 | * This routine initializes a workqueue triggered work item, prior to |
| 3418 | * its first use. |
| 3419 | * |
| 3420 | * @param work Address of triggered work item. |
| 3421 | * @param handler Function to invoke each time work item is processed. |
| 3422 | * |
| 3423 | * @return N/A |
| 3424 | */ |
| 3425 | extern void k_work_poll_init(struct k_work_poll *work, |
| 3426 | k_work_handler_t handler); |
| 3427 | |
| 3428 | /** |
| 3429 | * @brief Submit a triggered work item. |
| 3430 | * |
| 3431 | * This routine schedules work item @a work to be processed by workqueue |
| 3432 | * @a work_q when one of the given @a events is signaled. The routine |
| 3433 | * initiates internal poller for the work item and then returns to the caller. |
| 3434 | * Only when one of the watched events happen the work item is actually |
| 3435 | * submitted to the workqueue and becomes pending. |
| 3436 | * |
| 3437 | * Submitting a previously submitted triggered work item that is still |
| 3438 | * waiting for the event cancels the existing submission and reschedules it |
| 3439 | * the using the new event list. Note that this behavior is inherently subject |
David B. Kinder | 73896c0 | 2019-10-28 16:27:57 -0700 | [diff] [blame] | 3440 | * to race conditions with the pre-existing triggered work item and work queue, |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 3441 | * so care must be taken to synchronize such resubmissions externally. |
| 3442 | * |
| 3443 | * @note Can be called by ISRs. |
| 3444 | * |
| 3445 | * @warning |
| 3446 | * Provided array of events as well as a triggered work item must be placed |
| 3447 | * in persistent memory (valid until work handler execution or work |
| 3448 | * cancellation) and cannot be modified after submission. |
| 3449 | * |
| 3450 | * @param work_q Address of workqueue. |
| 3451 | * @param work Address of delayed work item. |
| 3452 | * @param events An array of pointers to events which trigger the work. |
| 3453 | * @param num_events The number of events in the array. |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 3454 | * @param timeout Timeout after which the work will be scheduled |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3455 | * for execution even if not triggered. |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 3456 | * |
| 3457 | * |
| 3458 | * @retval 0 Work item started watching for events. |
| 3459 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 3460 | * @retval -EADDRINUSE Work item is pending on a different workqueue. |
| 3461 | */ |
| 3462 | extern int k_work_poll_submit_to_queue(struct k_work_q *work_q, |
| 3463 | struct k_work_poll *work, |
| 3464 | struct k_poll_event *events, |
| 3465 | int num_events, |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 3466 | k_timeout_t timeout); |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 3467 | |
| 3468 | /** |
| 3469 | * @brief Submit a triggered work item to the system workqueue. |
| 3470 | * |
| 3471 | * This routine schedules work item @a work to be processed by system |
| 3472 | * workqueue when one of the given @a events is signaled. The routine |
| 3473 | * initiates internal poller for the work item and then returns to the caller. |
| 3474 | * Only when one of the watched events happen the work item is actually |
| 3475 | * submitted to the workqueue and becomes pending. |
| 3476 | * |
| 3477 | * Submitting a previously submitted triggered work item that is still |
| 3478 | * waiting for the event cancels the existing submission and reschedules it |
| 3479 | * the using the new event list. Note that this behavior is inherently subject |
David B. Kinder | 73896c0 | 2019-10-28 16:27:57 -0700 | [diff] [blame] | 3480 | * to race conditions with the pre-existing triggered work item and work queue, |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 3481 | * so care must be taken to synchronize such resubmissions externally. |
| 3482 | * |
| 3483 | * @note Can be called by ISRs. |
| 3484 | * |
| 3485 | * @warning |
| 3486 | * Provided array of events as well as a triggered work item must not be |
| 3487 | * modified until the item has been processed by the workqueue. |
| 3488 | * |
| 3489 | * @param work Address of delayed work item. |
| 3490 | * @param events An array of pointers to events which trigger the work. |
| 3491 | * @param num_events The number of events in the array. |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 3492 | * @param timeout Timeout after which the work will be scheduled |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3493 | * for execution even if not triggered. |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 3494 | * |
| 3495 | * @retval 0 Work item started watching for events. |
| 3496 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 3497 | * @retval -EADDRINUSE Work item is pending on a different workqueue. |
| 3498 | */ |
| 3499 | static inline int k_work_poll_submit(struct k_work_poll *work, |
| 3500 | struct k_poll_event *events, |
| 3501 | int num_events, |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 3502 | k_timeout_t timeout) |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 3503 | { |
| 3504 | return k_work_poll_submit_to_queue(&k_sys_work_q, work, |
| 3505 | events, num_events, timeout); |
| 3506 | } |
| 3507 | |
| 3508 | /** |
| 3509 | * @brief Cancel a triggered work item. |
| 3510 | * |
| 3511 | * This routine cancels the submission of triggered work item @a work. |
| 3512 | * A triggered work item can only be canceled if no event triggered work |
| 3513 | * submission. |
| 3514 | * |
| 3515 | * @note Can be called by ISRs. |
| 3516 | * |
| 3517 | * @param work Address of delayed work item. |
| 3518 | * |
David B. Kinder | 73896c0 | 2019-10-28 16:27:57 -0700 | [diff] [blame] | 3519 | * @retval 0 Work item canceled. |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 3520 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 3521 | */ |
| 3522 | extern int k_work_poll_cancel(struct k_work_poll *work); |
| 3523 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 3524 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3525 | /** |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3526 | * @defgroup mutex_apis Mutex APIs |
| 3527 | * @ingroup kernel_apis |
| 3528 | * @{ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3529 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3530 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3531 | /** |
| 3532 | * Mutex Structure |
| 3533 | * @ingroup mutex_apis |
| 3534 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3535 | struct k_mutex { |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3536 | /** Mutex wait queue */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3537 | _wait_q_t wait_q; |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3538 | /** Mutex owner */ |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 3539 | struct k_thread *owner; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3540 | |
| 3541 | /** Current lock count */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3542 | u32_t lock_count; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3543 | |
| 3544 | /** Original thread priority */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3545 | int owner_orig_prio; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3546 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 3547 | _OBJECT_TRACING_NEXT_PTR(k_mutex) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 3548 | _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3549 | }; |
| 3550 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3551 | /** |
| 3552 | * @cond INTERNAL_HIDDEN |
| 3553 | */ |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 3554 | #define Z_MUTEX_INITIALIZER(obj) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3555 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3556 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3557 | .owner = NULL, \ |
| 3558 | .lock_count = 0, \ |
| 3559 | .owner_orig_prio = K_LOWEST_THREAD_PRIO, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 3560 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3561 | } |
| 3562 | |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 3563 | #define K_MUTEX_INITIALIZER __DEPRECATED_MACRO Z_MUTEX_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3564 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3565 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3566 | * INTERNAL_HIDDEN @endcond |
| 3567 | */ |
| 3568 | |
| 3569 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3570 | * @brief Statically define and initialize a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3571 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3572 | * The mutex can be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3573 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 3574 | * @code extern struct k_mutex <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3575 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3576 | * @param name Name of the mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3577 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3578 | #define K_MUTEX_DEFINE(name) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 3579 | Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \ |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 3580 | Z_MUTEX_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3581 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3582 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3583 | * @brief Initialize a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3584 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3585 | * This routine initializes a mutex object, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3586 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3587 | * Upon completion, the mutex is available and does not have an owner. |
| 3588 | * |
| 3589 | * @param mutex Address of the mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3590 | * |
Anas Nashif | 86bb2d0 | 2019-05-04 10:18:13 -0400 | [diff] [blame] | 3591 | * @retval 0 Mutex object created |
| 3592 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3593 | */ |
Anas Nashif | 86bb2d0 | 2019-05-04 10:18:13 -0400 | [diff] [blame] | 3594 | __syscall int k_mutex_init(struct k_mutex *mutex); |
| 3595 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3596 | |
| 3597 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3598 | * @brief Lock a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3599 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3600 | * This routine locks @a mutex. If the mutex is locked by another thread, |
| 3601 | * the calling thread waits until the mutex becomes available or until |
| 3602 | * a timeout occurs. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3603 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3604 | * A thread is permitted to lock a mutex it has already locked. The operation |
| 3605 | * completes immediately and the lock count is increased by 1. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3606 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3607 | * @param mutex Address of the mutex. |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 3608 | * @param timeout Waiting period to lock the mutex, |
| 3609 | * or one of the special values K_NO_WAIT and |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3610 | * K_FOREVER. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3611 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3612 | * @retval 0 Mutex locked. |
| 3613 | * @retval -EBUSY Returned without waiting. |
| 3614 | * @retval -EAGAIN Waiting period timed out. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3615 | */ |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 3616 | __syscall int k_mutex_lock(struct k_mutex *mutex, k_timeout_t timeout); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3617 | |
| 3618 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3619 | * @brief Unlock a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3620 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3621 | * This routine unlocks @a mutex. The mutex must already be locked by the |
| 3622 | * calling thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3623 | * |
| 3624 | * The mutex cannot be claimed by another thread until it has been unlocked by |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3625 | * the calling thread as many times as it was previously locked by that |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3626 | * thread. |
| 3627 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3628 | * @param mutex Address of the mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3629 | * |
Anas Nashif | 86bb2d0 | 2019-05-04 10:18:13 -0400 | [diff] [blame] | 3630 | * @retval 0 Mutex unlocked. |
| 3631 | * @retval -EPERM The current thread does not own the mutex |
| 3632 | * @retval -EINVAL The mutex is not locked |
| 3633 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3634 | */ |
Anas Nashif | 86bb2d0 | 2019-05-04 10:18:13 -0400 | [diff] [blame] | 3635 | __syscall int k_mutex_unlock(struct k_mutex *mutex); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3636 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3637 | /** |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 3638 | * @} |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3639 | */ |
| 3640 | |
| 3641 | /** |
| 3642 | * @cond INTERNAL_HIDDEN |
| 3643 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3644 | |
| 3645 | struct k_sem { |
| 3646 | _wait_q_t wait_q; |
Adithya Baglody | 4b06621 | 2018-10-16 11:59:12 +0530 | [diff] [blame] | 3647 | u32_t count; |
| 3648 | u32_t limit; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 3649 | _POLL_EVENT; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3650 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 3651 | _OBJECT_TRACING_NEXT_PTR(k_sem) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 3652 | _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3653 | }; |
| 3654 | |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 3655 | #define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3656 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3657 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3658 | .count = initial_count, \ |
| 3659 | .limit = count_limit, \ |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 3660 | _POLL_EVENT_OBJ_INIT(obj) \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 3661 | _OBJECT_TRACING_INIT \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3662 | } |
| 3663 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 3664 | #define K_SEM_INITIALIZER __DEPRECATED_MACRO Z_SEM_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3665 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3666 | /** |
| 3667 | * INTERNAL_HIDDEN @endcond |
| 3668 | */ |
| 3669 | |
| 3670 | /** |
| 3671 | * @defgroup semaphore_apis Semaphore APIs |
| 3672 | * @ingroup kernel_apis |
| 3673 | * @{ |
| 3674 | */ |
| 3675 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3676 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3677 | * @brief Initialize a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3678 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3679 | * This routine initializes a semaphore object, prior to its first use. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3680 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3681 | * @param sem Address of the semaphore. |
| 3682 | * @param initial_count Initial semaphore count. |
| 3683 | * @param limit Maximum permitted semaphore count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3684 | * |
Anas Nashif | 928af3c | 2019-05-04 10:36:14 -0400 | [diff] [blame] | 3685 | * @retval 0 Semaphore created successfully |
| 3686 | * @retval -EINVAL Invalid values |
| 3687 | * |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3688 | */ |
Anas Nashif | 928af3c | 2019-05-04 10:36:14 -0400 | [diff] [blame] | 3689 | __syscall int k_sem_init(struct k_sem *sem, unsigned int initial_count, |
Andrew Boie | 9928023 | 2017-09-29 14:17:47 -0700 | [diff] [blame] | 3690 | unsigned int limit); |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3691 | |
| 3692 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3693 | * @brief Take a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3694 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3695 | * This routine takes @a sem. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3696 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3697 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 3698 | * |
| 3699 | * @param sem Address of the semaphore. |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 3700 | * @param timeout Waiting period to take the semaphore, |
| 3701 | * or one of the special values K_NO_WAIT and K_FOREVER. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3702 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3703 | * @retval 0 Semaphore taken. |
| 3704 | * @retval -EBUSY Returned without waiting. |
| 3705 | * @retval -EAGAIN Waiting period timed out. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3706 | */ |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 3707 | __syscall int k_sem_take(struct k_sem *sem, k_timeout_t timeout); |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3708 | |
| 3709 | /** |
| 3710 | * @brief Give a semaphore. |
| 3711 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3712 | * This routine gives @a sem, unless the semaphore is already at its maximum |
| 3713 | * permitted count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3714 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3715 | * @note Can be called by ISRs. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3716 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3717 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3718 | * |
| 3719 | * @return N/A |
| 3720 | */ |
Andrew Boie | 9928023 | 2017-09-29 14:17:47 -0700 | [diff] [blame] | 3721 | __syscall void k_sem_give(struct k_sem *sem); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3722 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3723 | /** |
| 3724 | * @brief Reset a semaphore's count to zero. |
| 3725 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3726 | * This routine sets the count of @a sem to zero. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3727 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3728 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3729 | * |
| 3730 | * @return N/A |
| 3731 | */ |
Andrew Boie | 990bf16 | 2017-10-03 12:36:49 -0700 | [diff] [blame] | 3732 | __syscall void k_sem_reset(struct k_sem *sem); |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 3733 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 3734 | /** |
| 3735 | * @internal |
| 3736 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3737 | static inline void z_impl_k_sem_reset(struct k_sem *sem) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3738 | { |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 3739 | sem->count = 0U; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3740 | } |
| 3741 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3742 | /** |
| 3743 | * @brief Get a semaphore's count. |
| 3744 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3745 | * This routine returns the current count of @a sem. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3746 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3747 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3748 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3749 | * @return Current semaphore count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3750 | */ |
Andrew Boie | 990bf16 | 2017-10-03 12:36:49 -0700 | [diff] [blame] | 3751 | __syscall unsigned int k_sem_count_get(struct k_sem *sem); |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 3752 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 3753 | /** |
| 3754 | * @internal |
| 3755 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3756 | static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3757 | { |
| 3758 | return sem->count; |
| 3759 | } |
| 3760 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3761 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3762 | * @brief Statically define and initialize a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3763 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3764 | * The semaphore can be accessed outside the module where it is defined using: |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3765 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 3766 | * @code extern struct k_sem <name>; @endcode |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3767 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3768 | * @param name Name of the semaphore. |
| 3769 | * @param initial_count Initial semaphore count. |
| 3770 | * @param count_limit Maximum permitted semaphore count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3771 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3772 | #define K_SEM_DEFINE(name, initial_count, count_limit) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 3773 | Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \ |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 3774 | Z_SEM_INITIALIZER(name, initial_count, count_limit); \ |
Rajavardhan Gundi | 68040c8 | 2018-04-27 10:15:15 +0530 | [diff] [blame] | 3775 | BUILD_ASSERT(((count_limit) != 0) && \ |
| 3776 | ((initial_count) <= (count_limit))); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3777 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 3778 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3779 | |
| 3780 | /** |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3781 | * @defgroup msgq_apis Message Queue APIs |
| 3782 | * @ingroup kernel_apis |
| 3783 | * @{ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3784 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3785 | |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3786 | /** |
| 3787 | * @brief Message Queue Structure |
| 3788 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3789 | struct k_msgq { |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3790 | /** Message queue wait queue */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3791 | _wait_q_t wait_q; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3792 | /** Lock */ |
Andy Ross | be03dbd | 2018-07-26 10:23:02 -0700 | [diff] [blame] | 3793 | struct k_spinlock lock; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3794 | /** Message size */ |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 3795 | size_t msg_size; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3796 | /** Maximal number of messages */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3797 | u32_t max_msgs; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3798 | /** Start of message buffer */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3799 | char *buffer_start; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3800 | /** End of message buffer */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3801 | char *buffer_end; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3802 | /** Read pointer */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3803 | char *read_ptr; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3804 | /** Write pointer */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3805 | char *write_ptr; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3806 | /** Number of used messages */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3807 | u32_t used_msgs; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3808 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 3809 | _OBJECT_TRACING_NEXT_PTR(k_msgq) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 3810 | _OBJECT_TRACING_LINKED_FLAG |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3811 | |
| 3812 | /** Message queue */ |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 3813 | u8_t flags; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3814 | }; |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3815 | /** |
| 3816 | * @cond INTERNAL_HIDDEN |
| 3817 | */ |
| 3818 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3819 | |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 3820 | #define Z_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3821 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3822 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3823 | .msg_size = q_msg_size, \ |
Charles E. Youse | 6d01f67 | 2019-03-18 10:27:34 -0700 | [diff] [blame] | 3824 | .max_msgs = q_max_msgs, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3825 | .buffer_start = q_buffer, \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3826 | .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3827 | .read_ptr = q_buffer, \ |
| 3828 | .write_ptr = q_buffer, \ |
| 3829 | .used_msgs = 0, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 3830 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3831 | } |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 3832 | #define K_MSGQ_INITIALIZER __DEPRECATED_MACRO Z_MSGQ_INITIALIZER |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3833 | /** |
| 3834 | * INTERNAL_HIDDEN @endcond |
| 3835 | */ |
| 3836 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3837 | |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 3838 | #define K_MSGQ_FLAG_ALLOC BIT(0) |
| 3839 | |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3840 | /** |
| 3841 | * @brief Message Queue Attributes |
| 3842 | */ |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 3843 | struct k_msgq_attrs { |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3844 | /** Message Size */ |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 3845 | size_t msg_size; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3846 | /** Maximal number of messages */ |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 3847 | u32_t max_msgs; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3848 | /** Used messages */ |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 3849 | u32_t used_msgs; |
| 3850 | }; |
| 3851 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3852 | |
| 3853 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3854 | * @brief Statically define and initialize a message queue. |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3855 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3856 | * The message queue's ring buffer contains space for @a q_max_msgs messages, |
| 3857 | * each of which is @a q_msg_size bytes long. The buffer is aligned to a |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 3858 | * @a q_align -byte boundary, which must be a power of 2. To ensure that each |
| 3859 | * message is similarly aligned to this boundary, @a q_msg_size must also be |
| 3860 | * a multiple of @a q_align. |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3861 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3862 | * The message queue can be accessed outside the module where it is defined |
| 3863 | * using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3864 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 3865 | * @code extern struct k_msgq <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3866 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3867 | * @param q_name Name of the message queue. |
| 3868 | * @param q_msg_size Message size (in bytes). |
| 3869 | * @param q_max_msgs Maximum number of messages that can be queued. |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 3870 | * @param q_align Alignment of the message queue's ring buffer. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3871 | * |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3872 | */ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 3873 | #define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \ |
| 3874 | static char __noinit __aligned(q_align) \ |
| 3875 | _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \ |
| 3876 | Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \ |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 3877 | Z_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3878 | q_msg_size, q_max_msgs) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3879 | |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3880 | /** |
| 3881 | * @brief Initialize a message queue. |
| 3882 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3883 | * This routine initializes a message queue object, prior to its first use. |
| 3884 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 3885 | * The message queue's ring buffer must contain space for @a max_msgs messages, |
| 3886 | * each of which is @a msg_size bytes long. The buffer must be aligned to an |
| 3887 | * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure |
| 3888 | * that each message is similarly aligned to this boundary, @a q_msg_size |
| 3889 | * must also be a multiple of N. |
| 3890 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3891 | * @param q Address of the message queue. |
| 3892 | * @param buffer Pointer to ring buffer that holds queued messages. |
| 3893 | * @param msg_size Message size (in bytes). |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3894 | * @param max_msgs Maximum number of messages that can be queued. |
| 3895 | * |
| 3896 | * @return N/A |
| 3897 | */ |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 3898 | void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size, |
| 3899 | u32_t max_msgs); |
| 3900 | |
| 3901 | /** |
| 3902 | * @brief Initialize a message queue. |
| 3903 | * |
| 3904 | * This routine initializes a message queue object, prior to its first use, |
| 3905 | * allocating its internal ring buffer from the calling thread's resource |
| 3906 | * pool. |
| 3907 | * |
| 3908 | * Memory allocated for the ring buffer can be released by calling |
| 3909 | * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses |
| 3910 | * all of its references. |
| 3911 | * |
Anas Nashif | 4b38659 | 2019-11-25 09:30:47 -0500 | [diff] [blame] | 3912 | * @param msgq Address of the message queue. |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 3913 | * @param msg_size Message size (in bytes). |
| 3914 | * @param max_msgs Maximum number of messages that can be queued. |
| 3915 | * |
| 3916 | * @return 0 on success, -ENOMEM if there was insufficient memory in the |
| 3917 | * thread's resource pool, or -EINVAL if the size parameters cause |
| 3918 | * an integer overflow. |
| 3919 | */ |
Anas Nashif | 4b38659 | 2019-11-25 09:30:47 -0500 | [diff] [blame] | 3920 | __syscall int k_msgq_alloc_init(struct k_msgq *msgq, size_t msg_size, |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 3921 | u32_t max_msgs); |
| 3922 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3923 | /** |
Anas Nashif | 4b38659 | 2019-11-25 09:30:47 -0500 | [diff] [blame] | 3924 | * @brief Release allocated buffer for a queue |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3925 | * |
| 3926 | * Releases memory allocated for the ring buffer. |
Anas Nashif | 4b38659 | 2019-11-25 09:30:47 -0500 | [diff] [blame] | 3927 | * |
| 3928 | * @param msgq message queue to cleanup |
| 3929 | * |
Anas Nashif | 11b9365 | 2019-06-16 08:43:48 -0400 | [diff] [blame] | 3930 | * @retval 0 on success |
| 3931 | * @retval -EBUSY Queue not empty |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3932 | */ |
Anas Nashif | 11b9365 | 2019-06-16 08:43:48 -0400 | [diff] [blame] | 3933 | int k_msgq_cleanup(struct k_msgq *msgq); |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3934 | |
| 3935 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3936 | * @brief Send a message to a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3937 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3938 | * This routine sends a message to message queue @a q. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3939 | * |
Benjamin Walsh | 8215ce1 | 2016-11-09 19:45:19 -0500 | [diff] [blame] | 3940 | * @note Can be called by ISRs. |
| 3941 | * |
Anas Nashif | 4b38659 | 2019-11-25 09:30:47 -0500 | [diff] [blame] | 3942 | * @param msgq Address of the message queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3943 | * @param data Pointer to the message. |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 3944 | * @param timeout Non-negative waiting period to add the message, |
| 3945 | * or one of the special values K_NO_WAIT and |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3946 | * K_FOREVER. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3947 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3948 | * @retval 0 Message sent. |
| 3949 | * @retval -ENOMSG Returned without waiting or queue purged. |
| 3950 | * @retval -EAGAIN Waiting period timed out. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3951 | */ |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 3952 | __syscall int k_msgq_put(struct k_msgq *msgq, void *data, k_timeout_t timeout); |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3953 | |
| 3954 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3955 | * @brief Receive a message from a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3956 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3957 | * This routine receives a message from message queue @a q in a "first in, |
| 3958 | * first out" manner. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3959 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3960 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
Benjamin Walsh | 8215ce1 | 2016-11-09 19:45:19 -0500 | [diff] [blame] | 3961 | * |
Anas Nashif | 4b38659 | 2019-11-25 09:30:47 -0500 | [diff] [blame] | 3962 | * @param msgq Address of the message queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3963 | * @param data Address of area to hold the received message. |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 3964 | * @param timeout Waiting period to receive the message, |
| 3965 | * or one of the special values K_NO_WAIT and |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3966 | * K_FOREVER. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3967 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3968 | * @retval 0 Message received. |
| 3969 | * @retval -ENOMSG Returned without waiting. |
| 3970 | * @retval -EAGAIN Waiting period timed out. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3971 | */ |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 3972 | __syscall int k_msgq_get(struct k_msgq *msgq, void *data, k_timeout_t timeout); |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3973 | |
| 3974 | /** |
Sathish Kuttan | 3efd8e1 | 2018-11-09 21:03:10 -0800 | [diff] [blame] | 3975 | * @brief Peek/read a message from a message queue. |
| 3976 | * |
| 3977 | * This routine reads a message from message queue @a q in a "first in, |
| 3978 | * first out" manner and leaves the message in the queue. |
| 3979 | * |
| 3980 | * @note Can be called by ISRs. |
| 3981 | * |
Anas Nashif | 4b38659 | 2019-11-25 09:30:47 -0500 | [diff] [blame] | 3982 | * @param msgq Address of the message queue. |
Sathish Kuttan | 3efd8e1 | 2018-11-09 21:03:10 -0800 | [diff] [blame] | 3983 | * @param data Address of area to hold the message read from the queue. |
| 3984 | * |
| 3985 | * @retval 0 Message read. |
| 3986 | * @retval -ENOMSG Returned when the queue has no message. |
Sathish Kuttan | 3efd8e1 | 2018-11-09 21:03:10 -0800 | [diff] [blame] | 3987 | */ |
Anas Nashif | 4b38659 | 2019-11-25 09:30:47 -0500 | [diff] [blame] | 3988 | __syscall int k_msgq_peek(struct k_msgq *msgq, void *data); |
Sathish Kuttan | 3efd8e1 | 2018-11-09 21:03:10 -0800 | [diff] [blame] | 3989 | |
| 3990 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3991 | * @brief Purge a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3992 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3993 | * This routine discards all unreceived messages in a message queue's ring |
| 3994 | * buffer. Any threads that are blocked waiting to send a message to the |
| 3995 | * message queue are unblocked and see an -ENOMSG error code. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3996 | * |
Anas Nashif | 4b38659 | 2019-11-25 09:30:47 -0500 | [diff] [blame] | 3997 | * @param msgq Address of the message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3998 | * |
| 3999 | * @return N/A |
| 4000 | */ |
Anas Nashif | 4b38659 | 2019-11-25 09:30:47 -0500 | [diff] [blame] | 4001 | __syscall void k_msgq_purge(struct k_msgq *msgq); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4002 | |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 4003 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4004 | * @brief Get the amount of free space in a message queue. |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 4005 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4006 | * This routine returns the number of unused entries in a message queue's |
| 4007 | * ring buffer. |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 4008 | * |
Anas Nashif | 4b38659 | 2019-11-25 09:30:47 -0500 | [diff] [blame] | 4009 | * @param msgq Address of the message queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4010 | * |
| 4011 | * @return Number of unused ring buffer entries. |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 4012 | */ |
Anas Nashif | 4b38659 | 2019-11-25 09:30:47 -0500 | [diff] [blame] | 4013 | __syscall u32_t k_msgq_num_free_get(struct k_msgq *msgq); |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 4014 | |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 4015 | /** |
| 4016 | * @brief Get basic attributes of a message queue. |
| 4017 | * |
| 4018 | * This routine fetches basic attributes of message queue into attr argument. |
| 4019 | * |
Anas Nashif | 4b38659 | 2019-11-25 09:30:47 -0500 | [diff] [blame] | 4020 | * @param msgq Address of the message queue. |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 4021 | * @param attrs pointer to message queue attribute structure. |
| 4022 | * |
| 4023 | * @return N/A |
| 4024 | */ |
Anas Nashif | 4b38659 | 2019-11-25 09:30:47 -0500 | [diff] [blame] | 4025 | __syscall void k_msgq_get_attrs(struct k_msgq *msgq, |
| 4026 | struct k_msgq_attrs *attrs); |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 4027 | |
| 4028 | |
Anas Nashif | 4b38659 | 2019-11-25 09:30:47 -0500 | [diff] [blame] | 4029 | static inline u32_t z_impl_k_msgq_num_free_get(struct k_msgq *msgq) |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 4030 | { |
Anas Nashif | 4b38659 | 2019-11-25 09:30:47 -0500 | [diff] [blame] | 4031 | return msgq->max_msgs - msgq->used_msgs; |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 4032 | } |
| 4033 | |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 4034 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4035 | * @brief Get the number of messages in a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 4036 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4037 | * This routine returns the number of messages in a message queue's ring buffer. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 4038 | * |
Anas Nashif | 4b38659 | 2019-11-25 09:30:47 -0500 | [diff] [blame] | 4039 | * @param msgq Address of the message queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4040 | * |
| 4041 | * @return Number of messages. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 4042 | */ |
Anas Nashif | 4b38659 | 2019-11-25 09:30:47 -0500 | [diff] [blame] | 4043 | __syscall u32_t k_msgq_num_used_get(struct k_msgq *msgq); |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 4044 | |
Anas Nashif | 4b38659 | 2019-11-25 09:30:47 -0500 | [diff] [blame] | 4045 | static inline u32_t z_impl_k_msgq_num_used_get(struct k_msgq *msgq) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4046 | { |
Anas Nashif | 4b38659 | 2019-11-25 09:30:47 -0500 | [diff] [blame] | 4047 | return msgq->used_msgs; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4048 | } |
| 4049 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4050 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4051 | |
| 4052 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4053 | * @defgroup mailbox_apis Mailbox APIs |
| 4054 | * @ingroup kernel_apis |
| 4055 | * @{ |
| 4056 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4057 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4058 | /** |
| 4059 | * @brief Mailbox Message Structure |
| 4060 | * |
| 4061 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4062 | struct k_mbox_msg { |
| 4063 | /** internal use only - needed for legacy API support */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4064 | u32_t _mailbox; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4065 | /** size of message (in bytes) */ |
Peter Mitsis | d93078c | 2016-10-14 12:59:37 -0400 | [diff] [blame] | 4066 | size_t size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4067 | /** application-defined information value */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4068 | u32_t info; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4069 | /** sender's message data buffer */ |
| 4070 | void *tx_data; |
| 4071 | /** internal use only - needed for legacy API support */ |
| 4072 | void *_rx_data; |
| 4073 | /** message data block descriptor */ |
| 4074 | struct k_mem_block tx_block; |
| 4075 | /** source thread id */ |
| 4076 | k_tid_t rx_source_thread; |
| 4077 | /** target thread id */ |
| 4078 | k_tid_t tx_target_thread; |
| 4079 | /** internal use only - thread waiting on send (may be a dummy) */ |
| 4080 | k_tid_t _syncing_thread; |
| 4081 | #if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0) |
| 4082 | /** internal use only - semaphore used during asynchronous send */ |
| 4083 | struct k_sem *_async_sem; |
| 4084 | #endif |
| 4085 | }; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4086 | /** |
| 4087 | * @brief Mailbox Structure |
| 4088 | * |
| 4089 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4090 | struct k_mbox { |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4091 | /** Transmit messages queue */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4092 | _wait_q_t tx_msg_queue; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4093 | /** Receive message queue */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4094 | _wait_q_t rx_msg_queue; |
Andy Ross | 9eeb6b8 | 2018-07-25 15:06:24 -0700 | [diff] [blame] | 4095 | struct k_spinlock lock; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4096 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 4097 | _OBJECT_TRACING_NEXT_PTR(k_mbox) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 4098 | _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4099 | }; |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4100 | /** |
| 4101 | * @cond INTERNAL_HIDDEN |
| 4102 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4103 | |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 4104 | #define Z_MBOX_INITIALIZER(obj) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4105 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4106 | .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \ |
| 4107 | .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 4108 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4109 | } |
| 4110 | |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 4111 | #define K_MBOX_INITIALIZER __DEPRECATED_MACRO Z_MBOX_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 4112 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4113 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4114 | * INTERNAL_HIDDEN @endcond |
| 4115 | */ |
| 4116 | |
| 4117 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4118 | * @brief Statically define and initialize a mailbox. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4119 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4120 | * The mailbox is to be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4121 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 4122 | * @code extern struct k_mbox <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4123 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4124 | * @param name Name of the mailbox. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4125 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4126 | #define K_MBOX_DEFINE(name) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 4127 | Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \ |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 4128 | Z_MBOX_INITIALIZER(name) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4129 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4130 | /** |
| 4131 | * @brief Initialize a mailbox. |
| 4132 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4133 | * This routine initializes a mailbox object, prior to its first use. |
| 4134 | * |
| 4135 | * @param mbox Address of the mailbox. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4136 | * |
| 4137 | * @return N/A |
| 4138 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4139 | extern void k_mbox_init(struct k_mbox *mbox); |
| 4140 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4141 | /** |
| 4142 | * @brief Send a mailbox message in a synchronous manner. |
| 4143 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4144 | * This routine sends a message to @a mbox and waits for a receiver to both |
| 4145 | * receive and process it. The message data may be in a buffer, in a memory |
| 4146 | * pool block, or non-existent (i.e. an empty message). |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4147 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4148 | * @param mbox Address of the mailbox. |
| 4149 | * @param tx_msg Address of the transmit message descriptor. |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 4150 | * @param timeout Waiting period for the message to be received, |
| 4151 | * or one of the special values K_NO_WAIT |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4152 | * and K_FOREVER. Once the message has been received, |
| 4153 | * this routine waits as long as necessary for the message |
| 4154 | * to be completely processed. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4155 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4156 | * @retval 0 Message sent. |
| 4157 | * @retval -ENOMSG Returned without waiting. |
| 4158 | * @retval -EAGAIN Waiting period timed out. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4159 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 4160 | extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg, |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 4161 | k_timeout_t timeout); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4162 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4163 | /** |
| 4164 | * @brief Send a mailbox message in an asynchronous manner. |
| 4165 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4166 | * This routine sends a message to @a mbox without waiting for a receiver |
| 4167 | * to process it. The message data may be in a buffer, in a memory pool block, |
| 4168 | * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem |
| 4169 | * will be given when the message has been both received and completely |
| 4170 | * processed by the receiver. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4171 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4172 | * @param mbox Address of the mailbox. |
| 4173 | * @param tx_msg Address of the transmit message descriptor. |
| 4174 | * @param sem Address of a semaphore, or NULL if none is needed. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4175 | * |
| 4176 | * @return N/A |
| 4177 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 4178 | extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg, |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4179 | struct k_sem *sem); |
| 4180 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4181 | /** |
| 4182 | * @brief Receive a mailbox message. |
| 4183 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4184 | * This routine receives a message from @a mbox, then optionally retrieves |
| 4185 | * its data and disposes of the message. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4186 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4187 | * @param mbox Address of the mailbox. |
| 4188 | * @param rx_msg Address of the receive message descriptor. |
| 4189 | * @param buffer Address of the buffer to receive data, or NULL to defer data |
| 4190 | * retrieval and message disposal until later. |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 4191 | * @param timeout Waiting period for a message to be received, |
| 4192 | * or one of the special values K_NO_WAIT and K_FOREVER. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4193 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4194 | * @retval 0 Message received. |
| 4195 | * @retval -ENOMSG Returned without waiting. |
| 4196 | * @retval -EAGAIN Waiting period timed out. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4197 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 4198 | extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg, |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 4199 | void *buffer, k_timeout_t timeout); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4200 | |
| 4201 | /** |
| 4202 | * @brief Retrieve mailbox message data into a buffer. |
| 4203 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4204 | * This routine completes the processing of a received message by retrieving |
| 4205 | * its data into a buffer, then disposing of the message. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4206 | * |
| 4207 | * Alternatively, this routine can be used to dispose of a received message |
| 4208 | * without retrieving its data. |
| 4209 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4210 | * @param rx_msg Address of the receive message descriptor. |
| 4211 | * @param buffer Address of the buffer to receive data, or NULL to discard |
| 4212 | * the data. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4213 | * |
| 4214 | * @return N/A |
| 4215 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 4216 | extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4217 | |
| 4218 | /** |
| 4219 | * @brief Retrieve mailbox message data into a memory pool block. |
| 4220 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4221 | * This routine completes the processing of a received message by retrieving |
| 4222 | * its data into a memory pool block, then disposing of the message. |
| 4223 | * The memory pool block that results from successful retrieval must be |
| 4224 | * returned to the pool once the data has been processed, even in cases |
| 4225 | * where zero bytes of data are retrieved. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4226 | * |
| 4227 | * Alternatively, this routine can be used to dispose of a received message |
| 4228 | * without retrieving its data. In this case there is no need to return a |
| 4229 | * memory pool block to the pool. |
| 4230 | * |
| 4231 | * This routine allocates a new memory pool block for the data only if the |
| 4232 | * data is not already in one. If a new block cannot be allocated, the routine |
| 4233 | * returns a failure code and the received message is left unchanged. This |
| 4234 | * permits the caller to reattempt data retrieval at a later time or to dispose |
| 4235 | * of the received message without retrieving its data. |
| 4236 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4237 | * @param rx_msg Address of a receive message descriptor. |
| 4238 | * @param pool Address of memory pool, or NULL to discard data. |
| 4239 | * @param block Address of the area to hold memory pool block info. |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 4240 | * @param timeout Time to wait for a memory pool block, |
| 4241 | * or one of the special values K_NO_WAIT |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4242 | * and K_FOREVER. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4243 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4244 | * @retval 0 Data retrieved. |
| 4245 | * @retval -ENOMEM Returned without waiting. |
| 4246 | * @retval -EAGAIN Waiting period timed out. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4247 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 4248 | extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg, |
Peter Mitsis | 0cb65c3 | 2016-09-29 14:07:36 -0400 | [diff] [blame] | 4249 | struct k_mem_pool *pool, |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 4250 | struct k_mem_block *block, |
| 4251 | k_timeout_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4252 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4253 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4254 | |
| 4255 | /** |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 4256 | * @defgroup pipe_apis Pipe APIs |
| 4257 | * @ingroup kernel_apis |
| 4258 | * @{ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4259 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4260 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 4261 | /** Pipe Structure */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4262 | struct k_pipe { |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 4263 | unsigned char *buffer; /**< Pipe buffer: may be NULL */ |
| 4264 | size_t size; /**< Buffer size */ |
| 4265 | size_t bytes_used; /**< # bytes used in buffer */ |
| 4266 | size_t read_index; /**< Where in buffer to read from */ |
| 4267 | size_t write_index; /**< Where in buffer to write */ |
Andy Ross | f582b55 | 2019-02-05 16:10:18 -0800 | [diff] [blame] | 4268 | struct k_spinlock lock; /**< Synchronization lock */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4269 | |
| 4270 | struct { |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 4271 | _wait_q_t readers; /**< Reader wait queue */ |
| 4272 | _wait_q_t writers; /**< Writer wait queue */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4273 | } wait_q; |
| 4274 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 4275 | _OBJECT_TRACING_NEXT_PTR(k_pipe) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 4276 | _OBJECT_TRACING_LINKED_FLAG |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 4277 | u8_t flags; /**< Flags */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4278 | }; |
| 4279 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 4280 | /** |
| 4281 | * @cond INTERNAL_HIDDEN |
| 4282 | */ |
| 4283 | #define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */ |
| 4284 | |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 4285 | #define Z_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \ |
Krzysztof Chruscinski | be06327 | 2019-02-13 11:19:54 +0100 | [diff] [blame] | 4286 | { \ |
| 4287 | .buffer = pipe_buffer, \ |
| 4288 | .size = pipe_buffer_size, \ |
| 4289 | .bytes_used = 0, \ |
| 4290 | .read_index = 0, \ |
| 4291 | .write_index = 0, \ |
| 4292 | .lock = {}, \ |
| 4293 | .wait_q = { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4294 | .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \ |
| 4295 | .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \ |
Krzysztof Chruscinski | be06327 | 2019-02-13 11:19:54 +0100 | [diff] [blame] | 4296 | }, \ |
| 4297 | _OBJECT_TRACING_INIT \ |
| 4298 | .flags = 0 \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4299 | } |
| 4300 | |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 4301 | #define K_PIPE_INITIALIZER __DEPRECATED_MACRO Z_PIPE_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 4302 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4303 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4304 | * INTERNAL_HIDDEN @endcond |
| 4305 | */ |
| 4306 | |
| 4307 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4308 | * @brief Statically define and initialize a pipe. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4309 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4310 | * The pipe can be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4311 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 4312 | * @code extern struct k_pipe <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4313 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4314 | * @param name Name of the pipe. |
| 4315 | * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes), |
| 4316 | * or zero if no ring buffer is used. |
| 4317 | * @param pipe_align Alignment of the pipe's ring buffer (power of 2). |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4318 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4319 | */ |
Andrew Boie | 44fe812 | 2018-04-12 17:38:12 -0700 | [diff] [blame] | 4320 | #define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \ |
Andrew Boie | 41f6011 | 2019-01-31 15:53:24 -0800 | [diff] [blame] | 4321 | static unsigned char __noinit __aligned(pipe_align) \ |
Andrew Boie | 44fe812 | 2018-04-12 17:38:12 -0700 | [diff] [blame] | 4322 | _k_pipe_buf_##name[pipe_buffer_size]; \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 4323 | Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \ |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 4324 | Z_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4325 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4326 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4327 | * @brief Initialize a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4328 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4329 | * This routine initializes a pipe object, prior to its first use. |
| 4330 | * |
| 4331 | * @param pipe Address of the pipe. |
| 4332 | * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer |
| 4333 | * is used. |
| 4334 | * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring |
| 4335 | * buffer is used. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4336 | * |
| 4337 | * @return N/A |
| 4338 | */ |
Andrew Boie | 44fe812 | 2018-04-12 17:38:12 -0700 | [diff] [blame] | 4339 | void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size); |
| 4340 | |
| 4341 | /** |
| 4342 | * @brief Release a pipe's allocated buffer |
| 4343 | * |
| 4344 | * If a pipe object was given a dynamically allocated buffer via |
| 4345 | * k_pipe_alloc_init(), this will free it. This function does nothing |
| 4346 | * if the buffer wasn't dynamically allocated. |
| 4347 | * |
| 4348 | * @param pipe Address of the pipe. |
Anas Nashif | 361a84d | 2019-06-16 08:22:08 -0400 | [diff] [blame] | 4349 | * @retval 0 on success |
| 4350 | * @retval -EAGAIN nothing to cleanup |
Andrew Boie | 44fe812 | 2018-04-12 17:38:12 -0700 | [diff] [blame] | 4351 | */ |
Anas Nashif | 361a84d | 2019-06-16 08:22:08 -0400 | [diff] [blame] | 4352 | int k_pipe_cleanup(struct k_pipe *pipe); |
Andrew Boie | 44fe812 | 2018-04-12 17:38:12 -0700 | [diff] [blame] | 4353 | |
| 4354 | /** |
| 4355 | * @brief Initialize a pipe and allocate a buffer for it |
| 4356 | * |
| 4357 | * Storage for the buffer region will be allocated from the calling thread's |
| 4358 | * resource pool. This memory will be released if k_pipe_cleanup() is called, |
| 4359 | * or userspace is enabled and the pipe object loses all references to it. |
| 4360 | * |
| 4361 | * This function should only be called on uninitialized pipe objects. |
| 4362 | * |
| 4363 | * @param pipe Address of the pipe. |
| 4364 | * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring |
| 4365 | * buffer is used. |
| 4366 | * @retval 0 on success |
David B. Kinder | fcbd8fb | 2018-05-23 12:06:24 -0700 | [diff] [blame] | 4367 | * @retval -ENOMEM if memory couldn't be allocated |
Andrew Boie | 44fe812 | 2018-04-12 17:38:12 -0700 | [diff] [blame] | 4368 | */ |
| 4369 | __syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4370 | |
| 4371 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4372 | * @brief Write data to a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4373 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4374 | * This routine writes up to @a bytes_to_write bytes of data to @a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4375 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4376 | * @param pipe Address of the pipe. |
| 4377 | * @param data Address of data to write. |
| 4378 | * @param bytes_to_write Size of data (in bytes). |
| 4379 | * @param bytes_written Address of area to hold the number of bytes written. |
| 4380 | * @param min_xfer Minimum number of bytes to write. |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 4381 | * @param timeout Waiting period to wait for the data to be written, |
| 4382 | * or one of the special values K_NO_WAIT and K_FOREVER. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4383 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4384 | * @retval 0 At least @a min_xfer bytes of data were written. |
| 4385 | * @retval -EIO Returned without waiting; zero data bytes were written. |
| 4386 | * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4387 | * minus one data bytes were written. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4388 | */ |
Andrew Boie | b9a0578 | 2017-09-29 16:05:32 -0700 | [diff] [blame] | 4389 | __syscall int k_pipe_put(struct k_pipe *pipe, void *data, |
| 4390 | size_t bytes_to_write, size_t *bytes_written, |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 4391 | size_t min_xfer, k_timeout_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4392 | |
| 4393 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4394 | * @brief Read data from a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4395 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4396 | * This routine reads up to @a bytes_to_read bytes of data from @a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4397 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4398 | * @param pipe Address of the pipe. |
| 4399 | * @param data Address to place the data read from pipe. |
| 4400 | * @param bytes_to_read Maximum number of data bytes to read. |
| 4401 | * @param bytes_read Address of area to hold the number of bytes read. |
| 4402 | * @param min_xfer Minimum number of data bytes to read. |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 4403 | * @param timeout Waiting period to wait for the data to be read, |
| 4404 | * or one of the special values K_NO_WAIT and K_FOREVER. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4405 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4406 | * @retval 0 At least @a min_xfer bytes of data were read. |
Anas Nashif | 361a84d | 2019-06-16 08:22:08 -0400 | [diff] [blame] | 4407 | * @retval -EINVAL invalid parameters supplied |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4408 | * @retval -EIO Returned without waiting; zero data bytes were read. |
| 4409 | * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4410 | * minus one data bytes were read. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4411 | */ |
Andrew Boie | b9a0578 | 2017-09-29 16:05:32 -0700 | [diff] [blame] | 4412 | __syscall int k_pipe_get(struct k_pipe *pipe, void *data, |
| 4413 | size_t bytes_to_read, size_t *bytes_read, |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 4414 | size_t min_xfer, k_timeout_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4415 | |
| 4416 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4417 | * @brief Write memory block to a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4418 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4419 | * This routine writes the data contained in a memory block to @a pipe. |
| 4420 | * Once all of the data in the block has been written to the pipe, it will |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4421 | * free the memory block @a block and give the semaphore @a sem (if specified). |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4422 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4423 | * @param pipe Address of the pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4424 | * @param block Memory block containing data to send |
| 4425 | * @param size Number of data bytes in memory block to send |
| 4426 | * @param sem Semaphore to signal upon completion (else NULL) |
| 4427 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4428 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4429 | */ |
| 4430 | extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block, |
| 4431 | size_t size, struct k_sem *sem); |
| 4432 | |
Christopher Friedt | 3315f8f | 2020-05-06 18:43:58 -0400 | [diff] [blame] | 4433 | /** |
| 4434 | * @brief Query the number of bytes that may be read from @a pipe. |
| 4435 | * |
| 4436 | * @param pipe Address of the pipe. |
| 4437 | * |
| 4438 | * @retval a number n such that 0 <= n <= @ref k_pipe.size; the |
| 4439 | * result is zero for unbuffered pipes. |
| 4440 | */ |
| 4441 | __syscall size_t k_pipe_read_avail(struct k_pipe *pipe); |
| 4442 | |
| 4443 | /** |
| 4444 | * @brief Query the number of bytes that may be written to @a pipe |
| 4445 | * |
| 4446 | * @param pipe Address of the pipe. |
| 4447 | * |
| 4448 | * @retval a number n such that 0 <= n <= @ref k_pipe.size; the |
| 4449 | * result is zero for unbuffered pipes. |
| 4450 | */ |
| 4451 | __syscall size_t k_pipe_write_avail(struct k_pipe *pipe); |
| 4452 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4453 | /** @} */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4454 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4455 | /** |
| 4456 | * @cond INTERNAL_HIDDEN |
| 4457 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4458 | |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4459 | struct k_mem_slab { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4460 | _wait_q_t wait_q; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4461 | u32_t num_blocks; |
Peter Mitsis | fb02d57 | 2016-10-13 16:55:45 -0400 | [diff] [blame] | 4462 | size_t block_size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4463 | char *buffer; |
| 4464 | char *free_list; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4465 | u32_t num_used; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4466 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 4467 | _OBJECT_TRACING_NEXT_PTR(k_mem_slab) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 4468 | _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4469 | }; |
| 4470 | |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 4471 | #define Z_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4472 | slab_num_blocks) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4473 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4474 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4475 | .num_blocks = slab_num_blocks, \ |
| 4476 | .block_size = slab_block_size, \ |
| 4477 | .buffer = slab_buffer, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4478 | .free_list = NULL, \ |
| 4479 | .num_used = 0, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 4480 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4481 | } |
| 4482 | |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 4483 | #define K_MEM_SLAB_INITIALIZER __DEPRECATED_MACRO Z_MEM_SLAB_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 4484 | |
| 4485 | |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 4486 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4487 | * INTERNAL_HIDDEN @endcond |
| 4488 | */ |
| 4489 | |
| 4490 | /** |
| 4491 | * @defgroup mem_slab_apis Memory Slab APIs |
| 4492 | * @ingroup kernel_apis |
| 4493 | * @{ |
| 4494 | */ |
| 4495 | |
| 4496 | /** |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 4497 | * @brief Statically define and initialize a memory slab. |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 4498 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 4499 | * The memory slab's buffer contains @a slab_num_blocks memory blocks |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4500 | * that are @a slab_block_size bytes long. The buffer is aligned to a |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 4501 | * @a slab_align -byte boundary. To ensure that each memory block is similarly |
| 4502 | * aligned to this boundary, @a slab_block_size must also be a multiple of |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4503 | * @a slab_align. |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 4504 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 4505 | * The memory slab can be accessed outside the module where it is defined |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4506 | * using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4507 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 4508 | * @code extern struct k_mem_slab <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4509 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4510 | * @param name Name of the memory slab. |
| 4511 | * @param slab_block_size Size of each memory block (in bytes). |
| 4512 | * @param slab_num_blocks Number memory blocks. |
| 4513 | * @param slab_align Alignment of the memory slab's buffer (power of 2). |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 4514 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4515 | #define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \ |
Nicolas Pitre | 46cd5a0 | 2019-05-21 21:40:38 -0400 | [diff] [blame] | 4516 | char __noinit __aligned(WB_UP(slab_align)) \ |
| 4517 | _k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 4518 | Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \ |
Anas Nashif | 45a1d8a | 2020-04-24 11:29:17 -0400 | [diff] [blame] | 4519 | Z_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \ |
Nicolas Pitre | 46cd5a0 | 2019-05-21 21:40:38 -0400 | [diff] [blame] | 4520 | WB_UP(slab_block_size), slab_num_blocks) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4521 | |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4522 | /** |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4523 | * @brief Initialize a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4524 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4525 | * Initializes a memory slab, prior to its first use. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4526 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 4527 | * The memory slab's buffer contains @a slab_num_blocks memory blocks |
| 4528 | * that are @a slab_block_size bytes long. The buffer must be aligned to an |
Nicolas Pitre | 46cd5a0 | 2019-05-21 21:40:38 -0400 | [diff] [blame] | 4529 | * N-byte boundary matching a word boundary, where N is a power of 2 |
| 4530 | * (i.e. 4 on 32-bit systems, 8, 16, ...). |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 4531 | * To ensure that each memory block is similarly aligned to this boundary, |
| 4532 | * @a slab_block_size must also be a multiple of N. |
| 4533 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4534 | * @param slab Address of the memory slab. |
| 4535 | * @param buffer Pointer to buffer used for the memory blocks. |
| 4536 | * @param block_size Size of each memory block (in bytes). |
| 4537 | * @param num_blocks Number of memory blocks. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4538 | * |
Anas Nashif | dfc2bbc | 2019-06-16 09:22:21 -0400 | [diff] [blame] | 4539 | * @retval 0 on success |
| 4540 | * @retval -EINVAL invalid data supplied |
| 4541 | * |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4542 | */ |
Anas Nashif | dfc2bbc | 2019-06-16 09:22:21 -0400 | [diff] [blame] | 4543 | extern int k_mem_slab_init(struct k_mem_slab *slab, void *buffer, |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4544 | size_t block_size, u32_t num_blocks); |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4545 | |
| 4546 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4547 | * @brief Allocate memory from a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4548 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4549 | * This routine allocates a memory block from a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4550 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4551 | * @param slab Address of the memory slab. |
| 4552 | * @param mem Pointer to block address area. |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 4553 | * @param timeout Non-negative waiting period to wait for operation to complete. |
| 4554 | * Use K_NO_WAIT to return without waiting, |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4555 | * or K_FOREVER to wait as long as necessary. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4556 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4557 | * @retval 0 Memory allocated. The block address area pointed at by @a mem |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4558 | * is set to the starting address of the memory block. |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4559 | * @retval -ENOMEM Returned without waiting. |
| 4560 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | dfc2bbc | 2019-06-16 09:22:21 -0400 | [diff] [blame] | 4561 | * @retval -EINVAL Invalid data supplied |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4562 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4563 | extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem, |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 4564 | k_timeout_t timeout); |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4565 | |
| 4566 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4567 | * @brief Free memory allocated from a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4568 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4569 | * This routine releases a previously allocated memory block back to its |
| 4570 | * associated memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4571 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4572 | * @param slab Address of the memory slab. |
| 4573 | * @param mem Pointer to block address area (as set by k_mem_slab_alloc()). |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4574 | * |
| 4575 | * @return N/A |
| 4576 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4577 | extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4578 | |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4579 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4580 | * @brief Get the number of used blocks in a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4581 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4582 | * This routine gets the number of memory blocks that are currently |
| 4583 | * allocated in @a slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4584 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4585 | * @param slab Address of the memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4586 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4587 | * @return Number of allocated memory blocks. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4588 | */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4589 | static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4590 | { |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4591 | return slab->num_used; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4592 | } |
| 4593 | |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4594 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4595 | * @brief Get the number of unused blocks in a memory slab. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4596 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4597 | * This routine gets the number of memory blocks that are currently |
| 4598 | * unallocated in @a slab. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4599 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4600 | * @param slab Address of the memory slab. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4601 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4602 | * @return Number of unallocated memory blocks. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4603 | */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4604 | static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab) |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4605 | { |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4606 | return slab->num_blocks - slab->num_used; |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4607 | } |
| 4608 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4609 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4610 | |
| 4611 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4612 | * @addtogroup mem_pool_apis |
| 4613 | * @{ |
| 4614 | */ |
| 4615 | |
| 4616 | /** |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 4617 | * @brief Initialize a k_heap |
| 4618 | * |
| 4619 | * This constructs a synchronized k_heap object over a memory region |
| 4620 | * specified by the user. Note that while any alignment and size can |
| 4621 | * be passed as valid parameters, internal alignment restrictions |
| 4622 | * inside the inner sys_heap mean that not all bytes may be usable as |
| 4623 | * allocated memory. |
| 4624 | * |
| 4625 | * @param h Heap struct to initialize |
| 4626 | * @param mem Pointer to memory. |
| 4627 | * @param bytes Size of memory region, in bytes |
| 4628 | */ |
| 4629 | void k_heap_init(struct k_heap *h, void *mem, size_t bytes); |
| 4630 | |
| 4631 | /** |
| 4632 | * @brief Allocate memory from a k_heap |
| 4633 | * |
| 4634 | * Allocates and returns a memory buffer from the memory region owned |
| 4635 | * by the heap. If no memory is available immediately, the call will |
| 4636 | * block for the specified timeout (constructed via the standard |
| 4637 | * timeout API, or K_NO_WAIT or K_FOREVER) waiting for memory to be |
| 4638 | * freed. If the allocation cannot be performed by the expiration of |
| 4639 | * the timeout, NULL will be returned. |
| 4640 | * |
| 4641 | * @param h Heap from which to allocate |
| 4642 | * @param bytes Desired size of block to allocate |
| 4643 | * @param timeout How long to wait, or K_NO_WAIT |
| 4644 | * @return A pointer to valid heap memory, or NULL |
| 4645 | */ |
| 4646 | void *k_heap_alloc(struct k_heap *h, size_t bytes, k_timeout_t timeout); |
| 4647 | |
| 4648 | /** |
| 4649 | * @brief Free memory allocated by k_heap_alloc() |
| 4650 | * |
| 4651 | * Returns the specified memory block, which must have been returned |
| 4652 | * from k_heap_alloc(), to the heap for use by other callers. Passing |
| 4653 | * a NULL block is legal, and has no effect. |
| 4654 | * |
| 4655 | * @param h Heap to which to return the memory |
| 4656 | * @param mem A valid memory block, or NULL |
| 4657 | */ |
| 4658 | void k_heap_free(struct k_heap *h, void *mem); |
| 4659 | |
| 4660 | /** |
| 4661 | * @brief Define a static k_heap |
| 4662 | * |
| 4663 | * This macro defines and initializes a static memory region and |
| 4664 | * k_heap of the requested size. After kernel start, &name can be |
| 4665 | * used as if k_heap_init() had been called. |
| 4666 | * |
| 4667 | * @param name Symbol name for the struct k_heap object |
| 4668 | * @param bytes Size of memory region, in bytes |
| 4669 | */ |
| 4670 | #define K_HEAP_DEFINE(name, bytes) \ |
| 4671 | char __aligned(sizeof(void *)) kheap_##name[bytes]; \ |
| 4672 | Z_STRUCT_SECTION_ITERABLE(k_heap, name) = { \ |
| 4673 | .heap = { \ |
| 4674 | .init_mem = kheap_##name, \ |
| 4675 | .init_bytes = (bytes), \ |
| 4676 | }, \ |
| 4677 | } |
| 4678 | |
| 4679 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4680 | * @brief Statically define and initialize a memory pool. |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 4681 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4682 | * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes |
| 4683 | * long. The memory pool allows blocks to be repeatedly partitioned into |
| 4684 | * quarters, down to blocks of @a min_size bytes long. The buffer is aligned |
Andy Ross | 73cb958 | 2017-05-09 10:42:39 -0700 | [diff] [blame] | 4685 | * to a @a align -byte boundary. |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 4686 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4687 | * If the pool is to be accessed outside the module where it is defined, it |
| 4688 | * can be declared via |
| 4689 | * |
Andy Ross | 8f0959c | 2020-04-03 15:39:25 -0700 | [diff] [blame] | 4690 | * @note When CONFIG_MEM_POOL_HEAP_BACKEND is enabled, the k_mem_pool |
| 4691 | * API is implemented on top of a k_heap, which is a more general |
| 4692 | * purpose allocator which does not make the same promises about |
| 4693 | * splitting or alignment detailed above. Blocks will be aligned only |
| 4694 | * to the 8 byte chunk stride of the underlying heap and may point |
| 4695 | * anywhere within the heap; they are not split into four as |
| 4696 | * described. |
| 4697 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 4698 | * @code extern struct k_mem_pool <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4699 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4700 | * @param name Name of the memory pool. |
Andy Ross | 73cb958 | 2017-05-09 10:42:39 -0700 | [diff] [blame] | 4701 | * @param minsz Size of the smallest blocks in the pool (in bytes). |
| 4702 | * @param maxsz Size of the largest blocks in the pool (in bytes). |
| 4703 | * @param nmax Number of maximum sized blocks in the pool. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4704 | * @param align Alignment of the pool's buffer (power of 2). |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 4705 | */ |
Andy Ross | e96ac90 | 2020-03-31 09:23:11 -0700 | [diff] [blame] | 4706 | #define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \ |
| 4707 | Z_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 4708 | |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4709 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4710 | * @brief Allocate memory from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4711 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4712 | * This routine allocates a memory block from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4713 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4714 | * @param pool Address of the memory pool. |
| 4715 | * @param block Pointer to block descriptor for the allocated memory. |
| 4716 | * @param size Amount of memory to allocate (in bytes). |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 4717 | * @param timeout Waiting period to wait for operation to complete. |
| 4718 | * Use K_NO_WAIT to return without waiting, |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4719 | * or K_FOREVER to wait as long as necessary. |
| 4720 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4721 | * @retval 0 Memory allocated. The @a data field of the block descriptor |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4722 | * is set to the starting address of the memory block. |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4723 | * @retval -ENOMEM Returned without waiting. |
| 4724 | * @retval -EAGAIN Waiting period timed out. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4725 | */ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 4726 | extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block, |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 4727 | size_t size, k_timeout_t timeout); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4728 | |
| 4729 | /** |
Andrew Boie | a2480bd | 2018-04-12 16:59:02 -0700 | [diff] [blame] | 4730 | * @brief Allocate memory from a memory pool with malloc() semantics |
| 4731 | * |
| 4732 | * Such memory must be released using k_free(). |
| 4733 | * |
| 4734 | * @param pool Address of the memory pool. |
| 4735 | * @param size Amount of memory to allocate (in bytes). |
| 4736 | * @return Address of the allocated memory if successful, otherwise NULL |
| 4737 | */ |
| 4738 | extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size); |
| 4739 | |
| 4740 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4741 | * @brief Free memory allocated from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4742 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4743 | * This routine releases a previously allocated memory block back to its |
| 4744 | * memory pool. |
| 4745 | * |
| 4746 | * @param block Pointer to block descriptor for the allocated memory. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4747 | * |
| 4748 | * @return N/A |
| 4749 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4750 | extern void k_mem_pool_free(struct k_mem_block *block); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4751 | |
| 4752 | /** |
Johan Hedberg | 7d887cb | 2018-01-11 20:45:27 +0200 | [diff] [blame] | 4753 | * @brief Free memory allocated from a memory pool. |
| 4754 | * |
| 4755 | * This routine releases a previously allocated memory block back to its |
| 4756 | * memory pool |
| 4757 | * |
| 4758 | * @param id Memory block identifier. |
| 4759 | * |
| 4760 | * @return N/A |
| 4761 | */ |
| 4762 | extern void k_mem_pool_free_id(struct k_mem_block_id *id); |
| 4763 | |
| 4764 | /** |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4765 | * @} |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4766 | */ |
| 4767 | |
| 4768 | /** |
| 4769 | * @defgroup heap_apis Heap Memory Pool APIs |
| 4770 | * @ingroup kernel_apis |
| 4771 | * @{ |
| 4772 | */ |
| 4773 | |
| 4774 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4775 | * @brief Allocate memory from heap. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4776 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4777 | * This routine provides traditional malloc() semantics. Memory is |
Allan Stephens | 480a131 | 2016-10-13 15:44:48 -0500 | [diff] [blame] | 4778 | * allocated from the heap memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4779 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4780 | * @param size Amount of memory requested (in bytes). |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4781 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4782 | * @return Address of the allocated memory if successful; otherwise NULL. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4783 | */ |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 4784 | extern void *k_malloc(size_t size); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4785 | |
| 4786 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4787 | * @brief Free memory allocated from heap. |
Allan Stephens | 480a131 | 2016-10-13 15:44:48 -0500 | [diff] [blame] | 4788 | * |
| 4789 | * This routine provides traditional free() semantics. The memory being |
Andrew Boie | a2480bd | 2018-04-12 16:59:02 -0700 | [diff] [blame] | 4790 | * returned must have been allocated from the heap memory pool or |
| 4791 | * k_mem_pool_malloc(). |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4792 | * |
Anas Nashif | 345fdd5 | 2016-12-20 08:36:04 -0500 | [diff] [blame] | 4793 | * If @a ptr is NULL, no operation is performed. |
| 4794 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4795 | * @param ptr Pointer to previously allocated memory. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4796 | * |
| 4797 | * @return N/A |
| 4798 | */ |
| 4799 | extern void k_free(void *ptr); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4800 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4801 | /** |
Andrew Boie | 7f95e83 | 2017-11-08 14:40:01 -0800 | [diff] [blame] | 4802 | * @brief Allocate memory from heap, array style |
| 4803 | * |
| 4804 | * This routine provides traditional calloc() semantics. Memory is |
| 4805 | * allocated from the heap memory pool and zeroed. |
| 4806 | * |
| 4807 | * @param nmemb Number of elements in the requested array |
| 4808 | * @param size Size of each array element (in bytes). |
| 4809 | * |
| 4810 | * @return Address of the allocated memory if successful; otherwise NULL. |
| 4811 | */ |
| 4812 | extern void *k_calloc(size_t nmemb, size_t size); |
| 4813 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4814 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4815 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4816 | /* polling API - PRIVATE */ |
| 4817 | |
Benjamin Walsh | b017986 | 2017-02-02 16:39:57 -0500 | [diff] [blame] | 4818 | #ifdef CONFIG_POLL |
Flavio Ceolin | 6fdc56d | 2018-09-18 12:32:27 -0700 | [diff] [blame] | 4819 | #define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false) |
Benjamin Walsh | b017986 | 2017-02-02 16:39:57 -0500 | [diff] [blame] | 4820 | #else |
Flavio Ceolin | 6fdc56d | 2018-09-18 12:32:27 -0700 | [diff] [blame] | 4821 | #define _INIT_OBJ_POLL_EVENT(obj) do { } while (false) |
Benjamin Walsh | b017986 | 2017-02-02 16:39:57 -0500 | [diff] [blame] | 4822 | #endif |
| 4823 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4824 | /* private - types bit positions */ |
| 4825 | enum _poll_types_bits { |
| 4826 | /* can be used to ignore an event */ |
| 4827 | _POLL_TYPE_IGNORE, |
| 4828 | |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 4829 | /* to be signaled by k_poll_signal_raise() */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4830 | _POLL_TYPE_SIGNAL, |
| 4831 | |
| 4832 | /* semaphore availability */ |
| 4833 | _POLL_TYPE_SEM_AVAILABLE, |
| 4834 | |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 4835 | /* queue/fifo/lifo data availability */ |
| 4836 | _POLL_TYPE_DATA_AVAILABLE, |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4837 | |
| 4838 | _POLL_NUM_TYPES |
| 4839 | }; |
| 4840 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4841 | #define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1)) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4842 | |
| 4843 | /* private - states bit positions */ |
| 4844 | enum _poll_states_bits { |
| 4845 | /* default state when creating event */ |
| 4846 | _POLL_STATE_NOT_READY, |
| 4847 | |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 4848 | /* signaled by k_poll_signal_raise() */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4849 | _POLL_STATE_SIGNALED, |
| 4850 | |
| 4851 | /* semaphore is available */ |
| 4852 | _POLL_STATE_SEM_AVAILABLE, |
| 4853 | |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 4854 | /* data is available to read on queue/fifo/lifo */ |
| 4855 | _POLL_STATE_DATA_AVAILABLE, |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4856 | |
Paul Sokolovsky | 45c0b20 | 2018-08-21 23:29:11 +0300 | [diff] [blame] | 4857 | /* queue/fifo/lifo wait was cancelled */ |
| 4858 | _POLL_STATE_CANCELLED, |
| 4859 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4860 | _POLL_NUM_STATES |
| 4861 | }; |
| 4862 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4863 | #define Z_POLL_STATE_BIT(state) (1 << ((state) - 1)) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4864 | |
| 4865 | #define _POLL_EVENT_NUM_UNUSED_BITS \ |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4866 | (32 - (0 \ |
| 4867 | + 8 /* tag */ \ |
| 4868 | + _POLL_NUM_TYPES \ |
| 4869 | + _POLL_NUM_STATES \ |
| 4870 | + 1 /* modes */ \ |
| 4871 | )) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4872 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4873 | /* end of polling API - PRIVATE */ |
| 4874 | |
| 4875 | |
| 4876 | /** |
| 4877 | * @defgroup poll_apis Async polling APIs |
| 4878 | * @ingroup kernel_apis |
| 4879 | * @{ |
| 4880 | */ |
| 4881 | |
| 4882 | /* Public polling API */ |
| 4883 | |
| 4884 | /* public - values for k_poll_event.type bitfield */ |
| 4885 | #define K_POLL_TYPE_IGNORE 0 |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4886 | #define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL) |
| 4887 | #define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE) |
| 4888 | #define K_POLL_TYPE_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE) |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 4889 | #define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4890 | |
| 4891 | /* public - polling modes */ |
| 4892 | enum k_poll_modes { |
| 4893 | /* polling thread does not take ownership of objects when available */ |
| 4894 | K_POLL_MODE_NOTIFY_ONLY = 0, |
| 4895 | |
| 4896 | K_POLL_NUM_MODES |
| 4897 | }; |
| 4898 | |
| 4899 | /* public - values for k_poll_event.state bitfield */ |
| 4900 | #define K_POLL_STATE_NOT_READY 0 |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4901 | #define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED) |
| 4902 | #define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE) |
| 4903 | #define K_POLL_STATE_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE) |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 4904 | #define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4905 | #define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4906 | |
| 4907 | /* public - poll signal object */ |
| 4908 | struct k_poll_signal { |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4909 | /** PRIVATE - DO NOT TOUCH */ |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 4910 | sys_dlist_t poll_events; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4911 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4912 | /** |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4913 | * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until |
| 4914 | * user resets it to 0. |
| 4915 | */ |
| 4916 | unsigned int signaled; |
| 4917 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4918 | /** custom result value passed to k_poll_signal_raise() if needed */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4919 | int result; |
| 4920 | }; |
| 4921 | |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 4922 | #define K_POLL_SIGNAL_INITIALIZER(obj) \ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4923 | { \ |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 4924 | .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4925 | .signaled = 0, \ |
| 4926 | .result = 0, \ |
| 4927 | } |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4928 | /** |
| 4929 | * @brief Poll Event |
| 4930 | * |
| 4931 | */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4932 | struct k_poll_event { |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4933 | /** PRIVATE - DO NOT TOUCH */ |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 4934 | sys_dnode_t _node; |
| 4935 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4936 | /** PRIVATE - DO NOT TOUCH */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4937 | struct _poller *poller; |
| 4938 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4939 | /** optional user-specified tag, opaque, untouched by the API */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4940 | u32_t tag:8; |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4941 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4942 | /** bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4943 | u32_t type:_POLL_NUM_TYPES; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4944 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4945 | /** bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4946 | u32_t state:_POLL_NUM_STATES; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4947 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4948 | /** mode of operation, from enum k_poll_modes */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4949 | u32_t mode:1; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4950 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4951 | /** unused bits in 32-bit word */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4952 | u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4953 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4954 | /** per-type data */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4955 | union { |
| 4956 | void *obj; |
| 4957 | struct k_poll_signal *signal; |
| 4958 | struct k_sem *sem; |
| 4959 | struct k_fifo *fifo; |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 4960 | struct k_queue *queue; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4961 | }; |
| 4962 | }; |
| 4963 | |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4964 | #define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4965 | { \ |
| 4966 | .poller = NULL, \ |
| 4967 | .type = event_type, \ |
| 4968 | .state = K_POLL_STATE_NOT_READY, \ |
| 4969 | .mode = event_mode, \ |
| 4970 | .unused = 0, \ |
Peter A. Bigot | 008f9c0 | 2020-04-26 12:30:58 -0500 | [diff] [blame] | 4971 | .obj = event_obj, \ |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4972 | } |
| 4973 | |
| 4974 | #define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \ |
| 4975 | event_tag) \ |
| 4976 | { \ |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4977 | .tag = event_tag, \ |
Markus Fuchs | be21d3f | 2019-10-09 21:31:25 +0200 | [diff] [blame] | 4978 | .type = event_type, \ |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4979 | .state = K_POLL_STATE_NOT_READY, \ |
| 4980 | .mode = event_mode, \ |
| 4981 | .unused = 0, \ |
Peter A. Bigot | 008f9c0 | 2020-04-26 12:30:58 -0500 | [diff] [blame] | 4982 | .obj = event_obj, \ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4983 | } |
| 4984 | |
| 4985 | /** |
| 4986 | * @brief Initialize one struct k_poll_event instance |
| 4987 | * |
| 4988 | * After this routine is called on a poll event, the event it ready to be |
| 4989 | * placed in an event array to be passed to k_poll(). |
| 4990 | * |
| 4991 | * @param event The event to initialize. |
| 4992 | * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx |
| 4993 | * values. Only values that apply to the same object being polled |
| 4994 | * can be used together. Choosing K_POLL_TYPE_IGNORE disables the |
| 4995 | * event. |
Paul Sokolovsky | cfef979 | 2017-07-18 11:53:06 +0300 | [diff] [blame] | 4996 | * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY. |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4997 | * @param obj Kernel object or poll signal. |
| 4998 | * |
| 4999 | * @return N/A |
| 5000 | */ |
| 5001 | |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 5002 | extern void k_poll_event_init(struct k_poll_event *event, u32_t type, |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 5003 | int mode, void *obj); |
| 5004 | |
| 5005 | /** |
| 5006 | * @brief Wait for one or many of multiple poll events to occur |
| 5007 | * |
| 5008 | * This routine allows a thread to wait concurrently for one or many of |
| 5009 | * multiple poll events to have occurred. Such events can be a kernel object |
| 5010 | * being available, like a semaphore, or a poll signal event. |
| 5011 | * |
| 5012 | * When an event notifies that a kernel object is available, the kernel object |
| 5013 | * is not "given" to the thread calling k_poll(): it merely signals the fact |
| 5014 | * that the object was available when the k_poll() call was in effect. Also, |
| 5015 | * all threads trying to acquire an object the regular way, i.e. by pending on |
| 5016 | * the object, have precedence over the thread polling on the object. This |
| 5017 | * means that the polling thread will never get the poll event on an object |
| 5018 | * until the object becomes available and its pend queue is empty. For this |
| 5019 | * reason, the k_poll() call is more effective when the objects being polled |
| 5020 | * only have one thread, the polling thread, trying to acquire them. |
| 5021 | * |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 5022 | * When k_poll() returns 0, the caller should loop on all the events that were |
| 5023 | * passed to k_poll() and check the state field for the values that were |
| 5024 | * expected and take the associated actions. |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 5025 | * |
| 5026 | * Before being reused for another call to k_poll(), the user has to reset the |
| 5027 | * state field to K_POLL_STATE_NOT_READY. |
| 5028 | * |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 5029 | * When called from user mode, a temporary memory allocation is required from |
| 5030 | * the caller's resource pool. |
| 5031 | * |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 5032 | * @param events An array of pointers to events to be polled for. |
| 5033 | * @param num_events The number of events in the array. |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 5034 | * @param timeout Waiting period for an event to be ready, |
| 5035 | * or one of the special values K_NO_WAIT and K_FOREVER. |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 5036 | * |
| 5037 | * @retval 0 One or more events are ready. |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 5038 | * @retval -EAGAIN Waiting period timed out. |
Paul Sokolovsky | 45c0b20 | 2018-08-21 23:29:11 +0300 | [diff] [blame] | 5039 | * @retval -EINTR Polling has been interrupted, e.g. with |
| 5040 | * k_queue_cancel_wait(). All output events are still set and valid, |
| 5041 | * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other |
| 5042 | * words, -EINTR status means that at least one of output events is |
| 5043 | * K_POLL_STATE_CANCELLED. |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 5044 | * @retval -ENOMEM Thread resource pool insufficient memory (user mode only) |
| 5045 | * @retval -EINVAL Bad parameters (user mode only) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 5046 | */ |
| 5047 | |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 5048 | __syscall int k_poll(struct k_poll_event *events, int num_events, |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 5049 | k_timeout_t timeout); |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 5050 | |
| 5051 | /** |
Benjamin Walsh | a304f16 | 2017-02-02 16:46:09 -0500 | [diff] [blame] | 5052 | * @brief Initialize a poll signal object. |
| 5053 | * |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 5054 | * Ready a poll signal object to be signaled via k_poll_signal_raise(). |
Benjamin Walsh | a304f16 | 2017-02-02 16:46:09 -0500 | [diff] [blame] | 5055 | * |
| 5056 | * @param signal A poll signal. |
| 5057 | * |
| 5058 | * @return N/A |
| 5059 | */ |
| 5060 | |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 5061 | __syscall void k_poll_signal_init(struct k_poll_signal *signal); |
| 5062 | |
| 5063 | /* |
| 5064 | * @brief Reset a poll signal object's state to unsignaled. |
| 5065 | * |
| 5066 | * @param signal A poll signal object |
| 5067 | */ |
| 5068 | __syscall void k_poll_signal_reset(struct k_poll_signal *signal); |
| 5069 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 5070 | static inline void z_impl_k_poll_signal_reset(struct k_poll_signal *signal) |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 5071 | { |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 5072 | signal->signaled = 0U; |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 5073 | } |
| 5074 | |
| 5075 | /** |
David B. Kinder | fcbd8fb | 2018-05-23 12:06:24 -0700 | [diff] [blame] | 5076 | * @brief Fetch the signaled state and result value of a poll signal |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 5077 | * |
| 5078 | * @param signal A poll signal object |
| 5079 | * @param signaled An integer buffer which will be written nonzero if the |
| 5080 | * object was signaled |
| 5081 | * @param result An integer destination buffer which will be written with the |
David B. Kinder | fcbd8fb | 2018-05-23 12:06:24 -0700 | [diff] [blame] | 5082 | * result value if the object was signaled, or an undefined |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 5083 | * value if it was not. |
| 5084 | */ |
| 5085 | __syscall void k_poll_signal_check(struct k_poll_signal *signal, |
| 5086 | unsigned int *signaled, int *result); |
Benjamin Walsh | a304f16 | 2017-02-02 16:46:09 -0500 | [diff] [blame] | 5087 | |
| 5088 | /** |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 5089 | * @brief Signal a poll signal object. |
| 5090 | * |
| 5091 | * This routine makes ready a poll signal, which is basically a poll event of |
| 5092 | * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be |
| 5093 | * made ready to run. A @a result value can be specified. |
| 5094 | * |
| 5095 | * The poll signal contains a 'signaled' field that, when set by |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 5096 | * k_poll_signal_raise(), stays set until the user sets it back to 0 with |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 5097 | * k_poll_signal_reset(). It thus has to be reset by the user before being |
| 5098 | * passed again to k_poll() or k_poll() will consider it being signaled, and |
| 5099 | * will return immediately. |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 5100 | * |
Peter A. Bigot | 773bd98 | 2019-04-30 07:06:39 -0500 | [diff] [blame] | 5101 | * @note The result is stored and the 'signaled' field is set even if |
| 5102 | * this function returns an error indicating that an expiring poll was |
| 5103 | * not notified. The next k_poll() will detect the missed raise. |
| 5104 | * |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 5105 | * @param signal A poll signal. |
| 5106 | * @param result The value to store in the result field of the signal. |
| 5107 | * |
| 5108 | * @retval 0 The signal was delivered successfully. |
| 5109 | * @retval -EAGAIN The polling thread's timeout is in the process of expiring. |
| 5110 | */ |
| 5111 | |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 5112 | __syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result); |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 5113 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 5114 | /** |
| 5115 | * @internal |
| 5116 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 5117 | extern void z_handle_obj_poll_events(sys_dlist_t *events, u32_t state); |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 5118 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 5119 | /** @} */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 5120 | |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 5121 | /** |
Anas Nashif | 30c3cff | 2019-01-22 08:18:13 -0500 | [diff] [blame] | 5122 | * @defgroup cpu_idle_apis CPU Idling APIs |
| 5123 | * @ingroup kernel_apis |
| 5124 | * @{ |
| 5125 | */ |
Anas Nashif | 30c3cff | 2019-01-22 08:18:13 -0500 | [diff] [blame] | 5126 | /** |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 5127 | * @brief Make the CPU idle. |
| 5128 | * |
| 5129 | * This function makes the CPU idle until an event wakes it up. |
| 5130 | * |
| 5131 | * In a regular system, the idle thread should be the only thread responsible |
| 5132 | * for making the CPU idle and triggering any type of power management. |
| 5133 | * However, in some more constrained systems, such as a single-threaded system, |
| 5134 | * the only thread would be responsible for this if needed. |
| 5135 | * |
Ioannis Glaropoulos | 91f6d98 | 2020-03-18 23:56:56 +0100 | [diff] [blame] | 5136 | * @note In some architectures, before returning, the function unmasks interrupts |
| 5137 | * unconditionally. |
| 5138 | * |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 5139 | * @return N/A |
| 5140 | */ |
Andrew Boie | 07525a3 | 2019-09-21 16:17:23 -0700 | [diff] [blame] | 5141 | static inline void k_cpu_idle(void) |
| 5142 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 5143 | arch_cpu_idle(); |
Andrew Boie | 07525a3 | 2019-09-21 16:17:23 -0700 | [diff] [blame] | 5144 | } |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 5145 | |
| 5146 | /** |
| 5147 | * @brief Make the CPU idle in an atomic fashion. |
| 5148 | * |
| 5149 | * Similar to k_cpu_idle(), but called with interrupts locked if operations |
| 5150 | * must be done atomically before making the CPU idle. |
| 5151 | * |
| 5152 | * @param key Interrupt locking key obtained from irq_lock(). |
| 5153 | * |
| 5154 | * @return N/A |
| 5155 | */ |
Andrew Boie | 07525a3 | 2019-09-21 16:17:23 -0700 | [diff] [blame] | 5156 | static inline void k_cpu_atomic_idle(unsigned int key) |
| 5157 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 5158 | arch_cpu_atomic_idle(key); |
Andrew Boie | 07525a3 | 2019-09-21 16:17:23 -0700 | [diff] [blame] | 5159 | } |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 5160 | |
Anas Nashif | 30c3cff | 2019-01-22 08:18:13 -0500 | [diff] [blame] | 5161 | /** |
| 5162 | * @} |
| 5163 | */ |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 5164 | |
| 5165 | /** |
| 5166 | * @internal |
| 5167 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 5168 | extern void z_sys_power_save_idle_exit(s32_t ticks); |
Andrew Boie | 350f88d | 2017-01-18 13:13:45 -0800 | [diff] [blame] | 5169 | |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 5170 | #ifdef ARCH_EXCEPT |
Ioannis Glaropoulos | df02923 | 2019-10-07 11:24:36 +0200 | [diff] [blame] | 5171 | /* This architecture has direct support for triggering a CPU exception */ |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 5172 | #define z_except_reason(reason) ARCH_EXCEPT(reason) |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 5173 | #else |
| 5174 | |
Joakim Andersson | e04e4c2 | 2019-12-20 15:42:38 +0100 | [diff] [blame] | 5175 | #if !defined(CONFIG_ASSERT_NO_FILE_INFO) |
| 5176 | #define __EXCEPT_LOC() __ASSERT_PRINT("@ %s:%d\n", __FILE__, __LINE__) |
| 5177 | #else |
| 5178 | #define __EXCEPT_LOC() |
| 5179 | #endif |
| 5180 | |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 5181 | /* NOTE: This is the implementation for arches that do not implement |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 5182 | * ARCH_EXCEPT() to generate a real CPU exception. |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 5183 | * |
| 5184 | * We won't have a real exception frame to determine the PC value when |
| 5185 | * the oops occurred, so print file and line number before we jump into |
| 5186 | * the fatal error handler. |
| 5187 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 5188 | #define z_except_reason(reason) do { \ |
Joakim Andersson | e04e4c2 | 2019-12-20 15:42:38 +0100 | [diff] [blame] | 5189 | __EXCEPT_LOC(); \ |
Andrew Boie | 5623637 | 2019-07-15 15:22:29 -0700 | [diff] [blame] | 5190 | z_fatal_error(reason, NULL); \ |
Flavio Ceolin | 6fdc56d | 2018-09-18 12:32:27 -0700 | [diff] [blame] | 5191 | } while (false) |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 5192 | |
| 5193 | #endif /* _ARCH__EXCEPT */ |
| 5194 | |
| 5195 | /** |
| 5196 | * @brief Fatally terminate a thread |
| 5197 | * |
| 5198 | * This should be called when a thread has encountered an unrecoverable |
| 5199 | * runtime condition and needs to terminate. What this ultimately |
| 5200 | * means is determined by the _fatal_error_handler() implementation, which |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 5201 | * will be called will reason code K_ERR_KERNEL_OOPS. |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 5202 | * |
| 5203 | * If this is called from ISR context, the default system fatal error handler |
| 5204 | * will treat it as an unrecoverable system error, just like k_panic(). |
| 5205 | */ |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 5206 | #define k_oops() z_except_reason(K_ERR_KERNEL_OOPS) |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 5207 | |
| 5208 | /** |
| 5209 | * @brief Fatally terminate the system |
| 5210 | * |
| 5211 | * This should be called when the Zephyr kernel has encountered an |
| 5212 | * unrecoverable runtime condition and needs to terminate. What this ultimately |
| 5213 | * means is determined by the _fatal_error_handler() implementation, which |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 5214 | * will be called will reason code K_ERR_KERNEL_PANIC. |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 5215 | */ |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 5216 | #define k_panic() z_except_reason(K_ERR_KERNEL_PANIC) |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 5217 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 5218 | /* |
| 5219 | * private APIs that are utilized by one or more public APIs |
| 5220 | */ |
| 5221 | |
Stephanos Ioannidis | 2d74604 | 2019-10-25 00:08:21 +0900 | [diff] [blame] | 5222 | /** |
| 5223 | * @internal |
| 5224 | */ |
| 5225 | extern void z_init_thread_base(struct _thread_base *thread_base, |
| 5226 | int priority, u32_t initial_state, |
| 5227 | unsigned int options); |
| 5228 | |
Benjamin Walsh | b12a8e0 | 2016-12-14 15:24:12 -0500 | [diff] [blame] | 5229 | #ifdef CONFIG_MULTITHREADING |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 5230 | /** |
| 5231 | * @internal |
| 5232 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 5233 | extern void z_init_static_threads(void); |
Benjamin Walsh | b12a8e0 | 2016-12-14 15:24:12 -0500 | [diff] [blame] | 5234 | #else |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 5235 | /** |
| 5236 | * @internal |
| 5237 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 5238 | #define z_init_static_threads() do { } while (false) |
Benjamin Walsh | b12a8e0 | 2016-12-14 15:24:12 -0500 | [diff] [blame] | 5239 | #endif |
| 5240 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 5241 | /** |
| 5242 | * @internal |
| 5243 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 5244 | extern bool z_is_thread_essential(void); |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 5245 | /** |
| 5246 | * @internal |
| 5247 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 5248 | extern void z_timer_expiration_handler(struct _timeout *t); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 5249 | |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5250 | /** |
| 5251 | * @defgroup mem_domain_apis Memory domain APIs |
| 5252 | * @ingroup kernel_apis |
| 5253 | * @{ |
| 5254 | */ |
| 5255 | |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5256 | /** |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5257 | * @def K_MEM_PARTITION_DEFINE |
| 5258 | * @brief Used to declare a memory partition |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5259 | */ |
| 5260 | #ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK |
| 5261 | #define K_MEM_PARTITION_DEFINE(name, start, size, attr) \ |
| 5262 | _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \ |
Andrew Boie | 41f6011 | 2019-01-31 15:53:24 -0800 | [diff] [blame] | 5263 | struct k_mem_partition name =\ |
Nicolas Pitre | 58d839b | 2019-05-21 11:32:21 -0400 | [diff] [blame] | 5264 | { (uintptr_t)start, size, attr} |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5265 | #else |
| 5266 | #define K_MEM_PARTITION_DEFINE(name, start, size, attr) \ |
Andrew Boie | 41f6011 | 2019-01-31 15:53:24 -0800 | [diff] [blame] | 5267 | struct k_mem_partition name =\ |
Nicolas Pitre | 58d839b | 2019-05-21 11:32:21 -0400 | [diff] [blame] | 5268 | { (uintptr_t)start, size, attr} |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5269 | #endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */ |
| 5270 | |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5271 | /* memory partition */ |
| 5272 | struct k_mem_partition { |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 5273 | /** start address of memory partition */ |
Nicolas Pitre | 58d839b | 2019-05-21 11:32:21 -0400 | [diff] [blame] | 5274 | uintptr_t start; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 5275 | /** size of memory partition */ |
Andrew Boie | a824821 | 2019-11-13 12:10:56 -0800 | [diff] [blame] | 5276 | size_t size; |
Ioannis Glaropoulos | 39bf24a | 2018-11-27 15:45:36 +0100 | [diff] [blame] | 5277 | #if defined(CONFIG_MEMORY_PROTECTION) |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 5278 | /** attribute of memory partition */ |
Adithya Baglody | 83bedcc | 2017-10-06 15:43:11 +0530 | [diff] [blame] | 5279 | k_mem_partition_attr_t attr; |
Ioannis Glaropoulos | 39bf24a | 2018-11-27 15:45:36 +0100 | [diff] [blame] | 5280 | #endif /* CONFIG_MEMORY_PROTECTION */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5281 | }; |
| 5282 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 5283 | /** |
| 5284 | * @brief Memory Domain |
| 5285 | * |
Adithya Baglody | 3a6d72e | 2018-02-13 16:44:44 +0530 | [diff] [blame] | 5286 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5287 | struct k_mem_domain { |
Adithya Baglody | 83bedcc | 2017-10-06 15:43:11 +0530 | [diff] [blame] | 5288 | #ifdef CONFIG_USERSPACE |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 5289 | /** partitions in the domain */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5290 | struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS]; |
Adithya Baglody | 83bedcc | 2017-10-06 15:43:11 +0530 | [diff] [blame] | 5291 | #endif /* CONFIG_USERSPACE */ |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 5292 | /** domain q */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5293 | sys_dlist_t mem_domain_q; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 5294 | /** number of partitions in the domain */ |
Leandro Pereira | 08de658 | 2018-02-28 14:22:57 -0800 | [diff] [blame] | 5295 | u8_t num_partitions; |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5296 | }; |
Adithya Baglody | 83bedcc | 2017-10-06 15:43:11 +0530 | [diff] [blame] | 5297 | |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5298 | |
| 5299 | /** |
| 5300 | * @brief Initialize a memory domain. |
| 5301 | * |
| 5302 | * Initialize a memory domain with given name and memory partitions. |
| 5303 | * |
Andrew Boie | fddd550 | 2019-07-30 18:07:32 -0700 | [diff] [blame] | 5304 | * See documentation for k_mem_domain_add_partition() for details about |
| 5305 | * partition constraints. |
| 5306 | * |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5307 | * @param domain The memory domain to be initialized. |
| 5308 | * @param num_parts The number of array items of "parts" parameter. |
| 5309 | * @param parts An array of pointers to the memory partitions. Can be NULL |
| 5310 | * if num_parts is zero. |
| 5311 | */ |
Leandro Pereira | 08de658 | 2018-02-28 14:22:57 -0800 | [diff] [blame] | 5312 | extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts, |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5313 | struct k_mem_partition *parts[]); |
| 5314 | /** |
| 5315 | * @brief Destroy a memory domain. |
| 5316 | * |
| 5317 | * Destroy a memory domain. |
| 5318 | * |
| 5319 | * @param domain The memory domain to be destroyed. |
| 5320 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5321 | extern void k_mem_domain_destroy(struct k_mem_domain *domain); |
| 5322 | |
| 5323 | /** |
| 5324 | * @brief Add a memory partition into a memory domain. |
| 5325 | * |
Andrew Boie | fddd550 | 2019-07-30 18:07:32 -0700 | [diff] [blame] | 5326 | * Add a memory partition into a memory domain. Partitions must conform to |
| 5327 | * the following constraints: |
| 5328 | * |
| 5329 | * - Partition bounds must be within system RAM boundaries on MMU-based |
| 5330 | * systems. |
| 5331 | * - Partitions in the same memory domain may not overlap each other. |
| 5332 | * - Partitions must not be defined which expose private kernel |
| 5333 | * data structures or kernel objects. |
| 5334 | * - The starting address alignment, and the partition size must conform to |
| 5335 | * the constraints of the underlying memory management hardware, which |
| 5336 | * varies per architecture. |
| 5337 | * - Memory domain partitions are only intended to control access to memory |
| 5338 | * from user mode threads. |
| 5339 | * |
| 5340 | * Violating these constraints may lead to CPU exceptions or undefined |
| 5341 | * behavior. |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5342 | * |
| 5343 | * @param domain The memory domain to be added a memory partition. |
| 5344 | * @param part The memory partition to be added |
| 5345 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5346 | extern void k_mem_domain_add_partition(struct k_mem_domain *domain, |
| 5347 | struct k_mem_partition *part); |
| 5348 | |
| 5349 | /** |
| 5350 | * @brief Remove a memory partition from a memory domain. |
| 5351 | * |
| 5352 | * Remove a memory partition from a memory domain. |
| 5353 | * |
| 5354 | * @param domain The memory domain to be removed a memory partition. |
| 5355 | * @param part The memory partition to be removed |
| 5356 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5357 | extern void k_mem_domain_remove_partition(struct k_mem_domain *domain, |
| 5358 | struct k_mem_partition *part); |
| 5359 | |
| 5360 | /** |
| 5361 | * @brief Add a thread into a memory domain. |
| 5362 | * |
| 5363 | * Add a thread into a memory domain. |
| 5364 | * |
| 5365 | * @param domain The memory domain that the thread is going to be added into. |
| 5366 | * @param thread ID of thread going to be added into the memory domain. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5367 | * |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5368 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5369 | extern void k_mem_domain_add_thread(struct k_mem_domain *domain, |
| 5370 | k_tid_t thread); |
| 5371 | |
| 5372 | /** |
| 5373 | * @brief Remove a thread from its memory domain. |
| 5374 | * |
| 5375 | * Remove a thread from its memory domain. |
| 5376 | * |
| 5377 | * @param thread ID of thread going to be removed from its memory domain. |
| 5378 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5379 | extern void k_mem_domain_remove_thread(k_tid_t thread); |
| 5380 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 5381 | /** @} */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5382 | |
Andrew Boie | d76ae46 | 2020-01-02 11:57:43 -0800 | [diff] [blame] | 5383 | #ifdef CONFIG_PRINTK |
Andrew Boie | 756f907 | 2017-10-10 16:01:49 -0700 | [diff] [blame] | 5384 | /** |
| 5385 | * @brief Emit a character buffer to the console device |
| 5386 | * |
| 5387 | * @param c String of characters to print |
| 5388 | * @param n The length of the string |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5389 | * |
Andrew Boie | 756f907 | 2017-10-10 16:01:49 -0700 | [diff] [blame] | 5390 | */ |
| 5391 | __syscall void k_str_out(char *c, size_t n); |
Andrew Boie | d76ae46 | 2020-01-02 11:57:43 -0800 | [diff] [blame] | 5392 | #endif |
Andrew Boie | 756f907 | 2017-10-10 16:01:49 -0700 | [diff] [blame] | 5393 | |
Ioannis Glaropoulos | a6cb8b0 | 2019-05-09 21:55:10 +0200 | [diff] [blame] | 5394 | /** |
| 5395 | * @brief Disable preservation of floating point context information. |
| 5396 | * |
| 5397 | * This routine informs the kernel that the specified thread |
| 5398 | * will no longer be using the floating point registers. |
| 5399 | * |
| 5400 | * @warning |
| 5401 | * Some architectures apply restrictions on how the disabling of floating |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 5402 | * point preservation may be requested, see arch_float_disable. |
Ioannis Glaropoulos | a6cb8b0 | 2019-05-09 21:55:10 +0200 | [diff] [blame] | 5403 | * |
| 5404 | * @warning |
| 5405 | * This routine should only be used to disable floating point support for |
| 5406 | * a thread that currently has such support enabled. |
| 5407 | * |
| 5408 | * @param thread ID of thread. |
| 5409 | * |
| 5410 | * @retval 0 On success. |
| 5411 | * @retval -ENOSYS If the floating point disabling is not implemented. |
| 5412 | * -EINVAL If the floating point disabling could not be performed. |
| 5413 | */ |
| 5414 | __syscall int k_float_disable(struct k_thread *thread); |
| 5415 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 5416 | #ifdef __cplusplus |
| 5417 | } |
| 5418 | #endif |
| 5419 | |
Anas Nashif | 73008b4 | 2020-02-06 09:14:51 -0500 | [diff] [blame] | 5420 | #include <tracing/tracing.h> |
Andrew Boie | fa94ee7 | 2017-09-28 16:54:35 -0700 | [diff] [blame] | 5421 | #include <syscalls/kernel.h> |
| 5422 | |
Benjamin Walsh | dfa7ce5 | 2017-01-22 17:06:05 -0500 | [diff] [blame] | 5423 | #endif /* !_ASMLANGUAGE */ |
| 5424 | |
Flavio Ceolin | 67ca176 | 2018-09-14 10:43:44 -0700 | [diff] [blame] | 5425 | #endif /* ZEPHYR_INCLUDE_KERNEL_H_ */ |