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 | |
Andy Ross | 225c74b | 2018-06-27 11:20:50 -0700 | [diff] [blame] | 79 | #ifdef CONFIG_WAITQ_SCALABLE |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 80 | |
| 81 | typedef struct { |
| 82 | struct _priq_rb waitq; |
| 83 | } _wait_q_t; |
| 84 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 85 | extern bool z_priq_rb_lessthan(struct rbnode *a, struct rbnode *b); |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 86 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 87 | #define Z_WAIT_Q_INIT(wait_q) { { { .lessthan_fn = z_priq_rb_lessthan } } } |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 88 | |
| 89 | #else |
| 90 | |
Andy Ross | ccf3bf7 | 2018-05-10 11:10:34 -0700 | [diff] [blame] | 91 | typedef struct { |
| 92 | sys_dlist_t waitq; |
| 93 | } _wait_q_t; |
| 94 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 95 | #define Z_WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) } |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 96 | |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 97 | #endif |
| 98 | |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 99 | #ifdef CONFIG_OBJECT_TRACING |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 100 | #define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next; |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 101 | #define _OBJECT_TRACING_LINKED_FLAG u8_t __linked; |
| 102 | #define _OBJECT_TRACING_INIT \ |
| 103 | .__next = NULL, \ |
| 104 | .__linked = 0, |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 105 | #else |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 106 | #define _OBJECT_TRACING_INIT |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 107 | #define _OBJECT_TRACING_NEXT_PTR(type) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 108 | #define _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 109 | #endif |
| 110 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 111 | #ifdef CONFIG_POLL |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 112 | #define _POLL_EVENT_OBJ_INIT(obj) \ |
| 113 | .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), |
| 114 | #define _POLL_EVENT sys_dlist_t poll_events |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 115 | #else |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 116 | #define _POLL_EVENT_OBJ_INIT(obj) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 117 | #define _POLL_EVENT |
| 118 | #endif |
| 119 | |
Benjamin Walsh | f6ca7de | 2016-11-08 10:36:50 -0500 | [diff] [blame] | 120 | struct k_thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 121 | struct k_mutex; |
| 122 | struct k_sem; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 123 | struct k_msgq; |
| 124 | struct k_mbox; |
| 125 | struct k_pipe; |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 126 | struct k_queue; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 127 | struct k_fifo; |
| 128 | struct k_lifo; |
| 129 | struct k_stack; |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 130 | struct k_mem_slab; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 131 | struct k_mem_pool; |
| 132 | struct k_timer; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 133 | struct k_poll_event; |
| 134 | struct k_poll_signal; |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 135 | struct k_mem_domain; |
| 136 | struct k_mem_partition; |
Wentong Wu | 5611e92 | 2019-06-20 23:51:27 +0800 | [diff] [blame] | 137 | struct k_futex; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 138 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 139 | /** |
| 140 | * @brief Kernel Object Types |
| 141 | * |
| 142 | * 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] | 143 | * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str() |
| 144 | * function in kernel/userspace.c |
| 145 | */ |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 146 | enum k_objects { |
Andrew Boie | 7e3d3d7 | 2017-10-10 09:31:32 -0700 | [diff] [blame] | 147 | K_OBJ_ANY, |
| 148 | |
Leandro Pereira | c200367 | 2018-04-04 13:50:32 -0700 | [diff] [blame] | 149 | /** @cond |
| 150 | * Doxygen should ignore this build-time generated include file |
| 151 | * when genrating API documentation. Enumeration values are |
| 152 | * generated during build by gen_kobject_list.py. It includes |
| 153 | * basic kernel objects (e.g. pipes and mutexes) and driver types. |
| 154 | */ |
| 155 | #include <kobj-types-enum.h> |
| 156 | /** @endcond |
| 157 | */ |
Andrew Boie | 5bd891d | 2017-09-27 12:59:28 -0700 | [diff] [blame] | 158 | |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 159 | K_OBJ_LAST |
| 160 | }; |
Anas Nashif | 4bcb294 | 2019-01-23 23:06:29 -0500 | [diff] [blame] | 161 | /** |
| 162 | * @defgroup usermode_apis User Mode APIs |
| 163 | * @ingroup kernel_apis |
| 164 | * @{ |
| 165 | */ |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 166 | |
| 167 | #ifdef CONFIG_USERSPACE |
| 168 | /* Table generated by gperf, these objects are retrieved via |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 169 | * z_object_find() */ |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 170 | struct _k_object { |
Andrew Boie | 22553a7 | 2019-11-19 18:27:42 -0800 | [diff] [blame] | 171 | void *name; |
Andrew Boie | a811af3 | 2017-10-14 13:50:26 -0700 | [diff] [blame] | 172 | u8_t perms[CONFIG_MAX_THREAD_BYTES]; |
| 173 | u8_t type; |
| 174 | u8_t flags; |
Andrew Boie | e48ed6a | 2019-11-13 12:52:00 -0800 | [diff] [blame] | 175 | uintptr_t data; |
Andrew Boie | df55524 | 2018-05-25 07:28:54 -0700 | [diff] [blame] | 176 | } __packed __aligned(4); |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 177 | |
Andrew Boie | 877f82e | 2017-10-17 11:20:22 -0700 | [diff] [blame] | 178 | struct _k_object_assignment { |
| 179 | struct k_thread *thread; |
| 180 | void * const *objects; |
| 181 | }; |
| 182 | |
| 183 | /** |
| 184 | * @brief Grant a static thread access to a list of kernel objects |
| 185 | * |
| 186 | * For threads declared with K_THREAD_DEFINE(), grant the thread access to |
| 187 | * a set of kernel objects. These objects do not need to be in an initialized |
| 188 | * state. The permissions will be granted when the threads are initialized |
| 189 | * in the early boot sequence. |
| 190 | * |
| 191 | * All arguments beyond the first must be pointers to kernel objects. |
| 192 | * |
| 193 | * @param name_ Name of the thread, as passed to K_THREAD_DEFINE() |
| 194 | */ |
| 195 | #define K_THREAD_ACCESS_GRANT(name_, ...) \ |
| 196 | static void * const _CONCAT(_object_list_, name_)[] = \ |
| 197 | { __VA_ARGS__, NULL }; \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 198 | static const Z_STRUCT_SECTION_ITERABLE(_k_object_assignment, \ |
| 199 | _CONCAT(_object_access_, name_)) = \ |
Andrew Boie | 877f82e | 2017-10-17 11:20:22 -0700 | [diff] [blame] | 200 | { (&_k_thread_obj_ ## name_), \ |
| 201 | (_CONCAT(_object_list_, name_)) } |
| 202 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 203 | /** Object initialized */ |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 204 | #define K_OBJ_FLAG_INITIALIZED BIT(0) |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 205 | /** Object is Public */ |
Andrew Boie | 04caa67 | 2017-10-13 13:57:07 -0700 | [diff] [blame] | 206 | #define K_OBJ_FLAG_PUBLIC BIT(1) |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 207 | /** Object allocated */ |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 208 | #define K_OBJ_FLAG_ALLOC BIT(2) |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 209 | /** Driver Object */ |
Andrew Boie | 7875707 | 2019-07-23 13:29:30 -0700 | [diff] [blame] | 210 | #define K_OBJ_FLAG_DRIVER BIT(3) |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 211 | |
| 212 | /** |
| 213 | * Lookup a kernel object and init its metadata if it exists |
| 214 | * |
| 215 | * Calling this on an object will make it usable from userspace. |
| 216 | * Intended to be called as the last statement in kernel object init |
| 217 | * functions. |
| 218 | * |
Anas Nashif | 50e3acd | 2018-12-08 13:26:18 -0500 | [diff] [blame] | 219 | * @param obj Address of the kernel object |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 220 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 221 | void z_object_init(void *obj); |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 222 | #else |
Andrew Boie | c3d4e65 | 2019-06-28 14:19:16 -0700 | [diff] [blame] | 223 | /* LCOV_EXCL_START */ |
Andrew Boie | 877f82e | 2017-10-17 11:20:22 -0700 | [diff] [blame] | 224 | #define K_THREAD_ACCESS_GRANT(thread, ...) |
| 225 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 226 | /** |
| 227 | * @internal |
| 228 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 229 | static inline void z_object_init(void *obj) |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 230 | { |
| 231 | ARG_UNUSED(obj); |
| 232 | } |
| 233 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 234 | /** |
| 235 | * @internal |
| 236 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 237 | static inline void z_impl_k_object_access_grant(void *object, |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 238 | struct k_thread *thread) |
| 239 | { |
| 240 | ARG_UNUSED(object); |
| 241 | ARG_UNUSED(thread); |
| 242 | } |
| 243 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 244 | /** |
| 245 | * @internal |
| 246 | */ |
Andrew Boie | e9cfc54 | 2018-04-13 13:15:28 -0700 | [diff] [blame] | 247 | static inline void k_object_access_revoke(void *object, |
| 248 | struct k_thread *thread) |
Andrew Boie | a89bf01 | 2017-10-09 14:47:55 -0700 | [diff] [blame] | 249 | { |
| 250 | ARG_UNUSED(object); |
| 251 | ARG_UNUSED(thread); |
| 252 | } |
| 253 | |
Andrew Boie | e9cfc54 | 2018-04-13 13:15:28 -0700 | [diff] [blame] | 254 | /** |
| 255 | * @internal |
| 256 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 257 | static inline void z_impl_k_object_release(void *object) |
Andrew Boie | e9cfc54 | 2018-04-13 13:15:28 -0700 | [diff] [blame] | 258 | { |
| 259 | ARG_UNUSED(object); |
| 260 | } |
| 261 | |
Andrew Boie | 41bab6e | 2017-10-14 14:42:23 -0700 | [diff] [blame] | 262 | static inline void k_object_access_all_grant(void *object) |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 263 | { |
| 264 | ARG_UNUSED(object); |
| 265 | } |
Andrew Boie | c3d4e65 | 2019-06-28 14:19:16 -0700 | [diff] [blame] | 266 | /* LCOV_EXCL_STOP */ |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 267 | #endif /* !CONFIG_USERSPACE */ |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 268 | |
| 269 | /** |
Marti Bolivar | 67db616 | 2019-08-27 19:12:51 -0600 | [diff] [blame] | 270 | * Grant a thread access to a kernel object |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 271 | * |
| 272 | * The thread will be granted access to the object if the caller is from |
| 273 | * supervisor mode, or the caller is from user mode AND has permissions |
Andrew Boie | a89bf01 | 2017-10-09 14:47:55 -0700 | [diff] [blame] | 274 | * on both the object and the thread whose access is being granted. |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 275 | * |
| 276 | * @param object Address of kernel object |
| 277 | * @param thread Thread to grant access to the object |
| 278 | */ |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 279 | __syscall void k_object_access_grant(void *object, struct k_thread *thread); |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 280 | |
Andrew Boie | a89bf01 | 2017-10-09 14:47:55 -0700 | [diff] [blame] | 281 | /** |
Marti Bolivar | 67db616 | 2019-08-27 19:12:51 -0600 | [diff] [blame] | 282 | * Revoke a thread's access to a kernel object |
Andrew Boie | a89bf01 | 2017-10-09 14:47:55 -0700 | [diff] [blame] | 283 | * |
| 284 | * The thread will lose access to the object if the caller is from |
| 285 | * supervisor mode, or the caller is from user mode AND has permissions |
| 286 | * on both the object and the thread whose access is being revoked. |
| 287 | * |
| 288 | * @param object Address of kernel object |
| 289 | * @param thread Thread to remove access to the object |
| 290 | */ |
Andrew Boie | e9cfc54 | 2018-04-13 13:15:28 -0700 | [diff] [blame] | 291 | void k_object_access_revoke(void *object, struct k_thread *thread); |
| 292 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 293 | /** |
| 294 | * @brief Release an object |
| 295 | * |
| 296 | * Allows user threads to drop their own permission on an object |
| 297 | * Their permissions are automatically cleared when a thread terminates. |
| 298 | * |
| 299 | * @param object The object to be released |
| 300 | * |
| 301 | */ |
Andrew Boie | e9cfc54 | 2018-04-13 13:15:28 -0700 | [diff] [blame] | 302 | __syscall void k_object_release(void *object); |
Andrew Boie | 3b5ae80 | 2017-10-04 12:10:32 -0700 | [diff] [blame] | 303 | |
| 304 | /** |
Marti Bolivar | 67db616 | 2019-08-27 19:12:51 -0600 | [diff] [blame] | 305 | * Grant all present and future threads access to an object |
Andrew Boie | 3b5ae80 | 2017-10-04 12:10:32 -0700 | [diff] [blame] | 306 | * |
| 307 | * If the caller is from supervisor mode, or the caller is from user mode and |
| 308 | * have sufficient permissions on the object, then that object will have |
| 309 | * permissions granted to it for *all* current and future threads running in |
| 310 | * the system, effectively becoming a public kernel object. |
| 311 | * |
| 312 | * Use of this API should be avoided on systems that are running untrusted code |
| 313 | * as it is possible for such code to derive the addresses of kernel objects |
| 314 | * and perform unwanted operations on them. |
| 315 | * |
Andrew Boie | 04caa67 | 2017-10-13 13:57:07 -0700 | [diff] [blame] | 316 | * It is not possible to revoke permissions on public objects; once public, |
| 317 | * any thread may use it. |
| 318 | * |
Andrew Boie | 3b5ae80 | 2017-10-04 12:10:32 -0700 | [diff] [blame] | 319 | * @param object Address of kernel object |
| 320 | */ |
Andrew Boie | 41bab6e | 2017-10-14 14:42:23 -0700 | [diff] [blame] | 321 | void k_object_access_all_grant(void *object); |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 322 | |
Andrew Boie | 31bdfc0 | 2017-11-08 16:38:03 -0800 | [diff] [blame] | 323 | /** |
| 324 | * Allocate a kernel object of a designated type |
| 325 | * |
| 326 | * This will instantiate at runtime a kernel object of the specified type, |
| 327 | * returning a pointer to it. The object will be returned in an uninitialized |
| 328 | * state, with the calling thread being granted permission on it. The memory |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 329 | * 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] | 330 | * |
| 331 | * Currently, allocation of thread stacks is not supported. |
| 332 | * |
| 333 | * @param otype Requested kernel object type |
| 334 | * @return A pointer to the allocated kernel object, or NULL if memory wasn't |
| 335 | * available |
| 336 | */ |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 337 | __syscall void *k_object_alloc(enum k_objects otype); |
Andrew Boie | 31bdfc0 | 2017-11-08 16:38:03 -0800 | [diff] [blame] | 338 | |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 339 | #ifdef CONFIG_DYNAMIC_OBJECTS |
Andrew Boie | 31bdfc0 | 2017-11-08 16:38:03 -0800 | [diff] [blame] | 340 | /** |
| 341 | * Free a kernel object previously allocated with k_object_alloc() |
| 342 | * |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 343 | * This will return memory for a kernel object back to resource pool it was |
| 344 | * allocated from. Care must be exercised that the object will not be used |
| 345 | * during or after when this call is made. |
Andrew Boie | 31bdfc0 | 2017-11-08 16:38:03 -0800 | [diff] [blame] | 346 | * |
| 347 | * @param obj Pointer to the kernel object memory address. |
| 348 | */ |
| 349 | void k_object_free(void *obj); |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 350 | #else |
Andrew Boie | c3d4e65 | 2019-06-28 14:19:16 -0700 | [diff] [blame] | 351 | /* LCOV_EXCL_START */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 352 | static inline void *z_impl_k_object_alloc(enum k_objects otype) |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 353 | { |
Kumar Gala | 85699f7 | 2018-05-17 09:26:03 -0500 | [diff] [blame] | 354 | ARG_UNUSED(otype); |
| 355 | |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 356 | return NULL; |
| 357 | } |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 358 | /** |
| 359 | * @brief Free an object |
| 360 | * |
| 361 | * @param obj |
| 362 | */ |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 363 | static inline void k_obj_free(void *obj) |
| 364 | { |
| 365 | ARG_UNUSED(obj); |
| 366 | } |
Andrew Boie | c3d4e65 | 2019-06-28 14:19:16 -0700 | [diff] [blame] | 367 | /* LCOV_EXCL_STOP */ |
Andrew Boie | 31bdfc0 | 2017-11-08 16:38:03 -0800 | [diff] [blame] | 368 | #endif /* CONFIG_DYNAMIC_OBJECTS */ |
| 369 | |
Anas Nashif | 4bcb294 | 2019-01-23 23:06:29 -0500 | [diff] [blame] | 370 | /** @} */ |
| 371 | |
Andrew Boie | bca15da | 2017-10-15 14:17:48 -0700 | [diff] [blame] | 372 | /* Using typedef deliberately here, this is quite intended to be an opaque |
Andrew Boie | 4e5c093 | 2019-04-04 12:05:28 -0700 | [diff] [blame] | 373 | * type. |
Andrew Boie | bca15da | 2017-10-15 14:17:48 -0700 | [diff] [blame] | 374 | * |
| 375 | * The purpose of this data type is to clearly distinguish between the |
| 376 | * declared symbol for a stack (of type k_thread_stack_t) and the underlying |
| 377 | * buffer which composes the stack data actually used by the underlying |
Anas Nashif | f2cb20c | 2019-06-18 14:45:40 -0400 | [diff] [blame] | 378 | * thread; they cannot be used interchangeably as some arches precede the |
Andrew Boie | bca15da | 2017-10-15 14:17:48 -0700 | [diff] [blame] | 379 | * stack buffer region with guard areas that trigger a MPU or MMU fault |
| 380 | * if written to. |
| 381 | * |
| 382 | * APIs that want to work with the buffer inside should continue to use |
| 383 | * char *. |
| 384 | * |
| 385 | * Stacks should always be created with K_THREAD_STACK_DEFINE(). |
| 386 | */ |
| 387 | struct __packed _k_thread_stack_element { |
| 388 | char data; |
| 389 | }; |
Daniel Leung | 7476a6e | 2019-11-25 13:58:40 -0800 | [diff] [blame] | 390 | |
| 391 | /** |
| 392 | * @typedef k_thread_stack_t |
| 393 | * @brief Typedef of struct _k_thread_stack_element |
| 394 | * |
| 395 | * @see _k_thread_stack_element |
| 396 | */ |
Andrew Boie | bca15da | 2017-10-15 14:17:48 -0700 | [diff] [blame] | 397 | |
Andrew Boie | 1e06ffc | 2017-09-11 09:30:04 -0700 | [diff] [blame] | 398 | /** |
| 399 | * @typedef k_thread_entry_t |
| 400 | * @brief Thread entry point function type. |
| 401 | * |
| 402 | * A thread's entry point function is invoked when the thread starts executing. |
| 403 | * Up to 3 argument values can be passed to the function. |
| 404 | * |
| 405 | * The thread terminates execution permanently if the entry point function |
| 406 | * returns. The thread is responsible for releasing any shared resources |
| 407 | * it may own (such as mutexes and dynamically allocated memory), prior to |
| 408 | * returning. |
| 409 | * |
| 410 | * @param p1 First argument. |
| 411 | * @param p2 Second argument. |
| 412 | * @param p3 Third argument. |
| 413 | * |
| 414 | * @return N/A |
| 415 | */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 416 | |
| 417 | #ifdef CONFIG_THREAD_MONITOR |
| 418 | struct __thread_entry { |
Andrew Boie | 1e06ffc | 2017-09-11 09:30:04 -0700 | [diff] [blame] | 419 | k_thread_entry_t pEntry; |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 420 | void *parameter1; |
| 421 | void *parameter2; |
| 422 | void *parameter3; |
| 423 | }; |
| 424 | #endif |
| 425 | |
| 426 | /* can be used for creating 'dummy' threads, e.g. for pending on objects */ |
| 427 | struct _thread_base { |
| 428 | |
| 429 | /* this thread's entry in a ready/wait queue */ |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 430 | union { |
Peter A. Bigot | 82ad0d2 | 2019-01-03 23:49:28 -0600 | [diff] [blame] | 431 | sys_dnode_t qnode_dlist; |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 432 | struct rbnode qnode_rb; |
| 433 | }; |
| 434 | |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 435 | /* wait queue on which the thread is pended (needed only for |
| 436 | * trees, not dumb lists) |
| 437 | */ |
| 438 | _wait_q_t *pended_on; |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 439 | |
| 440 | /* user facing 'thread options'; values defined in include/kernel.h */ |
| 441 | u8_t user_options; |
| 442 | |
| 443 | /* thread state */ |
| 444 | u8_t thread_state; |
| 445 | |
| 446 | /* |
| 447 | * scheduler lock count and thread priority |
| 448 | * |
| 449 | * These two fields control the preemptibility of a thread. |
| 450 | * |
| 451 | * When the scheduler is locked, sched_locked is decremented, which |
| 452 | * means that the scheduler is locked for values from 0xff to 0x01. A |
| 453 | * thread is coop if its prio is negative, thus 0x80 to 0xff when |
| 454 | * looked at the value as unsigned. |
| 455 | * |
| 456 | * By putting them end-to-end, this means that a thread is |
| 457 | * non-preemptible if the bundled value is greater than or equal to |
| 458 | * 0x0080. |
| 459 | */ |
| 460 | union { |
| 461 | struct { |
| 462 | #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 463 | u8_t sched_locked; |
| 464 | s8_t prio; |
| 465 | #else /* LITTLE and PDP */ |
| 466 | s8_t prio; |
| 467 | u8_t sched_locked; |
| 468 | #endif |
| 469 | }; |
| 470 | u16_t preempt; |
| 471 | }; |
| 472 | |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 473 | #ifdef CONFIG_SCHED_DEADLINE |
| 474 | int prio_deadline; |
| 475 | #endif |
| 476 | |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 477 | u32_t order_key; |
| 478 | |
Andy Ross | 2724fd1 | 2018-01-29 14:55:20 -0800 | [diff] [blame] | 479 | #ifdef CONFIG_SMP |
| 480 | /* True for the per-CPU idle threads */ |
| 481 | u8_t is_idle; |
| 482 | |
Andy Ross | 2724fd1 | 2018-01-29 14:55:20 -0800 | [diff] [blame] | 483 | /* CPU index on which thread was last run */ |
| 484 | u8_t cpu; |
Andy Ross | 15c4007 | 2018-04-12 12:50:05 -0700 | [diff] [blame] | 485 | |
| 486 | /* Recursive count of irq_lock() calls */ |
| 487 | u8_t global_lock_count; |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 488 | |
| 489 | #endif |
| 490 | |
| 491 | #ifdef CONFIG_SCHED_CPU_MASK |
| 492 | /* "May run on" bits for each CPU */ |
| 493 | u8_t cpu_mask; |
Andy Ross | 2724fd1 | 2018-01-29 14:55:20 -0800 | [diff] [blame] | 494 | #endif |
| 495 | |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 496 | /* data returned by APIs */ |
| 497 | void *swap_data; |
| 498 | |
| 499 | #ifdef CONFIG_SYS_CLOCK_EXISTS |
| 500 | /* this thread's entry in a timeout queue */ |
| 501 | struct _timeout timeout; |
| 502 | #endif |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 503 | }; |
| 504 | |
| 505 | typedef struct _thread_base _thread_base_t; |
| 506 | |
| 507 | #if defined(CONFIG_THREAD_STACK_INFO) |
| 508 | /* Contains the stack information of a thread */ |
| 509 | struct _thread_stack_info { |
Andrew Boie | 4e5c093 | 2019-04-04 12:05:28 -0700 | [diff] [blame] | 510 | /* Stack start - Represents the start address of the thread-writable |
| 511 | * stack area. |
Andrew Boie | b85ac3e | 2018-06-01 12:15:13 -0700 | [diff] [blame] | 512 | */ |
Nicolas Pitre | 58d839b | 2019-05-21 11:32:21 -0400 | [diff] [blame] | 513 | uintptr_t start; |
Andrew Boie | b85ac3e | 2018-06-01 12:15:13 -0700 | [diff] [blame] | 514 | |
| 515 | /* Stack Size - Thread writable stack buffer size. Represents |
| 516 | * the size of the actual area, starting from the start member, |
| 517 | * that should be writable by the thread |
| 518 | */ |
Andrew Boie | 528233e | 2019-12-11 14:54:15 -0800 | [diff] [blame] | 519 | size_t size; |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 520 | }; |
Andrew Boie | 41c68ec | 2017-05-11 15:38:20 -0700 | [diff] [blame] | 521 | |
| 522 | typedef struct _thread_stack_info _thread_stack_info_t; |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 523 | #endif /* CONFIG_THREAD_STACK_INFO */ |
| 524 | |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 525 | #if defined(CONFIG_USERSPACE) |
| 526 | struct _mem_domain_info { |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 527 | /** memory domain queue node */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 528 | sys_dnode_t mem_domain_q_node; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 529 | /** memory domain of the thread */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 530 | struct k_mem_domain *mem_domain; |
| 531 | }; |
| 532 | |
| 533 | #endif /* CONFIG_USERSPACE */ |
| 534 | |
Daniel Leung | fc18243 | 2018-08-16 15:42:28 -0700 | [diff] [blame] | 535 | #ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA |
| 536 | struct _thread_userspace_local_data { |
| 537 | int errno_var; |
| 538 | }; |
| 539 | #endif |
| 540 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 541 | /** |
| 542 | * @ingroup thread_apis |
| 543 | * Thread Structure |
| 544 | */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 545 | struct k_thread { |
| 546 | |
| 547 | struct _thread_base base; |
| 548 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 549 | /** defined by the architecture, but all archs need these */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 550 | struct _callee_saved callee_saved; |
| 551 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 552 | /** static thread init data */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 553 | void *init_data; |
| 554 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 555 | /** |
| 556 | * abort function |
| 557 | * @req K-THREAD-002 |
| 558 | * */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 559 | void (*fn_abort)(void); |
| 560 | |
| 561 | #if defined(CONFIG_THREAD_MONITOR) |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 562 | /** thread entry and parameters description */ |
Andrew Boie | 2dd91ec | 2018-06-06 08:45:01 -0700 | [diff] [blame] | 563 | struct __thread_entry entry; |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 564 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 565 | /** next item in list of all threads */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 566 | struct k_thread *next_thread; |
| 567 | #endif |
| 568 | |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 569 | #if defined(CONFIG_THREAD_NAME) |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 570 | /** Thread name */ |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 571 | char name[CONFIG_THREAD_MAX_NAME_LEN]; |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 572 | #endif |
| 573 | |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 574 | #ifdef CONFIG_THREAD_CUSTOM_DATA |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 575 | /** crude thread-local storage */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 576 | void *custom_data; |
| 577 | #endif |
| 578 | |
Daniel Leung | fc18243 | 2018-08-16 15:42:28 -0700 | [diff] [blame] | 579 | #ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA |
| 580 | struct _thread_userspace_local_data *userspace_local_data; |
| 581 | #endif |
| 582 | |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 583 | #ifdef CONFIG_ERRNO |
Daniel Leung | fc18243 | 2018-08-16 15:42:28 -0700 | [diff] [blame] | 584 | #ifndef CONFIG_USERSPACE |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 585 | /** per-thread errno variable */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 586 | int errno_var; |
| 587 | #endif |
Andrew Boie | 7f4d006 | 2018-07-19 11:09:33 -0700 | [diff] [blame] | 588 | #endif |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 589 | |
| 590 | #if defined(CONFIG_THREAD_STACK_INFO) |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 591 | /** Stack Info */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 592 | struct _thread_stack_info stack_info; |
| 593 | #endif /* CONFIG_THREAD_STACK_INFO */ |
| 594 | |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 595 | #if defined(CONFIG_USERSPACE) |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 596 | /** memory domain info of the thread */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 597 | struct _mem_domain_info mem_domain_info; |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 598 | /** Base address of thread stack */ |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 599 | k_thread_stack_t *stack_obj; |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 600 | #endif /* CONFIG_USERSPACE */ |
| 601 | |
Andy Ross | 042d8ec | 2017-12-09 08:37:20 -0800 | [diff] [blame] | 602 | #if defined(CONFIG_USE_SWITCH) |
| 603 | /* When using __switch() a few previously arch-specific items |
| 604 | * become part of the core OS |
| 605 | */ |
| 606 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 607 | /** z_swap() return value */ |
Andy Ross | 042d8ec | 2017-12-09 08:37:20 -0800 | [diff] [blame] | 608 | int swap_retval; |
| 609 | |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 610 | /** Context handle returned via arch_switch() */ |
Andy Ross | 042d8ec | 2017-12-09 08:37:20 -0800 | [diff] [blame] | 611 | void *switch_handle; |
| 612 | #endif |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 613 | /** resource pool */ |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 614 | struct k_mem_pool *resource_pool; |
Andy Ross | 042d8ec | 2017-12-09 08:37:20 -0800 | [diff] [blame] | 615 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 616 | /** arch-specifics: must always be at the end */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 617 | struct _thread_arch arch; |
| 618 | }; |
| 619 | |
| 620 | typedef struct k_thread _thread_t; |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 621 | typedef struct k_thread *k_tid_t; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 622 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 623 | enum execution_context_types { |
| 624 | K_ISR = 0, |
| 625 | K_COOP_THREAD, |
| 626 | K_PREEMPT_THREAD, |
| 627 | }; |
| 628 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 629 | /** |
Anas Nashif | 4bcb294 | 2019-01-23 23:06:29 -0500 | [diff] [blame] | 630 | * @addtogroup thread_apis |
Carles Cufi | cb0cf9f | 2017-01-10 10:57:38 +0100 | [diff] [blame] | 631 | * @{ |
| 632 | */ |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 633 | |
Ramakrishna Pallala | 110b8e4 | 2018-04-27 12:55:43 +0530 | [diff] [blame] | 634 | typedef void (*k_thread_user_cb_t)(const struct k_thread *thread, |
| 635 | void *user_data); |
Carles Cufi | cb0cf9f | 2017-01-10 10:57:38 +0100 | [diff] [blame] | 636 | |
| 637 | /** |
Ramakrishna Pallala | 110b8e4 | 2018-04-27 12:55:43 +0530 | [diff] [blame] | 638 | * @brief Iterate over all the threads in the system. |
| 639 | * |
| 640 | * This routine iterates over all the threads in the system and |
| 641 | * calls the user_cb function for each thread. |
| 642 | * |
| 643 | * @param user_cb Pointer to the user callback function. |
| 644 | * @param user_data Pointer to user data. |
| 645 | * |
| 646 | * @note CONFIG_THREAD_MONITOR must be set for this function |
Radoslaw Koppel | 2c529ce | 2019-11-27 14:20:37 +0100 | [diff] [blame^] | 647 | * to be effective. |
| 648 | * @note This API uses @ref k_spin_lock to protect the _kernel.threads |
| 649 | * list which means creation of new threads and terminations of existing |
| 650 | * threads are blocked until this API returns. |
Ramakrishna Pallala | 110b8e4 | 2018-04-27 12:55:43 +0530 | [diff] [blame] | 651 | * |
| 652 | * @return N/A |
| 653 | */ |
| 654 | extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data); |
| 655 | |
Radoslaw Koppel | 2c529ce | 2019-11-27 14:20:37 +0100 | [diff] [blame^] | 656 | /** |
| 657 | * @brief Iterate over all the threads in the system without locking. |
| 658 | * |
| 659 | * This routine works exactly the same like @ref k_thread_foreach |
| 660 | * but unlocks interrupts when user_cb is executed. |
| 661 | * |
| 662 | * @param user_cb Pointer to the user callback function. |
| 663 | * @param user_data Pointer to user data. |
| 664 | * |
| 665 | * @note CONFIG_THREAD_MONITOR must be set for this function |
| 666 | * to be effective. |
| 667 | * @note This API uses @ref k_spin_lock only when accessing the _kernel.threads |
| 668 | * queue elements. It unlocks it during user callback function processing. |
| 669 | * If a new task is created when this @c foreach function is in progress, |
| 670 | * the added new task would not be included in the enumeration. |
| 671 | * If a task is aborted during this enumeration, there would be a race here |
| 672 | * and there is a possibility that this aborted task would be included in the |
| 673 | * enumeration. |
| 674 | * @note If the task is aborted and the memory occupied by its @c k_thread |
| 675 | * structure is reused when this @c k_thread_foreach_unlocked is in progress |
| 676 | * it might even lead to the system behave unstable. |
| 677 | * This function may never return, as it would follow some @c next task |
| 678 | * pointers treating given pointer as a pointer to the k_thread structure |
| 679 | * while it is something different right now. |
| 680 | * Do not reuse the memory that was occupied by k_thread structure of aborted |
| 681 | * task if it was aborted after this function was called in any context. |
| 682 | */ |
| 683 | extern void k_thread_foreach_unlocked( |
| 684 | k_thread_user_cb_t user_cb, void *user_data); |
| 685 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 686 | /** @} */ |
Carles Cufi | cb0cf9f | 2017-01-10 10:57:38 +0100 | [diff] [blame] | 687 | |
| 688 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 689 | * @defgroup thread_apis Thread APIs |
| 690 | * @ingroup kernel_apis |
| 691 | * @{ |
| 692 | */ |
| 693 | |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 694 | #endif /* !_ASMLANGUAGE */ |
| 695 | |
| 696 | |
| 697 | /* |
| 698 | * Thread user options. May be needed by assembly code. Common part uses low |
| 699 | * bits, arch-specific use high bits. |
| 700 | */ |
| 701 | |
Anas Nashif | a541e93 | 2018-05-24 11:19:16 -0500 | [diff] [blame] | 702 | /** |
| 703 | * @brief system thread that must not abort |
| 704 | * @req K-THREAD-000 |
| 705 | * */ |
Flavio Ceolin | 8aec087 | 2018-08-15 11:52:00 -0700 | [diff] [blame] | 706 | #define K_ESSENTIAL (BIT(0)) |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 707 | |
| 708 | #if defined(CONFIG_FP_SHARING) |
Anas Nashif | a541e93 | 2018-05-24 11:19:16 -0500 | [diff] [blame] | 709 | /** |
| 710 | * @brief thread uses floating point registers |
| 711 | */ |
Flavio Ceolin | 8aec087 | 2018-08-15 11:52:00 -0700 | [diff] [blame] | 712 | #define K_FP_REGS (BIT(1)) |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 713 | #endif |
| 714 | |
Anas Nashif | a541e93 | 2018-05-24 11:19:16 -0500 | [diff] [blame] | 715 | /** |
| 716 | * @brief user mode thread |
| 717 | * |
| 718 | * This thread has dropped from supervisor mode to user mode and consequently |
Andrew Boie | 5cfa5dc | 2017-08-30 14:17:44 -0700 | [diff] [blame] | 719 | * has additional restrictions |
| 720 | */ |
Flavio Ceolin | 8aec087 | 2018-08-15 11:52:00 -0700 | [diff] [blame] | 721 | #define K_USER (BIT(2)) |
Andrew Boie | 5cfa5dc | 2017-08-30 14:17:44 -0700 | [diff] [blame] | 722 | |
Anas Nashif | a541e93 | 2018-05-24 11:19:16 -0500 | [diff] [blame] | 723 | /** |
| 724 | * @brief Inherit Permissions |
| 725 | * |
| 726 | * @details |
| 727 | * Indicates that the thread being created should inherit all kernel object |
Andrew Boie | 47f8fd1 | 2017-10-05 11:11:02 -0700 | [diff] [blame] | 728 | * permissions from the thread that created it. No effect if CONFIG_USERSPACE |
| 729 | * is not enabled. |
| 730 | */ |
Flavio Ceolin | 8aec087 | 2018-08-15 11:52:00 -0700 | [diff] [blame] | 731 | #define K_INHERIT_PERMS (BIT(3)) |
Andrew Boie | 47f8fd1 | 2017-10-05 11:11:02 -0700 | [diff] [blame] | 732 | |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 733 | #ifdef CONFIG_X86 |
| 734 | /* x86 Bitmask definitions for threads user options */ |
| 735 | |
| 736 | #if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE) |
| 737 | /* thread uses SSEx (and also FP) registers */ |
Flavio Ceolin | 8aec087 | 2018-08-15 11:52:00 -0700 | [diff] [blame] | 738 | #define K_SSE_REGS (BIT(7)) |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 739 | #endif |
| 740 | #endif |
| 741 | |
| 742 | /* end - thread options */ |
| 743 | |
| 744 | #if !defined(_ASMLANGUAGE) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 745 | /** |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 746 | * @brief Create a thread. |
| 747 | * |
| 748 | * This routine initializes a thread, then schedules it for execution. |
| 749 | * |
| 750 | * The new thread may be scheduled for immediate execution or a delayed start. |
| 751 | * If the newly spawned thread does not have a delayed start the kernel |
| 752 | * scheduler may preempt the current thread to allow the new thread to |
| 753 | * execute. |
| 754 | * |
| 755 | * Thread options are architecture-specific, and can include K_ESSENTIAL, |
| 756 | * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating |
| 757 | * them using "|" (the logical OR operator). |
| 758 | * |
| 759 | * Historically, users often would use the beginning of the stack memory region |
| 760 | * to store the struct k_thread data, although corruption will occur if the |
| 761 | * stack overflows this region and stack protection features may not detect this |
| 762 | * situation. |
| 763 | * |
| 764 | * @param new_thread Pointer to uninitialized struct k_thread |
| 765 | * @param stack Pointer to the stack space. |
| 766 | * @param stack_size Stack size in bytes. |
| 767 | * @param entry Thread entry function. |
| 768 | * @param p1 1st entry point parameter. |
| 769 | * @param p2 2nd entry point parameter. |
| 770 | * @param p3 3rd entry point parameter. |
| 771 | * @param prio Thread priority. |
| 772 | * @param options Thread options. |
| 773 | * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay). |
| 774 | * |
| 775 | * @return ID of new thread. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 776 | * |
| 777 | * @req K-THREAD-001 |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 778 | */ |
Andrew Boie | 662c345 | 2017-10-02 10:51:18 -0700 | [diff] [blame] | 779 | __syscall k_tid_t k_thread_create(struct k_thread *new_thread, |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 780 | k_thread_stack_t *stack, |
Andrew Boie | 662c345 | 2017-10-02 10:51:18 -0700 | [diff] [blame] | 781 | size_t stack_size, |
| 782 | k_thread_entry_t entry, |
| 783 | void *p1, void *p2, void *p3, |
| 784 | int prio, u32_t options, s32_t delay); |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 785 | |
Andrew Boie | 3f091b5 | 2017-08-30 14:34:14 -0700 | [diff] [blame] | 786 | /** |
| 787 | * @brief Drop a thread's privileges permanently to user mode |
| 788 | * |
| 789 | * @param entry Function to start executing from |
| 790 | * @param p1 1st entry point parameter |
| 791 | * @param p2 2nd entry point parameter |
| 792 | * @param p3 3rd entry point parameter |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 793 | * @req K-THREAD-003 |
Andrew Boie | 3f091b5 | 2017-08-30 14:34:14 -0700 | [diff] [blame] | 794 | */ |
| 795 | extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry, |
| 796 | void *p1, void *p2, |
| 797 | void *p3); |
Andrew Boie | 3f091b5 | 2017-08-30 14:34:14 -0700 | [diff] [blame] | 798 | |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 799 | /** |
Adithya Baglody | 392219e | 2019-01-02 14:40:39 +0530 | [diff] [blame] | 800 | * @brief Grant a thread access to a set of kernel objects |
Andrew Boie | e12857a | 2017-10-17 11:38:26 -0700 | [diff] [blame] | 801 | * |
| 802 | * This is a convenience function. For the provided thread, grant access to |
| 803 | * the remaining arguments, which must be pointers to kernel objects. |
Andrew Boie | e12857a | 2017-10-17 11:38:26 -0700 | [diff] [blame] | 804 | * |
| 805 | * The thread object must be initialized (i.e. running). The objects don't |
| 806 | * need to be. |
Adithya Baglody | 392219e | 2019-01-02 14:40:39 +0530 | [diff] [blame] | 807 | * Note that NULL shouldn't be passed as an argument. |
Andrew Boie | e12857a | 2017-10-17 11:38:26 -0700 | [diff] [blame] | 808 | * |
| 809 | * @param thread Thread to grant access to objects |
Adithya Baglody | 392219e | 2019-01-02 14:40:39 +0530 | [diff] [blame] | 810 | * @param ... list of kernel object pointers |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 811 | * @req K-THREAD-004 |
Andrew Boie | e12857a | 2017-10-17 11:38:26 -0700 | [diff] [blame] | 812 | */ |
Adithya Baglody | 392219e | 2019-01-02 14:40:39 +0530 | [diff] [blame] | 813 | #define k_thread_access_grant(thread, ...) \ |
| 814 | FOR_EACH_FIXED_ARG(k_object_access_grant, thread, __VA_ARGS__) |
Andrew Boie | e12857a | 2017-10-17 11:38:26 -0700 | [diff] [blame] | 815 | |
| 816 | /** |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 817 | * @brief Assign a resource memory pool to a thread |
| 818 | * |
| 819 | * By default, threads have no resource pool assigned unless their parent |
| 820 | * thread has a resource pool, in which case it is inherited. Multiple |
| 821 | * threads may be assigned to the same memory pool. |
| 822 | * |
| 823 | * Changing a thread's resource pool will not migrate allocations from the |
| 824 | * previous pool. |
| 825 | * |
| 826 | * @param thread Target thread to assign a memory pool for resource requests, |
| 827 | * or NULL if the thread should no longer have a memory pool. |
| 828 | * @param pool Memory pool to use for resources. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 829 | * @req K-THREAD-005 |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 830 | */ |
| 831 | static inline void k_thread_resource_pool_assign(struct k_thread *thread, |
| 832 | struct k_mem_pool *pool) |
| 833 | { |
| 834 | thread->resource_pool = pool; |
| 835 | } |
| 836 | |
| 837 | #if (CONFIG_HEAP_MEM_POOL_SIZE > 0) |
| 838 | /** |
| 839 | * @brief Assign the system heap as a thread's resource pool |
| 840 | * |
| 841 | * Similar to k_thread_resource_pool_assign(), but the thread will use |
| 842 | * the kernel heap to draw memory. |
| 843 | * |
| 844 | * Use with caution, as a malicious thread could perform DoS attacks on the |
| 845 | * kernel heap. |
| 846 | * |
| 847 | * @param thread Target thread to assign the system heap for resource requests |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 848 | * |
| 849 | * @req K-THREAD-004 |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 850 | */ |
| 851 | void k_thread_system_pool_assign(struct k_thread *thread); |
| 852 | #endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */ |
| 853 | |
| 854 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 855 | * @brief Put the current thread to sleep. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 856 | * |
Charles E. Youse | a567831 | 2019-05-09 16:46:46 -0700 | [diff] [blame] | 857 | * This routine puts the current thread to sleep for @a duration milliseconds. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 858 | * |
Charles E. Youse | a567831 | 2019-05-09 16:46:46 -0700 | [diff] [blame] | 859 | * @param ms Number of milliseconds to sleep. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 860 | * |
Piotr Zięcik | 7700eb2 | 2018-10-25 17:45:08 +0200 | [diff] [blame] | 861 | * @return Zero if the requested time has elapsed or the number of milliseconds |
| 862 | * 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] | 863 | */ |
Charles E. Youse | a567831 | 2019-05-09 16:46:46 -0700 | [diff] [blame] | 864 | __syscall s32_t k_sleep(s32_t ms); |
| 865 | |
| 866 | /** |
| 867 | * @brief Put the current thread to sleep with microsecond resolution. |
| 868 | * |
| 869 | * This function is unlikely to work as expected without kernel tuning. |
| 870 | * In particular, because the lower bound on the duration of a sleep is |
| 871 | * the duration of a tick, CONFIG_SYS_CLOCK_TICKS_PER_SEC must be adjusted |
| 872 | * to achieve the resolution desired. The implications of doing this must |
| 873 | * be understood before attempting to use k_usleep(). Use with caution. |
| 874 | * |
| 875 | * @param us Number of microseconds to sleep. |
| 876 | * |
| 877 | * @return Zero if the requested time has elapsed or the number of microseconds |
| 878 | * left to sleep, if thread was woken up by \ref k_wakeup call. |
| 879 | */ |
| 880 | __syscall s32_t k_usleep(s32_t us); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 881 | |
| 882 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 883 | * @brief Cause the current thread to busy wait. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 884 | * |
| 885 | * 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] | 886 | * @a usec_to_wait microseconds. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 887 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 888 | * @return N/A |
| 889 | */ |
Andrew Boie | 42cfd4f | 2018-11-14 14:29:24 -0800 | [diff] [blame] | 890 | __syscall void k_busy_wait(u32_t usec_to_wait); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 891 | |
| 892 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 893 | * @brief Yield the current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 894 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 895 | * This routine causes the current thread to yield execution to another |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 896 | * 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] | 897 | * of the same or higher priority, the routine returns immediately. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 898 | * |
| 899 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 900 | * @req K-THREAD-015 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 901 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 902 | __syscall void k_yield(void); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 903 | |
| 904 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 905 | * @brief Wake up a sleeping thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 906 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 907 | * This routine prematurely wakes up @a thread from sleeping. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 908 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 909 | * If @a thread is not currently sleeping, the routine has no effect. |
| 910 | * |
| 911 | * @param thread ID of thread to wake. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 912 | * |
| 913 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 914 | * @req K-THREAD-014 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 915 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 916 | __syscall void k_wakeup(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 917 | |
| 918 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 919 | * @brief Get thread ID of the current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 920 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 921 | * @return ID of current thread. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 922 | * |
| 923 | * @req K-THREAD-013 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 924 | */ |
Andrew Boie | 76c04a2 | 2017-09-27 14:45:10 -0700 | [diff] [blame] | 925 | __syscall k_tid_t k_current_get(void); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 926 | |
| 927 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 928 | * @brief Abort a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 929 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 930 | * This routine permanently stops execution of @a thread. The thread is taken |
| 931 | * off all kernel queues it is part of (i.e. the ready queue, the timeout |
| 932 | * queue, or a kernel object wait queue). However, any kernel resources the |
| 933 | * thread might currently own (such as mutexes or memory blocks) are not |
| 934 | * released. It is the responsibility of the caller of this routine to ensure |
| 935 | * all necessary cleanup is performed. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 936 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 937 | * @param thread ID of thread to abort. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 938 | * |
| 939 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 940 | * @req K-THREAD-012 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 941 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 942 | __syscall void k_thread_abort(k_tid_t thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 943 | |
Andrew Boie | 7d627c5 | 2017-08-30 11:01:56 -0700 | [diff] [blame] | 944 | |
| 945 | /** |
| 946 | * @brief Start an inactive thread |
| 947 | * |
| 948 | * If a thread was created with K_FOREVER in the delay parameter, it will |
| 949 | * not be added to the scheduling queue until this function is called |
| 950 | * on it. |
| 951 | * |
| 952 | * @param thread thread to start |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 953 | * @req K-THREAD-011 |
Andrew Boie | 7d627c5 | 2017-08-30 11:01:56 -0700 | [diff] [blame] | 954 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 955 | __syscall void k_thread_start(k_tid_t thread); |
Andrew Boie | 7d627c5 | 2017-08-30 11:01:56 -0700 | [diff] [blame] | 956 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 957 | /** |
| 958 | * @cond INTERNAL_HIDDEN |
| 959 | */ |
| 960 | |
Benjamin Walsh | d211a52 | 2016-12-06 11:44:01 -0500 | [diff] [blame] | 961 | /* timeout has timed out and is not on _timeout_q anymore */ |
| 962 | #define _EXPIRED (-2) |
| 963 | |
Peter Mitsis | a04c0d7 | 2016-09-28 19:26:00 -0400 | [diff] [blame] | 964 | struct _static_thread_data { |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 965 | struct k_thread *init_thread; |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 966 | k_thread_stack_t *init_stack; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 967 | unsigned int init_stack_size; |
Andrew Boie | 1e06ffc | 2017-09-11 09:30:04 -0700 | [diff] [blame] | 968 | k_thread_entry_t init_entry; |
Allan Stephens | 7c5bffa | 2016-10-26 10:01:28 -0500 | [diff] [blame] | 969 | void *init_p1; |
| 970 | void *init_p2; |
| 971 | void *init_p3; |
| 972 | int init_prio; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 973 | u32_t init_options; |
| 974 | s32_t init_delay; |
Allan Stephens | 7c5bffa | 2016-10-26 10:01:28 -0500 | [diff] [blame] | 975 | void (*init_abort)(void); |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 976 | const char *init_name; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 977 | }; |
| 978 | |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 979 | #define _THREAD_INITIALIZER(thread, stack, stack_size, \ |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 980 | entry, p1, p2, p3, \ |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 981 | prio, options, delay, abort, tname) \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 982 | { \ |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 983 | .init_thread = (thread), \ |
| 984 | .init_stack = (stack), \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 985 | .init_stack_size = (stack_size), \ |
Andrew Boie | 1e06ffc | 2017-09-11 09:30:04 -0700 | [diff] [blame] | 986 | .init_entry = (k_thread_entry_t)entry, \ |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 987 | .init_p1 = (void *)p1, \ |
| 988 | .init_p2 = (void *)p2, \ |
| 989 | .init_p3 = (void *)p3, \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 990 | .init_prio = (prio), \ |
| 991 | .init_options = (options), \ |
| 992 | .init_delay = (delay), \ |
| 993 | .init_abort = (abort), \ |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 994 | .init_name = STRINGIFY(tname), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 995 | } |
| 996 | |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 997 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 998 | * INTERNAL_HIDDEN @endcond |
| 999 | */ |
| 1000 | |
| 1001 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1002 | * @brief Statically define and initialize a thread. |
| 1003 | * |
| 1004 | * The thread may be scheduled for immediate execution or a delayed start. |
| 1005 | * |
| 1006 | * Thread options are architecture-specific, and can include K_ESSENTIAL, |
| 1007 | * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating |
| 1008 | * them using "|" (the logical OR operator). |
| 1009 | * |
| 1010 | * The ID of the thread can be accessed using: |
| 1011 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 1012 | * @code extern const k_tid_t <name>; @endcode |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1013 | * |
| 1014 | * @param name Name of the thread. |
| 1015 | * @param stack_size Stack size in bytes. |
| 1016 | * @param entry Thread entry function. |
| 1017 | * @param p1 1st entry point parameter. |
| 1018 | * @param p2 2nd entry point parameter. |
| 1019 | * @param p3 3rd entry point parameter. |
| 1020 | * @param prio Thread priority. |
| 1021 | * @param options Thread options. |
| 1022 | * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay). |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 1023 | * |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1024 | * @req K-THREAD-010 |
| 1025 | * |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 1026 | * @internal It has been observed that the x86 compiler by default aligns |
| 1027 | * these _static_thread_data structures to 32-byte boundaries, thereby |
| 1028 | * wasting space. To work around this, force a 4-byte alignment. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1029 | * |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 1030 | */ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 1031 | #define K_THREAD_DEFINE(name, stack_size, \ |
| 1032 | entry, p1, p2, p3, \ |
| 1033 | prio, options, delay) \ |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 1034 | K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 1035 | struct k_thread _k_thread_obj_##name; \ |
| 1036 | Z_STRUCT_SECTION_ITERABLE(_static_thread_data, _k_thread_data_##name) =\ |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 1037 | _THREAD_INITIALIZER(&_k_thread_obj_##name, \ |
| 1038 | _k_thread_stack_##name, stack_size, \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 1039 | entry, p1, p2, p3, prio, options, delay, \ |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 1040 | NULL, name); \ |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 1041 | const k_tid_t name = (k_tid_t)&_k_thread_obj_##name |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1042 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1043 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1044 | * @brief Get a thread's priority. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1045 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1046 | * This routine gets the priority of @a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1047 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1048 | * @param thread ID of thread whose priority is needed. |
| 1049 | * |
| 1050 | * @return Priority of @a thread. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1051 | * @req K-THREAD-009 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1052 | */ |
Andrew Boie | 76c04a2 | 2017-09-27 14:45:10 -0700 | [diff] [blame] | 1053 | __syscall int k_thread_priority_get(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1054 | |
| 1055 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1056 | * @brief Set a thread's priority. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1057 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1058 | * This routine immediately changes the priority of @a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1059 | * |
| 1060 | * Rescheduling can occur immediately depending on the priority @a thread is |
| 1061 | * set to: |
| 1062 | * |
| 1063 | * - If its priority is raised above the priority of the caller of this |
| 1064 | * function, and the caller is preemptible, @a thread will be scheduled in. |
| 1065 | * |
| 1066 | * - If the caller operates on itself, it lowers its priority below that of |
| 1067 | * other threads in the system, and the caller is preemptible, the thread of |
| 1068 | * highest priority will be scheduled in. |
| 1069 | * |
| 1070 | * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to |
| 1071 | * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the |
| 1072 | * highest priority. |
| 1073 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1074 | * @param thread ID of thread whose priority is to be set. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1075 | * @param prio New priority. |
| 1076 | * |
| 1077 | * @warning Changing the priority of a thread currently involved in mutex |
| 1078 | * priority inheritance may result in undefined behavior. |
| 1079 | * |
| 1080 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1081 | * @req K-THREAD-008 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1082 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1083 | __syscall void k_thread_priority_set(k_tid_t thread, int prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1084 | |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 1085 | |
| 1086 | #ifdef CONFIG_SCHED_DEADLINE |
| 1087 | /** |
| 1088 | * @brief Set deadline expiration time for scheduler |
| 1089 | * |
| 1090 | * This sets the "deadline" expiration as a time delta from the |
| 1091 | * current time, in the same units used by k_cycle_get_32(). The |
| 1092 | * scheduler (when deadline scheduling is enabled) will choose the |
| 1093 | * next expiring thread when selecting between threads at the same |
| 1094 | * static priority. Threads at different priorities will be scheduled |
| 1095 | * according to their static priority. |
| 1096 | * |
| 1097 | * @note Deadlines that are negative (i.e. in the past) are still seen |
| 1098 | * as higher priority than others, even if the thread has "finished" |
| 1099 | * its work. If you don't want it scheduled anymore, you have to |
| 1100 | * reset the deadline into the future, block/pend the thread, or |
| 1101 | * modify its priority with k_thread_priority_set(). |
| 1102 | * |
| 1103 | * @note Despite the API naming, the scheduler makes no guarantees the |
| 1104 | * the thread WILL be scheduled within that deadline, nor does it take |
| 1105 | * extra metadata (like e.g. the "runtime" and "period" parameters in |
| 1106 | * Linux sched_setattr()) that allows the kernel to validate the |
| 1107 | * scheduling for achievability. Such features could be implemented |
| 1108 | * above this call, which is simply input to the priority selection |
| 1109 | * logic. |
| 1110 | * |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1111 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1112 | * @rst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1113 | * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project |
| 1114 | * configuration. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1115 | * @endrst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1116 | * |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 1117 | * @param thread A thread on which to set the deadline |
| 1118 | * @param deadline A time delta, in cycle units |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1119 | * |
| 1120 | * @req K-THREAD-007 |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 1121 | */ |
| 1122 | __syscall void k_thread_deadline_set(k_tid_t thread, int deadline); |
| 1123 | #endif |
| 1124 | |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1125 | #ifdef CONFIG_SCHED_CPU_MASK |
| 1126 | /** |
| 1127 | * @brief Sets all CPU enable masks to zero |
| 1128 | * |
| 1129 | * After this returns, the thread will no longer be schedulable on any |
| 1130 | * CPUs. The thread must not be currently runnable. |
| 1131 | * |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1132 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1133 | * @rst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1134 | * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project |
| 1135 | * configuration. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1136 | * @endrst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1137 | * |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1138 | * @param thread Thread to operate upon |
| 1139 | * @return Zero on success, otherwise error code |
| 1140 | */ |
| 1141 | int k_thread_cpu_mask_clear(k_tid_t thread); |
| 1142 | |
| 1143 | /** |
| 1144 | * @brief Sets all CPU enable masks to one |
| 1145 | * |
| 1146 | * After this returns, the thread will be schedulable on any CPU. The |
| 1147 | * thread must not be currently runnable. |
| 1148 | * |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1149 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1150 | * @rst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1151 | * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project |
| 1152 | * configuration. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1153 | * @endrst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1154 | * |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1155 | * @param thread Thread to operate upon |
| 1156 | * @return Zero on success, otherwise error code |
| 1157 | */ |
| 1158 | int k_thread_cpu_mask_enable_all(k_tid_t thread); |
| 1159 | |
| 1160 | /** |
| 1161 | * @brief Enable thread to run on specified CPU |
| 1162 | * |
| 1163 | * The thread must not be currently runnable. |
| 1164 | * |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1165 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1166 | * @rst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1167 | * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project |
| 1168 | * configuration. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1169 | * @endrst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1170 | * |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1171 | * @param thread Thread to operate upon |
| 1172 | * @param cpu CPU index |
| 1173 | * @return Zero on success, otherwise error code |
| 1174 | */ |
| 1175 | int k_thread_cpu_mask_enable(k_tid_t thread, int cpu); |
| 1176 | |
| 1177 | /** |
| 1178 | * @brief Prevent thread to run on specified CPU |
| 1179 | * |
| 1180 | * The thread must not be currently runnable. |
| 1181 | * |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1182 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1183 | * @rst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1184 | * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project |
| 1185 | * configuration. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1186 | * @endrst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1187 | * |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1188 | * @param thread Thread to operate upon |
| 1189 | * @param cpu CPU index |
| 1190 | * @return Zero on success, otherwise error code |
| 1191 | */ |
| 1192 | int k_thread_cpu_mask_disable(k_tid_t thread, int cpu); |
| 1193 | #endif |
| 1194 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1195 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1196 | * @brief Suspend a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1197 | * |
Andy Ross | 50d0942 | 2019-11-19 11:20:07 -0800 | [diff] [blame] | 1198 | * This routine prevents the kernel scheduler from making @a thread |
| 1199 | * the current thread. All other internal operations on @a thread are |
| 1200 | * still performed; for example, kernel objects it is waiting on are |
| 1201 | * still handed to it. Note that any existing timeouts |
| 1202 | * (e.g. k_sleep(), or a timeout argument to k_sem_take() et. al.) |
| 1203 | * will be canceled. On resume, the thread will begin running |
| 1204 | * immediately and return from the blocked call. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1205 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1206 | * If @a thread is already suspended, the routine has no effect. |
| 1207 | * |
| 1208 | * @param thread ID of thread to suspend. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1209 | * |
| 1210 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1211 | * @req K-THREAD-005 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1212 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1213 | __syscall void k_thread_suspend(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1214 | |
| 1215 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1216 | * @brief Resume a suspended thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1217 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1218 | * This routine allows the kernel scheduler to make @a thread the current |
| 1219 | * thread, when it is next eligible for that role. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1220 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1221 | * If @a thread is not currently suspended, the routine has no effect. |
| 1222 | * |
| 1223 | * @param thread ID of thread to resume. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1224 | * |
| 1225 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1226 | * @req K-THREAD-006 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1227 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1228 | __syscall void k_thread_resume(k_tid_t thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1229 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1230 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1231 | * @brief Set time-slicing period and scope. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1232 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1233 | * This routine specifies how the scheduler will perform time slicing of |
| 1234 | * preemptible threads. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1235 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1236 | * To enable time slicing, @a slice must be non-zero. The scheduler |
| 1237 | * ensures that no thread runs for more than the specified time limit |
| 1238 | * before other threads of that priority are given a chance to execute. |
| 1239 | * 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] | 1240 | * execute as long as desired without being preempted due to time slicing. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1241 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1242 | * 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] | 1243 | * execute. Once the scheduler selects a thread for execution, there is no |
| 1244 | * minimum guaranteed time the thread will execute before threads of greater or |
| 1245 | * equal priority are scheduled. |
| 1246 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1247 | * When the current thread is the only one of that priority eligible |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1248 | * for execution, this routine has no effect; the thread is immediately |
| 1249 | * rescheduled after the slice period expires. |
| 1250 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1251 | * To disable timeslicing, set both @a slice and @a prio to zero. |
| 1252 | * |
| 1253 | * @param slice Maximum time slice length (in milliseconds). |
| 1254 | * @param prio Highest thread priority level eligible for time slicing. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1255 | * |
| 1256 | * @return N/A |
| 1257 | */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 1258 | extern void k_sched_time_slice_set(s32_t slice, int prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1259 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 1260 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1261 | |
| 1262 | /** |
| 1263 | * @addtogroup isr_apis |
| 1264 | * @{ |
| 1265 | */ |
| 1266 | |
| 1267 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1268 | * @brief Determine if code is running at interrupt level. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1269 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1270 | * This routine allows the caller to customize its actions, depending on |
| 1271 | * whether it is a thread or an ISR. |
| 1272 | * |
| 1273 | * @note Can be called by ISRs. |
| 1274 | * |
Flavio Ceolin | 6a4a86e | 2018-12-17 12:40:22 -0800 | [diff] [blame] | 1275 | * @return false if invoked by a thread. |
| 1276 | * @return true if invoked by an ISR. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1277 | */ |
Flavio Ceolin | 6a4a86e | 2018-12-17 12:40:22 -0800 | [diff] [blame] | 1278 | extern bool k_is_in_isr(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1279 | |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1280 | /** |
| 1281 | * @brief Determine if code is running in a preemptible thread. |
| 1282 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1283 | * This routine allows the caller to customize its actions, depending on |
| 1284 | * whether it can be preempted by another thread. The routine returns a 'true' |
| 1285 | * value if all of the following conditions are met: |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1286 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1287 | * - The code is running in a thread, not at ISR. |
| 1288 | * - The thread's priority is in the preemptible range. |
| 1289 | * - The thread has not locked the scheduler. |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1290 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1291 | * @note Can be called by ISRs. |
| 1292 | * |
| 1293 | * @return 0 if invoked by an ISR or by a cooperative thread. |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1294 | * @return Non-zero if invoked by a preemptible thread. |
| 1295 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1296 | __syscall int k_is_preempt_thread(void); |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1297 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1298 | /** |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 1299 | * @} |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1300 | */ |
| 1301 | |
| 1302 | /** |
| 1303 | * @addtogroup thread_apis |
| 1304 | * @{ |
| 1305 | */ |
| 1306 | |
| 1307 | /** |
| 1308 | * @brief Lock the scheduler. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1309 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1310 | * This routine prevents the current thread from being preempted by another |
| 1311 | * thread by instructing the scheduler to treat it as a cooperative thread. |
| 1312 | * If the thread subsequently performs an operation that makes it unready, |
| 1313 | * it will be context switched out in the normal manner. When the thread |
| 1314 | * again becomes the current thread, its non-preemptible status is maintained. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1315 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1316 | * This routine can be called recursively. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1317 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1318 | * @note k_sched_lock() and k_sched_unlock() should normally be used |
| 1319 | * when the operation being performed can be safely interrupted by ISRs. |
| 1320 | * However, if the amount of processing involved is very small, better |
| 1321 | * performance may be obtained by using irq_lock() and irq_unlock(). |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1322 | * |
| 1323 | * @return N/A |
| 1324 | */ |
| 1325 | extern void k_sched_lock(void); |
| 1326 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1327 | /** |
| 1328 | * @brief Unlock the scheduler. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1329 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1330 | * This routine reverses the effect of a previous call to k_sched_lock(). |
| 1331 | * A thread must call the routine once for each time it called k_sched_lock() |
| 1332 | * before the thread becomes preemptible. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1333 | * |
| 1334 | * @return N/A |
| 1335 | */ |
| 1336 | extern void k_sched_unlock(void); |
| 1337 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1338 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1339 | * @brief Set current thread's custom data. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1340 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1341 | * This routine sets the custom data for the current thread to @ value. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1342 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1343 | * Custom data is not used by the kernel itself, and is freely available |
| 1344 | * for a thread to use as it sees fit. It can be used as a framework |
| 1345 | * upon which to build thread-local storage. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1346 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1347 | * @param value New custom data value. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1348 | * |
| 1349 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1350 | * |
| 1351 | * @req K-THREAD-016 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1352 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1353 | __syscall void k_thread_custom_data_set(void *value); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1354 | |
| 1355 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1356 | * @brief Get current thread's custom data. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1357 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1358 | * This routine returns the custom data for the current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1359 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1360 | * @return Current custom data value. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1361 | * @req K-THREAD-007 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1362 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1363 | __syscall void *k_thread_custom_data_get(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1364 | |
| 1365 | /** |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 1366 | * @brief Set current thread name |
| 1367 | * |
| 1368 | * Set the name of the thread to be used when THREAD_MONITOR is enabled for |
| 1369 | * tracing and debugging. |
| 1370 | * |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 1371 | * @param thread_id Thread to set name, or NULL to set the current thread |
| 1372 | * @param value Name string |
| 1373 | * @retval 0 on success |
| 1374 | * @retval -EFAULT Memory access error with supplied string |
| 1375 | * @retval -ENOSYS Thread name configuration option not enabled |
| 1376 | * @retval -EINVAL Thread name too long |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 1377 | */ |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 1378 | __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] | 1379 | |
| 1380 | /** |
| 1381 | * @brief Get thread name |
| 1382 | * |
| 1383 | * Get the name of a thread |
| 1384 | * |
| 1385 | * @param thread_id Thread ID |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 1386 | * @retval Thread name, or NULL if configuration not enabled |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 1387 | */ |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 1388 | const char *k_thread_name_get(k_tid_t thread_id); |
| 1389 | |
| 1390 | /** |
| 1391 | * @brief Copy the thread name into a supplied buffer |
| 1392 | * |
| 1393 | * @param thread_id Thread to obtain name information |
| 1394 | * @param buf Destination buffer |
David B. Kinder | 73896c0 | 2019-10-28 16:27:57 -0700 | [diff] [blame] | 1395 | * @param size Destination buffer size |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 1396 | * @retval -ENOSPC Destination buffer too small |
| 1397 | * @retval -EFAULT Memory access error |
| 1398 | * @retval -ENOSYS Thread name feature not enabled |
| 1399 | * @retval 0 Success |
| 1400 | */ |
| 1401 | __syscall int k_thread_name_copy(k_tid_t thread_id, char *buf, |
| 1402 | size_t size); |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 1403 | |
| 1404 | /** |
Pavlo Hamov | 8076c80 | 2019-07-31 12:43:54 +0300 | [diff] [blame] | 1405 | * @brief Get thread state string |
| 1406 | * |
| 1407 | * Get the human friendly thread state string |
| 1408 | * |
| 1409 | * @param thread_id Thread ID |
| 1410 | * @retval Thread state string, empty if no state flag is set |
| 1411 | */ |
| 1412 | const char *k_thread_state_str(k_tid_t thread_id); |
| 1413 | |
| 1414 | /** |
Andy Ross | cfe6203 | 2018-09-29 07:34:55 -0700 | [diff] [blame] | 1415 | * @} |
| 1416 | */ |
| 1417 | |
| 1418 | /** |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1419 | * @addtogroup clock_apis |
| 1420 | * @{ |
| 1421 | */ |
| 1422 | |
| 1423 | /** |
| 1424 | * @brief Generate null timeout delay. |
| 1425 | * |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1426 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1427 | * not to wait if the requested operation cannot be performed immediately. |
| 1428 | * |
| 1429 | * @return Timeout delay value. |
| 1430 | */ |
| 1431 | #define K_NO_WAIT 0 |
| 1432 | |
| 1433 | /** |
| 1434 | * @brief Generate timeout delay from milliseconds. |
| 1435 | * |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1436 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1437 | * to wait up to @a ms milliseconds to perform the requested operation. |
| 1438 | * |
| 1439 | * @param ms Duration in milliseconds. |
| 1440 | * |
| 1441 | * @return Timeout delay value. |
| 1442 | */ |
Johan Hedberg | 1447169 | 2016-11-13 10:52:15 +0200 | [diff] [blame] | 1443 | #define K_MSEC(ms) (ms) |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1444 | |
| 1445 | /** |
| 1446 | * @brief Generate timeout delay from seconds. |
| 1447 | * |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1448 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1449 | * to wait up to @a s seconds to perform the requested operation. |
| 1450 | * |
| 1451 | * @param s Duration in seconds. |
| 1452 | * |
| 1453 | * @return Timeout delay value. |
| 1454 | */ |
Johan Hedberg | 1447169 | 2016-11-13 10:52:15 +0200 | [diff] [blame] | 1455 | #define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC) |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1456 | |
| 1457 | /** |
| 1458 | * @brief Generate timeout delay from minutes. |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1459 | |
| 1460 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1461 | * to wait up to @a m minutes to perform the requested operation. |
| 1462 | * |
| 1463 | * @param m Duration in minutes. |
| 1464 | * |
| 1465 | * @return Timeout delay value. |
| 1466 | */ |
Johan Hedberg | 1447169 | 2016-11-13 10:52:15 +0200 | [diff] [blame] | 1467 | #define K_MINUTES(m) K_SECONDS((m) * 60) |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1468 | |
| 1469 | /** |
| 1470 | * @brief Generate timeout delay from hours. |
| 1471 | * |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1472 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1473 | * to wait up to @a h hours to perform the requested operation. |
| 1474 | * |
| 1475 | * @param h Duration in hours. |
| 1476 | * |
| 1477 | * @return Timeout delay value. |
| 1478 | */ |
Johan Hedberg | 1447169 | 2016-11-13 10:52:15 +0200 | [diff] [blame] | 1479 | #define K_HOURS(h) K_MINUTES((h) * 60) |
| 1480 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1481 | /** |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1482 | * @brief Generate infinite timeout delay. |
| 1483 | * |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1484 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1485 | * to wait as long as necessary to perform the requested operation. |
| 1486 | * |
| 1487 | * @return Timeout delay value. |
| 1488 | */ |
| 1489 | #define K_FOREVER (-1) |
| 1490 | |
| 1491 | /** |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 1492 | * @} |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1493 | */ |
| 1494 | |
| 1495 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1496 | * @cond INTERNAL_HIDDEN |
| 1497 | */ |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 1498 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1499 | struct k_timer { |
| 1500 | /* |
| 1501 | * _timeout structure must be first here if we want to use |
| 1502 | * dynamic timer allocation. timeout.node is used in the double-linked |
| 1503 | * list of free timers |
| 1504 | */ |
| 1505 | struct _timeout timeout; |
| 1506 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1507 | /* wait queue for the (single) thread waiting on this timer */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1508 | _wait_q_t wait_q; |
| 1509 | |
| 1510 | /* runs in ISR context */ |
Flavio Ceolin | 4b35dd2 | 2018-11-16 19:06:59 -0800 | [diff] [blame] | 1511 | void (*expiry_fn)(struct k_timer *timer); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1512 | |
| 1513 | /* runs in the context of the thread that calls k_timer_stop() */ |
Flavio Ceolin | 4b35dd2 | 2018-11-16 19:06:59 -0800 | [diff] [blame] | 1514 | void (*stop_fn)(struct k_timer *timer); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1515 | |
| 1516 | /* timer period */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 1517 | s32_t period; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1518 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1519 | /* timer status */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 1520 | u32_t status; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1521 | |
Benjamin Walsh | e4e98f9 | 2017-01-12 19:38:53 -0500 | [diff] [blame] | 1522 | /* user-specific data, also used to support legacy features */ |
| 1523 | void *user_data; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1524 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 1525 | _OBJECT_TRACING_NEXT_PTR(k_timer) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 1526 | _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1527 | }; |
| 1528 | |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 1529 | #define Z_TIMER_INITIALIZER(obj, expiry, stop) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1530 | { \ |
Krzysztof Chruscinski | be06327 | 2019-02-13 11:19:54 +0100 | [diff] [blame] | 1531 | .timeout = { \ |
| 1532 | .node = {},\ |
| 1533 | .dticks = 0, \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1534 | .fn = z_timer_expiration_handler \ |
Krzysztof Chruscinski | be06327 | 2019-02-13 11:19:54 +0100 | [diff] [blame] | 1535 | }, \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1536 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 1537 | .expiry_fn = expiry, \ |
| 1538 | .stop_fn = stop, \ |
Krzysztof Chruscinski | be06327 | 2019-02-13 11:19:54 +0100 | [diff] [blame] | 1539 | .period = 0, \ |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 1540 | .status = 0, \ |
Benjamin Walsh | e4e98f9 | 2017-01-12 19:38:53 -0500 | [diff] [blame] | 1541 | .user_data = 0, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 1542 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1543 | } |
| 1544 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 1545 | #define K_TIMER_INITIALIZER __DEPRECATED_MACRO Z_TIMER_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 1546 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1547 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1548 | * INTERNAL_HIDDEN @endcond |
| 1549 | */ |
| 1550 | |
| 1551 | /** |
| 1552 | * @defgroup timer_apis Timer APIs |
| 1553 | * @ingroup kernel_apis |
| 1554 | * @{ |
| 1555 | */ |
| 1556 | |
| 1557 | /** |
Allan Stephens | 5eceb85 | 2016-11-16 10:16:30 -0500 | [diff] [blame] | 1558 | * @typedef k_timer_expiry_t |
| 1559 | * @brief Timer expiry function type. |
| 1560 | * |
| 1561 | * A timer's expiry function is executed by the system clock interrupt handler |
| 1562 | * each time the timer expires. The expiry function is optional, and is only |
| 1563 | * invoked if the timer has been initialized with one. |
| 1564 | * |
| 1565 | * @param timer Address of timer. |
| 1566 | * |
| 1567 | * @return N/A |
| 1568 | */ |
| 1569 | typedef void (*k_timer_expiry_t)(struct k_timer *timer); |
| 1570 | |
| 1571 | /** |
| 1572 | * @typedef k_timer_stop_t |
| 1573 | * @brief Timer stop function type. |
| 1574 | * |
| 1575 | * A timer's stop function is executed if the timer is stopped prematurely. |
| 1576 | * The function runs in the context of the thread that stops the timer. |
| 1577 | * The stop function is optional, and is only invoked if the timer has been |
| 1578 | * initialized with one. |
| 1579 | * |
| 1580 | * @param timer Address of timer. |
| 1581 | * |
| 1582 | * @return N/A |
| 1583 | */ |
| 1584 | typedef void (*k_timer_stop_t)(struct k_timer *timer); |
| 1585 | |
| 1586 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1587 | * @brief Statically define and initialize a timer. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1588 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1589 | * 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] | 1590 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 1591 | * @code extern struct k_timer <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1592 | * |
| 1593 | * @param name Name of the timer variable. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1594 | * @param expiry_fn Function to invoke each time the timer expires. |
| 1595 | * @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] | 1596 | */ |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 1597 | #define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 1598 | Z_STRUCT_SECTION_ITERABLE(k_timer, name) = \ |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 1599 | Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1600 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1601 | /** |
| 1602 | * @brief Initialize a timer. |
| 1603 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1604 | * This routine initializes a timer, prior to its first use. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1605 | * |
| 1606 | * @param timer Address of timer. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1607 | * @param expiry_fn Function to invoke each time the timer expires. |
| 1608 | * @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] | 1609 | * |
| 1610 | * @return N/A |
| 1611 | */ |
| 1612 | extern void k_timer_init(struct k_timer *timer, |
Allan Stephens | 5eceb85 | 2016-11-16 10:16:30 -0500 | [diff] [blame] | 1613 | k_timer_expiry_t expiry_fn, |
| 1614 | k_timer_stop_t stop_fn); |
Andy Ross | 8d8b2ac | 2016-09-23 10:08:54 -0700 | [diff] [blame] | 1615 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1616 | /** |
| 1617 | * @brief Start a timer. |
| 1618 | * |
| 1619 | * This routine starts a timer, and resets its status to zero. The timer |
| 1620 | * begins counting down using the specified duration and period values. |
| 1621 | * |
| 1622 | * Attempting to start a timer that is already running is permitted. |
| 1623 | * The timer's status is reset to zero and the timer begins counting down |
| 1624 | * using the new duration and period values. |
| 1625 | * |
| 1626 | * @param timer Address of timer. |
| 1627 | * @param duration Initial timer duration (in milliseconds). |
| 1628 | * @param period Timer period (in milliseconds). |
| 1629 | * |
| 1630 | * @return N/A |
| 1631 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1632 | __syscall void k_timer_start(struct k_timer *timer, |
| 1633 | s32_t duration, s32_t period); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1634 | |
| 1635 | /** |
| 1636 | * @brief Stop a timer. |
| 1637 | * |
| 1638 | * This routine stops a running timer prematurely. The timer's stop function, |
| 1639 | * if one exists, is invoked by the caller. |
| 1640 | * |
| 1641 | * 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] | 1642 | * effect on the timer. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1643 | * |
Anas Nashif | 4fb12ae | 2017-02-01 20:06:55 -0500 | [diff] [blame] | 1644 | * @note Can be called by ISRs. The stop handler has to be callable from ISRs |
| 1645 | * if @a k_timer_stop is to be called from ISRs. |
| 1646 | * |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1647 | * @param timer Address of timer. |
| 1648 | * |
| 1649 | * @return N/A |
| 1650 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1651 | __syscall void k_timer_stop(struct k_timer *timer); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1652 | |
| 1653 | /** |
| 1654 | * @brief Read timer status. |
| 1655 | * |
| 1656 | * This routine reads the timer's status, which indicates the number of times |
| 1657 | * it has expired since its status was last read. |
| 1658 | * |
| 1659 | * Calling this routine resets the timer's status to zero. |
| 1660 | * |
| 1661 | * @param timer Address of timer. |
| 1662 | * |
| 1663 | * @return Timer status. |
| 1664 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1665 | __syscall u32_t k_timer_status_get(struct k_timer *timer); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1666 | |
| 1667 | /** |
| 1668 | * @brief Synchronize thread to timer expiration. |
| 1669 | * |
| 1670 | * This routine blocks the calling thread until the timer's status is non-zero |
| 1671 | * (indicating that it has expired at least once since it was last examined) |
| 1672 | * or the timer is stopped. If the timer status is already non-zero, |
| 1673 | * or the timer is already stopped, the caller continues without waiting. |
| 1674 | * |
| 1675 | * Calling this routine resets the timer's status to zero. |
| 1676 | * |
| 1677 | * This routine must not be used by interrupt handlers, since they are not |
| 1678 | * allowed to block. |
| 1679 | * |
| 1680 | * @param timer Address of timer. |
| 1681 | * |
| 1682 | * @return Timer status. |
| 1683 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1684 | __syscall u32_t k_timer_status_sync(struct k_timer *timer); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1685 | |
Andy Ross | 52e444b | 2018-09-28 09:06:37 -0700 | [diff] [blame] | 1686 | extern s32_t z_timeout_remaining(struct _timeout *timeout); |
| 1687 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1688 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1689 | * @brief Get time remaining before a timer next expires. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1690 | * |
| 1691 | * This routine computes the (approximate) time remaining before a running |
| 1692 | * timer next expires. If the timer is not running, it returns zero. |
| 1693 | * |
| 1694 | * @param timer Address of timer. |
| 1695 | * |
| 1696 | * @return Remaining time (in milliseconds). |
| 1697 | */ |
Flavio Ceolin | f1e5303 | 2018-12-04 16:03:13 -0800 | [diff] [blame] | 1698 | __syscall u32_t k_timer_remaining_get(struct k_timer *timer); |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1699 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1700 | static inline u32_t z_impl_k_timer_remaining_get(struct k_timer *timer) |
Johan Hedberg | f99ad3f | 2016-12-09 10:39:49 +0200 | [diff] [blame] | 1701 | { |
Charles E. Youse | 0ad4022 | 2019-03-01 10:51:04 -0800 | [diff] [blame] | 1702 | const s32_t ticks = z_timeout_remaining(&timer->timeout); |
Andy Ross | 8892406 | 2019-10-03 11:43:10 -0700 | [diff] [blame] | 1703 | return (ticks > 0) ? (u32_t)k_ticks_to_ms_floor64(ticks) : 0U; |
Johan Hedberg | f99ad3f | 2016-12-09 10:39:49 +0200 | [diff] [blame] | 1704 | } |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1705 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1706 | /** |
Benjamin Walsh | e4e98f9 | 2017-01-12 19:38:53 -0500 | [diff] [blame] | 1707 | * @brief Associate user-specific data with a timer. |
| 1708 | * |
| 1709 | * This routine records the @a user_data with the @a timer, to be retrieved |
| 1710 | * later. |
| 1711 | * |
| 1712 | * It can be used e.g. in a timer handler shared across multiple subsystems to |
| 1713 | * retrieve data specific to the subsystem this timer is associated with. |
| 1714 | * |
| 1715 | * @param timer Address of timer. |
| 1716 | * @param user_data User data to associate with the timer. |
| 1717 | * |
| 1718 | * @return N/A |
| 1719 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1720 | __syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data); |
| 1721 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 1722 | /** |
| 1723 | * @internal |
| 1724 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1725 | 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] | 1726 | void *user_data) |
Benjamin Walsh | e4e98f9 | 2017-01-12 19:38:53 -0500 | [diff] [blame] | 1727 | { |
| 1728 | timer->user_data = user_data; |
| 1729 | } |
| 1730 | |
| 1731 | /** |
| 1732 | * @brief Retrieve the user-specific data from a timer. |
| 1733 | * |
| 1734 | * @param timer Address of timer. |
| 1735 | * |
| 1736 | * @return The user data. |
| 1737 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1738 | __syscall void *k_timer_user_data_get(struct k_timer *timer); |
| 1739 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1740 | 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] | 1741 | { |
| 1742 | return timer->user_data; |
| 1743 | } |
| 1744 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 1745 | /** @} */ |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1746 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1747 | /** |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1748 | * @addtogroup clock_apis |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1749 | * @{ |
| 1750 | */ |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1751 | |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1752 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1753 | * @brief Get system uptime. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1754 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1755 | * This routine returns the elapsed time since the system booted, |
| 1756 | * in milliseconds. |
| 1757 | * |
David B. Kinder | 00c41ea | 2019-06-10 11:13:33 -0700 | [diff] [blame] | 1758 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1759 | * @rst |
David B. Kinder | 00c41ea | 2019-06-10 11:13:33 -0700 | [diff] [blame] | 1760 | * While this function returns time in milliseconds, it does |
| 1761 | * not mean it has millisecond resolution. The actual resolution depends on |
Andy Ross | 669730f | 2019-06-11 11:18:20 -0700 | [diff] [blame] | 1762 | * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1763 | * @endrst |
Paul Sokolovsky | 65d51fd | 2019-02-04 22:44:50 +0300 | [diff] [blame] | 1764 | * |
| 1765 | * @return Current uptime in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1766 | */ |
Andrew Boie | a73d373 | 2017-10-08 12:23:55 -0700 | [diff] [blame] | 1767 | __syscall s64_t k_uptime_get(void); |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1768 | |
Ramesh Thomas | 89ffd44 | 2017-02-05 19:37:19 -0800 | [diff] [blame] | 1769 | /** |
| 1770 | * @brief Enable clock always on in tickless kernel |
| 1771 | * |
Andy Ross | 1db9f18 | 2019-06-25 10:09:45 -0700 | [diff] [blame] | 1772 | * Deprecated. This does nothing (it was always just a hint). This |
| 1773 | * functionality has been migrated to the SYSTEM_CLOCK_SLOPPY_IDLE |
| 1774 | * kconfig. |
Ramesh Thomas | 89ffd44 | 2017-02-05 19:37:19 -0800 | [diff] [blame] | 1775 | * |
| 1776 | * @retval prev_status Previous status of always on flag |
| 1777 | */ |
Andy Ross | 1db9f18 | 2019-06-25 10:09:45 -0700 | [diff] [blame] | 1778 | /* LCOV_EXCL_START */ |
| 1779 | __deprecated static inline int k_enable_sys_clock_always_on(void) |
| 1780 | { |
| 1781 | __ASSERT(IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE), |
| 1782 | "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead"); |
| 1783 | |
| 1784 | return !IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE); |
| 1785 | } |
| 1786 | /* LCOV_EXCL_STOP */ |
Ramesh Thomas | 89ffd44 | 2017-02-05 19:37:19 -0800 | [diff] [blame] | 1787 | |
| 1788 | /** |
| 1789 | * @brief Disable clock always on in tickless kernel |
| 1790 | * |
Andy Ross | 1db9f18 | 2019-06-25 10:09:45 -0700 | [diff] [blame] | 1791 | * Deprecated. This does nothing (it was always just a hint). This |
| 1792 | * functionality has been migrated to the SYS_CLOCK_SLOPPY_IDLE |
| 1793 | * kconfig. |
Ramesh Thomas | 89ffd44 | 2017-02-05 19:37:19 -0800 | [diff] [blame] | 1794 | */ |
Andy Ross | 1db9f18 | 2019-06-25 10:09:45 -0700 | [diff] [blame] | 1795 | /* LCOV_EXCL_START */ |
| 1796 | __deprecated static inline void k_disable_sys_clock_always_on(void) |
| 1797 | { |
| 1798 | __ASSERT(!IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE), |
| 1799 | "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead"); |
| 1800 | } |
| 1801 | /* LCOV_EXCL_STOP */ |
Ramesh Thomas | 89ffd44 | 2017-02-05 19:37:19 -0800 | [diff] [blame] | 1802 | |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1803 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1804 | * @brief Get system uptime (32-bit version). |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1805 | * |
Peter Bigot | a6067a3 | 2019-08-28 08:19:26 -0500 | [diff] [blame] | 1806 | * This routine returns the lower 32 bits of the system uptime in |
| 1807 | * milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1808 | * |
Peter Bigot | a6067a3 | 2019-08-28 08:19:26 -0500 | [diff] [blame] | 1809 | * Because correct conversion requires full precision of the system |
| 1810 | * clock there is no benefit to using this over k_uptime_get() unless |
| 1811 | * you know the application will never run long enough for the system |
| 1812 | * clock to approach 2^32 ticks. Calls to this function may involve |
| 1813 | * interrupt blocking and 64-bit math. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1814 | * |
David B. Kinder | 00c41ea | 2019-06-10 11:13:33 -0700 | [diff] [blame] | 1815 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1816 | * @rst |
David B. Kinder | 00c41ea | 2019-06-10 11:13:33 -0700 | [diff] [blame] | 1817 | * While this function returns time in milliseconds, it does |
| 1818 | * not mean it has millisecond resolution. The actual resolution depends on |
Andy Ross | 669730f | 2019-06-11 11:18:20 -0700 | [diff] [blame] | 1819 | * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1820 | * @endrst |
Paul Sokolovsky | 65d51fd | 2019-02-04 22:44:50 +0300 | [diff] [blame] | 1821 | * |
Peter Bigot | a6067a3 | 2019-08-28 08:19:26 -0500 | [diff] [blame] | 1822 | * @return The low 32 bits of the current uptime, in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1823 | */ |
Peter Bigot | a6067a3 | 2019-08-28 08:19:26 -0500 | [diff] [blame] | 1824 | static inline u32_t k_uptime_get_32(void) |
| 1825 | { |
| 1826 | return (u32_t)k_uptime_get(); |
| 1827 | } |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1828 | |
| 1829 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1830 | * @brief Get elapsed time. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1831 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1832 | * This routine computes the elapsed time between the current system uptime |
| 1833 | * and an earlier reference time, in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1834 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1835 | * @param reftime Pointer to a reference time, which is updated to the current |
| 1836 | * uptime upon return. |
| 1837 | * |
| 1838 | * @return Elapsed time. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1839 | */ |
Andy Ross | 987c0e5 | 2018-09-27 16:50:00 -0700 | [diff] [blame] | 1840 | static inline s64_t k_uptime_delta(s64_t *reftime) |
| 1841 | { |
| 1842 | s64_t uptime, delta; |
| 1843 | |
| 1844 | uptime = k_uptime_get(); |
| 1845 | delta = uptime - *reftime; |
| 1846 | *reftime = uptime; |
| 1847 | |
| 1848 | return delta; |
| 1849 | } |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1850 | |
| 1851 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1852 | * @brief Get elapsed time (32-bit version). |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1853 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1854 | * This routine computes the elapsed time between the current system uptime |
| 1855 | * and an earlier reference time, in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1856 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1857 | * This routine can be more efficient than k_uptime_delta(), as it reduces the |
| 1858 | * need for interrupt locking and 64-bit math. However, the 32-bit result |
| 1859 | * cannot hold an elapsed time larger than approximately 50 days, so the |
| 1860 | * caller must handle possible rollovers. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1861 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1862 | * @param reftime Pointer to a reference time, which is updated to the current |
| 1863 | * uptime upon return. |
| 1864 | * |
| 1865 | * @return Elapsed time. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1866 | */ |
Andy Ross | 987c0e5 | 2018-09-27 16:50:00 -0700 | [diff] [blame] | 1867 | static inline u32_t k_uptime_delta_32(s64_t *reftime) |
| 1868 | { |
| 1869 | return (u32_t)k_uptime_delta(reftime); |
| 1870 | } |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1871 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1872 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1873 | * @brief Read the hardware clock. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1874 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1875 | * This routine returns the current time, as measured by the system's hardware |
| 1876 | * clock. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1877 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1878 | * @return Current hardware clock up-counter (in cycles). |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1879 | */ |
Andrew Boie | 979b17f | 2019-10-03 15:20:41 -0700 | [diff] [blame] | 1880 | static inline u32_t k_cycle_get_32(void) |
| 1881 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 1882 | return arch_k_cycle_get_32(); |
Andrew Boie | 979b17f | 2019-10-03 15:20:41 -0700 | [diff] [blame] | 1883 | } |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1884 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1885 | /** |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 1886 | * @} |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1887 | */ |
| 1888 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1889 | /** |
| 1890 | * @cond INTERNAL_HIDDEN |
| 1891 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1892 | |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1893 | struct k_queue { |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1894 | sys_sflist_t data_q; |
Andy Ross | 603ea42 | 2018-07-25 13:01:54 -0700 | [diff] [blame] | 1895 | struct k_spinlock lock; |
Luiz Augusto von Dentz | 84db641 | 2017-07-13 12:43:59 +0300 | [diff] [blame] | 1896 | union { |
| 1897 | _wait_q_t wait_q; |
| 1898 | |
| 1899 | _POLL_EVENT; |
| 1900 | }; |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1901 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 1902 | _OBJECT_TRACING_NEXT_PTR(k_queue) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 1903 | _OBJECT_TRACING_LINKED_FLAG |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1904 | }; |
| 1905 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 1906 | #define _K_QUEUE_INITIALIZER(obj) \ |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1907 | { \ |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1908 | .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \ |
Stephanos Ioannidis | f628dcd | 2019-09-11 18:09:49 +0900 | [diff] [blame] | 1909 | .lock = { }, \ |
| 1910 | { \ |
| 1911 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
| 1912 | _POLL_EVENT_OBJ_INIT(obj) \ |
| 1913 | }, \ |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1914 | _OBJECT_TRACING_INIT \ |
| 1915 | } |
| 1916 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 1917 | #define K_QUEUE_INITIALIZER __DEPRECATED_MACRO _K_QUEUE_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 1918 | |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1919 | extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free); |
| 1920 | |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1921 | /** |
| 1922 | * INTERNAL_HIDDEN @endcond |
| 1923 | */ |
| 1924 | |
| 1925 | /** |
| 1926 | * @defgroup queue_apis Queue APIs |
| 1927 | * @ingroup kernel_apis |
| 1928 | * @{ |
| 1929 | */ |
| 1930 | |
| 1931 | /** |
| 1932 | * @brief Initialize a queue. |
| 1933 | * |
| 1934 | * This routine initializes a queue object, prior to its first use. |
| 1935 | * |
| 1936 | * @param queue Address of the queue. |
| 1937 | * |
| 1938 | * @return N/A |
| 1939 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1940 | __syscall void k_queue_init(struct k_queue *queue); |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1941 | |
| 1942 | /** |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 1943 | * @brief Cancel waiting on a queue. |
| 1944 | * |
| 1945 | * This routine causes first thread pending on @a queue, if any, to |
| 1946 | * 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] | 1947 | * If the queue is being waited on by k_poll(), it will return with |
| 1948 | * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent |
| 1949 | * k_queue_get() will return NULL). |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 1950 | * |
| 1951 | * @note Can be called by ISRs. |
| 1952 | * |
| 1953 | * @param queue Address of the queue. |
| 1954 | * |
| 1955 | * @return N/A |
| 1956 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1957 | __syscall void k_queue_cancel_wait(struct k_queue *queue); |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 1958 | |
| 1959 | /** |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1960 | * @brief Append an element to the end of a queue. |
| 1961 | * |
| 1962 | * 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] | 1963 | * aligned on a word boundary, and the first word of the item is reserved |
| 1964 | * for the kernel's use. |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1965 | * |
| 1966 | * @note Can be called by ISRs. |
| 1967 | * |
| 1968 | * @param queue Address of the queue. |
| 1969 | * @param data Address of the data item. |
| 1970 | * |
| 1971 | * @return N/A |
| 1972 | */ |
| 1973 | extern void k_queue_append(struct k_queue *queue, void *data); |
| 1974 | |
| 1975 | /** |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1976 | * @brief Append an element to a queue. |
| 1977 | * |
Andrew Boie | ac3dcc1 | 2019-04-01 12:28:03 -0700 | [diff] [blame] | 1978 | * This routine appends a data item to @a queue. There is an implicit memory |
| 1979 | * allocation to create an additional temporary bookkeeping data structure from |
| 1980 | * the calling thread's resource pool, which is automatically freed when the |
| 1981 | * item is removed. The data itself is not copied. |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1982 | * |
| 1983 | * @note Can be called by ISRs. |
| 1984 | * |
| 1985 | * @param queue Address of the queue. |
| 1986 | * @param data Address of the data item. |
| 1987 | * |
| 1988 | * @retval 0 on success |
| 1989 | * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool |
| 1990 | */ |
Adithya Baglody | 2a78b8d | 2018-10-25 12:09:04 +0530 | [diff] [blame] | 1991 | __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] | 1992 | |
| 1993 | /** |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1994 | * @brief Prepend an element to a queue. |
| 1995 | * |
| 1996 | * 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] | 1997 | * aligned on a word boundary, and the first word of the item is reserved |
| 1998 | * for the kernel's use. |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1999 | * |
| 2000 | * @note Can be called by ISRs. |
| 2001 | * |
| 2002 | * @param queue Address of the queue. |
| 2003 | * @param data Address of the data item. |
| 2004 | * |
| 2005 | * @return N/A |
| 2006 | */ |
| 2007 | extern void k_queue_prepend(struct k_queue *queue, void *data); |
| 2008 | |
| 2009 | /** |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2010 | * @brief Prepend an element to a queue. |
| 2011 | * |
Andrew Boie | ac3dcc1 | 2019-04-01 12:28:03 -0700 | [diff] [blame] | 2012 | * This routine prepends a data item to @a queue. There is an implicit memory |
| 2013 | * allocation to create an additional temporary bookkeeping data structure from |
| 2014 | * the calling thread's resource pool, which is automatically freed when the |
| 2015 | * item is removed. The data itself is not copied. |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2016 | * |
| 2017 | * @note Can be called by ISRs. |
| 2018 | * |
| 2019 | * @param queue Address of the queue. |
| 2020 | * @param data Address of the data item. |
| 2021 | * |
| 2022 | * @retval 0 on success |
| 2023 | * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool |
| 2024 | */ |
Adithya Baglody | 2a78b8d | 2018-10-25 12:09:04 +0530 | [diff] [blame] | 2025 | __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] | 2026 | |
| 2027 | /** |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2028 | * @brief Inserts an element to a queue. |
| 2029 | * |
| 2030 | * 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] | 2031 | * data item must be aligned on a word boundary, and the first word of |
| 2032 | * the item is reserved for the kernel's use. |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2033 | * |
| 2034 | * @note Can be called by ISRs. |
| 2035 | * |
| 2036 | * @param queue Address of the queue. |
| 2037 | * @param prev Address of the previous data item. |
| 2038 | * @param data Address of the data item. |
| 2039 | * |
| 2040 | * @return N/A |
| 2041 | */ |
| 2042 | extern void k_queue_insert(struct k_queue *queue, void *prev, void *data); |
| 2043 | |
| 2044 | /** |
| 2045 | * @brief Atomically append a list of elements to a queue. |
| 2046 | * |
| 2047 | * 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] | 2048 | * 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] | 2049 | * in each data item pointing to the next data item; the list must be |
| 2050 | * NULL-terminated. |
| 2051 | * |
| 2052 | * @note Can be called by ISRs. |
| 2053 | * |
| 2054 | * @param queue Address of the queue. |
| 2055 | * @param head Pointer to first node in singly-linked list. |
| 2056 | * @param tail Pointer to last node in singly-linked list. |
| 2057 | * |
| 2058 | * @return N/A |
| 2059 | */ |
| 2060 | extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail); |
| 2061 | |
| 2062 | /** |
| 2063 | * @brief Atomically add a list of elements to a queue. |
| 2064 | * |
| 2065 | * This routine adds a list of data items to @a queue in one operation. |
| 2066 | * The data items must be in a singly-linked list implemented using a |
| 2067 | * sys_slist_t object. Upon completion, the original list is empty. |
| 2068 | * |
| 2069 | * @note Can be called by ISRs. |
| 2070 | * |
| 2071 | * @param queue Address of the queue. |
| 2072 | * @param list Pointer to sys_slist_t object. |
| 2073 | * |
| 2074 | * @return N/A |
| 2075 | */ |
| 2076 | extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list); |
| 2077 | |
| 2078 | /** |
| 2079 | * @brief Get an element from a queue. |
| 2080 | * |
Nicolas Pitre | 659fa0d | 2019-05-21 22:13:01 -0400 | [diff] [blame] | 2081 | * This routine removes first data item from @a queue. The first word of the |
| 2082 | * data item is reserved for the kernel's use. |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2083 | * |
| 2084 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 2085 | * |
| 2086 | * @param queue Address of the queue. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 2087 | * @param timeout Non-negative waiting period to obtain a data item (in |
| 2088 | * milliseconds), or one of the special values K_NO_WAIT and |
| 2089 | * K_FOREVER. |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2090 | * |
| 2091 | * @return Address of the data item if successful; NULL if returned |
| 2092 | * without waiting, or waiting period timed out. |
| 2093 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2094 | __syscall void *k_queue_get(struct k_queue *queue, s32_t timeout); |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2095 | |
| 2096 | /** |
Luiz Augusto von Dentz | 50b9377 | 2017-07-03 16:52:45 +0300 | [diff] [blame] | 2097 | * @brief Remove an element from a queue. |
| 2098 | * |
Nicolas Pitre | 659fa0d | 2019-05-21 22:13:01 -0400 | [diff] [blame] | 2099 | * This routine removes data item from @a queue. The first word of the |
| 2100 | * 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] | 2101 | * rely on sys_slist_find_and_remove which is not a constant time operation. |
| 2102 | * |
| 2103 | * @note Can be called by ISRs |
| 2104 | * |
| 2105 | * @param queue Address of the queue. |
| 2106 | * @param data Address of the data item. |
| 2107 | * |
| 2108 | * @return true if data item was removed |
| 2109 | */ |
| 2110 | static inline bool k_queue_remove(struct k_queue *queue, void *data) |
| 2111 | { |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2112 | 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] | 2113 | } |
| 2114 | |
| 2115 | /** |
Dhananjay Gundapu Jayakrishnan | 24bfa40 | 2018-08-22 12:33:00 +0200 | [diff] [blame] | 2116 | * @brief Append an element to a queue only if it's not present already. |
| 2117 | * |
Nicolas Pitre | 659fa0d | 2019-05-21 22:13:01 -0400 | [diff] [blame] | 2118 | * This routine appends data item to @a queue. The first word of the data |
| 2119 | * 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] | 2120 | * relies on sys_slist_is_node_in_list which is not a constant time operation. |
| 2121 | * |
| 2122 | * @note Can be called by ISRs |
| 2123 | * |
| 2124 | * @param queue Address of the queue. |
| 2125 | * @param data Address of the data item. |
| 2126 | * |
| 2127 | * @return true if data item was added, false if not |
| 2128 | */ |
| 2129 | static inline bool k_queue_unique_append(struct k_queue *queue, void *data) |
| 2130 | { |
| 2131 | sys_sfnode_t *test; |
| 2132 | |
| 2133 | SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) { |
| 2134 | if (test == (sys_sfnode_t *) data) { |
| 2135 | return false; |
| 2136 | } |
| 2137 | } |
| 2138 | |
| 2139 | k_queue_append(queue, data); |
| 2140 | return true; |
| 2141 | } |
| 2142 | |
| 2143 | /** |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2144 | * @brief Query a queue to see if it has data available. |
| 2145 | * |
| 2146 | * Note that the data might be already gone by the time this function returns |
| 2147 | * if other threads are also trying to read from the queue. |
| 2148 | * |
| 2149 | * @note Can be called by ISRs. |
| 2150 | * |
| 2151 | * @param queue Address of the queue. |
| 2152 | * |
| 2153 | * @return Non-zero if the queue is empty. |
| 2154 | * @return 0 if data is available. |
| 2155 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2156 | __syscall int k_queue_is_empty(struct k_queue *queue); |
| 2157 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2158 | 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] | 2159 | { |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2160 | return (int)sys_sflist_is_empty(&queue->data_q); |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2161 | } |
| 2162 | |
| 2163 | /** |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2164 | * @brief Peek element at the head of queue. |
| 2165 | * |
| 2166 | * Return element from the head of queue without removing it. |
| 2167 | * |
| 2168 | * @param queue Address of the queue. |
| 2169 | * |
| 2170 | * @return Head element, or NULL if queue is empty. |
| 2171 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2172 | __syscall void *k_queue_peek_head(struct k_queue *queue); |
| 2173 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2174 | 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] | 2175 | { |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2176 | 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] | 2177 | } |
| 2178 | |
| 2179 | /** |
| 2180 | * @brief Peek element at the tail of queue. |
| 2181 | * |
| 2182 | * Return element from the tail of queue without removing it. |
| 2183 | * |
| 2184 | * @param queue Address of the queue. |
| 2185 | * |
| 2186 | * @return Tail element, or NULL if queue is empty. |
| 2187 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2188 | __syscall void *k_queue_peek_tail(struct k_queue *queue); |
| 2189 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2190 | 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] | 2191 | { |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2192 | 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] | 2193 | } |
| 2194 | |
| 2195 | /** |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2196 | * @brief Statically define and initialize a queue. |
| 2197 | * |
| 2198 | * The queue can be accessed outside the module where it is defined using: |
| 2199 | * |
| 2200 | * @code extern struct k_queue <name>; @endcode |
| 2201 | * |
| 2202 | * @param name Name of the queue. |
| 2203 | */ |
| 2204 | #define K_QUEUE_DEFINE(name) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 2205 | Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2206 | _K_QUEUE_INITIALIZER(name) |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2207 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 2208 | /** @} */ |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2209 | |
Wentong Wu | 5611e92 | 2019-06-20 23:51:27 +0800 | [diff] [blame] | 2210 | #ifdef CONFIG_USERSPACE |
| 2211 | /** |
| 2212 | * @brief futex structure |
| 2213 | * |
| 2214 | * A k_futex is a lightweight mutual exclusion primitive designed |
| 2215 | * to minimize kernel involvement. Uncontended operation relies |
| 2216 | * only on atomic access to shared memory. k_futex are tracked as |
| 2217 | * kernel objects and can live in user memory so any access bypass |
| 2218 | * the kernel object permission management mechanism. |
| 2219 | */ |
| 2220 | struct k_futex { |
| 2221 | atomic_t val; |
| 2222 | }; |
| 2223 | |
| 2224 | /** |
| 2225 | * @brief futex kernel data structure |
| 2226 | * |
| 2227 | * z_futex_data are the helper data structure for k_futex to complete |
| 2228 | * futex contended operation on kernel side, structure z_futex_data |
| 2229 | * of every futex object is invisible in user mode. |
| 2230 | */ |
| 2231 | struct z_futex_data { |
| 2232 | _wait_q_t wait_q; |
| 2233 | struct k_spinlock lock; |
| 2234 | }; |
| 2235 | |
| 2236 | #define Z_FUTEX_DATA_INITIALIZER(obj) \ |
| 2237 | { \ |
| 2238 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q) \ |
| 2239 | } |
| 2240 | |
| 2241 | /** |
| 2242 | * @defgroup futex_apis FUTEX APIs |
| 2243 | * @ingroup kernel_apis |
| 2244 | * @{ |
| 2245 | */ |
| 2246 | |
| 2247 | /** |
Wentong Wu | 5611e92 | 2019-06-20 23:51:27 +0800 | [diff] [blame] | 2248 | * @brief Pend the current thread on a futex |
| 2249 | * |
| 2250 | * Tests that the supplied futex contains the expected value, and if so, |
| 2251 | * goes to sleep until some other thread calls k_futex_wake() on it. |
| 2252 | * |
| 2253 | * @param futex Address of the futex. |
| 2254 | * @param expected Expected value of the futex, if it is different the caller |
| 2255 | * will not wait on it. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 2256 | * @param timeout Non-negative waiting period on the futex, in milliseconds, or |
| 2257 | * one of the special values K_NO_WAIT or K_FOREVER. |
Wentong Wu | 5611e92 | 2019-06-20 23:51:27 +0800 | [diff] [blame] | 2258 | * @retval -EACCES Caller does not have read access to futex address. |
| 2259 | * @retval -EAGAIN If the futex value did not match the expected parameter. |
| 2260 | * @retval -EINVAL Futex parameter address not recognized by the kernel. |
| 2261 | * @retval -ETIMEDOUT Thread woke up due to timeout and not a futex wakeup. |
| 2262 | * @retval 0 if the caller went to sleep and was woken up. The caller |
| 2263 | * should check the futex's value on wakeup to determine if it needs |
| 2264 | * to block again. |
| 2265 | */ |
| 2266 | __syscall int k_futex_wait(struct k_futex *futex, int expected, s32_t timeout); |
| 2267 | |
| 2268 | /** |
| 2269 | * @brief Wake one/all threads pending on a futex |
| 2270 | * |
| 2271 | * Wake up the highest priority thread pending on the supplied futex, or |
| 2272 | * wakeup all the threads pending on the supplied futex, and the behavior |
| 2273 | * depends on wake_all. |
| 2274 | * |
| 2275 | * @param futex Futex to wake up pending threads. |
| 2276 | * @param wake_all If true, wake up all pending threads; If false, |
| 2277 | * wakeup the highest priority thread. |
| 2278 | * @retval -EACCES Caller does not have access to the futex address. |
| 2279 | * @retval -EINVAL Futex parameter address not recognized by the kernel. |
| 2280 | * @retval Number of threads that were woken up. |
| 2281 | */ |
| 2282 | __syscall int k_futex_wake(struct k_futex *futex, bool wake_all); |
| 2283 | |
| 2284 | /** @} */ |
| 2285 | #endif |
| 2286 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2287 | struct k_fifo { |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2288 | struct k_queue _queue; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2289 | }; |
| 2290 | |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2291 | /** |
| 2292 | * @cond INTERNAL_HIDDEN |
| 2293 | */ |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 2294 | #define Z_FIFO_INITIALIZER(obj) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2295 | { \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2296 | ._queue = _K_QUEUE_INITIALIZER(obj._queue) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2297 | } |
| 2298 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 2299 | #define K_FIFO_INITIALIZER __DEPRECATED_MACRO Z_FIFO_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2300 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2301 | /** |
| 2302 | * INTERNAL_HIDDEN @endcond |
| 2303 | */ |
| 2304 | |
| 2305 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2306 | * @defgroup fifo_apis FIFO APIs |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2307 | * @ingroup kernel_apis |
| 2308 | * @{ |
| 2309 | */ |
| 2310 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2311 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2312 | * @brief Initialize a FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2313 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2314 | * This routine initializes a FIFO queue, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2315 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2316 | * @param fifo Address of the FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2317 | * |
| 2318 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2319 | * @req K-FIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2320 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2321 | #define k_fifo_init(fifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2322 | k_queue_init(&(fifo)->_queue) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2323 | |
| 2324 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2325 | * @brief Cancel waiting on a FIFO queue. |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 2326 | * |
| 2327 | * This routine causes first thread pending on @a fifo, if any, to |
| 2328 | * return from k_fifo_get() call with NULL value (as if timeout |
| 2329 | * expired). |
| 2330 | * |
| 2331 | * @note Can be called by ISRs. |
| 2332 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2333 | * @param fifo Address of the FIFO queue. |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 2334 | * |
| 2335 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2336 | * @req K-FIFO-001 |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 2337 | */ |
| 2338 | #define k_fifo_cancel_wait(fifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2339 | k_queue_cancel_wait(&(fifo)->_queue) |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 2340 | |
| 2341 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2342 | * @brief Add an element to a FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2343 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2344 | * 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] | 2345 | * aligned on a word boundary, and the first word of the item is reserved |
| 2346 | * for the kernel's use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2347 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2348 | * @note Can be called by ISRs. |
| 2349 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2350 | * @param fifo Address of the FIFO. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2351 | * @param data Address of the data item. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2352 | * |
| 2353 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2354 | * @req K-FIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2355 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2356 | #define k_fifo_put(fifo, data) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2357 | k_queue_append(&(fifo)->_queue, data) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2358 | |
| 2359 | /** |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2360 | * @brief Add an element to a FIFO queue. |
| 2361 | * |
Andrew Boie | ac3dcc1 | 2019-04-01 12:28:03 -0700 | [diff] [blame] | 2362 | * This routine adds a data item to @a fifo. There is an implicit memory |
| 2363 | * allocation to create an additional temporary bookkeeping data structure from |
| 2364 | * the calling thread's resource pool, which is automatically freed when the |
| 2365 | * item is removed. The data itself is not copied. |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2366 | * |
| 2367 | * @note Can be called by ISRs. |
| 2368 | * |
| 2369 | * @param fifo Address of the FIFO. |
| 2370 | * @param data Address of the data item. |
| 2371 | * |
| 2372 | * @retval 0 on success |
| 2373 | * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2374 | * @req K-FIFO-001 |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2375 | */ |
| 2376 | #define k_fifo_alloc_put(fifo, data) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2377 | k_queue_alloc_append(&(fifo)->_queue, data) |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2378 | |
| 2379 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2380 | * @brief Atomically add a list of elements to a FIFO. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2381 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2382 | * 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] | 2383 | * 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] | 2384 | * each data item pointing to the next data item; the list must be |
| 2385 | * NULL-terminated. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2386 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2387 | * @note Can be called by ISRs. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2388 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2389 | * @param fifo Address of the FIFO queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2390 | * @param head Pointer to first node in singly-linked list. |
| 2391 | * @param tail Pointer to last node in singly-linked list. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2392 | * |
| 2393 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2394 | * @req K-FIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2395 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2396 | #define k_fifo_put_list(fifo, head, tail) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2397 | k_queue_append_list(&(fifo)->_queue, head, tail) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2398 | |
| 2399 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2400 | * @brief Atomically add a list of elements to a FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2401 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2402 | * This routine adds a list of data items to @a fifo in one operation. |
| 2403 | * The data items must be in a singly-linked list implemented using a |
| 2404 | * 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] | 2405 | * and must be re-initialized via sys_slist_init(). |
| 2406 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2407 | * @note Can be called by ISRs. |
| 2408 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2409 | * @param fifo Address of the FIFO queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2410 | * @param list Pointer to sys_slist_t object. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2411 | * |
| 2412 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2413 | * @req K-FIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2414 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2415 | #define k_fifo_put_slist(fifo, list) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2416 | k_queue_merge_slist(&(fifo)->_queue, list) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2417 | |
| 2418 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2419 | * @brief Get an element from a FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2420 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2421 | * 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] | 2422 | * 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] | 2423 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2424 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 2425 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2426 | * @param fifo Address of the FIFO queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2427 | * @param timeout Waiting period to obtain a data item (in milliseconds), |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2428 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 2429 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2430 | * @return Address of the data item if successful; NULL if returned |
| 2431 | * without waiting, or waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2432 | * @req K-FIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2433 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2434 | #define k_fifo_get(fifo, timeout) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2435 | k_queue_get(&(fifo)->_queue, timeout) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2436 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2437 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2438 | * @brief Query a FIFO queue to see if it has data available. |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2439 | * |
| 2440 | * 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] | 2441 | * if other threads is also trying to read from the FIFO. |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2442 | * |
| 2443 | * @note Can be called by ISRs. |
| 2444 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2445 | * @param fifo Address of the FIFO queue. |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2446 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2447 | * @return Non-zero if the FIFO queue is empty. |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2448 | * @return 0 if data is available. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2449 | * @req K-FIFO-001 |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2450 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2451 | #define k_fifo_is_empty(fifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2452 | k_queue_is_empty(&(fifo)->_queue) |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2453 | |
| 2454 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2455 | * @brief Peek element at the head of a FIFO queue. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2456 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2457 | * 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] | 2458 | * 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] | 2459 | * on each iteration of processing, a head container will be peeked, |
| 2460 | * 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] | 2461 | * it will be completely remove from the FIFO queue. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2462 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2463 | * @param fifo Address of the FIFO queue. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2464 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2465 | * @return Head element, or NULL if the FIFO queue is empty. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2466 | * @req K-FIFO-001 |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2467 | */ |
| 2468 | #define k_fifo_peek_head(fifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2469 | k_queue_peek_head(&(fifo)->_queue) |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2470 | |
| 2471 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2472 | * @brief Peek element at the tail of FIFO queue. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2473 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2474 | * Return element from the tail of FIFO queue (without removing it). A usecase |
| 2475 | * for this is if elements of the FIFO queue are themselves containers. Then |
| 2476 | * 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] | 2477 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2478 | * @param fifo Address of the FIFO queue. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2479 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2480 | * @return Tail element, or NULL if a FIFO queue is empty. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2481 | * @req K-FIFO-001 |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2482 | */ |
| 2483 | #define k_fifo_peek_tail(fifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2484 | k_queue_peek_tail(&(fifo)->_queue) |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2485 | |
| 2486 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2487 | * @brief Statically define and initialize a FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2488 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2489 | * 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] | 2490 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 2491 | * @code extern struct k_fifo <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2492 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2493 | * @param name Name of the FIFO queue. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2494 | * @req K-FIFO-002 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2495 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2496 | #define K_FIFO_DEFINE(name) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 2497 | Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \ |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 2498 | Z_FIFO_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2499 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 2500 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2501 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2502 | struct k_lifo { |
Luiz Augusto von Dentz | 0dc4dd4 | 2017-02-21 15:49:52 +0200 | [diff] [blame] | 2503 | struct k_queue _queue; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2504 | }; |
| 2505 | |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2506 | /** |
| 2507 | * @cond INTERNAL_HIDDEN |
| 2508 | */ |
| 2509 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2510 | #define _K_LIFO_INITIALIZER(obj) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2511 | { \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2512 | ._queue = _K_QUEUE_INITIALIZER(obj._queue) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2513 | } |
| 2514 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 2515 | #define K_LIFO_INITIALIZER __DEPRECATED_MACRO _K_LIFO_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2516 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2517 | /** |
| 2518 | * INTERNAL_HIDDEN @endcond |
| 2519 | */ |
| 2520 | |
| 2521 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2522 | * @defgroup lifo_apis LIFO APIs |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2523 | * @ingroup kernel_apis |
| 2524 | * @{ |
| 2525 | */ |
| 2526 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2527 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2528 | * @brief Initialize a LIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2529 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2530 | * This routine initializes a LIFO queue object, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2531 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2532 | * @param lifo Address of the LIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2533 | * |
| 2534 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2535 | * @req K-LIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2536 | */ |
Luiz Augusto von Dentz | 0dc4dd4 | 2017-02-21 15:49:52 +0200 | [diff] [blame] | 2537 | #define k_lifo_init(lifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2538 | k_queue_init(&(lifo)->_queue) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2539 | |
| 2540 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2541 | * @brief Add an element to a LIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2542 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2543 | * 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] | 2544 | * 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] | 2545 | * reserved for the kernel's use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2546 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2547 | * @note Can be called by ISRs. |
| 2548 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2549 | * @param lifo Address of the LIFO queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2550 | * @param data Address of the data item. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2551 | * |
| 2552 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2553 | * @req K-LIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2554 | */ |
Luiz Augusto von Dentz | 0dc4dd4 | 2017-02-21 15:49:52 +0200 | [diff] [blame] | 2555 | #define k_lifo_put(lifo, data) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2556 | k_queue_prepend(&(lifo)->_queue, data) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2557 | |
| 2558 | /** |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2559 | * @brief Add an element to a LIFO queue. |
| 2560 | * |
Andrew Boie | ac3dcc1 | 2019-04-01 12:28:03 -0700 | [diff] [blame] | 2561 | * This routine adds a data item to @a lifo. There is an implicit memory |
| 2562 | * allocation to create an additional temporary bookkeeping data structure from |
| 2563 | * the calling thread's resource pool, which is automatically freed when the |
| 2564 | * item is removed. The data itself is not copied. |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2565 | * |
| 2566 | * @note Can be called by ISRs. |
| 2567 | * |
| 2568 | * @param lifo Address of the LIFO. |
| 2569 | * @param data Address of the data item. |
| 2570 | * |
| 2571 | * @retval 0 on success |
| 2572 | * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2573 | * @req K-LIFO-001 |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2574 | */ |
| 2575 | #define k_lifo_alloc_put(lifo, data) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2576 | k_queue_alloc_prepend(&(lifo)->_queue, data) |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2577 | |
| 2578 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2579 | * @brief Get an element from a LIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2580 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2581 | * 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] | 2582 | * 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] | 2583 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2584 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 2585 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2586 | * @param lifo Address of the LIFO queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2587 | * @param timeout Waiting period to obtain a data item (in milliseconds), |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2588 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 2589 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2590 | * @return Address of the data item if successful; NULL if returned |
| 2591 | * without waiting, or waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2592 | * @req K-LIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2593 | */ |
Luiz Augusto von Dentz | 0dc4dd4 | 2017-02-21 15:49:52 +0200 | [diff] [blame] | 2594 | #define k_lifo_get(lifo, timeout) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2595 | k_queue_get(&(lifo)->_queue, timeout) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2596 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2597 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2598 | * @brief Statically define and initialize a LIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2599 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2600 | * 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] | 2601 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 2602 | * @code extern struct k_lifo <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2603 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2604 | * @param name Name of the fifo. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2605 | * @req K-LIFO-002 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2606 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2607 | #define K_LIFO_DEFINE(name) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 2608 | Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2609 | _K_LIFO_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2610 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 2611 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2612 | |
| 2613 | /** |
| 2614 | * @cond INTERNAL_HIDDEN |
| 2615 | */ |
Adithya Baglody | 28080d3 | 2018-10-15 11:48:51 +0530 | [diff] [blame] | 2616 | #define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2617 | |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2618 | typedef uintptr_t stack_data_t; |
| 2619 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2620 | struct k_stack { |
| 2621 | _wait_q_t wait_q; |
Andy Ross | f0933d0 | 2018-07-26 10:23:02 -0700 | [diff] [blame] | 2622 | struct k_spinlock lock; |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2623 | stack_data_t *base, *next, *top; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2624 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 2625 | _OBJECT_TRACING_NEXT_PTR(k_stack) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 2626 | _OBJECT_TRACING_LINKED_FLAG |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2627 | u8_t flags; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2628 | }; |
| 2629 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2630 | #define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2631 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2632 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2633 | .base = stack_buffer, \ |
| 2634 | .next = stack_buffer, \ |
| 2635 | .top = stack_buffer + stack_num_entries, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 2636 | _OBJECT_TRACING_INIT \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2637 | } |
| 2638 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 2639 | #define K_STACK_INITIALIZER __DEPRECATED_MACRO _K_STACK_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2640 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2641 | /** |
| 2642 | * INTERNAL_HIDDEN @endcond |
| 2643 | */ |
| 2644 | |
| 2645 | /** |
| 2646 | * @defgroup stack_apis Stack APIs |
| 2647 | * @ingroup kernel_apis |
| 2648 | * @{ |
| 2649 | */ |
| 2650 | |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2651 | /** |
| 2652 | * @brief Initialize a stack. |
| 2653 | * |
| 2654 | * This routine initializes a stack object, prior to its first use. |
| 2655 | * |
| 2656 | * @param stack Address of the stack. |
| 2657 | * @param buffer Address of array used to hold stacked values. |
| 2658 | * @param num_entries Maximum number of values that can be stacked. |
| 2659 | * |
| 2660 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2661 | * @req K-STACK-001 |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2662 | */ |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2663 | void k_stack_init(struct k_stack *stack, |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2664 | stack_data_t *buffer, u32_t num_entries); |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2665 | |
| 2666 | |
| 2667 | /** |
| 2668 | * @brief Initialize a stack. |
| 2669 | * |
| 2670 | * This routine initializes a stack object, prior to its first use. Internal |
| 2671 | * buffers will be allocated from the calling thread's resource pool. |
| 2672 | * This memory will be released if k_stack_cleanup() is called, or |
| 2673 | * userspace is enabled and the stack object loses all references to it. |
| 2674 | * |
| 2675 | * @param stack Address of the stack. |
| 2676 | * @param num_entries Maximum number of values that can be stacked. |
| 2677 | * |
| 2678 | * @return -ENOMEM if memory couldn't be allocated |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2679 | * @req K-STACK-001 |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2680 | */ |
| 2681 | |
Adithya Baglody | 28080d3 | 2018-10-15 11:48:51 +0530 | [diff] [blame] | 2682 | __syscall s32_t k_stack_alloc_init(struct k_stack *stack, |
| 2683 | u32_t num_entries); |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2684 | |
| 2685 | /** |
| 2686 | * @brief Release a stack's allocated buffer |
| 2687 | * |
| 2688 | * If a stack object was given a dynamically allocated buffer via |
| 2689 | * k_stack_alloc_init(), this will free it. This function does nothing |
| 2690 | * if the buffer wasn't dynamically allocated. |
| 2691 | * |
| 2692 | * @param stack Address of the stack. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2693 | * @req K-STACK-001 |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2694 | */ |
| 2695 | void k_stack_cleanup(struct k_stack *stack); |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2696 | |
| 2697 | /** |
| 2698 | * @brief Push an element onto a stack. |
| 2699 | * |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2700 | * 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] | 2701 | * |
| 2702 | * @note Can be called by ISRs. |
| 2703 | * |
| 2704 | * @param stack Address of the stack. |
| 2705 | * @param data Value to push onto the stack. |
| 2706 | * |
| 2707 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2708 | * @req K-STACK-001 |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2709 | */ |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2710 | __syscall void k_stack_push(struct k_stack *stack, stack_data_t data); |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2711 | |
| 2712 | /** |
| 2713 | * @brief Pop an element from a stack. |
| 2714 | * |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2715 | * This routine removes a stack_data_t value from @a stack in a "last in, |
| 2716 | * first out" manner and stores the value in @a data. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2717 | * |
| 2718 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 2719 | * |
| 2720 | * @param stack Address of the stack. |
| 2721 | * @param data Address of area to hold the value popped from the stack. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 2722 | * @param timeout Non-negative waiting period to obtain a value (in |
| 2723 | * milliseconds), or one of the special values K_NO_WAIT and |
| 2724 | * K_FOREVER. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2725 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2726 | * @retval 0 Element popped from stack. |
| 2727 | * @retval -EBUSY Returned without waiting. |
| 2728 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2729 | * @req K-STACK-001 |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2730 | */ |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 2731 | __syscall int k_stack_pop(struct k_stack *stack, stack_data_t *data, |
| 2732 | s32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2733 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2734 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2735 | * @brief Statically define and initialize a stack |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2736 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2737 | * 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] | 2738 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 2739 | * @code extern struct k_stack <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2740 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2741 | * @param name Name of the stack. |
| 2742 | * @param stack_num_entries Maximum number of values that can be stacked. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2743 | * @req K-STACK-002 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2744 | */ |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 2745 | #define K_STACK_DEFINE(name, stack_num_entries) \ |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2746 | stack_data_t __noinit \ |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 2747 | _k_stack_buf_##name[stack_num_entries]; \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 2748 | Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2749 | _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \ |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 2750 | stack_num_entries) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2751 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 2752 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2753 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2754 | struct k_work; |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 2755 | struct k_work_poll; |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2756 | |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 2757 | /* private, used by k_poll and k_work_poll */ |
Piotr Zięcik | 1c4177d | 2019-08-27 12:19:26 +0200 | [diff] [blame] | 2758 | typedef int (*_poller_cb_t)(struct k_poll_event *event, u32_t state); |
| 2759 | struct _poller { |
| 2760 | volatile bool is_polling; |
| 2761 | struct k_thread *thread; |
| 2762 | _poller_cb_t cb; |
| 2763 | }; |
| 2764 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2765 | /** |
Anas Nashif | 29f37f0 | 2019-01-21 14:30:35 -0500 | [diff] [blame] | 2766 | * @addtogroup thread_apis |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2767 | * @{ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2768 | */ |
| 2769 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2770 | /** |
| 2771 | * @typedef k_work_handler_t |
| 2772 | * @brief Work item handler function type. |
| 2773 | * |
| 2774 | * A work item's handler function is executed by a workqueue's thread |
| 2775 | * when the work item is processed by the workqueue. |
| 2776 | * |
| 2777 | * @param work Address of the work item. |
| 2778 | * |
| 2779 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2780 | * @req K-WORK-001 |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2781 | */ |
| 2782 | typedef void (*k_work_handler_t)(struct k_work *work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2783 | |
| 2784 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2785 | * @cond INTERNAL_HIDDEN |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2786 | */ |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2787 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2788 | struct k_work_q { |
Luiz Augusto von Dentz | adb581b | 2017-07-03 19:09:44 +0300 | [diff] [blame] | 2789 | struct k_queue queue; |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 2790 | struct k_thread thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2791 | }; |
| 2792 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2793 | enum { |
Iván Briano | 9c7b5ea | 2016-10-04 18:11:05 -0300 | [diff] [blame] | 2794 | K_WORK_STATE_PENDING, /* Work item pending state */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2795 | }; |
| 2796 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2797 | struct k_work { |
Luiz Augusto von Dentz | adb581b | 2017-07-03 19:09:44 +0300 | [diff] [blame] | 2798 | void *_reserved; /* Used by k_queue implementation. */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2799 | k_work_handler_t handler; |
| 2800 | atomic_t flags[1]; |
| 2801 | }; |
| 2802 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2803 | struct k_delayed_work { |
| 2804 | struct k_work work; |
| 2805 | struct _timeout timeout; |
| 2806 | struct k_work_q *work_q; |
| 2807 | }; |
| 2808 | |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 2809 | struct k_work_poll { |
| 2810 | struct k_work work; |
| 2811 | struct _poller poller; |
| 2812 | struct k_poll_event *events; |
| 2813 | int num_events; |
| 2814 | k_work_handler_t real_handler; |
| 2815 | struct _timeout timeout; |
| 2816 | int poll_result; |
| 2817 | }; |
| 2818 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2819 | extern struct k_work_q k_sys_work_q; |
| 2820 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2821 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2822 | * INTERNAL_HIDDEN @endcond |
| 2823 | */ |
| 2824 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2825 | #define Z_WORK_INITIALIZER(work_handler) \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2826 | { \ |
| 2827 | ._reserved = NULL, \ |
| 2828 | .handler = work_handler, \ |
| 2829 | .flags = { 0 } \ |
| 2830 | } |
| 2831 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 2832 | #define K_WORK_INITIALIZER __DEPRECATED_MACRO Z_WORK_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2833 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2834 | /** |
| 2835 | * @brief Initialize a statically-defined work item. |
| 2836 | * |
| 2837 | * This macro can be used to initialize a statically-defined workqueue work |
| 2838 | * item, prior to its first use. For example, |
| 2839 | * |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2840 | * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2841 | * |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2842 | * @param work Symbol name for work item object |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2843 | * @param work_handler Function to invoke each time work item is processed. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2844 | * @req K-WORK-002 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2845 | */ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2846 | #define K_WORK_DEFINE(work, work_handler) \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2847 | struct k_work work = Z_WORK_INITIALIZER(work_handler) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2848 | |
| 2849 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2850 | * @brief Initialize a work item. |
| 2851 | * |
| 2852 | * This routine initializes a workqueue work item, prior to its first use. |
| 2853 | * |
| 2854 | * @param work Address of work item. |
| 2855 | * @param handler Function to invoke each time work item is processed. |
| 2856 | * |
| 2857 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2858 | * @req K-WORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2859 | */ |
| 2860 | static inline void k_work_init(struct k_work *work, k_work_handler_t handler) |
| 2861 | { |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2862 | *work = (struct k_work)Z_WORK_INITIALIZER(handler); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2863 | } |
| 2864 | |
| 2865 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2866 | * @brief Submit a work item. |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 2867 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2868 | * This routine submits work item @a work to be processed by workqueue |
| 2869 | * @a work_q. If the work item is already pending in the workqueue's queue |
| 2870 | * as a result of an earlier submission, this routine has no effect on the |
| 2871 | * work item. If the work item has already been processed, or is currently |
| 2872 | * being processed, its work is considered complete and the work item can be |
| 2873 | * resubmitted. |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 2874 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2875 | * @warning |
| 2876 | * A submitted work item must not be modified until it has been processed |
| 2877 | * by the workqueue. |
| 2878 | * |
| 2879 | * @note Can be called by ISRs. |
| 2880 | * |
| 2881 | * @param work_q Address of workqueue. |
| 2882 | * @param work Address of work item. |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 2883 | * |
| 2884 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2885 | * @req K-WORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2886 | */ |
| 2887 | static inline void k_work_submit_to_queue(struct k_work_q *work_q, |
| 2888 | struct k_work *work) |
| 2889 | { |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 2890 | 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] | 2891 | k_queue_append(&work_q->queue, work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2892 | } |
| 2893 | } |
| 2894 | |
| 2895 | /** |
Andrew Boie | 2b1d54e | 2018-11-12 14:25:19 -0800 | [diff] [blame] | 2896 | * @brief Submit a work item to a user mode workqueue |
| 2897 | * |
David B. Kinder | 06d7835 | 2018-12-17 14:32:40 -0800 | [diff] [blame] | 2898 | * 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] | 2899 | * memory allocation is made from the caller's resource pool which is freed |
| 2900 | * once the worker thread consumes the k_work item. The workqueue |
| 2901 | * thread must have memory access to the k_work item being submitted. The caller |
| 2902 | * must have permission granted on the work_q parameter's queue object. |
| 2903 | * |
| 2904 | * Otherwise this works the same as k_work_submit_to_queue(). |
| 2905 | * |
| 2906 | * @note Can be called by ISRs. |
| 2907 | * |
| 2908 | * @param work_q Address of workqueue. |
| 2909 | * @param work Address of work item. |
| 2910 | * |
| 2911 | * @retval -EBUSY if the work item was already in some workqueue |
| 2912 | * @retval -ENOMEM if no memory for thread resource pool allocation |
| 2913 | * @retval 0 Success |
| 2914 | * @req K-WORK-001 |
| 2915 | */ |
| 2916 | static inline int k_work_submit_to_user_queue(struct k_work_q *work_q, |
| 2917 | struct k_work *work) |
| 2918 | { |
| 2919 | int ret = -EBUSY; |
| 2920 | |
| 2921 | if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) { |
| 2922 | ret = k_queue_alloc_append(&work_q->queue, work); |
| 2923 | |
| 2924 | /* Couldn't insert into the queue. Clear the pending bit |
| 2925 | * so the work item can be submitted again |
| 2926 | */ |
Flavio Ceolin | 76b3518 | 2018-12-16 12:48:29 -0800 | [diff] [blame] | 2927 | if (ret != 0) { |
Andrew Boie | 2b1d54e | 2018-11-12 14:25:19 -0800 | [diff] [blame] | 2928 | atomic_clear_bit(work->flags, K_WORK_STATE_PENDING); |
| 2929 | } |
| 2930 | } |
| 2931 | |
| 2932 | return ret; |
| 2933 | } |
| 2934 | |
| 2935 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2936 | * @brief Check if a work item is pending. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2937 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2938 | * This routine indicates if work item @a work is pending in a workqueue's |
| 2939 | * queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2940 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2941 | * @note Can be called by ISRs. |
| 2942 | * |
| 2943 | * @param work Address of work item. |
| 2944 | * |
Flavio Ceolin | 82ef4f8 | 2018-11-21 18:12:34 -0800 | [diff] [blame] | 2945 | * @return true if work item is pending, or false if it is not pending. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2946 | * @req K-WORK-001 |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 2947 | */ |
Flavio Ceolin | 82ef4f8 | 2018-11-21 18:12:34 -0800 | [diff] [blame] | 2948 | static inline bool k_work_pending(struct k_work *work) |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 2949 | { |
Iván Briano | 9c7b5ea | 2016-10-04 18:11:05 -0300 | [diff] [blame] | 2950 | return atomic_test_bit(work->flags, K_WORK_STATE_PENDING); |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 2951 | } |
| 2952 | |
| 2953 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2954 | * @brief Start a workqueue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2955 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2956 | * This routine starts workqueue @a work_q. The workqueue spawns its work |
| 2957 | * processing thread, which runs forever. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2958 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2959 | * @param work_q Address of workqueue. |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 2960 | * @param stack Pointer to work queue thread's stack space, as defined by |
| 2961 | * K_THREAD_STACK_DEFINE() |
| 2962 | * @param stack_size Size of the work queue thread's stack (in bytes), which |
| 2963 | * should either be the same constant passed to |
| 2964 | * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF(). |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2965 | * @param prio Priority of the work queue's thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2966 | * |
| 2967 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2968 | * @req K-WORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2969 | */ |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 2970 | extern void k_work_q_start(struct k_work_q *work_q, |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 2971 | k_thread_stack_t *stack, |
Benjamin Walsh | 669360d | 2016-11-14 16:46:14 -0500 | [diff] [blame] | 2972 | size_t stack_size, int prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2973 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2974 | /** |
Andrew Boie | 2b1d54e | 2018-11-12 14:25:19 -0800 | [diff] [blame] | 2975 | * @brief Start a workqueue in user mode |
| 2976 | * |
| 2977 | * This works identically to k_work_q_start() except it is callable from user |
| 2978 | * mode, and the worker thread created will run in user mode. |
| 2979 | * The caller must have permissions granted on both the work_q parameter's |
| 2980 | * thread and queue objects, and the same restrictions on priority apply as |
| 2981 | * k_thread_create(). |
| 2982 | * |
| 2983 | * @param work_q Address of workqueue. |
| 2984 | * @param stack Pointer to work queue thread's stack space, as defined by |
| 2985 | * K_THREAD_STACK_DEFINE() |
| 2986 | * @param stack_size Size of the work queue thread's stack (in bytes), which |
| 2987 | * should either be the same constant passed to |
| 2988 | * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF(). |
| 2989 | * @param prio Priority of the work queue's thread. |
| 2990 | * |
| 2991 | * @return N/A |
| 2992 | * @req K-WORK-001 |
| 2993 | */ |
| 2994 | extern void k_work_q_user_start(struct k_work_q *work_q, |
| 2995 | k_thread_stack_t *stack, |
| 2996 | size_t stack_size, int prio); |
| 2997 | |
| 2998 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2999 | * @brief Initialize a delayed work item. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3000 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3001 | * This routine initializes a workqueue delayed work item, prior to |
| 3002 | * its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3003 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3004 | * @param work Address of delayed work item. |
| 3005 | * @param handler Function to invoke each time work item is processed. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3006 | * |
| 3007 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3008 | * @req K-DWORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3009 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 3010 | extern void k_delayed_work_init(struct k_delayed_work *work, |
| 3011 | k_work_handler_t handler); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3012 | |
| 3013 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3014 | * @brief Submit a delayed work item. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3015 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3016 | * This routine schedules work item @a work to be processed by workqueue |
| 3017 | * @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] | 3018 | * 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] | 3019 | * Only when the countdown completes is the work item actually submitted to |
| 3020 | * the workqueue and becomes pending. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3021 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3022 | * Submitting a previously submitted delayed work item that is still |
Andy Ross | 03c1d28 | 2018-02-13 12:13:25 -0800 | [diff] [blame] | 3023 | * counting down cancels the existing submission and restarts the |
| 3024 | * countdown using the new delay. Note that this behavior is |
| 3025 | * inherently subject to race conditions with the pre-existing |
| 3026 | * timeouts and work queue, so care must be taken to synchronize such |
| 3027 | * resubmissions externally. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3028 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3029 | * @warning |
| 3030 | * A delayed work item must not be modified until it has been processed |
| 3031 | * by the workqueue. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3032 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3033 | * @note Can be called by ISRs. |
| 3034 | * |
| 3035 | * @param work_q Address of workqueue. |
| 3036 | * @param work Address of delayed work item. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3037 | * @param delay Non-negative delay before submitting the work item (in |
| 3038 | * milliseconds). |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3039 | * |
| 3040 | * @retval 0 Work item countdown started. |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3041 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 3042 | * @retval -EADDRINUSE Work item is pending on a different workqueue. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3043 | * @req K-DWORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3044 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 3045 | extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q, |
| 3046 | struct k_delayed_work *work, |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3047 | s32_t delay); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3048 | |
| 3049 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3050 | * @brief Cancel a delayed work item. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3051 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3052 | * This routine cancels the submission of delayed work item @a work. |
David B. Kinder | 8b986d7 | 2017-04-18 15:56:26 -0700 | [diff] [blame] | 3053 | * 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] | 3054 | * underway. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3055 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3056 | * @note Can be called by ISRs. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3057 | * |
Andy Ross | d7ae2a8 | 2019-03-08 08:51:13 -0800 | [diff] [blame] | 3058 | * @note The result of calling this on a k_delayed_work item that has |
| 3059 | * not been submitted (i.e. before the return of the |
| 3060 | * k_delayed_work_submit_to_queue() call) is undefined. |
| 3061 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3062 | * @param work Address of delayed work item. |
| 3063 | * |
David B. Kinder | 8b986d7 | 2017-04-18 15:56:26 -0700 | [diff] [blame] | 3064 | * @retval 0 Work item countdown canceled. |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3065 | * @retval -EINVAL Work item is being processed or has completed its work. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3066 | * @req K-DWORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3067 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 3068 | extern int k_delayed_work_cancel(struct k_delayed_work *work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3069 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3070 | /** |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3071 | * @brief Submit a work item to the system workqueue. |
| 3072 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3073 | * This routine submits work item @a work to be processed by the system |
| 3074 | * workqueue. If the work item is already pending in the workqueue's queue |
| 3075 | * as a result of an earlier submission, this routine has no effect on the |
| 3076 | * work item. If the work item has already been processed, or is currently |
| 3077 | * being processed, its work is considered complete and the work item can be |
| 3078 | * resubmitted. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3079 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3080 | * @warning |
| 3081 | * Work items submitted to the system workqueue should avoid using handlers |
| 3082 | * that block or yield since this may prevent the system workqueue from |
| 3083 | * processing other work items in a timely manner. |
| 3084 | * |
| 3085 | * @note Can be called by ISRs. |
| 3086 | * |
| 3087 | * @param work Address of work item. |
| 3088 | * |
| 3089 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3090 | * @req K-WORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3091 | */ |
| 3092 | static inline void k_work_submit(struct k_work *work) |
| 3093 | { |
| 3094 | k_work_submit_to_queue(&k_sys_work_q, work); |
| 3095 | } |
| 3096 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3097 | /** |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3098 | * @brief Submit a delayed work item to the system workqueue. |
| 3099 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3100 | * This routine schedules work item @a work to be processed by the system |
| 3101 | * workqueue after a delay of @a delay milliseconds. The routine initiates |
David B. Kinder | 8b986d7 | 2017-04-18 15:56:26 -0700 | [diff] [blame] | 3102 | * 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] | 3103 | * Only when the countdown completes is the work item actually submitted to |
| 3104 | * the workqueue and becomes pending. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3105 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3106 | * Submitting a previously submitted delayed work item that is still |
| 3107 | * counting down cancels the existing submission and restarts the countdown |
| 3108 | * using the new delay. If the work item is currently pending on the |
| 3109 | * workqueue's queue because the countdown has completed it is too late to |
| 3110 | * resubmit the item, and resubmission fails without impacting the work item. |
| 3111 | * If the work item has already been processed, or is currently being processed, |
| 3112 | * its work is considered complete and the work item can be resubmitted. |
| 3113 | * |
| 3114 | * @warning |
| 3115 | * Work items submitted to the system workqueue should avoid using handlers |
| 3116 | * that block or yield since this may prevent the system workqueue from |
| 3117 | * processing other work items in a timely manner. |
| 3118 | * |
| 3119 | * @note Can be called by ISRs. |
| 3120 | * |
| 3121 | * @param work Address of delayed work item. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3122 | * @param delay Non-negative delay before submitting the work item (in |
| 3123 | * milliseconds). |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3124 | * |
| 3125 | * @retval 0 Work item countdown started. |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3126 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 3127 | * @retval -EADDRINUSE Work item is pending on a different workqueue. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3128 | * @req K-DWORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3129 | */ |
| 3130 | static inline int k_delayed_work_submit(struct k_delayed_work *work, |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3131 | s32_t delay) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3132 | { |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 3133 | 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] | 3134 | } |
| 3135 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3136 | /** |
Johan Hedberg | c8201b2 | 2016-12-09 10:42:22 +0200 | [diff] [blame] | 3137 | * @brief Get time remaining before a delayed work gets scheduled. |
| 3138 | * |
| 3139 | * This routine computes the (approximate) time remaining before a |
| 3140 | * 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] | 3141 | * scheduled, it returns zero. |
Johan Hedberg | c8201b2 | 2016-12-09 10:42:22 +0200 | [diff] [blame] | 3142 | * |
| 3143 | * @param work Delayed work item. |
| 3144 | * |
| 3145 | * @return Remaining time (in milliseconds). |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3146 | * @req K-DWORK-001 |
Johan Hedberg | c8201b2 | 2016-12-09 10:42:22 +0200 | [diff] [blame] | 3147 | */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3148 | 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] | 3149 | { |
Andy Ross | 8892406 | 2019-10-03 11:43:10 -0700 | [diff] [blame] | 3150 | return k_ticks_to_ms_floor64(z_timeout_remaining(&work->timeout)); |
Johan Hedberg | c8201b2 | 2016-12-09 10:42:22 +0200 | [diff] [blame] | 3151 | } |
| 3152 | |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 3153 | /** |
| 3154 | * @brief Initialize a triggered work item. |
| 3155 | * |
| 3156 | * This routine initializes a workqueue triggered work item, prior to |
| 3157 | * its first use. |
| 3158 | * |
| 3159 | * @param work Address of triggered work item. |
| 3160 | * @param handler Function to invoke each time work item is processed. |
| 3161 | * |
| 3162 | * @return N/A |
| 3163 | */ |
| 3164 | extern void k_work_poll_init(struct k_work_poll *work, |
| 3165 | k_work_handler_t handler); |
| 3166 | |
| 3167 | /** |
| 3168 | * @brief Submit a triggered work item. |
| 3169 | * |
| 3170 | * This routine schedules work item @a work to be processed by workqueue |
| 3171 | * @a work_q when one of the given @a events is signaled. The routine |
| 3172 | * initiates internal poller for the work item and then returns to the caller. |
| 3173 | * Only when one of the watched events happen the work item is actually |
| 3174 | * submitted to the workqueue and becomes pending. |
| 3175 | * |
| 3176 | * Submitting a previously submitted triggered work item that is still |
| 3177 | * waiting for the event cancels the existing submission and reschedules it |
| 3178 | * 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] | 3179 | * 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] | 3180 | * so care must be taken to synchronize such resubmissions externally. |
| 3181 | * |
| 3182 | * @note Can be called by ISRs. |
| 3183 | * |
| 3184 | * @warning |
| 3185 | * Provided array of events as well as a triggered work item must be placed |
| 3186 | * in persistent memory (valid until work handler execution or work |
| 3187 | * cancellation) and cannot be modified after submission. |
| 3188 | * |
| 3189 | * @param work_q Address of workqueue. |
| 3190 | * @param work Address of delayed work item. |
| 3191 | * @param events An array of pointers to events which trigger the work. |
| 3192 | * @param num_events The number of events in the array. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3193 | * @param timeout Non-negative timeout after which the work will be scheduled |
| 3194 | * for execution even if not triggered. |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 3195 | * |
| 3196 | * |
| 3197 | * @retval 0 Work item started watching for events. |
| 3198 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 3199 | * @retval -EADDRINUSE Work item is pending on a different workqueue. |
| 3200 | */ |
| 3201 | extern int k_work_poll_submit_to_queue(struct k_work_q *work_q, |
| 3202 | struct k_work_poll *work, |
| 3203 | struct k_poll_event *events, |
| 3204 | int num_events, |
| 3205 | s32_t timeout); |
| 3206 | |
| 3207 | /** |
| 3208 | * @brief Submit a triggered work item to the system workqueue. |
| 3209 | * |
| 3210 | * This routine schedules work item @a work to be processed by system |
| 3211 | * workqueue when one of the given @a events is signaled. The routine |
| 3212 | * initiates internal poller for the work item and then returns to the caller. |
| 3213 | * Only when one of the watched events happen the work item is actually |
| 3214 | * submitted to the workqueue and becomes pending. |
| 3215 | * |
| 3216 | * Submitting a previously submitted triggered work item that is still |
| 3217 | * waiting for the event cancels the existing submission and reschedules it |
| 3218 | * 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] | 3219 | * 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] | 3220 | * so care must be taken to synchronize such resubmissions externally. |
| 3221 | * |
| 3222 | * @note Can be called by ISRs. |
| 3223 | * |
| 3224 | * @warning |
| 3225 | * Provided array of events as well as a triggered work item must not be |
| 3226 | * modified until the item has been processed by the workqueue. |
| 3227 | * |
| 3228 | * @param work Address of delayed work item. |
| 3229 | * @param events An array of pointers to events which trigger the work. |
| 3230 | * @param num_events The number of events in the array. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3231 | * @param timeout Non-negative timeout after which the work will be scheduled |
| 3232 | * for execution even if not triggered. |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 3233 | * |
| 3234 | * @retval 0 Work item started watching for events. |
| 3235 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 3236 | * @retval -EADDRINUSE Work item is pending on a different workqueue. |
| 3237 | */ |
| 3238 | static inline int k_work_poll_submit(struct k_work_poll *work, |
| 3239 | struct k_poll_event *events, |
| 3240 | int num_events, |
| 3241 | s32_t timeout) |
| 3242 | { |
| 3243 | return k_work_poll_submit_to_queue(&k_sys_work_q, work, |
| 3244 | events, num_events, timeout); |
| 3245 | } |
| 3246 | |
| 3247 | /** |
| 3248 | * @brief Cancel a triggered work item. |
| 3249 | * |
| 3250 | * This routine cancels the submission of triggered work item @a work. |
| 3251 | * A triggered work item can only be canceled if no event triggered work |
| 3252 | * submission. |
| 3253 | * |
| 3254 | * @note Can be called by ISRs. |
| 3255 | * |
| 3256 | * @param work Address of delayed work item. |
| 3257 | * |
David B. Kinder | 73896c0 | 2019-10-28 16:27:57 -0700 | [diff] [blame] | 3258 | * @retval 0 Work item canceled. |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 3259 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 3260 | */ |
| 3261 | extern int k_work_poll_cancel(struct k_work_poll *work); |
| 3262 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 3263 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3264 | /** |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3265 | * @defgroup mutex_apis Mutex APIs |
| 3266 | * @ingroup kernel_apis |
| 3267 | * @{ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3268 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3269 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3270 | /** |
| 3271 | * Mutex Structure |
| 3272 | * @ingroup mutex_apis |
| 3273 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3274 | struct k_mutex { |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3275 | /** Mutex wait queue */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3276 | _wait_q_t wait_q; |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3277 | /** Mutex owner */ |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 3278 | struct k_thread *owner; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3279 | |
| 3280 | /** Current lock count */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3281 | u32_t lock_count; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3282 | |
| 3283 | /** Original thread priority */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3284 | int owner_orig_prio; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3285 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 3286 | _OBJECT_TRACING_NEXT_PTR(k_mutex) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 3287 | _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3288 | }; |
| 3289 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3290 | /** |
| 3291 | * @cond INTERNAL_HIDDEN |
| 3292 | */ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3293 | #define _K_MUTEX_INITIALIZER(obj) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3294 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3295 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3296 | .owner = NULL, \ |
| 3297 | .lock_count = 0, \ |
| 3298 | .owner_orig_prio = K_LOWEST_THREAD_PRIO, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 3299 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3300 | } |
| 3301 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 3302 | #define K_MUTEX_INITIALIZER __DEPRECATED_MACRO _K_MUTEX_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3303 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3304 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3305 | * INTERNAL_HIDDEN @endcond |
| 3306 | */ |
| 3307 | |
| 3308 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3309 | * @brief Statically define and initialize a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3310 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3311 | * 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] | 3312 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 3313 | * @code extern struct k_mutex <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3314 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3315 | * @param name Name of the mutex. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3316 | * @req K-MUTEX-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3317 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3318 | #define K_MUTEX_DEFINE(name) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 3319 | Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3320 | _K_MUTEX_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3321 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3322 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3323 | * @brief Initialize a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3324 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3325 | * This routine initializes a mutex object, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3326 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3327 | * Upon completion, the mutex is available and does not have an owner. |
| 3328 | * |
| 3329 | * @param mutex Address of the mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3330 | * |
| 3331 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3332 | * @req K-MUTEX-002 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3333 | */ |
Andrew Boie | 2f7519b | 2017-09-29 03:33:06 -0700 | [diff] [blame] | 3334 | __syscall void k_mutex_init(struct k_mutex *mutex); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3335 | |
| 3336 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3337 | * @brief Lock a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3338 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3339 | * This routine locks @a mutex. If the mutex is locked by another thread, |
| 3340 | * the calling thread waits until the mutex becomes available or until |
| 3341 | * a timeout occurs. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3342 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3343 | * A thread is permitted to lock a mutex it has already locked. The operation |
| 3344 | * completes immediately and the lock count is increased by 1. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3345 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3346 | * @param mutex Address of the mutex. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3347 | * @param timeout Non-negative waiting period to lock the mutex (in |
| 3348 | * milliseconds), or one of the special values K_NO_WAIT and |
| 3349 | * K_FOREVER. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3350 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3351 | * @retval 0 Mutex locked. |
| 3352 | * @retval -EBUSY Returned without waiting. |
| 3353 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3354 | * @req K-MUTEX-002 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3355 | */ |
Andrew Boie | 2f7519b | 2017-09-29 03:33:06 -0700 | [diff] [blame] | 3356 | __syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3357 | |
| 3358 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3359 | * @brief Unlock a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3360 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3361 | * This routine unlocks @a mutex. The mutex must already be locked by the |
| 3362 | * calling thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3363 | * |
| 3364 | * 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] | 3365 | * 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] | 3366 | * thread. |
| 3367 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3368 | * @param mutex Address of the mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3369 | * |
| 3370 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3371 | * @req K-MUTEX-002 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3372 | */ |
Andrew Boie | 2f7519b | 2017-09-29 03:33:06 -0700 | [diff] [blame] | 3373 | __syscall void k_mutex_unlock(struct k_mutex *mutex); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3374 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3375 | /** |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 3376 | * @} |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3377 | */ |
| 3378 | |
| 3379 | /** |
| 3380 | * @cond INTERNAL_HIDDEN |
| 3381 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3382 | |
| 3383 | struct k_sem { |
| 3384 | _wait_q_t wait_q; |
Adithya Baglody | 4b06621 | 2018-10-16 11:59:12 +0530 | [diff] [blame] | 3385 | u32_t count; |
| 3386 | u32_t limit; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 3387 | _POLL_EVENT; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3388 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 3389 | _OBJECT_TRACING_NEXT_PTR(k_sem) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 3390 | _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3391 | }; |
| 3392 | |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 3393 | #define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3394 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3395 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3396 | .count = initial_count, \ |
| 3397 | .limit = count_limit, \ |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 3398 | _POLL_EVENT_OBJ_INIT(obj) \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 3399 | _OBJECT_TRACING_INIT \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3400 | } |
| 3401 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 3402 | #define K_SEM_INITIALIZER __DEPRECATED_MACRO Z_SEM_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3403 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3404 | /** |
| 3405 | * INTERNAL_HIDDEN @endcond |
| 3406 | */ |
| 3407 | |
| 3408 | /** |
| 3409 | * @defgroup semaphore_apis Semaphore APIs |
| 3410 | * @ingroup kernel_apis |
| 3411 | * @{ |
| 3412 | */ |
| 3413 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3414 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3415 | * @brief Initialize a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3416 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3417 | * This routine initializes a semaphore object, prior to its first use. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3418 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3419 | * @param sem Address of the semaphore. |
| 3420 | * @param initial_count Initial semaphore count. |
| 3421 | * @param limit Maximum permitted semaphore count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3422 | * |
| 3423 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3424 | * @req K-SEM-001 |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3425 | */ |
Andrew Boie | 9928023 | 2017-09-29 14:17:47 -0700 | [diff] [blame] | 3426 | __syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count, |
| 3427 | unsigned int limit); |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3428 | |
| 3429 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3430 | * @brief Take a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3431 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3432 | * This routine takes @a sem. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3433 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3434 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 3435 | * |
| 3436 | * @param sem Address of the semaphore. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3437 | * @param timeout Non-negative waiting period to take the semaphore (in |
| 3438 | * milliseconds), or one of the special values K_NO_WAIT and |
| 3439 | * K_FOREVER. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3440 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3441 | * @retval 0 Semaphore taken. |
| 3442 | * @retval -EBUSY Returned without waiting. |
| 3443 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3444 | * @req K-SEM-001 |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3445 | */ |
Andrew Boie | 9928023 | 2017-09-29 14:17:47 -0700 | [diff] [blame] | 3446 | __syscall int k_sem_take(struct k_sem *sem, s32_t timeout); |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3447 | |
| 3448 | /** |
| 3449 | * @brief Give a semaphore. |
| 3450 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3451 | * This routine gives @a sem, unless the semaphore is already at its maximum |
| 3452 | * permitted count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3453 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3454 | * @note Can be called by ISRs. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3455 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3456 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3457 | * |
| 3458 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3459 | * @req K-SEM-001 |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3460 | */ |
Andrew Boie | 9928023 | 2017-09-29 14:17:47 -0700 | [diff] [blame] | 3461 | __syscall void k_sem_give(struct k_sem *sem); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3462 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3463 | /** |
| 3464 | * @brief Reset a semaphore's count to zero. |
| 3465 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3466 | * This routine sets the count of @a sem to zero. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3467 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3468 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3469 | * |
| 3470 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3471 | * @req K-SEM-001 |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3472 | */ |
Andrew Boie | 990bf16 | 2017-10-03 12:36:49 -0700 | [diff] [blame] | 3473 | __syscall void k_sem_reset(struct k_sem *sem); |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 3474 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 3475 | /** |
| 3476 | * @internal |
| 3477 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3478 | static inline void z_impl_k_sem_reset(struct k_sem *sem) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3479 | { |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 3480 | sem->count = 0U; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3481 | } |
| 3482 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3483 | /** |
| 3484 | * @brief Get a semaphore's count. |
| 3485 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3486 | * This routine returns the current count of @a sem. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3487 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3488 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3489 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3490 | * @return Current semaphore count. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3491 | * @req K-SEM-001 |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3492 | */ |
Andrew Boie | 990bf16 | 2017-10-03 12:36:49 -0700 | [diff] [blame] | 3493 | __syscall unsigned int k_sem_count_get(struct k_sem *sem); |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 3494 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 3495 | /** |
| 3496 | * @internal |
| 3497 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3498 | 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] | 3499 | { |
| 3500 | return sem->count; |
| 3501 | } |
| 3502 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3503 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3504 | * @brief Statically define and initialize a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3505 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3506 | * 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] | 3507 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 3508 | * @code extern struct k_sem <name>; @endcode |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3509 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3510 | * @param name Name of the semaphore. |
| 3511 | * @param initial_count Initial semaphore count. |
| 3512 | * @param count_limit Maximum permitted semaphore count. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3513 | * @req K-SEM-002 |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3514 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3515 | #define K_SEM_DEFINE(name, initial_count, count_limit) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 3516 | Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \ |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 3517 | Z_SEM_INITIALIZER(name, initial_count, count_limit); \ |
Rajavardhan Gundi | 68040c8 | 2018-04-27 10:15:15 +0530 | [diff] [blame] | 3518 | BUILD_ASSERT(((count_limit) != 0) && \ |
| 3519 | ((initial_count) <= (count_limit))); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3520 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 3521 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3522 | |
| 3523 | /** |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3524 | * @defgroup msgq_apis Message Queue APIs |
| 3525 | * @ingroup kernel_apis |
| 3526 | * @{ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3527 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3528 | |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3529 | /** |
| 3530 | * @brief Message Queue Structure |
| 3531 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3532 | struct k_msgq { |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3533 | /** Message queue wait queue */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3534 | _wait_q_t wait_q; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3535 | /** Lock */ |
Andy Ross | be03dbd | 2018-07-26 10:23:02 -0700 | [diff] [blame] | 3536 | struct k_spinlock lock; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3537 | /** Message size */ |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 3538 | size_t msg_size; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3539 | /** Maximal number of messages */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3540 | u32_t max_msgs; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3541 | /** Start of message buffer */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3542 | char *buffer_start; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3543 | /** End of message buffer */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3544 | char *buffer_end; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3545 | /** Read pointer */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3546 | char *read_ptr; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3547 | /** Write pointer */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3548 | char *write_ptr; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3549 | /** Number of used messages */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3550 | u32_t used_msgs; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3551 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 3552 | _OBJECT_TRACING_NEXT_PTR(k_msgq) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 3553 | _OBJECT_TRACING_LINKED_FLAG |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3554 | |
| 3555 | /** Message queue */ |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 3556 | u8_t flags; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3557 | }; |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3558 | /** |
| 3559 | * @cond INTERNAL_HIDDEN |
| 3560 | */ |
| 3561 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3562 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3563 | #define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3564 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3565 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3566 | .msg_size = q_msg_size, \ |
Charles E. Youse | 6d01f67 | 2019-03-18 10:27:34 -0700 | [diff] [blame] | 3567 | .max_msgs = q_max_msgs, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3568 | .buffer_start = q_buffer, \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3569 | .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3570 | .read_ptr = q_buffer, \ |
| 3571 | .write_ptr = q_buffer, \ |
| 3572 | .used_msgs = 0, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 3573 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3574 | } |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 3575 | #define K_MSGQ_INITIALIZER __DEPRECATED_MACRO _K_MSGQ_INITIALIZER |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3576 | /** |
| 3577 | * INTERNAL_HIDDEN @endcond |
| 3578 | */ |
| 3579 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3580 | |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 3581 | #define K_MSGQ_FLAG_ALLOC BIT(0) |
| 3582 | |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3583 | /** |
| 3584 | * @brief Message Queue Attributes |
| 3585 | */ |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 3586 | struct k_msgq_attrs { |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3587 | /** Message Size */ |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 3588 | size_t msg_size; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3589 | /** Maximal number of messages */ |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 3590 | u32_t max_msgs; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3591 | /** Used messages */ |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 3592 | u32_t used_msgs; |
| 3593 | }; |
| 3594 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3595 | |
| 3596 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3597 | * @brief Statically define and initialize a message queue. |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3598 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3599 | * The message queue's ring buffer contains space for @a q_max_msgs messages, |
| 3600 | * 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] | 3601 | * @a q_align -byte boundary, which must be a power of 2. To ensure that each |
| 3602 | * message is similarly aligned to this boundary, @a q_msg_size must also be |
| 3603 | * a multiple of @a q_align. |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3604 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3605 | * The message queue can be accessed outside the module where it is defined |
| 3606 | * using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3607 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 3608 | * @code extern struct k_msgq <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3609 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3610 | * @param q_name Name of the message queue. |
| 3611 | * @param q_msg_size Message size (in bytes). |
| 3612 | * @param q_max_msgs Maximum number of messages that can be queued. |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 3613 | * @param q_align Alignment of the message queue's ring buffer. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3614 | * |
| 3615 | * @req K-MSGQ-001 |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3616 | */ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 3617 | #define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \ |
| 3618 | static char __noinit __aligned(q_align) \ |
| 3619 | _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \ |
| 3620 | Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \ |
| 3621 | _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3622 | q_msg_size, q_max_msgs) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3623 | |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3624 | /** |
| 3625 | * @brief Initialize a message queue. |
| 3626 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3627 | * This routine initializes a message queue object, prior to its first use. |
| 3628 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 3629 | * The message queue's ring buffer must contain space for @a max_msgs messages, |
| 3630 | * each of which is @a msg_size bytes long. The buffer must be aligned to an |
| 3631 | * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure |
| 3632 | * that each message is similarly aligned to this boundary, @a q_msg_size |
| 3633 | * must also be a multiple of N. |
| 3634 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3635 | * @param q Address of the message queue. |
| 3636 | * @param buffer Pointer to ring buffer that holds queued messages. |
| 3637 | * @param msg_size Message size (in bytes). |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3638 | * @param max_msgs Maximum number of messages that can be queued. |
| 3639 | * |
| 3640 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3641 | * @req K-MSGQ-002 |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3642 | */ |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 3643 | void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size, |
| 3644 | u32_t max_msgs); |
| 3645 | |
| 3646 | /** |
| 3647 | * @brief Initialize a message queue. |
| 3648 | * |
| 3649 | * This routine initializes a message queue object, prior to its first use, |
| 3650 | * allocating its internal ring buffer from the calling thread's resource |
| 3651 | * pool. |
| 3652 | * |
| 3653 | * Memory allocated for the ring buffer can be released by calling |
| 3654 | * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses |
| 3655 | * all of its references. |
| 3656 | * |
| 3657 | * @param q Address of the message queue. |
| 3658 | * @param msg_size Message size (in bytes). |
| 3659 | * @param max_msgs Maximum number of messages that can be queued. |
| 3660 | * |
| 3661 | * @return 0 on success, -ENOMEM if there was insufficient memory in the |
| 3662 | * thread's resource pool, or -EINVAL if the size parameters cause |
| 3663 | * an integer overflow. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3664 | * @req K-MSGQ-002 |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 3665 | */ |
| 3666 | __syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size, |
| 3667 | u32_t max_msgs); |
| 3668 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3669 | /** |
| 3670 | * @brief Cleanup message queue |
| 3671 | * |
| 3672 | * Releases memory allocated for the ring buffer. |
| 3673 | * @param q |
| 3674 | */ |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 3675 | void k_msgq_cleanup(struct k_msgq *q); |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3676 | |
| 3677 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3678 | * @brief Send a message to a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3679 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3680 | * This routine sends a message to message queue @a q. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3681 | * |
Benjamin Walsh | 8215ce1 | 2016-11-09 19:45:19 -0500 | [diff] [blame] | 3682 | * @note Can be called by ISRs. |
| 3683 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3684 | * @param q Address of the message queue. |
| 3685 | * @param data Pointer to the message. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3686 | * @param timeout Non-negative waiting period to add the message (in |
| 3687 | * milliseconds), or one of the special values K_NO_WAIT and |
| 3688 | * K_FOREVER. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3689 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3690 | * @retval 0 Message sent. |
| 3691 | * @retval -ENOMSG Returned without waiting or queue purged. |
| 3692 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3693 | * @req K-MSGQ-002 |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3694 | */ |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 3695 | __syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout); |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3696 | |
| 3697 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3698 | * @brief Receive a message from a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3699 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3700 | * This routine receives a message from message queue @a q in a "first in, |
| 3701 | * first out" manner. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3702 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3703 | * @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] | 3704 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3705 | * @param q Address of the message queue. |
| 3706 | * @param data Address of area to hold the received message. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3707 | * @param timeout Non-negative waiting period to receive the message (in |
| 3708 | * milliseconds), or one of the special values K_NO_WAIT and |
| 3709 | * K_FOREVER. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3710 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3711 | * @retval 0 Message received. |
| 3712 | * @retval -ENOMSG Returned without waiting. |
| 3713 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3714 | * @req K-MSGQ-002 |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3715 | */ |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 3716 | __syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout); |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3717 | |
| 3718 | /** |
Sathish Kuttan | 3efd8e1 | 2018-11-09 21:03:10 -0800 | [diff] [blame] | 3719 | * @brief Peek/read a message from a message queue. |
| 3720 | * |
| 3721 | * This routine reads a message from message queue @a q in a "first in, |
| 3722 | * first out" manner and leaves the message in the queue. |
| 3723 | * |
| 3724 | * @note Can be called by ISRs. |
| 3725 | * |
| 3726 | * @param q Address of the message queue. |
| 3727 | * @param data Address of area to hold the message read from the queue. |
| 3728 | * |
| 3729 | * @retval 0 Message read. |
| 3730 | * @retval -ENOMSG Returned when the queue has no message. |
| 3731 | * @req K-MSGQ-002 |
| 3732 | */ |
| 3733 | __syscall int k_msgq_peek(struct k_msgq *q, void *data); |
| 3734 | |
| 3735 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3736 | * @brief Purge a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3737 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3738 | * This routine discards all unreceived messages in a message queue's ring |
| 3739 | * buffer. Any threads that are blocked waiting to send a message to the |
| 3740 | * message queue are unblocked and see an -ENOMSG error code. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3741 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3742 | * @param q Address of the message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3743 | * |
| 3744 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3745 | * @req K-MSGQ-002 |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3746 | */ |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 3747 | __syscall void k_msgq_purge(struct k_msgq *q); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3748 | |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 3749 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3750 | * @brief Get the amount of free space in a message queue. |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 3751 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3752 | * This routine returns the number of unused entries in a message queue's |
| 3753 | * ring buffer. |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 3754 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3755 | * @param q Address of the message queue. |
| 3756 | * |
| 3757 | * @return Number of unused ring buffer entries. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3758 | * @req K-MSGQ-002 |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 3759 | */ |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 3760 | __syscall u32_t k_msgq_num_free_get(struct k_msgq *q); |
| 3761 | |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 3762 | /** |
| 3763 | * @brief Get basic attributes of a message queue. |
| 3764 | * |
| 3765 | * This routine fetches basic attributes of message queue into attr argument. |
| 3766 | * |
| 3767 | * @param q Address of the message queue. |
| 3768 | * @param attrs pointer to message queue attribute structure. |
| 3769 | * |
| 3770 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3771 | * @req K-MSGQ-003 |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 3772 | */ |
| 3773 | __syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs); |
| 3774 | |
| 3775 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3776 | static inline u32_t z_impl_k_msgq_num_free_get(struct k_msgq *q) |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 3777 | { |
| 3778 | return q->max_msgs - q->used_msgs; |
| 3779 | } |
| 3780 | |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3781 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3782 | * @brief Get the number of messages in a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3783 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3784 | * 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] | 3785 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3786 | * @param q Address of the message queue. |
| 3787 | * |
| 3788 | * @return Number of messages. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3789 | * @req K-MSGQ-002 |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3790 | */ |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 3791 | __syscall u32_t k_msgq_num_used_get(struct k_msgq *q); |
| 3792 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3793 | static inline u32_t z_impl_k_msgq_num_used_get(struct k_msgq *q) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3794 | { |
| 3795 | return q->used_msgs; |
| 3796 | } |
| 3797 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 3798 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3799 | |
| 3800 | /** |
| 3801 | * @defgroup mem_pool_apis Memory Pool APIs |
| 3802 | * @ingroup kernel_apis |
| 3803 | * @{ |
| 3804 | */ |
| 3805 | |
Andy Ross | 73cb958 | 2017-05-09 10:42:39 -0700 | [diff] [blame] | 3806 | /* Note on sizing: the use of a 20 bit field for block means that, |
| 3807 | * assuming a reasonable minimum block size of 16 bytes, we're limited |
| 3808 | * to 16M of memory managed by a single pool. Long term it would be |
| 3809 | * good to move to a variable bit size based on configuration. |
| 3810 | */ |
| 3811 | struct k_mem_block_id { |
| 3812 | u32_t pool : 8; |
| 3813 | u32_t level : 4; |
| 3814 | u32_t block : 20; |
| 3815 | }; |
| 3816 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3817 | struct k_mem_block { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3818 | void *data; |
Andy Ross | 73cb958 | 2017-05-09 10:42:39 -0700 | [diff] [blame] | 3819 | struct k_mem_block_id id; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3820 | }; |
| 3821 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 3822 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3823 | |
| 3824 | /** |
| 3825 | * @defgroup mailbox_apis Mailbox APIs |
| 3826 | * @ingroup kernel_apis |
| 3827 | * @{ |
| 3828 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3829 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3830 | /** |
| 3831 | * @brief Mailbox Message Structure |
| 3832 | * |
| 3833 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3834 | struct k_mbox_msg { |
| 3835 | /** internal use only - needed for legacy API support */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3836 | u32_t _mailbox; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3837 | /** size of message (in bytes) */ |
Peter Mitsis | d93078c | 2016-10-14 12:59:37 -0400 | [diff] [blame] | 3838 | size_t size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3839 | /** application-defined information value */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3840 | u32_t info; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3841 | /** sender's message data buffer */ |
| 3842 | void *tx_data; |
| 3843 | /** internal use only - needed for legacy API support */ |
| 3844 | void *_rx_data; |
| 3845 | /** message data block descriptor */ |
| 3846 | struct k_mem_block tx_block; |
| 3847 | /** source thread id */ |
| 3848 | k_tid_t rx_source_thread; |
| 3849 | /** target thread id */ |
| 3850 | k_tid_t tx_target_thread; |
| 3851 | /** internal use only - thread waiting on send (may be a dummy) */ |
| 3852 | k_tid_t _syncing_thread; |
| 3853 | #if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0) |
| 3854 | /** internal use only - semaphore used during asynchronous send */ |
| 3855 | struct k_sem *_async_sem; |
| 3856 | #endif |
| 3857 | }; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3858 | /** |
| 3859 | * @brief Mailbox Structure |
| 3860 | * |
| 3861 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3862 | struct k_mbox { |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3863 | /** Transmit messages queue */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3864 | _wait_q_t tx_msg_queue; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 3865 | /** Receive message queue */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3866 | _wait_q_t rx_msg_queue; |
Andy Ross | 9eeb6b8 | 2018-07-25 15:06:24 -0700 | [diff] [blame] | 3867 | struct k_spinlock lock; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3868 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 3869 | _OBJECT_TRACING_NEXT_PTR(k_mbox) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 3870 | _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3871 | }; |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3872 | /** |
| 3873 | * @cond INTERNAL_HIDDEN |
| 3874 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3875 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3876 | #define _K_MBOX_INITIALIZER(obj) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3877 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3878 | .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \ |
| 3879 | .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 3880 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3881 | } |
| 3882 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 3883 | #define K_MBOX_INITIALIZER __DEPRECATED_MACRO _K_MBOX_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3884 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3885 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3886 | * INTERNAL_HIDDEN @endcond |
| 3887 | */ |
| 3888 | |
| 3889 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3890 | * @brief Statically define and initialize a mailbox. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3891 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3892 | * 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] | 3893 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 3894 | * @code extern struct k_mbox <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3895 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3896 | * @param name Name of the mailbox. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3897 | * @req K-MBOX-001 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3898 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3899 | #define K_MBOX_DEFINE(name) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 3900 | Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3901 | _K_MBOX_INITIALIZER(name) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3902 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3903 | /** |
| 3904 | * @brief Initialize a mailbox. |
| 3905 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3906 | * This routine initializes a mailbox object, prior to its first use. |
| 3907 | * |
| 3908 | * @param mbox Address of the mailbox. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3909 | * |
| 3910 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3911 | * @req K-MBOX-002 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3912 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3913 | extern void k_mbox_init(struct k_mbox *mbox); |
| 3914 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3915 | /** |
| 3916 | * @brief Send a mailbox message in a synchronous manner. |
| 3917 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3918 | * This routine sends a message to @a mbox and waits for a receiver to both |
| 3919 | * receive and process it. The message data may be in a buffer, in a memory |
| 3920 | * pool block, or non-existent (i.e. an empty message). |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3921 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3922 | * @param mbox Address of the mailbox. |
| 3923 | * @param tx_msg Address of the transmit message descriptor. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3924 | * @param timeout Non-negative waiting period for the message to be received (in |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3925 | * milliseconds), or one of the special values K_NO_WAIT |
| 3926 | * and K_FOREVER. Once the message has been received, |
| 3927 | * this routine waits as long as necessary for the message |
| 3928 | * to be completely processed. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3929 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3930 | * @retval 0 Message sent. |
| 3931 | * @retval -ENOMSG Returned without waiting. |
| 3932 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3933 | * @req K-MBOX-002 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3934 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 3935 | extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg, |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3936 | s32_t timeout); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3937 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3938 | /** |
| 3939 | * @brief Send a mailbox message in an asynchronous manner. |
| 3940 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3941 | * This routine sends a message to @a mbox without waiting for a receiver |
| 3942 | * to process it. The message data may be in a buffer, in a memory pool block, |
| 3943 | * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem |
| 3944 | * will be given when the message has been both received and completely |
| 3945 | * processed by the receiver. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3946 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3947 | * @param mbox Address of the mailbox. |
| 3948 | * @param tx_msg Address of the transmit message descriptor. |
| 3949 | * @param sem Address of a semaphore, or NULL if none is needed. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3950 | * |
| 3951 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3952 | * @req K-MBOX-002 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3953 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 3954 | 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] | 3955 | struct k_sem *sem); |
| 3956 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3957 | /** |
| 3958 | * @brief Receive a mailbox message. |
| 3959 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3960 | * This routine receives a message from @a mbox, then optionally retrieves |
| 3961 | * its data and disposes of the message. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3962 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3963 | * @param mbox Address of the mailbox. |
| 3964 | * @param rx_msg Address of the receive message descriptor. |
| 3965 | * @param buffer Address of the buffer to receive data, or NULL to defer data |
| 3966 | * retrieval and message disposal until later. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3967 | * @param timeout Non-negative waiting period for a message to be received (in |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3968 | * milliseconds), or one of the special values K_NO_WAIT |
| 3969 | * and K_FOREVER. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3970 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3971 | * @retval 0 Message received. |
| 3972 | * @retval -ENOMSG Returned without waiting. |
| 3973 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3974 | * @req K-MBOX-002 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3975 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 3976 | extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg, |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3977 | void *buffer, s32_t timeout); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3978 | |
| 3979 | /** |
| 3980 | * @brief Retrieve mailbox message data into a buffer. |
| 3981 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3982 | * This routine completes the processing of a received message by retrieving |
| 3983 | * its data into a buffer, then disposing of the message. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3984 | * |
| 3985 | * Alternatively, this routine can be used to dispose of a received message |
| 3986 | * without retrieving its data. |
| 3987 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3988 | * @param rx_msg Address of the receive message descriptor. |
| 3989 | * @param buffer Address of the buffer to receive data, or NULL to discard |
| 3990 | * the data. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3991 | * |
| 3992 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3993 | * @req K-MBOX-002 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3994 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 3995 | 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] | 3996 | |
| 3997 | /** |
| 3998 | * @brief Retrieve mailbox message data into a memory pool block. |
| 3999 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4000 | * This routine completes the processing of a received message by retrieving |
| 4001 | * its data into a memory pool block, then disposing of the message. |
| 4002 | * The memory pool block that results from successful retrieval must be |
| 4003 | * returned to the pool once the data has been processed, even in cases |
| 4004 | * where zero bytes of data are retrieved. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4005 | * |
| 4006 | * Alternatively, this routine can be used to dispose of a received message |
| 4007 | * without retrieving its data. In this case there is no need to return a |
| 4008 | * memory pool block to the pool. |
| 4009 | * |
| 4010 | * This routine allocates a new memory pool block for the data only if the |
| 4011 | * data is not already in one. If a new block cannot be allocated, the routine |
| 4012 | * returns a failure code and the received message is left unchanged. This |
| 4013 | * permits the caller to reattempt data retrieval at a later time or to dispose |
| 4014 | * of the received message without retrieving its data. |
| 4015 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4016 | * @param rx_msg Address of a receive message descriptor. |
| 4017 | * @param pool Address of memory pool, or NULL to discard data. |
| 4018 | * @param block Address of the area to hold memory pool block info. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 4019 | * @param timeout Non-negative waiting period to wait for a memory pool block |
| 4020 | * (in milliseconds), or one of the special values K_NO_WAIT |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4021 | * and K_FOREVER. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4022 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4023 | * @retval 0 Data retrieved. |
| 4024 | * @retval -ENOMEM Returned without waiting. |
| 4025 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4026 | * @req K-MBOX-002 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 4027 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 4028 | 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] | 4029 | struct k_mem_pool *pool, |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4030 | struct k_mem_block *block, s32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4031 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4032 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4033 | |
| 4034 | /** |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 4035 | * @defgroup pipe_apis Pipe APIs |
| 4036 | * @ingroup kernel_apis |
| 4037 | * @{ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4038 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4039 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 4040 | /** Pipe Structure */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4041 | struct k_pipe { |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 4042 | unsigned char *buffer; /**< Pipe buffer: may be NULL */ |
| 4043 | size_t size; /**< Buffer size */ |
| 4044 | size_t bytes_used; /**< # bytes used in buffer */ |
| 4045 | size_t read_index; /**< Where in buffer to read from */ |
| 4046 | size_t write_index; /**< Where in buffer to write */ |
Andy Ross | f582b55 | 2019-02-05 16:10:18 -0800 | [diff] [blame] | 4047 | struct k_spinlock lock; /**< Synchronization lock */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4048 | |
| 4049 | struct { |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 4050 | _wait_q_t readers; /**< Reader wait queue */ |
| 4051 | _wait_q_t writers; /**< Writer wait queue */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4052 | } wait_q; |
| 4053 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 4054 | _OBJECT_TRACING_NEXT_PTR(k_pipe) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 4055 | _OBJECT_TRACING_LINKED_FLAG |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 4056 | u8_t flags; /**< Flags */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4057 | }; |
| 4058 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 4059 | /** |
| 4060 | * @cond INTERNAL_HIDDEN |
| 4061 | */ |
| 4062 | #define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */ |
| 4063 | |
Krzysztof Chruscinski | be06327 | 2019-02-13 11:19:54 +0100 | [diff] [blame] | 4064 | #define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \ |
| 4065 | { \ |
| 4066 | .buffer = pipe_buffer, \ |
| 4067 | .size = pipe_buffer_size, \ |
| 4068 | .bytes_used = 0, \ |
| 4069 | .read_index = 0, \ |
| 4070 | .write_index = 0, \ |
| 4071 | .lock = {}, \ |
| 4072 | .wait_q = { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4073 | .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \ |
| 4074 | .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \ |
Krzysztof Chruscinski | be06327 | 2019-02-13 11:19:54 +0100 | [diff] [blame] | 4075 | }, \ |
| 4076 | _OBJECT_TRACING_INIT \ |
| 4077 | .flags = 0 \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4078 | } |
| 4079 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 4080 | #define K_PIPE_INITIALIZER __DEPRECATED_MACRO _K_PIPE_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 4081 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4082 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4083 | * INTERNAL_HIDDEN @endcond |
| 4084 | */ |
| 4085 | |
| 4086 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4087 | * @brief Statically define and initialize a pipe. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4088 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4089 | * 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] | 4090 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 4091 | * @code extern struct k_pipe <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4092 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4093 | * @param name Name of the pipe. |
| 4094 | * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes), |
| 4095 | * or zero if no ring buffer is used. |
| 4096 | * @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] | 4097 | * |
| 4098 | * @req K-PIPE-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4099 | */ |
Andrew Boie | 44fe812 | 2018-04-12 17:38:12 -0700 | [diff] [blame] | 4100 | #define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \ |
Andrew Boie | 41f6011 | 2019-01-31 15:53:24 -0800 | [diff] [blame] | 4101 | static unsigned char __noinit __aligned(pipe_align) \ |
Andrew Boie | 44fe812 | 2018-04-12 17:38:12 -0700 | [diff] [blame] | 4102 | _k_pipe_buf_##name[pipe_buffer_size]; \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 4103 | Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 4104 | _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4105 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4106 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4107 | * @brief Initialize a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4108 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4109 | * This routine initializes a pipe object, prior to its first use. |
| 4110 | * |
| 4111 | * @param pipe Address of the pipe. |
| 4112 | * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer |
| 4113 | * is used. |
| 4114 | * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring |
| 4115 | * buffer is used. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4116 | * |
| 4117 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4118 | * @req K-PIPE-002 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4119 | */ |
Andrew Boie | 44fe812 | 2018-04-12 17:38:12 -0700 | [diff] [blame] | 4120 | void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size); |
| 4121 | |
| 4122 | /** |
| 4123 | * @brief Release a pipe's allocated buffer |
| 4124 | * |
| 4125 | * If a pipe object was given a dynamically allocated buffer via |
| 4126 | * k_pipe_alloc_init(), this will free it. This function does nothing |
| 4127 | * if the buffer wasn't dynamically allocated. |
| 4128 | * |
| 4129 | * @param pipe Address of the pipe. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4130 | * @req K-PIPE-002 |
Andrew Boie | 44fe812 | 2018-04-12 17:38:12 -0700 | [diff] [blame] | 4131 | */ |
| 4132 | void k_pipe_cleanup(struct k_pipe *pipe); |
| 4133 | |
| 4134 | /** |
| 4135 | * @brief Initialize a pipe and allocate a buffer for it |
| 4136 | * |
| 4137 | * Storage for the buffer region will be allocated from the calling thread's |
| 4138 | * resource pool. This memory will be released if k_pipe_cleanup() is called, |
| 4139 | * or userspace is enabled and the pipe object loses all references to it. |
| 4140 | * |
| 4141 | * This function should only be called on uninitialized pipe objects. |
| 4142 | * |
| 4143 | * @param pipe Address of the pipe. |
| 4144 | * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring |
| 4145 | * buffer is used. |
| 4146 | * @retval 0 on success |
David B. Kinder | fcbd8fb | 2018-05-23 12:06:24 -0700 | [diff] [blame] | 4147 | * @retval -ENOMEM if memory couldn't be allocated |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4148 | * @req K-PIPE-002 |
Andrew Boie | 44fe812 | 2018-04-12 17:38:12 -0700 | [diff] [blame] | 4149 | */ |
| 4150 | __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] | 4151 | |
| 4152 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4153 | * @brief Write data to a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4154 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4155 | * 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] | 4156 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4157 | * @param pipe Address of the pipe. |
| 4158 | * @param data Address of data to write. |
| 4159 | * @param bytes_to_write Size of data (in bytes). |
| 4160 | * @param bytes_written Address of area to hold the number of bytes written. |
| 4161 | * @param min_xfer Minimum number of bytes to write. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 4162 | * @param timeout Non-negative waiting period to wait for the data to be written |
| 4163 | * (in milliseconds), or one of the special values K_NO_WAIT |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4164 | * and K_FOREVER. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4165 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4166 | * @retval 0 At least @a min_xfer bytes of data were written. |
| 4167 | * @retval -EIO Returned without waiting; zero data bytes were written. |
| 4168 | * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4169 | * minus one data bytes were written. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4170 | * @req K-PIPE-002 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4171 | */ |
Andrew Boie | b9a0578 | 2017-09-29 16:05:32 -0700 | [diff] [blame] | 4172 | __syscall int k_pipe_put(struct k_pipe *pipe, void *data, |
| 4173 | size_t bytes_to_write, size_t *bytes_written, |
| 4174 | size_t min_xfer, s32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4175 | |
| 4176 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4177 | * @brief Read data from a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4178 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4179 | * 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] | 4180 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4181 | * @param pipe Address of the pipe. |
| 4182 | * @param data Address to place the data read from pipe. |
| 4183 | * @param bytes_to_read Maximum number of data bytes to read. |
| 4184 | * @param bytes_read Address of area to hold the number of bytes read. |
| 4185 | * @param min_xfer Minimum number of data bytes to read. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 4186 | * @param timeout Non-negative waiting period to wait for the data to be read |
| 4187 | * (in milliseconds), or one of the special values K_NO_WAIT |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4188 | * and K_FOREVER. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4189 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4190 | * @retval 0 At least @a min_xfer bytes of data were read. |
| 4191 | * @retval -EIO Returned without waiting; zero data bytes were read. |
| 4192 | * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4193 | * minus one data bytes were read. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4194 | * @req K-PIPE-002 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4195 | */ |
Andrew Boie | b9a0578 | 2017-09-29 16:05:32 -0700 | [diff] [blame] | 4196 | __syscall int k_pipe_get(struct k_pipe *pipe, void *data, |
| 4197 | size_t bytes_to_read, size_t *bytes_read, |
| 4198 | size_t min_xfer, s32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4199 | |
| 4200 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4201 | * @brief Write memory block to a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4202 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4203 | * This routine writes the data contained in a memory block to @a pipe. |
| 4204 | * 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] | 4205 | * 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] | 4206 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4207 | * @param pipe Address of the pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4208 | * @param block Memory block containing data to send |
| 4209 | * @param size Number of data bytes in memory block to send |
| 4210 | * @param sem Semaphore to signal upon completion (else NULL) |
| 4211 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4212 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4213 | * @req K-PIPE-002 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4214 | */ |
| 4215 | extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block, |
| 4216 | size_t size, struct k_sem *sem); |
| 4217 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4218 | /** @} */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4219 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4220 | /** |
| 4221 | * @cond INTERNAL_HIDDEN |
| 4222 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4223 | |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4224 | struct k_mem_slab { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4225 | _wait_q_t wait_q; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4226 | u32_t num_blocks; |
Peter Mitsis | fb02d57 | 2016-10-13 16:55:45 -0400 | [diff] [blame] | 4227 | size_t block_size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4228 | char *buffer; |
| 4229 | char *free_list; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4230 | u32_t num_used; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4231 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 4232 | _OBJECT_TRACING_NEXT_PTR(k_mem_slab) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 4233 | _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4234 | }; |
| 4235 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 4236 | #define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4237 | slab_num_blocks) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4238 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4239 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4240 | .num_blocks = slab_num_blocks, \ |
| 4241 | .block_size = slab_block_size, \ |
| 4242 | .buffer = slab_buffer, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4243 | .free_list = NULL, \ |
| 4244 | .num_used = 0, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 4245 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4246 | } |
| 4247 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 4248 | #define K_MEM_SLAB_INITIALIZER __DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 4249 | |
| 4250 | |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 4251 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4252 | * INTERNAL_HIDDEN @endcond |
| 4253 | */ |
| 4254 | |
| 4255 | /** |
| 4256 | * @defgroup mem_slab_apis Memory Slab APIs |
| 4257 | * @ingroup kernel_apis |
| 4258 | * @{ |
| 4259 | */ |
| 4260 | |
| 4261 | /** |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 4262 | * @brief Statically define and initialize a memory slab. |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 4263 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 4264 | * The memory slab's buffer contains @a slab_num_blocks memory blocks |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4265 | * 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] | 4266 | * @a slab_align -byte boundary. To ensure that each memory block is similarly |
| 4267 | * 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] | 4268 | * @a slab_align. |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 4269 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 4270 | * 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] | 4271 | * using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4272 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 4273 | * @code extern struct k_mem_slab <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4274 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4275 | * @param name Name of the memory slab. |
| 4276 | * @param slab_block_size Size of each memory block (in bytes). |
| 4277 | * @param slab_num_blocks Number memory blocks. |
| 4278 | * @param slab_align Alignment of the memory slab's buffer (power of 2). |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4279 | * @req K-MSLAB-001 |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 4280 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4281 | #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] | 4282 | char __noinit __aligned(WB_UP(slab_align)) \ |
| 4283 | _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] | 4284 | Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 4285 | _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \ |
Nicolas Pitre | 46cd5a0 | 2019-05-21 21:40:38 -0400 | [diff] [blame] | 4286 | WB_UP(slab_block_size), slab_num_blocks) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4287 | |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4288 | /** |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4289 | * @brief Initialize a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4290 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4291 | * Initializes a memory slab, prior to its first use. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4292 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 4293 | * The memory slab's buffer contains @a slab_num_blocks memory blocks |
| 4294 | * 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] | 4295 | * N-byte boundary matching a word boundary, where N is a power of 2 |
| 4296 | * (i.e. 4 on 32-bit systems, 8, 16, ...). |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 4297 | * To ensure that each memory block is similarly aligned to this boundary, |
| 4298 | * @a slab_block_size must also be a multiple of N. |
| 4299 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4300 | * @param slab Address of the memory slab. |
| 4301 | * @param buffer Pointer to buffer used for the memory blocks. |
| 4302 | * @param block_size Size of each memory block (in bytes). |
| 4303 | * @param num_blocks Number of memory blocks. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4304 | * |
| 4305 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4306 | * @req K-MSLAB-002 |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4307 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4308 | extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer, |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4309 | size_t block_size, u32_t num_blocks); |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4310 | |
| 4311 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4312 | * @brief Allocate memory from a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4313 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4314 | * This routine allocates a memory block from a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4315 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4316 | * @param slab Address of the memory slab. |
| 4317 | * @param mem Pointer to block address area. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 4318 | * @param timeout Non-negative waiting period to wait for operation to complete |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4319 | * (in milliseconds). Use K_NO_WAIT to return without waiting, |
| 4320 | * or K_FOREVER to wait as long as necessary. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4321 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4322 | * @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] | 4323 | * is set to the starting address of the memory block. |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4324 | * @retval -ENOMEM Returned without waiting. |
| 4325 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4326 | * @req K-MSLAB-002 |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4327 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4328 | extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem, |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4329 | s32_t timeout); |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4330 | |
| 4331 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4332 | * @brief Free memory allocated from a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4333 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4334 | * This routine releases a previously allocated memory block back to its |
| 4335 | * associated memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4336 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4337 | * @param slab Address of the memory slab. |
| 4338 | * @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] | 4339 | * |
| 4340 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4341 | * @req K-MSLAB-002 |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4342 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4343 | 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] | 4344 | |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4345 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4346 | * @brief Get the number of used blocks in a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4347 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4348 | * This routine gets the number of memory blocks that are currently |
| 4349 | * allocated in @a slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4350 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4351 | * @param slab Address of the memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4352 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4353 | * @return Number of allocated memory blocks. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4354 | * @req K-MSLAB-002 |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4355 | */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4356 | 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] | 4357 | { |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4358 | return slab->num_used; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4359 | } |
| 4360 | |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4361 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4362 | * @brief Get the number of unused blocks in a memory slab. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4363 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4364 | * This routine gets the number of memory blocks that are currently |
| 4365 | * unallocated in @a slab. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4366 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4367 | * @param slab Address of the memory slab. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4368 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4369 | * @return Number of unallocated memory blocks. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4370 | * @req K-MSLAB-002 |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4371 | */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4372 | 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] | 4373 | { |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4374 | return slab->num_blocks - slab->num_used; |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4375 | } |
| 4376 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4377 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4378 | |
| 4379 | /** |
| 4380 | * @cond INTERNAL_HIDDEN |
| 4381 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4382 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 4383 | struct k_mem_pool { |
Andrew Boie | aa6de29 | 2018-03-06 17:12:37 -0800 | [diff] [blame] | 4384 | struct sys_mem_pool_base base; |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 4385 | _wait_q_t wait_q; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4386 | }; |
| 4387 | |
Dmitriy Korovkin | 0741467 | 2016-11-03 13:35:42 -0400 | [diff] [blame] | 4388 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4389 | * INTERNAL_HIDDEN @endcond |
Dmitriy Korovkin | 0741467 | 2016-11-03 13:35:42 -0400 | [diff] [blame] | 4390 | */ |
| 4391 | |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 4392 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4393 | * @addtogroup mem_pool_apis |
| 4394 | * @{ |
| 4395 | */ |
| 4396 | |
| 4397 | /** |
| 4398 | * @brief Statically define and initialize a memory pool. |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 4399 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4400 | * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes |
| 4401 | * long. The memory pool allows blocks to be repeatedly partitioned into |
| 4402 | * 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] | 4403 | * to a @a align -byte boundary. |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 4404 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4405 | * If the pool is to be accessed outside the module where it is defined, it |
| 4406 | * can be declared via |
| 4407 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 4408 | * @code extern struct k_mem_pool <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4409 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4410 | * @param name Name of the memory pool. |
Andy Ross | 73cb958 | 2017-05-09 10:42:39 -0700 | [diff] [blame] | 4411 | * @param minsz Size of the smallest blocks in the pool (in bytes). |
| 4412 | * @param maxsz Size of the largest blocks in the pool (in bytes). |
| 4413 | * @param nmax Number of maximum sized blocks in the pool. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4414 | * @param align Alignment of the pool's buffer (power of 2). |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4415 | * @req K-MPOOL-001 |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 4416 | */ |
Andy Ross | 73cb958 | 2017-05-09 10:42:39 -0700 | [diff] [blame] | 4417 | #define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \ |
Nicolas Pitre | cf97437 | 2019-06-26 11:32:58 -0400 | [diff] [blame] | 4418 | char __aligned(WB_UP(align)) _mpool_buf_##name[WB_UP(maxsz) * nmax \ |
Andy Ross | 73cb958 | 2017-05-09 10:42:39 -0700 | [diff] [blame] | 4419 | + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4420 | struct sys_mem_pool_lvl _mpool_lvls_##name[Z_MPOOL_LVLS(maxsz, minsz)]; \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 4421 | Z_STRUCT_SECTION_ITERABLE(k_mem_pool, name) = { \ |
Andrew Boie | aa6de29 | 2018-03-06 17:12:37 -0800 | [diff] [blame] | 4422 | .base = { \ |
| 4423 | .buf = _mpool_buf_##name, \ |
Nicolas Pitre | cf97437 | 2019-06-26 11:32:58 -0400 | [diff] [blame] | 4424 | .max_sz = WB_UP(maxsz), \ |
Andrew Boie | aa6de29 | 2018-03-06 17:12:37 -0800 | [diff] [blame] | 4425 | .n_max = nmax, \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4426 | .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \ |
Andrew Boie | aa6de29 | 2018-03-06 17:12:37 -0800 | [diff] [blame] | 4427 | .levels = _mpool_lvls_##name, \ |
| 4428 | .flags = SYS_MEM_POOL_KERNEL \ |
| 4429 | } \ |
Johann Fischer | 223a2b9 | 2019-07-04 15:55:20 +0200 | [diff] [blame] | 4430 | }; \ |
Nicolas Pitre | b2a022b | 2019-09-26 16:36:40 -0400 | [diff] [blame] | 4431 | BUILD_ASSERT(WB_UP(maxsz) >= _MPOOL_MINBLK) |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 4432 | |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4433 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4434 | * @brief Allocate memory from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4435 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4436 | * This routine allocates a memory block from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4437 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4438 | * @param pool Address of the memory pool. |
| 4439 | * @param block Pointer to block descriptor for the allocated memory. |
| 4440 | * @param size Amount of memory to allocate (in bytes). |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 4441 | * @param timeout Non-negative waiting period to wait for operation to complete |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4442 | * (in milliseconds). Use K_NO_WAIT to return without waiting, |
| 4443 | * or K_FOREVER to wait as long as necessary. |
| 4444 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4445 | * @retval 0 Memory allocated. The @a data field of the block descriptor |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4446 | * is set to the starting address of the memory block. |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4447 | * @retval -ENOMEM Returned without waiting. |
| 4448 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4449 | * @req K-MPOOL-002 |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4450 | */ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 4451 | extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block, |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4452 | size_t size, s32_t timeout); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4453 | |
| 4454 | /** |
Andrew Boie | a2480bd | 2018-04-12 16:59:02 -0700 | [diff] [blame] | 4455 | * @brief Allocate memory from a memory pool with malloc() semantics |
| 4456 | * |
| 4457 | * Such memory must be released using k_free(). |
| 4458 | * |
| 4459 | * @param pool Address of the memory pool. |
| 4460 | * @param size Amount of memory to allocate (in bytes). |
| 4461 | * @return Address of the allocated memory if successful, otherwise NULL |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4462 | * @req K-MPOOL-002 |
Andrew Boie | a2480bd | 2018-04-12 16:59:02 -0700 | [diff] [blame] | 4463 | */ |
| 4464 | extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size); |
| 4465 | |
| 4466 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4467 | * @brief Free memory allocated from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4468 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4469 | * This routine releases a previously allocated memory block back to its |
| 4470 | * memory pool. |
| 4471 | * |
| 4472 | * @param block Pointer to block descriptor for the allocated memory. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4473 | * |
| 4474 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4475 | * @req K-MPOOL-002 |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4476 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4477 | extern void k_mem_pool_free(struct k_mem_block *block); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4478 | |
| 4479 | /** |
Johan Hedberg | 7d887cb | 2018-01-11 20:45:27 +0200 | [diff] [blame] | 4480 | * @brief Free memory allocated from a memory pool. |
| 4481 | * |
| 4482 | * This routine releases a previously allocated memory block back to its |
| 4483 | * memory pool |
| 4484 | * |
| 4485 | * @param id Memory block identifier. |
| 4486 | * |
| 4487 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4488 | * @req K-MPOOL-002 |
Johan Hedberg | 7d887cb | 2018-01-11 20:45:27 +0200 | [diff] [blame] | 4489 | */ |
| 4490 | extern void k_mem_pool_free_id(struct k_mem_block_id *id); |
| 4491 | |
| 4492 | /** |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4493 | * @} |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4494 | */ |
| 4495 | |
| 4496 | /** |
| 4497 | * @defgroup heap_apis Heap Memory Pool APIs |
| 4498 | * @ingroup kernel_apis |
| 4499 | * @{ |
| 4500 | */ |
| 4501 | |
| 4502 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4503 | * @brief Allocate memory from heap. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4504 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4505 | * This routine provides traditional malloc() semantics. Memory is |
Allan Stephens | 480a131 | 2016-10-13 15:44:48 -0500 | [diff] [blame] | 4506 | * allocated from the heap memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4507 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4508 | * @param size Amount of memory requested (in bytes). |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4509 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4510 | * @return Address of the allocated memory if successful; otherwise NULL. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4511 | * @req K-HEAP-001 |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4512 | */ |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 4513 | extern void *k_malloc(size_t size); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4514 | |
| 4515 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4516 | * @brief Free memory allocated from heap. |
Allan Stephens | 480a131 | 2016-10-13 15:44:48 -0500 | [diff] [blame] | 4517 | * |
| 4518 | * This routine provides traditional free() semantics. The memory being |
Andrew Boie | a2480bd | 2018-04-12 16:59:02 -0700 | [diff] [blame] | 4519 | * returned must have been allocated from the heap memory pool or |
| 4520 | * k_mem_pool_malloc(). |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4521 | * |
Anas Nashif | 345fdd5 | 2016-12-20 08:36:04 -0500 | [diff] [blame] | 4522 | * If @a ptr is NULL, no operation is performed. |
| 4523 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4524 | * @param ptr Pointer to previously allocated memory. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4525 | * |
| 4526 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4527 | * @req K-HEAP-001 |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4528 | */ |
| 4529 | extern void k_free(void *ptr); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4530 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4531 | /** |
Andrew Boie | 7f95e83 | 2017-11-08 14:40:01 -0800 | [diff] [blame] | 4532 | * @brief Allocate memory from heap, array style |
| 4533 | * |
| 4534 | * This routine provides traditional calloc() semantics. Memory is |
| 4535 | * allocated from the heap memory pool and zeroed. |
| 4536 | * |
| 4537 | * @param nmemb Number of elements in the requested array |
| 4538 | * @param size Size of each array element (in bytes). |
| 4539 | * |
| 4540 | * @return Address of the allocated memory if successful; otherwise NULL. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4541 | * @req K-HEAP-001 |
Andrew Boie | 7f95e83 | 2017-11-08 14:40:01 -0800 | [diff] [blame] | 4542 | */ |
| 4543 | extern void *k_calloc(size_t nmemb, size_t size); |
| 4544 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4545 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4546 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4547 | /* polling API - PRIVATE */ |
| 4548 | |
Benjamin Walsh | b017986 | 2017-02-02 16:39:57 -0500 | [diff] [blame] | 4549 | #ifdef CONFIG_POLL |
Flavio Ceolin | 6fdc56d | 2018-09-18 12:32:27 -0700 | [diff] [blame] | 4550 | #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] | 4551 | #else |
Flavio Ceolin | 6fdc56d | 2018-09-18 12:32:27 -0700 | [diff] [blame] | 4552 | #define _INIT_OBJ_POLL_EVENT(obj) do { } while (false) |
Benjamin Walsh | b017986 | 2017-02-02 16:39:57 -0500 | [diff] [blame] | 4553 | #endif |
| 4554 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4555 | /* private - types bit positions */ |
| 4556 | enum _poll_types_bits { |
| 4557 | /* can be used to ignore an event */ |
| 4558 | _POLL_TYPE_IGNORE, |
| 4559 | |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 4560 | /* to be signaled by k_poll_signal_raise() */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4561 | _POLL_TYPE_SIGNAL, |
| 4562 | |
| 4563 | /* semaphore availability */ |
| 4564 | _POLL_TYPE_SEM_AVAILABLE, |
| 4565 | |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 4566 | /* queue/fifo/lifo data availability */ |
| 4567 | _POLL_TYPE_DATA_AVAILABLE, |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4568 | |
| 4569 | _POLL_NUM_TYPES |
| 4570 | }; |
| 4571 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4572 | #define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1)) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4573 | |
| 4574 | /* private - states bit positions */ |
| 4575 | enum _poll_states_bits { |
| 4576 | /* default state when creating event */ |
| 4577 | _POLL_STATE_NOT_READY, |
| 4578 | |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 4579 | /* signaled by k_poll_signal_raise() */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4580 | _POLL_STATE_SIGNALED, |
| 4581 | |
| 4582 | /* semaphore is available */ |
| 4583 | _POLL_STATE_SEM_AVAILABLE, |
| 4584 | |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 4585 | /* data is available to read on queue/fifo/lifo */ |
| 4586 | _POLL_STATE_DATA_AVAILABLE, |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4587 | |
Paul Sokolovsky | 45c0b20 | 2018-08-21 23:29:11 +0300 | [diff] [blame] | 4588 | /* queue/fifo/lifo wait was cancelled */ |
| 4589 | _POLL_STATE_CANCELLED, |
| 4590 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4591 | _POLL_NUM_STATES |
| 4592 | }; |
| 4593 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4594 | #define Z_POLL_STATE_BIT(state) (1 << ((state) - 1)) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4595 | |
| 4596 | #define _POLL_EVENT_NUM_UNUSED_BITS \ |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4597 | (32 - (0 \ |
| 4598 | + 8 /* tag */ \ |
| 4599 | + _POLL_NUM_TYPES \ |
| 4600 | + _POLL_NUM_STATES \ |
| 4601 | + 1 /* modes */ \ |
| 4602 | )) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4603 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4604 | /* end of polling API - PRIVATE */ |
| 4605 | |
| 4606 | |
| 4607 | /** |
| 4608 | * @defgroup poll_apis Async polling APIs |
| 4609 | * @ingroup kernel_apis |
| 4610 | * @{ |
| 4611 | */ |
| 4612 | |
| 4613 | /* Public polling API */ |
| 4614 | |
| 4615 | /* public - values for k_poll_event.type bitfield */ |
| 4616 | #define K_POLL_TYPE_IGNORE 0 |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4617 | #define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL) |
| 4618 | #define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE) |
| 4619 | #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] | 4620 | #define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4621 | |
| 4622 | /* public - polling modes */ |
| 4623 | enum k_poll_modes { |
| 4624 | /* polling thread does not take ownership of objects when available */ |
| 4625 | K_POLL_MODE_NOTIFY_ONLY = 0, |
| 4626 | |
| 4627 | K_POLL_NUM_MODES |
| 4628 | }; |
| 4629 | |
| 4630 | /* public - values for k_poll_event.state bitfield */ |
| 4631 | #define K_POLL_STATE_NOT_READY 0 |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4632 | #define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED) |
| 4633 | #define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE) |
| 4634 | #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] | 4635 | #define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4636 | #define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4637 | |
| 4638 | /* public - poll signal object */ |
| 4639 | struct k_poll_signal { |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4640 | /** PRIVATE - DO NOT TOUCH */ |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 4641 | sys_dlist_t poll_events; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4642 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4643 | /** |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4644 | * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until |
| 4645 | * user resets it to 0. |
| 4646 | */ |
| 4647 | unsigned int signaled; |
| 4648 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4649 | /** custom result value passed to k_poll_signal_raise() if needed */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4650 | int result; |
| 4651 | }; |
| 4652 | |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 4653 | #define K_POLL_SIGNAL_INITIALIZER(obj) \ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4654 | { \ |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 4655 | .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4656 | .signaled = 0, \ |
| 4657 | .result = 0, \ |
| 4658 | } |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4659 | /** |
| 4660 | * @brief Poll Event |
| 4661 | * |
| 4662 | */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4663 | struct k_poll_event { |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4664 | /** PRIVATE - DO NOT TOUCH */ |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 4665 | sys_dnode_t _node; |
| 4666 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4667 | /** PRIVATE - DO NOT TOUCH */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4668 | struct _poller *poller; |
| 4669 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4670 | /** optional user-specified tag, opaque, untouched by the API */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4671 | u32_t tag:8; |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4672 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4673 | /** bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4674 | u32_t type:_POLL_NUM_TYPES; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4675 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4676 | /** bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4677 | u32_t state:_POLL_NUM_STATES; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4678 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4679 | /** mode of operation, from enum k_poll_modes */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4680 | u32_t mode:1; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4681 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4682 | /** unused bits in 32-bit word */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4683 | u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4684 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 4685 | /** per-type data */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4686 | union { |
| 4687 | void *obj; |
| 4688 | struct k_poll_signal *signal; |
| 4689 | struct k_sem *sem; |
| 4690 | struct k_fifo *fifo; |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 4691 | struct k_queue *queue; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4692 | }; |
| 4693 | }; |
| 4694 | |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4695 | #define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4696 | { \ |
| 4697 | .poller = NULL, \ |
| 4698 | .type = event_type, \ |
| 4699 | .state = K_POLL_STATE_NOT_READY, \ |
| 4700 | .mode = event_mode, \ |
| 4701 | .unused = 0, \ |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4702 | { .obj = event_obj }, \ |
| 4703 | } |
| 4704 | |
| 4705 | #define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \ |
| 4706 | event_tag) \ |
| 4707 | { \ |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4708 | .tag = event_tag, \ |
Markus Fuchs | be21d3f | 2019-10-09 21:31:25 +0200 | [diff] [blame] | 4709 | .type = event_type, \ |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4710 | .state = K_POLL_STATE_NOT_READY, \ |
| 4711 | .mode = event_mode, \ |
| 4712 | .unused = 0, \ |
| 4713 | { .obj = event_obj }, \ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4714 | } |
| 4715 | |
| 4716 | /** |
| 4717 | * @brief Initialize one struct k_poll_event instance |
| 4718 | * |
| 4719 | * After this routine is called on a poll event, the event it ready to be |
| 4720 | * placed in an event array to be passed to k_poll(). |
| 4721 | * |
| 4722 | * @param event The event to initialize. |
| 4723 | * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx |
| 4724 | * values. Only values that apply to the same object being polled |
| 4725 | * can be used together. Choosing K_POLL_TYPE_IGNORE disables the |
| 4726 | * event. |
Paul Sokolovsky | cfef979 | 2017-07-18 11:53:06 +0300 | [diff] [blame] | 4727 | * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY. |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4728 | * @param obj Kernel object or poll signal. |
| 4729 | * |
| 4730 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4731 | * @req K-POLL-001 |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4732 | */ |
| 4733 | |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4734 | 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] | 4735 | int mode, void *obj); |
| 4736 | |
| 4737 | /** |
| 4738 | * @brief Wait for one or many of multiple poll events to occur |
| 4739 | * |
| 4740 | * This routine allows a thread to wait concurrently for one or many of |
| 4741 | * multiple poll events to have occurred. Such events can be a kernel object |
| 4742 | * being available, like a semaphore, or a poll signal event. |
| 4743 | * |
| 4744 | * When an event notifies that a kernel object is available, the kernel object |
| 4745 | * is not "given" to the thread calling k_poll(): it merely signals the fact |
| 4746 | * that the object was available when the k_poll() call was in effect. Also, |
| 4747 | * all threads trying to acquire an object the regular way, i.e. by pending on |
| 4748 | * the object, have precedence over the thread polling on the object. This |
| 4749 | * means that the polling thread will never get the poll event on an object |
| 4750 | * until the object becomes available and its pend queue is empty. For this |
| 4751 | * reason, the k_poll() call is more effective when the objects being polled |
| 4752 | * only have one thread, the polling thread, trying to acquire them. |
| 4753 | * |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 4754 | * When k_poll() returns 0, the caller should loop on all the events that were |
| 4755 | * passed to k_poll() and check the state field for the values that were |
| 4756 | * expected and take the associated actions. |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4757 | * |
| 4758 | * Before being reused for another call to k_poll(), the user has to reset the |
| 4759 | * state field to K_POLL_STATE_NOT_READY. |
| 4760 | * |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4761 | * When called from user mode, a temporary memory allocation is required from |
| 4762 | * the caller's resource pool. |
| 4763 | * |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4764 | * @param events An array of pointers to events to be polled for. |
| 4765 | * @param num_events The number of events in the array. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 4766 | * @param timeout Non-negative waiting period for an event to be ready (in |
| 4767 | * milliseconds), or one of the special values K_NO_WAIT and |
| 4768 | * K_FOREVER. |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4769 | * |
| 4770 | * @retval 0 One or more events are ready. |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4771 | * @retval -EAGAIN Waiting period timed out. |
Paul Sokolovsky | 45c0b20 | 2018-08-21 23:29:11 +0300 | [diff] [blame] | 4772 | * @retval -EINTR Polling has been interrupted, e.g. with |
| 4773 | * k_queue_cancel_wait(). All output events are still set and valid, |
| 4774 | * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other |
| 4775 | * words, -EINTR status means that at least one of output events is |
| 4776 | * K_POLL_STATE_CANCELLED. |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4777 | * @retval -ENOMEM Thread resource pool insufficient memory (user mode only) |
| 4778 | * @retval -EINVAL Bad parameters (user mode only) |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4779 | * @req K-POLL-001 |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4780 | */ |
| 4781 | |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4782 | __syscall int k_poll(struct k_poll_event *events, int num_events, |
| 4783 | s32_t timeout); |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4784 | |
| 4785 | /** |
Benjamin Walsh | a304f16 | 2017-02-02 16:46:09 -0500 | [diff] [blame] | 4786 | * @brief Initialize a poll signal object. |
| 4787 | * |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 4788 | * 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] | 4789 | * |
| 4790 | * @param signal A poll signal. |
| 4791 | * |
| 4792 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4793 | * @req K-POLL-001 |
Benjamin Walsh | a304f16 | 2017-02-02 16:46:09 -0500 | [diff] [blame] | 4794 | */ |
| 4795 | |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4796 | __syscall void k_poll_signal_init(struct k_poll_signal *signal); |
| 4797 | |
| 4798 | /* |
| 4799 | * @brief Reset a poll signal object's state to unsignaled. |
| 4800 | * |
| 4801 | * @param signal A poll signal object |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4802 | * @req K-POLL-001 |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4803 | */ |
| 4804 | __syscall void k_poll_signal_reset(struct k_poll_signal *signal); |
| 4805 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4806 | 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] | 4807 | { |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 4808 | signal->signaled = 0U; |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4809 | } |
| 4810 | |
| 4811 | /** |
David B. Kinder | fcbd8fb | 2018-05-23 12:06:24 -0700 | [diff] [blame] | 4812 | * @brief Fetch the signaled state and result value of a poll signal |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4813 | * |
| 4814 | * @param signal A poll signal object |
| 4815 | * @param signaled An integer buffer which will be written nonzero if the |
| 4816 | * object was signaled |
| 4817 | * @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] | 4818 | * result value if the object was signaled, or an undefined |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4819 | * value if it was not. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4820 | * @req K-POLL-001 |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4821 | */ |
| 4822 | __syscall void k_poll_signal_check(struct k_poll_signal *signal, |
| 4823 | unsigned int *signaled, int *result); |
Benjamin Walsh | a304f16 | 2017-02-02 16:46:09 -0500 | [diff] [blame] | 4824 | |
| 4825 | /** |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4826 | * @brief Signal a poll signal object. |
| 4827 | * |
| 4828 | * This routine makes ready a poll signal, which is basically a poll event of |
| 4829 | * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be |
| 4830 | * made ready to run. A @a result value can be specified. |
| 4831 | * |
| 4832 | * The poll signal contains a 'signaled' field that, when set by |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 4833 | * 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] | 4834 | * k_poll_signal_reset(). It thus has to be reset by the user before being |
| 4835 | * passed again to k_poll() or k_poll() will consider it being signaled, and |
| 4836 | * will return immediately. |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4837 | * |
Peter A. Bigot | 773bd98 | 2019-04-30 07:06:39 -0500 | [diff] [blame] | 4838 | * @note The result is stored and the 'signaled' field is set even if |
| 4839 | * this function returns an error indicating that an expiring poll was |
| 4840 | * not notified. The next k_poll() will detect the missed raise. |
| 4841 | * |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4842 | * @param signal A poll signal. |
| 4843 | * @param result The value to store in the result field of the signal. |
| 4844 | * |
| 4845 | * @retval 0 The signal was delivered successfully. |
| 4846 | * @retval -EAGAIN The polling thread's timeout is in the process of expiring. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4847 | * @req K-POLL-001 |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4848 | */ |
| 4849 | |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 4850 | __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] | 4851 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 4852 | /** |
| 4853 | * @internal |
| 4854 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4855 | 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] | 4856 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4857 | /** @} */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4858 | |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 4859 | /** |
Anas Nashif | 30c3cff | 2019-01-22 08:18:13 -0500 | [diff] [blame] | 4860 | * @defgroup cpu_idle_apis CPU Idling APIs |
| 4861 | * @ingroup kernel_apis |
| 4862 | * @{ |
| 4863 | */ |
Anas Nashif | 30c3cff | 2019-01-22 08:18:13 -0500 | [diff] [blame] | 4864 | /** |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 4865 | * @brief Make the CPU idle. |
| 4866 | * |
| 4867 | * This function makes the CPU idle until an event wakes it up. |
| 4868 | * |
| 4869 | * In a regular system, the idle thread should be the only thread responsible |
| 4870 | * for making the CPU idle and triggering any type of power management. |
| 4871 | * However, in some more constrained systems, such as a single-threaded system, |
| 4872 | * the only thread would be responsible for this if needed. |
| 4873 | * |
| 4874 | * @return N/A |
Anas Nashif | 30c3cff | 2019-01-22 08:18:13 -0500 | [diff] [blame] | 4875 | * @req K-CPU-IDLE-001 |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 4876 | */ |
Andrew Boie | 07525a3 | 2019-09-21 16:17:23 -0700 | [diff] [blame] | 4877 | static inline void k_cpu_idle(void) |
| 4878 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4879 | arch_cpu_idle(); |
Andrew Boie | 07525a3 | 2019-09-21 16:17:23 -0700 | [diff] [blame] | 4880 | } |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 4881 | |
| 4882 | /** |
| 4883 | * @brief Make the CPU idle in an atomic fashion. |
| 4884 | * |
| 4885 | * Similar to k_cpu_idle(), but called with interrupts locked if operations |
| 4886 | * must be done atomically before making the CPU idle. |
| 4887 | * |
| 4888 | * @param key Interrupt locking key obtained from irq_lock(). |
| 4889 | * |
| 4890 | * @return N/A |
Anas Nashif | 30c3cff | 2019-01-22 08:18:13 -0500 | [diff] [blame] | 4891 | * @req K-CPU-IDLE-002 |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 4892 | */ |
Andrew Boie | 07525a3 | 2019-09-21 16:17:23 -0700 | [diff] [blame] | 4893 | static inline void k_cpu_atomic_idle(unsigned int key) |
| 4894 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4895 | arch_cpu_atomic_idle(key); |
Andrew Boie | 07525a3 | 2019-09-21 16:17:23 -0700 | [diff] [blame] | 4896 | } |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 4897 | |
Anas Nashif | 30c3cff | 2019-01-22 08:18:13 -0500 | [diff] [blame] | 4898 | /** |
| 4899 | * @} |
| 4900 | */ |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 4901 | |
| 4902 | /** |
| 4903 | * @internal |
| 4904 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4905 | extern void z_sys_power_save_idle_exit(s32_t ticks); |
Andrew Boie | 350f88d | 2017-01-18 13:13:45 -0800 | [diff] [blame] | 4906 | |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4907 | #ifdef ARCH_EXCEPT |
Ioannis Glaropoulos | df02923 | 2019-10-07 11:24:36 +0200 | [diff] [blame] | 4908 | /* This architecture has direct support for triggering a CPU exception */ |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4909 | #define z_except_reason(reason) ARCH_EXCEPT(reason) |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4910 | #else |
| 4911 | |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4912 | /* NOTE: This is the implementation for arches that do not implement |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4913 | * ARCH_EXCEPT() to generate a real CPU exception. |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4914 | * |
| 4915 | * We won't have a real exception frame to determine the PC value when |
| 4916 | * the oops occurred, so print file and line number before we jump into |
| 4917 | * the fatal error handler. |
| 4918 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4919 | #define z_except_reason(reason) do { \ |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4920 | printk("@ %s:%d:\n", __FILE__, __LINE__); \ |
Andrew Boie | 5623637 | 2019-07-15 15:22:29 -0700 | [diff] [blame] | 4921 | z_fatal_error(reason, NULL); \ |
Flavio Ceolin | 6fdc56d | 2018-09-18 12:32:27 -0700 | [diff] [blame] | 4922 | } while (false) |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4923 | |
| 4924 | #endif /* _ARCH__EXCEPT */ |
| 4925 | |
| 4926 | /** |
| 4927 | * @brief Fatally terminate a thread |
| 4928 | * |
| 4929 | * This should be called when a thread has encountered an unrecoverable |
| 4930 | * runtime condition and needs to terminate. What this ultimately |
| 4931 | * means is determined by the _fatal_error_handler() implementation, which |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 4932 | * will be called will reason code K_ERR_KERNEL_OOPS. |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4933 | * |
| 4934 | * If this is called from ISR context, the default system fatal error handler |
| 4935 | * will treat it as an unrecoverable system error, just like k_panic(). |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4936 | * @req K-MISC-003 |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4937 | */ |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 4938 | #define k_oops() z_except_reason(K_ERR_KERNEL_OOPS) |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4939 | |
| 4940 | /** |
| 4941 | * @brief Fatally terminate the system |
| 4942 | * |
| 4943 | * This should be called when the Zephyr kernel has encountered an |
| 4944 | * unrecoverable runtime condition and needs to terminate. What this ultimately |
| 4945 | * means is determined by the _fatal_error_handler() implementation, which |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 4946 | * will be called will reason code K_ERR_KERNEL_PANIC. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4947 | * @req K-MISC-004 |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4948 | */ |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 4949 | #define k_panic() z_except_reason(K_ERR_KERNEL_PANIC) |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4950 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4951 | /* |
| 4952 | * private APIs that are utilized by one or more public APIs |
| 4953 | */ |
| 4954 | |
Stephanos Ioannidis | 2d74604 | 2019-10-25 00:08:21 +0900 | [diff] [blame] | 4955 | /** |
| 4956 | * @internal |
| 4957 | */ |
| 4958 | extern void z_init_thread_base(struct _thread_base *thread_base, |
| 4959 | int priority, u32_t initial_state, |
| 4960 | unsigned int options); |
| 4961 | |
Benjamin Walsh | b12a8e0 | 2016-12-14 15:24:12 -0500 | [diff] [blame] | 4962 | #ifdef CONFIG_MULTITHREADING |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 4963 | /** |
| 4964 | * @internal |
| 4965 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4966 | extern void z_init_static_threads(void); |
Benjamin Walsh | b12a8e0 | 2016-12-14 15:24:12 -0500 | [diff] [blame] | 4967 | #else |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 4968 | /** |
| 4969 | * @internal |
| 4970 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4971 | #define z_init_static_threads() do { } while (false) |
Benjamin Walsh | b12a8e0 | 2016-12-14 15:24:12 -0500 | [diff] [blame] | 4972 | #endif |
| 4973 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 4974 | /** |
| 4975 | * @internal |
| 4976 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4977 | extern bool z_is_thread_essential(void); |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 4978 | /** |
| 4979 | * @internal |
| 4980 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4981 | extern void z_timer_expiration_handler(struct _timeout *t); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4982 | |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4983 | /* arch/cpu.h may declare an architecture or platform-specific macro |
| 4984 | * for properly declaring stacks, compatible with MMU/MPU constraints if |
| 4985 | * enabled |
| 4986 | */ |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 4987 | |
| 4988 | /** |
| 4989 | * @brief Obtain an extern reference to a stack |
| 4990 | * |
| 4991 | * This macro properly brings the symbol of a thread stack declared |
| 4992 | * elsewhere into scope. |
| 4993 | * |
| 4994 | * @param sym Thread stack symbol name |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4995 | * @req K-MISC-005 |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 4996 | */ |
| 4997 | #define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[] |
| 4998 | |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4999 | #ifdef ARCH_THREAD_STACK_DEFINE |
| 5000 | #define K_THREAD_STACK_DEFINE(sym, size) ARCH_THREAD_STACK_DEFINE(sym, size) |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5001 | #define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \ |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 5002 | ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) |
| 5003 | #define K_THREAD_STACK_LEN(size) ARCH_THREAD_STACK_LEN(size) |
| 5004 | #define K_THREAD_STACK_MEMBER(sym, size) ARCH_THREAD_STACK_MEMBER(sym, size) |
| 5005 | #define K_THREAD_STACK_SIZEOF(sym) ARCH_THREAD_STACK_SIZEOF(sym) |
Andrew Boie | b5c6810 | 2019-11-21 17:38:17 -0800 | [diff] [blame] | 5006 | #define K_THREAD_STACK_RESERVED ((size_t)ARCH_THREAD_STACK_RESERVED) |
Andrew Boie | 4e5c093 | 2019-04-04 12:05:28 -0700 | [diff] [blame] | 5007 | static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym) |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 5008 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 5009 | return ARCH_THREAD_STACK_BUFFER(sym); |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 5010 | } |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5011 | #else |
| 5012 | /** |
| 5013 | * @brief Declare a toplevel thread stack memory region |
| 5014 | * |
| 5015 | * This declares a region of memory suitable for use as a thread's stack. |
| 5016 | * |
| 5017 | * This is the generic, historical definition. Align to STACK_ALIGN and put in |
| 5018 | * 'noinit' section so that it isn't zeroed at boot |
| 5019 | * |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 5020 | * The declared symbol will always be a k_thread_stack_t which can be passed to |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 5021 | * k_thread_create(), but should otherwise not be manipulated. If the buffer |
Andrew Boie | 4e5c093 | 2019-04-04 12:05:28 -0700 | [diff] [blame] | 5022 | * inside needs to be examined, examine thread->stack_info for the associated |
| 5023 | * thread object to obtain the boundaries. |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5024 | * |
| 5025 | * It is legal to precede this definition with the 'static' keyword. |
| 5026 | * |
| 5027 | * It is NOT legal to take the sizeof(sym) and pass that to the stackSize |
| 5028 | * parameter of k_thread_create(), it may not be the same as the |
| 5029 | * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead. |
| 5030 | * |
Andrew Boie | e2d7791 | 2018-05-30 09:45:35 -0700 | [diff] [blame] | 5031 | * Some arches may round the size of the usable stack region up to satisfy |
| 5032 | * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned |
| 5033 | * size. |
| 5034 | * |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5035 | * @param sym Thread stack symbol name |
| 5036 | * @param size Size of the stack memory region |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5037 | * @req K-TSTACK-001 |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5038 | */ |
| 5039 | #define K_THREAD_STACK_DEFINE(sym, size) \ |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 5040 | struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size] |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5041 | |
| 5042 | /** |
Rajavardhan Gundi | d4dd928 | 2018-06-27 13:26:20 +0530 | [diff] [blame] | 5043 | * @brief Calculate size of stacks to be allocated in a stack array |
| 5044 | * |
| 5045 | * This macro calculates the size to be allocated for the stacks |
| 5046 | * inside a stack array. It accepts the indicated "size" as a parameter |
| 5047 | * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer |
| 5048 | * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used. |
| 5049 | * |
| 5050 | * @param size Size of the stack memory region |
| 5051 | * @req K-TSTACK-001 |
| 5052 | */ |
| 5053 | #define K_THREAD_STACK_LEN(size) (size) |
| 5054 | |
| 5055 | /** |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5056 | * @brief Declare a toplevel array of thread stack memory regions |
| 5057 | * |
| 5058 | * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE |
| 5059 | * definition for additional details and constraints. |
| 5060 | * |
| 5061 | * This is the generic, historical definition. Align to STACK_ALIGN and put in |
| 5062 | * 'noinit' section so that it isn't zeroed at boot |
| 5063 | * |
| 5064 | * @param sym Thread stack symbol name |
| 5065 | * @param nmemb Number of stacks to declare |
| 5066 | * @param size Size of the stack memory region |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5067 | * @req K-TSTACK-001 |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5068 | */ |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5069 | #define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \ |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 5070 | struct _k_thread_stack_element __noinit \ |
Rajavardhan Gundi | d4dd928 | 2018-06-27 13:26:20 +0530 | [diff] [blame] | 5071 | __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)] |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5072 | |
| 5073 | /** |
| 5074 | * @brief Declare an embedded stack memory region |
| 5075 | * |
| 5076 | * Used for stacks embedded within other data structures. Use is highly |
| 5077 | * discouraged but in some cases necessary. For memory protection scenarios, |
| 5078 | * it is very important that any RAM preceding this member not be writable |
| 5079 | * by threads else a stack overflow will lead to silent corruption. In other |
| 5080 | * words, the containing data structure should live in RAM owned by the kernel. |
| 5081 | * |
| 5082 | * @param sym Thread stack symbol name |
| 5083 | * @param size Size of the stack memory region |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5084 | * @req K-TSTACK-001 |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5085 | */ |
| 5086 | #define K_THREAD_STACK_MEMBER(sym, size) \ |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 5087 | struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size] |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5088 | |
| 5089 | /** |
| 5090 | * @brief Return the size in bytes of a stack memory region |
| 5091 | * |
| 5092 | * Convenience macro for passing the desired stack size to k_thread_create() |
| 5093 | * since the underlying implementation may actually create something larger |
| 5094 | * (for instance a guard area). |
| 5095 | * |
Andrew Boie | e2d7791 | 2018-05-30 09:45:35 -0700 | [diff] [blame] | 5096 | * The value returned here is not guaranteed to match the 'size' parameter |
| 5097 | * passed to K_THREAD_STACK_DEFINE and may be larger. |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5098 | * |
| 5099 | * @param sym Stack memory symbol |
| 5100 | * @return Size of the stack |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5101 | * @req K-TSTACK-001 |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5102 | */ |
| 5103 | #define K_THREAD_STACK_SIZEOF(sym) sizeof(sym) |
| 5104 | |
Andrew Boie | 575abc0 | 2019-03-19 10:42:24 -0700 | [diff] [blame] | 5105 | |
| 5106 | /** |
| 5107 | * @brief Indicate how much additional memory is reserved for stack objects |
| 5108 | * |
| 5109 | * Any given stack declaration may have additional memory in it for guard |
| 5110 | * areas or supervisor mode stacks. This macro indicates how much space |
| 5111 | * is reserved for this. The memory reserved may not be contiguous within |
| 5112 | * the stack object, and does not account for additional space used due to |
| 5113 | * enforce alignment. |
| 5114 | */ |
Andrew Boie | b5c6810 | 2019-11-21 17:38:17 -0800 | [diff] [blame] | 5115 | #define K_THREAD_STACK_RESERVED ((size_t)0U) |
Andrew Boie | 575abc0 | 2019-03-19 10:42:24 -0700 | [diff] [blame] | 5116 | |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5117 | /** |
| 5118 | * @brief Get a pointer to the physical stack buffer |
| 5119 | * |
Andrew Boie | 4e5c093 | 2019-04-04 12:05:28 -0700 | [diff] [blame] | 5120 | * This macro is deprecated. If a stack buffer needs to be examined, the |
| 5121 | * bounds should be obtained from the associated thread's stack_info struct. |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5122 | * |
| 5123 | * @param sym Declared stack symbol name |
| 5124 | * @return The buffer itself, a char * |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5125 | * @req K-TSTACK-001 |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5126 | */ |
Andrew Boie | 4e5c093 | 2019-04-04 12:05:28 -0700 | [diff] [blame] | 5127 | static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym) |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 5128 | { |
| 5129 | return (char *)sym; |
| 5130 | } |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5131 | |
| 5132 | #endif /* _ARCH_DECLARE_STACK */ |
| 5133 | |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5134 | /** |
| 5135 | * @defgroup mem_domain_apis Memory domain APIs |
| 5136 | * @ingroup kernel_apis |
| 5137 | * @{ |
| 5138 | */ |
| 5139 | |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5140 | /** |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5141 | * @def K_MEM_PARTITION_DEFINE |
| 5142 | * @brief Used to declare a memory partition |
| 5143 | * @req K-MP-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5144 | */ |
| 5145 | #ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK |
| 5146 | #define K_MEM_PARTITION_DEFINE(name, start, size, attr) \ |
| 5147 | _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \ |
Andrew Boie | 41f6011 | 2019-01-31 15:53:24 -0800 | [diff] [blame] | 5148 | struct k_mem_partition name =\ |
Nicolas Pitre | 58d839b | 2019-05-21 11:32:21 -0400 | [diff] [blame] | 5149 | { (uintptr_t)start, size, attr} |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5150 | #else |
| 5151 | #define K_MEM_PARTITION_DEFINE(name, start, size, attr) \ |
Andrew Boie | 41f6011 | 2019-01-31 15:53:24 -0800 | [diff] [blame] | 5152 | struct k_mem_partition name =\ |
Nicolas Pitre | 58d839b | 2019-05-21 11:32:21 -0400 | [diff] [blame] | 5153 | { (uintptr_t)start, size, attr} |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5154 | #endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */ |
| 5155 | |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5156 | /* memory partition */ |
| 5157 | struct k_mem_partition { |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 5158 | /** start address of memory partition */ |
Nicolas Pitre | 58d839b | 2019-05-21 11:32:21 -0400 | [diff] [blame] | 5159 | uintptr_t start; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 5160 | /** size of memory partition */ |
Andrew Boie | a824821 | 2019-11-13 12:10:56 -0800 | [diff] [blame] | 5161 | size_t size; |
Ioannis Glaropoulos | 39bf24a | 2018-11-27 15:45:36 +0100 | [diff] [blame] | 5162 | #if defined(CONFIG_MEMORY_PROTECTION) |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 5163 | /** attribute of memory partition */ |
Adithya Baglody | 83bedcc | 2017-10-06 15:43:11 +0530 | [diff] [blame] | 5164 | k_mem_partition_attr_t attr; |
Ioannis Glaropoulos | 39bf24a | 2018-11-27 15:45:36 +0100 | [diff] [blame] | 5165 | #endif /* CONFIG_MEMORY_PROTECTION */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5166 | }; |
| 5167 | |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 5168 | /** |
| 5169 | * @brief Memory Domain |
| 5170 | * |
Adithya Baglody | 3a6d72e | 2018-02-13 16:44:44 +0530 | [diff] [blame] | 5171 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5172 | struct k_mem_domain { |
Adithya Baglody | 83bedcc | 2017-10-06 15:43:11 +0530 | [diff] [blame] | 5173 | #ifdef CONFIG_USERSPACE |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 5174 | /** partitions in the domain */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5175 | struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS]; |
Adithya Baglody | 83bedcc | 2017-10-06 15:43:11 +0530 | [diff] [blame] | 5176 | #endif /* CONFIG_USERSPACE */ |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 5177 | /** domain q */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5178 | sys_dlist_t mem_domain_q; |
Anas Nashif | e71293e | 2019-12-04 20:00:14 -0500 | [diff] [blame] | 5179 | /** number of partitions in the domain */ |
Leandro Pereira | 08de658 | 2018-02-28 14:22:57 -0800 | [diff] [blame] | 5180 | u8_t num_partitions; |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5181 | }; |
Adithya Baglody | 83bedcc | 2017-10-06 15:43:11 +0530 | [diff] [blame] | 5182 | |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5183 | |
| 5184 | /** |
| 5185 | * @brief Initialize a memory domain. |
| 5186 | * |
| 5187 | * Initialize a memory domain with given name and memory partitions. |
| 5188 | * |
Andrew Boie | fddd550 | 2019-07-30 18:07:32 -0700 | [diff] [blame] | 5189 | * See documentation for k_mem_domain_add_partition() for details about |
| 5190 | * partition constraints. |
| 5191 | * |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5192 | * @param domain The memory domain to be initialized. |
| 5193 | * @param num_parts The number of array items of "parts" parameter. |
| 5194 | * @param parts An array of pointers to the memory partitions. Can be NULL |
| 5195 | * if num_parts is zero. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5196 | * @req K-MD-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5197 | */ |
Leandro Pereira | 08de658 | 2018-02-28 14:22:57 -0800 | [diff] [blame] | 5198 | 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] | 5199 | struct k_mem_partition *parts[]); |
| 5200 | /** |
| 5201 | * @brief Destroy a memory domain. |
| 5202 | * |
| 5203 | * Destroy a memory domain. |
| 5204 | * |
| 5205 | * @param domain The memory domain to be destroyed. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5206 | * @req K-MD-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5207 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5208 | extern void k_mem_domain_destroy(struct k_mem_domain *domain); |
| 5209 | |
| 5210 | /** |
| 5211 | * @brief Add a memory partition into a memory domain. |
| 5212 | * |
Andrew Boie | fddd550 | 2019-07-30 18:07:32 -0700 | [diff] [blame] | 5213 | * Add a memory partition into a memory domain. Partitions must conform to |
| 5214 | * the following constraints: |
| 5215 | * |
| 5216 | * - Partition bounds must be within system RAM boundaries on MMU-based |
| 5217 | * systems. |
| 5218 | * - Partitions in the same memory domain may not overlap each other. |
| 5219 | * - Partitions must not be defined which expose private kernel |
| 5220 | * data structures or kernel objects. |
| 5221 | * - The starting address alignment, and the partition size must conform to |
| 5222 | * the constraints of the underlying memory management hardware, which |
| 5223 | * varies per architecture. |
| 5224 | * - Memory domain partitions are only intended to control access to memory |
| 5225 | * from user mode threads. |
| 5226 | * |
| 5227 | * Violating these constraints may lead to CPU exceptions or undefined |
| 5228 | * behavior. |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5229 | * |
| 5230 | * @param domain The memory domain to be added a memory partition. |
| 5231 | * @param part The memory partition to be added |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5232 | * @req K-MD-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5233 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5234 | extern void k_mem_domain_add_partition(struct k_mem_domain *domain, |
| 5235 | struct k_mem_partition *part); |
| 5236 | |
| 5237 | /** |
| 5238 | * @brief Remove a memory partition from a memory domain. |
| 5239 | * |
| 5240 | * Remove a memory partition from a memory domain. |
| 5241 | * |
| 5242 | * @param domain The memory domain to be removed a memory partition. |
| 5243 | * @param part The memory partition to be removed |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5244 | * @req K-MD-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5245 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5246 | extern void k_mem_domain_remove_partition(struct k_mem_domain *domain, |
| 5247 | struct k_mem_partition *part); |
| 5248 | |
| 5249 | /** |
| 5250 | * @brief Add a thread into a memory domain. |
| 5251 | * |
| 5252 | * Add a thread into a memory domain. |
| 5253 | * |
| 5254 | * @param domain The memory domain that the thread is going to be added into. |
| 5255 | * @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] | 5256 | * |
| 5257 | * @req K-MD-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5258 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5259 | extern void k_mem_domain_add_thread(struct k_mem_domain *domain, |
| 5260 | k_tid_t thread); |
| 5261 | |
| 5262 | /** |
| 5263 | * @brief Remove a thread from its memory domain. |
| 5264 | * |
| 5265 | * Remove a thread from its memory domain. |
| 5266 | * |
| 5267 | * @param thread ID of thread going to be removed from its memory domain. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5268 | * @req K-MD-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5269 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5270 | extern void k_mem_domain_remove_thread(k_tid_t thread); |
| 5271 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 5272 | /** @} */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5273 | |
Andrew Boie | 756f907 | 2017-10-10 16:01:49 -0700 | [diff] [blame] | 5274 | /** |
| 5275 | * @brief Emit a character buffer to the console device |
| 5276 | * |
| 5277 | * @param c String of characters to print |
| 5278 | * @param n The length of the string |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5279 | * |
| 5280 | * @req K-MISC-006 |
Andrew Boie | 756f907 | 2017-10-10 16:01:49 -0700 | [diff] [blame] | 5281 | */ |
| 5282 | __syscall void k_str_out(char *c, size_t n); |
| 5283 | |
Ioannis Glaropoulos | a6cb8b0 | 2019-05-09 21:55:10 +0200 | [diff] [blame] | 5284 | /** |
| 5285 | * @brief Disable preservation of floating point context information. |
| 5286 | * |
| 5287 | * This routine informs the kernel that the specified thread |
| 5288 | * will no longer be using the floating point registers. |
| 5289 | * |
| 5290 | * @warning |
| 5291 | * Some architectures apply restrictions on how the disabling of floating |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 5292 | * point preservation may be requested, see arch_float_disable. |
Ioannis Glaropoulos | a6cb8b0 | 2019-05-09 21:55:10 +0200 | [diff] [blame] | 5293 | * |
| 5294 | * @warning |
| 5295 | * This routine should only be used to disable floating point support for |
| 5296 | * a thread that currently has such support enabled. |
| 5297 | * |
| 5298 | * @param thread ID of thread. |
| 5299 | * |
| 5300 | * @retval 0 On success. |
| 5301 | * @retval -ENOSYS If the floating point disabling is not implemented. |
| 5302 | * -EINVAL If the floating point disabling could not be performed. |
| 5303 | */ |
| 5304 | __syscall int k_float_disable(struct k_thread *thread); |
| 5305 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 5306 | #ifdef __cplusplus |
| 5307 | } |
| 5308 | #endif |
| 5309 | |
Anas Nashif | 10291a0 | 2019-06-25 12:25:12 -0400 | [diff] [blame] | 5310 | #include <debug/tracing.h> |
Andrew Boie | fa94ee7 | 2017-09-28 16:54:35 -0700 | [diff] [blame] | 5311 | #include <syscalls/kernel.h> |
| 5312 | |
Benjamin Walsh | dfa7ce5 | 2017-01-22 17:06:05 -0500 | [diff] [blame] | 5313 | #endif /* !_ASMLANGUAGE */ |
| 5314 | |
Flavio Ceolin | 67ca176 | 2018-09-14 10:43:44 -0700 | [diff] [blame] | 5315 | #endif /* ZEPHYR_INCLUDE_KERNEL_H_ */ |