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 | |
Andrew Boie | 5bd891d | 2017-09-27 12:59:28 -0700 | [diff] [blame] | 139 | /* This enumeration needs to be kept in sync with the lists of kernel objects |
| 140 | * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str() |
| 141 | * function in kernel/userspace.c |
| 142 | */ |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 143 | enum k_objects { |
Andrew Boie | 7e3d3d7 | 2017-10-10 09:31:32 -0700 | [diff] [blame] | 144 | K_OBJ_ANY, |
| 145 | |
Leandro Pereira | c200367 | 2018-04-04 13:50:32 -0700 | [diff] [blame] | 146 | /** @cond |
| 147 | * Doxygen should ignore this build-time generated include file |
| 148 | * when genrating API documentation. Enumeration values are |
| 149 | * generated during build by gen_kobject_list.py. It includes |
| 150 | * basic kernel objects (e.g. pipes and mutexes) and driver types. |
| 151 | */ |
| 152 | #include <kobj-types-enum.h> |
| 153 | /** @endcond |
| 154 | */ |
Andrew Boie | 5bd891d | 2017-09-27 12:59:28 -0700 | [diff] [blame] | 155 | |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 156 | K_OBJ_LAST |
| 157 | }; |
Anas Nashif | 4bcb294 | 2019-01-23 23:06:29 -0500 | [diff] [blame] | 158 | /** |
| 159 | * @defgroup usermode_apis User Mode APIs |
| 160 | * @ingroup kernel_apis |
| 161 | * @{ |
| 162 | */ |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 163 | |
| 164 | #ifdef CONFIG_USERSPACE |
| 165 | /* Table generated by gperf, these objects are retrieved via |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 166 | * z_object_find() */ |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 167 | struct _k_object { |
| 168 | char *name; |
Andrew Boie | a811af3 | 2017-10-14 13:50:26 -0700 | [diff] [blame] | 169 | u8_t perms[CONFIG_MAX_THREAD_BYTES]; |
| 170 | u8_t type; |
| 171 | u8_t flags; |
Andrew Boie | bca15da | 2017-10-15 14:17:48 -0700 | [diff] [blame] | 172 | u32_t data; |
Andrew Boie | df55524 | 2018-05-25 07:28:54 -0700 | [diff] [blame] | 173 | } __packed __aligned(4); |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 174 | |
Andrew Boie | 877f82e | 2017-10-17 11:20:22 -0700 | [diff] [blame] | 175 | struct _k_object_assignment { |
| 176 | struct k_thread *thread; |
| 177 | void * const *objects; |
| 178 | }; |
| 179 | |
| 180 | /** |
| 181 | * @brief Grant a static thread access to a list of kernel objects |
| 182 | * |
| 183 | * For threads declared with K_THREAD_DEFINE(), grant the thread access to |
| 184 | * a set of kernel objects. These objects do not need to be in an initialized |
| 185 | * state. The permissions will be granted when the threads are initialized |
| 186 | * in the early boot sequence. |
| 187 | * |
| 188 | * All arguments beyond the first must be pointers to kernel objects. |
| 189 | * |
| 190 | * @param name_ Name of the thread, as passed to K_THREAD_DEFINE() |
| 191 | */ |
| 192 | #define K_THREAD_ACCESS_GRANT(name_, ...) \ |
| 193 | static void * const _CONCAT(_object_list_, name_)[] = \ |
| 194 | { __VA_ARGS__, NULL }; \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 195 | static const Z_STRUCT_SECTION_ITERABLE(_k_object_assignment, \ |
| 196 | _CONCAT(_object_access_, name_)) = \ |
Andrew Boie | 877f82e | 2017-10-17 11:20:22 -0700 | [diff] [blame] | 197 | { (&_k_thread_obj_ ## name_), \ |
| 198 | (_CONCAT(_object_list_, name_)) } |
| 199 | |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 200 | #define K_OBJ_FLAG_INITIALIZED BIT(0) |
Andrew Boie | 04caa67 | 2017-10-13 13:57:07 -0700 | [diff] [blame] | 201 | #define K_OBJ_FLAG_PUBLIC BIT(1) |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 202 | #define K_OBJ_FLAG_ALLOC BIT(2) |
Andrew Boie | 7875707 | 2019-07-23 13:29:30 -0700 | [diff] [blame] | 203 | #define K_OBJ_FLAG_DRIVER BIT(3) |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 204 | |
| 205 | /** |
| 206 | * Lookup a kernel object and init its metadata if it exists |
| 207 | * |
| 208 | * Calling this on an object will make it usable from userspace. |
| 209 | * Intended to be called as the last statement in kernel object init |
| 210 | * functions. |
| 211 | * |
Anas Nashif | 50e3acd | 2018-12-08 13:26:18 -0500 | [diff] [blame] | 212 | * @param obj Address of the kernel object |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 213 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 214 | void z_object_init(void *obj); |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 215 | #else |
Andrew Boie | c3d4e65 | 2019-06-28 14:19:16 -0700 | [diff] [blame] | 216 | /* LCOV_EXCL_START */ |
Andrew Boie | 877f82e | 2017-10-17 11:20:22 -0700 | [diff] [blame] | 217 | #define K_THREAD_ACCESS_GRANT(thread, ...) |
| 218 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 219 | /** |
| 220 | * @internal |
| 221 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 222 | static inline void z_object_init(void *obj) |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 223 | { |
| 224 | ARG_UNUSED(obj); |
| 225 | } |
| 226 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 227 | /** |
| 228 | * @internal |
| 229 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 230 | static inline void z_impl_k_object_access_grant(void *object, |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 231 | struct k_thread *thread) |
| 232 | { |
| 233 | ARG_UNUSED(object); |
| 234 | ARG_UNUSED(thread); |
| 235 | } |
| 236 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 237 | /** |
| 238 | * @internal |
| 239 | */ |
Andrew Boie | e9cfc54 | 2018-04-13 13:15:28 -0700 | [diff] [blame] | 240 | static inline void k_object_access_revoke(void *object, |
| 241 | struct k_thread *thread) |
Andrew Boie | a89bf01 | 2017-10-09 14:47:55 -0700 | [diff] [blame] | 242 | { |
| 243 | ARG_UNUSED(object); |
| 244 | ARG_UNUSED(thread); |
| 245 | } |
| 246 | |
Andrew Boie | e9cfc54 | 2018-04-13 13:15:28 -0700 | [diff] [blame] | 247 | /** |
| 248 | * @internal |
| 249 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 250 | static inline void z_impl_k_object_release(void *object) |
Andrew Boie | e9cfc54 | 2018-04-13 13:15:28 -0700 | [diff] [blame] | 251 | { |
| 252 | ARG_UNUSED(object); |
| 253 | } |
| 254 | |
Andrew Boie | 41bab6e | 2017-10-14 14:42:23 -0700 | [diff] [blame] | 255 | static inline void k_object_access_all_grant(void *object) |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 256 | { |
| 257 | ARG_UNUSED(object); |
| 258 | } |
Andrew Boie | c3d4e65 | 2019-06-28 14:19:16 -0700 | [diff] [blame] | 259 | /* LCOV_EXCL_STOP */ |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 260 | #endif /* !CONFIG_USERSPACE */ |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 261 | |
| 262 | /** |
Marti Bolivar | 67db616 | 2019-08-27 19:12:51 -0600 | [diff] [blame] | 263 | * Grant a thread access to a kernel object |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 264 | * |
| 265 | * The thread will be granted access to the object if the caller is from |
| 266 | * supervisor mode, or the caller is from user mode AND has permissions |
Andrew Boie | a89bf01 | 2017-10-09 14:47:55 -0700 | [diff] [blame] | 267 | * on both the object and the thread whose access is being granted. |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 268 | * |
| 269 | * @param object Address of kernel object |
| 270 | * @param thread Thread to grant access to the object |
| 271 | */ |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 272 | __syscall void k_object_access_grant(void *object, struct k_thread *thread); |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 273 | |
Andrew Boie | a89bf01 | 2017-10-09 14:47:55 -0700 | [diff] [blame] | 274 | /** |
Marti Bolivar | 67db616 | 2019-08-27 19:12:51 -0600 | [diff] [blame] | 275 | * Revoke a thread's access to a kernel object |
Andrew Boie | a89bf01 | 2017-10-09 14:47:55 -0700 | [diff] [blame] | 276 | * |
| 277 | * The thread will lose access to the object if the caller is from |
| 278 | * supervisor mode, or the caller is from user mode AND has permissions |
| 279 | * on both the object and the thread whose access is being revoked. |
| 280 | * |
| 281 | * @param object Address of kernel object |
| 282 | * @param thread Thread to remove access to the object |
| 283 | */ |
Andrew Boie | e9cfc54 | 2018-04-13 13:15:28 -0700 | [diff] [blame] | 284 | void k_object_access_revoke(void *object, struct k_thread *thread); |
| 285 | |
| 286 | |
| 287 | __syscall void k_object_release(void *object); |
Andrew Boie | 3b5ae80 | 2017-10-04 12:10:32 -0700 | [diff] [blame] | 288 | |
| 289 | /** |
Marti Bolivar | 67db616 | 2019-08-27 19:12:51 -0600 | [diff] [blame] | 290 | * Grant all present and future threads access to an object |
Andrew Boie | 3b5ae80 | 2017-10-04 12:10:32 -0700 | [diff] [blame] | 291 | * |
| 292 | * If the caller is from supervisor mode, or the caller is from user mode and |
| 293 | * have sufficient permissions on the object, then that object will have |
| 294 | * permissions granted to it for *all* current and future threads running in |
| 295 | * the system, effectively becoming a public kernel object. |
| 296 | * |
| 297 | * Use of this API should be avoided on systems that are running untrusted code |
| 298 | * as it is possible for such code to derive the addresses of kernel objects |
| 299 | * and perform unwanted operations on them. |
| 300 | * |
Andrew Boie | 04caa67 | 2017-10-13 13:57:07 -0700 | [diff] [blame] | 301 | * It is not possible to revoke permissions on public objects; once public, |
| 302 | * any thread may use it. |
| 303 | * |
Andrew Boie | 3b5ae80 | 2017-10-04 12:10:32 -0700 | [diff] [blame] | 304 | * @param object Address of kernel object |
| 305 | */ |
Andrew Boie | 41bab6e | 2017-10-14 14:42:23 -0700 | [diff] [blame] | 306 | void k_object_access_all_grant(void *object); |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 307 | |
Andrew Boie | 31bdfc0 | 2017-11-08 16:38:03 -0800 | [diff] [blame] | 308 | /** |
| 309 | * Allocate a kernel object of a designated type |
| 310 | * |
| 311 | * This will instantiate at runtime a kernel object of the specified type, |
| 312 | * returning a pointer to it. The object will be returned in an uninitialized |
| 313 | * state, with the calling thread being granted permission on it. The memory |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 314 | * 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] | 315 | * |
| 316 | * Currently, allocation of thread stacks is not supported. |
| 317 | * |
| 318 | * @param otype Requested kernel object type |
| 319 | * @return A pointer to the allocated kernel object, or NULL if memory wasn't |
| 320 | * available |
| 321 | */ |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 322 | __syscall void *k_object_alloc(enum k_objects otype); |
Andrew Boie | 31bdfc0 | 2017-11-08 16:38:03 -0800 | [diff] [blame] | 323 | |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 324 | #ifdef CONFIG_DYNAMIC_OBJECTS |
Andrew Boie | 31bdfc0 | 2017-11-08 16:38:03 -0800 | [diff] [blame] | 325 | /** |
| 326 | * Free a kernel object previously allocated with k_object_alloc() |
| 327 | * |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 328 | * This will return memory for a kernel object back to resource pool it was |
| 329 | * allocated from. Care must be exercised that the object will not be used |
| 330 | * during or after when this call is made. |
Andrew Boie | 31bdfc0 | 2017-11-08 16:38:03 -0800 | [diff] [blame] | 331 | * |
| 332 | * @param obj Pointer to the kernel object memory address. |
| 333 | */ |
| 334 | void k_object_free(void *obj); |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 335 | #else |
Andrew Boie | c3d4e65 | 2019-06-28 14:19:16 -0700 | [diff] [blame] | 336 | /* LCOV_EXCL_START */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 337 | static inline void *z_impl_k_object_alloc(enum k_objects otype) |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 338 | { |
Kumar Gala | 85699f7 | 2018-05-17 09:26:03 -0500 | [diff] [blame] | 339 | ARG_UNUSED(otype); |
| 340 | |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 341 | return NULL; |
| 342 | } |
| 343 | |
| 344 | static inline void k_obj_free(void *obj) |
| 345 | { |
| 346 | ARG_UNUSED(obj); |
| 347 | } |
Andrew Boie | c3d4e65 | 2019-06-28 14:19:16 -0700 | [diff] [blame] | 348 | /* LCOV_EXCL_STOP */ |
Andrew Boie | 31bdfc0 | 2017-11-08 16:38:03 -0800 | [diff] [blame] | 349 | #endif /* CONFIG_DYNAMIC_OBJECTS */ |
| 350 | |
Anas Nashif | 4bcb294 | 2019-01-23 23:06:29 -0500 | [diff] [blame] | 351 | /** @} */ |
| 352 | |
Andrew Boie | bca15da | 2017-10-15 14:17:48 -0700 | [diff] [blame] | 353 | /* Using typedef deliberately here, this is quite intended to be an opaque |
Andrew Boie | 4e5c093 | 2019-04-04 12:05:28 -0700 | [diff] [blame] | 354 | * type. |
Andrew Boie | bca15da | 2017-10-15 14:17:48 -0700 | [diff] [blame] | 355 | * |
| 356 | * The purpose of this data type is to clearly distinguish between the |
| 357 | * declared symbol for a stack (of type k_thread_stack_t) and the underlying |
| 358 | * buffer which composes the stack data actually used by the underlying |
Anas Nashif | f2cb20c | 2019-06-18 14:45:40 -0400 | [diff] [blame] | 359 | * thread; they cannot be used interchangeably as some arches precede the |
Andrew Boie | bca15da | 2017-10-15 14:17:48 -0700 | [diff] [blame] | 360 | * stack buffer region with guard areas that trigger a MPU or MMU fault |
| 361 | * if written to. |
| 362 | * |
| 363 | * APIs that want to work with the buffer inside should continue to use |
| 364 | * char *. |
| 365 | * |
| 366 | * Stacks should always be created with K_THREAD_STACK_DEFINE(). |
| 367 | */ |
| 368 | struct __packed _k_thread_stack_element { |
| 369 | char data; |
| 370 | }; |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 371 | typedef struct _k_thread_stack_element k_thread_stack_t; |
Andrew Boie | bca15da | 2017-10-15 14:17:48 -0700 | [diff] [blame] | 372 | |
Andrew Boie | 1e06ffc | 2017-09-11 09:30:04 -0700 | [diff] [blame] | 373 | /** |
| 374 | * @typedef k_thread_entry_t |
| 375 | * @brief Thread entry point function type. |
| 376 | * |
| 377 | * A thread's entry point function is invoked when the thread starts executing. |
| 378 | * Up to 3 argument values can be passed to the function. |
| 379 | * |
| 380 | * The thread terminates execution permanently if the entry point function |
| 381 | * returns. The thread is responsible for releasing any shared resources |
| 382 | * it may own (such as mutexes and dynamically allocated memory), prior to |
| 383 | * returning. |
| 384 | * |
| 385 | * @param p1 First argument. |
| 386 | * @param p2 Second argument. |
| 387 | * @param p3 Third argument. |
| 388 | * |
| 389 | * @return N/A |
| 390 | */ |
| 391 | typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3); |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 392 | |
| 393 | #ifdef CONFIG_THREAD_MONITOR |
| 394 | struct __thread_entry { |
Andrew Boie | 1e06ffc | 2017-09-11 09:30:04 -0700 | [diff] [blame] | 395 | k_thread_entry_t pEntry; |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 396 | void *parameter1; |
| 397 | void *parameter2; |
| 398 | void *parameter3; |
| 399 | }; |
| 400 | #endif |
| 401 | |
| 402 | /* can be used for creating 'dummy' threads, e.g. for pending on objects */ |
| 403 | struct _thread_base { |
| 404 | |
| 405 | /* this thread's entry in a ready/wait queue */ |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 406 | union { |
Peter A. Bigot | 82ad0d2 | 2019-01-03 23:49:28 -0600 | [diff] [blame] | 407 | sys_dnode_t qnode_dlist; |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 408 | struct rbnode qnode_rb; |
| 409 | }; |
| 410 | |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 411 | /* wait queue on which the thread is pended (needed only for |
| 412 | * trees, not dumb lists) |
| 413 | */ |
| 414 | _wait_q_t *pended_on; |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 415 | |
| 416 | /* user facing 'thread options'; values defined in include/kernel.h */ |
| 417 | u8_t user_options; |
| 418 | |
| 419 | /* thread state */ |
| 420 | u8_t thread_state; |
| 421 | |
| 422 | /* |
| 423 | * scheduler lock count and thread priority |
| 424 | * |
| 425 | * These two fields control the preemptibility of a thread. |
| 426 | * |
| 427 | * When the scheduler is locked, sched_locked is decremented, which |
| 428 | * means that the scheduler is locked for values from 0xff to 0x01. A |
| 429 | * thread is coop if its prio is negative, thus 0x80 to 0xff when |
| 430 | * looked at the value as unsigned. |
| 431 | * |
| 432 | * By putting them end-to-end, this means that a thread is |
| 433 | * non-preemptible if the bundled value is greater than or equal to |
| 434 | * 0x0080. |
| 435 | */ |
| 436 | union { |
| 437 | struct { |
| 438 | #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 439 | u8_t sched_locked; |
| 440 | s8_t prio; |
| 441 | #else /* LITTLE and PDP */ |
| 442 | s8_t prio; |
| 443 | u8_t sched_locked; |
| 444 | #endif |
| 445 | }; |
| 446 | u16_t preempt; |
| 447 | }; |
| 448 | |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 449 | #ifdef CONFIG_SCHED_DEADLINE |
| 450 | int prio_deadline; |
| 451 | #endif |
| 452 | |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 453 | u32_t order_key; |
| 454 | |
Andy Ross | 2724fd1 | 2018-01-29 14:55:20 -0800 | [diff] [blame] | 455 | #ifdef CONFIG_SMP |
| 456 | /* True for the per-CPU idle threads */ |
| 457 | u8_t is_idle; |
| 458 | |
Andy Ross | 2724fd1 | 2018-01-29 14:55:20 -0800 | [diff] [blame] | 459 | /* CPU index on which thread was last run */ |
| 460 | u8_t cpu; |
Andy Ross | 15c4007 | 2018-04-12 12:50:05 -0700 | [diff] [blame] | 461 | |
| 462 | /* Recursive count of irq_lock() calls */ |
| 463 | u8_t global_lock_count; |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 464 | |
| 465 | #endif |
| 466 | |
| 467 | #ifdef CONFIG_SCHED_CPU_MASK |
| 468 | /* "May run on" bits for each CPU */ |
| 469 | u8_t cpu_mask; |
Andy Ross | 2724fd1 | 2018-01-29 14:55:20 -0800 | [diff] [blame] | 470 | #endif |
| 471 | |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 472 | /* data returned by APIs */ |
| 473 | void *swap_data; |
| 474 | |
| 475 | #ifdef CONFIG_SYS_CLOCK_EXISTS |
| 476 | /* this thread's entry in a timeout queue */ |
| 477 | struct _timeout timeout; |
| 478 | #endif |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 479 | }; |
| 480 | |
| 481 | typedef struct _thread_base _thread_base_t; |
| 482 | |
| 483 | #if defined(CONFIG_THREAD_STACK_INFO) |
| 484 | /* Contains the stack information of a thread */ |
| 485 | struct _thread_stack_info { |
Andrew Boie | 4e5c093 | 2019-04-04 12:05:28 -0700 | [diff] [blame] | 486 | /* Stack start - Represents the start address of the thread-writable |
| 487 | * stack area. |
Andrew Boie | b85ac3e | 2018-06-01 12:15:13 -0700 | [diff] [blame] | 488 | */ |
Nicolas Pitre | 58d839b | 2019-05-21 11:32:21 -0400 | [diff] [blame] | 489 | uintptr_t start; |
Andrew Boie | b85ac3e | 2018-06-01 12:15:13 -0700 | [diff] [blame] | 490 | |
| 491 | /* Stack Size - Thread writable stack buffer size. Represents |
| 492 | * the size of the actual area, starting from the start member, |
| 493 | * that should be writable by the thread |
| 494 | */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 495 | u32_t size; |
| 496 | }; |
Andrew Boie | 41c68ec | 2017-05-11 15:38:20 -0700 | [diff] [blame] | 497 | |
| 498 | typedef struct _thread_stack_info _thread_stack_info_t; |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 499 | #endif /* CONFIG_THREAD_STACK_INFO */ |
| 500 | |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 501 | #if defined(CONFIG_USERSPACE) |
| 502 | struct _mem_domain_info { |
| 503 | /* memory domain queue node */ |
| 504 | sys_dnode_t mem_domain_q_node; |
| 505 | /* memory domain of the thread */ |
| 506 | struct k_mem_domain *mem_domain; |
| 507 | }; |
| 508 | |
| 509 | #endif /* CONFIG_USERSPACE */ |
| 510 | |
Daniel Leung | fc18243 | 2018-08-16 15:42:28 -0700 | [diff] [blame] | 511 | #ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA |
| 512 | struct _thread_userspace_local_data { |
| 513 | int errno_var; |
| 514 | }; |
| 515 | #endif |
| 516 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 517 | /** |
| 518 | * @ingroup thread_apis |
| 519 | * Thread Structure |
| 520 | */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 521 | struct k_thread { |
| 522 | |
| 523 | struct _thread_base base; |
| 524 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 525 | /** defined by the architecture, but all archs need these */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 526 | struct _callee_saved callee_saved; |
| 527 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 528 | /** static thread init data */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 529 | void *init_data; |
| 530 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 531 | /** |
| 532 | * abort function |
| 533 | * @req K-THREAD-002 |
| 534 | * */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 535 | void (*fn_abort)(void); |
| 536 | |
| 537 | #if defined(CONFIG_THREAD_MONITOR) |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 538 | /** thread entry and parameters description */ |
Andrew Boie | 2dd91ec | 2018-06-06 08:45:01 -0700 | [diff] [blame] | 539 | struct __thread_entry entry; |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 540 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 541 | /** next item in list of all threads */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 542 | struct k_thread *next_thread; |
| 543 | #endif |
| 544 | |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 545 | #if defined(CONFIG_THREAD_NAME) |
| 546 | /* Thread name */ |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 547 | char name[CONFIG_THREAD_MAX_NAME_LEN]; |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 548 | #endif |
| 549 | |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 550 | #ifdef CONFIG_THREAD_CUSTOM_DATA |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 551 | /** crude thread-local storage */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 552 | void *custom_data; |
| 553 | #endif |
| 554 | |
Daniel Leung | fc18243 | 2018-08-16 15:42:28 -0700 | [diff] [blame] | 555 | #ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA |
| 556 | struct _thread_userspace_local_data *userspace_local_data; |
| 557 | #endif |
| 558 | |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 559 | #ifdef CONFIG_ERRNO |
Daniel Leung | fc18243 | 2018-08-16 15:42:28 -0700 | [diff] [blame] | 560 | #ifndef CONFIG_USERSPACE |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 561 | /** per-thread errno variable */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 562 | int errno_var; |
| 563 | #endif |
Andrew Boie | 7f4d006 | 2018-07-19 11:09:33 -0700 | [diff] [blame] | 564 | #endif |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 565 | |
| 566 | #if defined(CONFIG_THREAD_STACK_INFO) |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 567 | /** Stack Info */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 568 | struct _thread_stack_info stack_info; |
| 569 | #endif /* CONFIG_THREAD_STACK_INFO */ |
| 570 | |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 571 | #if defined(CONFIG_USERSPACE) |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 572 | /** memory domain info of the thread */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 573 | struct _mem_domain_info mem_domain_info; |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 574 | /** Base address of thread stack */ |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 575 | k_thread_stack_t *stack_obj; |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 576 | #endif /* CONFIG_USERSPACE */ |
| 577 | |
Andy Ross | 042d8ec | 2017-12-09 08:37:20 -0800 | [diff] [blame] | 578 | #if defined(CONFIG_USE_SWITCH) |
| 579 | /* When using __switch() a few previously arch-specific items |
| 580 | * become part of the core OS |
| 581 | */ |
| 582 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 583 | /** z_swap() return value */ |
Andy Ross | 042d8ec | 2017-12-09 08:37:20 -0800 | [diff] [blame] | 584 | int swap_retval; |
| 585 | |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 586 | /** Context handle returned via arch_switch() */ |
Andy Ross | 042d8ec | 2017-12-09 08:37:20 -0800 | [diff] [blame] | 587 | void *switch_handle; |
| 588 | #endif |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 589 | /** resource pool */ |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 590 | struct k_mem_pool *resource_pool; |
Andy Ross | 042d8ec | 2017-12-09 08:37:20 -0800 | [diff] [blame] | 591 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 592 | /** arch-specifics: must always be at the end */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 593 | struct _thread_arch arch; |
| 594 | }; |
| 595 | |
| 596 | typedef struct k_thread _thread_t; |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 597 | typedef struct k_thread *k_tid_t; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 598 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 599 | enum execution_context_types { |
| 600 | K_ISR = 0, |
| 601 | K_COOP_THREAD, |
| 602 | K_PREEMPT_THREAD, |
| 603 | }; |
| 604 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 605 | /** |
Anas Nashif | 4bcb294 | 2019-01-23 23:06:29 -0500 | [diff] [blame] | 606 | * @addtogroup thread_apis |
Carles Cufi | cb0cf9f | 2017-01-10 10:57:38 +0100 | [diff] [blame] | 607 | * @{ |
| 608 | */ |
Ramakrishna Pallala | 110b8e4 | 2018-04-27 12:55:43 +0530 | [diff] [blame] | 609 | typedef void (*k_thread_user_cb_t)(const struct k_thread *thread, |
| 610 | void *user_data); |
Carles Cufi | cb0cf9f | 2017-01-10 10:57:38 +0100 | [diff] [blame] | 611 | |
| 612 | /** |
Ramakrishna Pallala | 110b8e4 | 2018-04-27 12:55:43 +0530 | [diff] [blame] | 613 | * @brief Iterate over all the threads in the system. |
| 614 | * |
| 615 | * This routine iterates over all the threads in the system and |
| 616 | * calls the user_cb function for each thread. |
| 617 | * |
| 618 | * @param user_cb Pointer to the user callback function. |
| 619 | * @param user_data Pointer to user data. |
| 620 | * |
| 621 | * @note CONFIG_THREAD_MONITOR must be set for this function |
| 622 | * to be effective. Also this API uses irq_lock to protect the |
| 623 | * _kernel.threads list which means creation of new threads and |
| 624 | * terminations of existing threads are blocked until this |
| 625 | * API returns. |
| 626 | * |
| 627 | * @return N/A |
| 628 | */ |
| 629 | extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data); |
| 630 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 631 | /** @} */ |
Carles Cufi | cb0cf9f | 2017-01-10 10:57:38 +0100 | [diff] [blame] | 632 | |
| 633 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 634 | * @defgroup thread_apis Thread APIs |
| 635 | * @ingroup kernel_apis |
| 636 | * @{ |
| 637 | */ |
| 638 | |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 639 | #endif /* !_ASMLANGUAGE */ |
| 640 | |
| 641 | |
| 642 | /* |
| 643 | * Thread user options. May be needed by assembly code. Common part uses low |
| 644 | * bits, arch-specific use high bits. |
| 645 | */ |
| 646 | |
Anas Nashif | a541e93 | 2018-05-24 11:19:16 -0500 | [diff] [blame] | 647 | /** |
| 648 | * @brief system thread that must not abort |
| 649 | * @req K-THREAD-000 |
| 650 | * */ |
Flavio Ceolin | 8aec087 | 2018-08-15 11:52:00 -0700 | [diff] [blame] | 651 | #define K_ESSENTIAL (BIT(0)) |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 652 | |
| 653 | #if defined(CONFIG_FP_SHARING) |
Anas Nashif | a541e93 | 2018-05-24 11:19:16 -0500 | [diff] [blame] | 654 | /** |
| 655 | * @brief thread uses floating point registers |
| 656 | */ |
Flavio Ceolin | 8aec087 | 2018-08-15 11:52:00 -0700 | [diff] [blame] | 657 | #define K_FP_REGS (BIT(1)) |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 658 | #endif |
| 659 | |
Anas Nashif | a541e93 | 2018-05-24 11:19:16 -0500 | [diff] [blame] | 660 | /** |
| 661 | * @brief user mode thread |
| 662 | * |
| 663 | * This thread has dropped from supervisor mode to user mode and consequently |
Andrew Boie | 5cfa5dc | 2017-08-30 14:17:44 -0700 | [diff] [blame] | 664 | * has additional restrictions |
| 665 | */ |
Flavio Ceolin | 8aec087 | 2018-08-15 11:52:00 -0700 | [diff] [blame] | 666 | #define K_USER (BIT(2)) |
Andrew Boie | 5cfa5dc | 2017-08-30 14:17:44 -0700 | [diff] [blame] | 667 | |
Anas Nashif | a541e93 | 2018-05-24 11:19:16 -0500 | [diff] [blame] | 668 | /** |
| 669 | * @brief Inherit Permissions |
| 670 | * |
| 671 | * @details |
| 672 | * Indicates that the thread being created should inherit all kernel object |
Andrew Boie | 47f8fd1 | 2017-10-05 11:11:02 -0700 | [diff] [blame] | 673 | * permissions from the thread that created it. No effect if CONFIG_USERSPACE |
| 674 | * is not enabled. |
| 675 | */ |
Flavio Ceolin | 8aec087 | 2018-08-15 11:52:00 -0700 | [diff] [blame] | 676 | #define K_INHERIT_PERMS (BIT(3)) |
Andrew Boie | 47f8fd1 | 2017-10-05 11:11:02 -0700 | [diff] [blame] | 677 | |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 678 | #ifdef CONFIG_X86 |
| 679 | /* x86 Bitmask definitions for threads user options */ |
| 680 | |
| 681 | #if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE) |
| 682 | /* thread uses SSEx (and also FP) registers */ |
Flavio Ceolin | 8aec087 | 2018-08-15 11:52:00 -0700 | [diff] [blame] | 683 | #define K_SSE_REGS (BIT(7)) |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 684 | #endif |
| 685 | #endif |
| 686 | |
| 687 | /* end - thread options */ |
| 688 | |
| 689 | #if !defined(_ASMLANGUAGE) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 690 | /** |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 691 | * @brief Create a thread. |
| 692 | * |
| 693 | * This routine initializes a thread, then schedules it for execution. |
| 694 | * |
| 695 | * The new thread may be scheduled for immediate execution or a delayed start. |
| 696 | * If the newly spawned thread does not have a delayed start the kernel |
| 697 | * scheduler may preempt the current thread to allow the new thread to |
| 698 | * execute. |
| 699 | * |
| 700 | * Thread options are architecture-specific, and can include K_ESSENTIAL, |
| 701 | * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating |
| 702 | * them using "|" (the logical OR operator). |
| 703 | * |
| 704 | * Historically, users often would use the beginning of the stack memory region |
| 705 | * to store the struct k_thread data, although corruption will occur if the |
| 706 | * stack overflows this region and stack protection features may not detect this |
| 707 | * situation. |
| 708 | * |
| 709 | * @param new_thread Pointer to uninitialized struct k_thread |
| 710 | * @param stack Pointer to the stack space. |
| 711 | * @param stack_size Stack size in bytes. |
| 712 | * @param entry Thread entry function. |
| 713 | * @param p1 1st entry point parameter. |
| 714 | * @param p2 2nd entry point parameter. |
| 715 | * @param p3 3rd entry point parameter. |
| 716 | * @param prio Thread priority. |
| 717 | * @param options Thread options. |
| 718 | * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay). |
| 719 | * |
| 720 | * @return ID of new thread. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 721 | * |
| 722 | * @req K-THREAD-001 |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 723 | */ |
Andrew Boie | 662c345 | 2017-10-02 10:51:18 -0700 | [diff] [blame] | 724 | __syscall k_tid_t k_thread_create(struct k_thread *new_thread, |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 725 | k_thread_stack_t *stack, |
Andrew Boie | 662c345 | 2017-10-02 10:51:18 -0700 | [diff] [blame] | 726 | size_t stack_size, |
| 727 | k_thread_entry_t entry, |
| 728 | void *p1, void *p2, void *p3, |
| 729 | int prio, u32_t options, s32_t delay); |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 730 | |
Andrew Boie | 3f091b5 | 2017-08-30 14:34:14 -0700 | [diff] [blame] | 731 | /** |
| 732 | * @brief Drop a thread's privileges permanently to user mode |
| 733 | * |
| 734 | * @param entry Function to start executing from |
| 735 | * @param p1 1st entry point parameter |
| 736 | * @param p2 2nd entry point parameter |
| 737 | * @param p3 3rd entry point parameter |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 738 | * @req K-THREAD-003 |
Andrew Boie | 3f091b5 | 2017-08-30 14:34:14 -0700 | [diff] [blame] | 739 | */ |
| 740 | extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry, |
| 741 | void *p1, void *p2, |
| 742 | void *p3); |
Andrew Boie | 3f091b5 | 2017-08-30 14:34:14 -0700 | [diff] [blame] | 743 | |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 744 | /** |
Adithya Baglody | 392219e | 2019-01-02 14:40:39 +0530 | [diff] [blame] | 745 | * @brief Grant a thread access to a set of kernel objects |
Andrew Boie | e12857a | 2017-10-17 11:38:26 -0700 | [diff] [blame] | 746 | * |
| 747 | * This is a convenience function. For the provided thread, grant access to |
| 748 | * the remaining arguments, which must be pointers to kernel objects. |
Andrew Boie | e12857a | 2017-10-17 11:38:26 -0700 | [diff] [blame] | 749 | * |
| 750 | * The thread object must be initialized (i.e. running). The objects don't |
| 751 | * need to be. |
Adithya Baglody | 392219e | 2019-01-02 14:40:39 +0530 | [diff] [blame] | 752 | * Note that NULL shouldn't be passed as an argument. |
Andrew Boie | e12857a | 2017-10-17 11:38:26 -0700 | [diff] [blame] | 753 | * |
| 754 | * @param thread Thread to grant access to objects |
Adithya Baglody | 392219e | 2019-01-02 14:40:39 +0530 | [diff] [blame] | 755 | * @param ... list of kernel object pointers |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 756 | * @req K-THREAD-004 |
Andrew Boie | e12857a | 2017-10-17 11:38:26 -0700 | [diff] [blame] | 757 | */ |
Adithya Baglody | 392219e | 2019-01-02 14:40:39 +0530 | [diff] [blame] | 758 | #define k_thread_access_grant(thread, ...) \ |
| 759 | FOR_EACH_FIXED_ARG(k_object_access_grant, thread, __VA_ARGS__) |
Andrew Boie | e12857a | 2017-10-17 11:38:26 -0700 | [diff] [blame] | 760 | |
| 761 | /** |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 762 | * @brief Assign a resource memory pool to a thread |
| 763 | * |
| 764 | * By default, threads have no resource pool assigned unless their parent |
| 765 | * thread has a resource pool, in which case it is inherited. Multiple |
| 766 | * threads may be assigned to the same memory pool. |
| 767 | * |
| 768 | * Changing a thread's resource pool will not migrate allocations from the |
| 769 | * previous pool. |
| 770 | * |
| 771 | * @param thread Target thread to assign a memory pool for resource requests, |
| 772 | * or NULL if the thread should no longer have a memory pool. |
| 773 | * @param pool Memory pool to use for resources. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 774 | * @req K-THREAD-005 |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 775 | */ |
| 776 | static inline void k_thread_resource_pool_assign(struct k_thread *thread, |
| 777 | struct k_mem_pool *pool) |
| 778 | { |
| 779 | thread->resource_pool = pool; |
| 780 | } |
| 781 | |
| 782 | #if (CONFIG_HEAP_MEM_POOL_SIZE > 0) |
| 783 | /** |
| 784 | * @brief Assign the system heap as a thread's resource pool |
| 785 | * |
| 786 | * Similar to k_thread_resource_pool_assign(), but the thread will use |
| 787 | * the kernel heap to draw memory. |
| 788 | * |
| 789 | * Use with caution, as a malicious thread could perform DoS attacks on the |
| 790 | * kernel heap. |
| 791 | * |
| 792 | * @param thread Target thread to assign the system heap for resource requests |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 793 | * |
| 794 | * @req K-THREAD-004 |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 795 | */ |
| 796 | void k_thread_system_pool_assign(struct k_thread *thread); |
| 797 | #endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */ |
| 798 | |
| 799 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 800 | * @brief Put the current thread to sleep. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 801 | * |
Charles E. Youse | a567831 | 2019-05-09 16:46:46 -0700 | [diff] [blame] | 802 | * This routine puts the current thread to sleep for @a duration milliseconds. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 803 | * |
Charles E. Youse | a567831 | 2019-05-09 16:46:46 -0700 | [diff] [blame] | 804 | * @param ms Number of milliseconds to sleep. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 805 | * |
Piotr Zięcik | 7700eb2 | 2018-10-25 17:45:08 +0200 | [diff] [blame] | 806 | * @return Zero if the requested time has elapsed or the number of milliseconds |
| 807 | * 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] | 808 | */ |
Charles E. Youse | a567831 | 2019-05-09 16:46:46 -0700 | [diff] [blame] | 809 | __syscall s32_t k_sleep(s32_t ms); |
| 810 | |
| 811 | /** |
| 812 | * @brief Put the current thread to sleep with microsecond resolution. |
| 813 | * |
| 814 | * This function is unlikely to work as expected without kernel tuning. |
| 815 | * In particular, because the lower bound on the duration of a sleep is |
| 816 | * the duration of a tick, CONFIG_SYS_CLOCK_TICKS_PER_SEC must be adjusted |
| 817 | * to achieve the resolution desired. The implications of doing this must |
| 818 | * be understood before attempting to use k_usleep(). Use with caution. |
| 819 | * |
| 820 | * @param us Number of microseconds to sleep. |
| 821 | * |
| 822 | * @return Zero if the requested time has elapsed or the number of microseconds |
| 823 | * left to sleep, if thread was woken up by \ref k_wakeup call. |
| 824 | */ |
| 825 | __syscall s32_t k_usleep(s32_t us); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 826 | |
| 827 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 828 | * @brief Cause the current thread to busy wait. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 829 | * |
| 830 | * 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] | 831 | * @a usec_to_wait microseconds. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 832 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 833 | * @return N/A |
| 834 | */ |
Andrew Boie | 42cfd4f | 2018-11-14 14:29:24 -0800 | [diff] [blame] | 835 | __syscall void k_busy_wait(u32_t usec_to_wait); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 836 | |
| 837 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 838 | * @brief Yield the current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 839 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 840 | * This routine causes the current thread to yield execution to another |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 841 | * 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] | 842 | * of the same or higher priority, the routine returns immediately. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 843 | * |
| 844 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 845 | * @req K-THREAD-015 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 846 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 847 | __syscall void k_yield(void); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 848 | |
| 849 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 850 | * @brief Wake up a sleeping thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 851 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 852 | * This routine prematurely wakes up @a thread from sleeping. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 853 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 854 | * If @a thread is not currently sleeping, the routine has no effect. |
| 855 | * |
| 856 | * @param thread ID of thread to wake. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 857 | * |
| 858 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 859 | * @req K-THREAD-014 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 860 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 861 | __syscall void k_wakeup(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 862 | |
| 863 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 864 | * @brief Get thread ID of the current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 865 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 866 | * @return ID of current thread. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 867 | * |
| 868 | * @req K-THREAD-013 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 869 | */ |
Andrew Boie | 76c04a2 | 2017-09-27 14:45:10 -0700 | [diff] [blame] | 870 | __syscall k_tid_t k_current_get(void); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 871 | |
| 872 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 873 | * @brief Abort a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 874 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 875 | * This routine permanently stops execution of @a thread. The thread is taken |
| 876 | * off all kernel queues it is part of (i.e. the ready queue, the timeout |
| 877 | * queue, or a kernel object wait queue). However, any kernel resources the |
| 878 | * thread might currently own (such as mutexes or memory blocks) are not |
| 879 | * released. It is the responsibility of the caller of this routine to ensure |
| 880 | * all necessary cleanup is performed. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 881 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 882 | * @param thread ID of thread to abort. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 883 | * |
| 884 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 885 | * @req K-THREAD-012 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 886 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 887 | __syscall void k_thread_abort(k_tid_t thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 888 | |
Andrew Boie | 7d627c5 | 2017-08-30 11:01:56 -0700 | [diff] [blame] | 889 | |
| 890 | /** |
| 891 | * @brief Start an inactive thread |
| 892 | * |
| 893 | * If a thread was created with K_FOREVER in the delay parameter, it will |
| 894 | * not be added to the scheduling queue until this function is called |
| 895 | * on it. |
| 896 | * |
| 897 | * @param thread thread to start |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 898 | * @req K-THREAD-011 |
Andrew Boie | 7d627c5 | 2017-08-30 11:01:56 -0700 | [diff] [blame] | 899 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 900 | __syscall void k_thread_start(k_tid_t thread); |
Andrew Boie | 7d627c5 | 2017-08-30 11:01:56 -0700 | [diff] [blame] | 901 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 902 | /** |
| 903 | * @cond INTERNAL_HIDDEN |
| 904 | */ |
| 905 | |
Benjamin Walsh | d211a52 | 2016-12-06 11:44:01 -0500 | [diff] [blame] | 906 | /* timeout has timed out and is not on _timeout_q anymore */ |
| 907 | #define _EXPIRED (-2) |
| 908 | |
Peter Mitsis | a04c0d7 | 2016-09-28 19:26:00 -0400 | [diff] [blame] | 909 | struct _static_thread_data { |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 910 | struct k_thread *init_thread; |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 911 | k_thread_stack_t *init_stack; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 912 | unsigned int init_stack_size; |
Andrew Boie | 1e06ffc | 2017-09-11 09:30:04 -0700 | [diff] [blame] | 913 | k_thread_entry_t init_entry; |
Allan Stephens | 7c5bffa | 2016-10-26 10:01:28 -0500 | [diff] [blame] | 914 | void *init_p1; |
| 915 | void *init_p2; |
| 916 | void *init_p3; |
| 917 | int init_prio; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 918 | u32_t init_options; |
| 919 | s32_t init_delay; |
Allan Stephens | 7c5bffa | 2016-10-26 10:01:28 -0500 | [diff] [blame] | 920 | void (*init_abort)(void); |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 921 | const char *init_name; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 922 | }; |
| 923 | |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 924 | #define _THREAD_INITIALIZER(thread, stack, stack_size, \ |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 925 | entry, p1, p2, p3, \ |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 926 | prio, options, delay, abort, tname) \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 927 | { \ |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 928 | .init_thread = (thread), \ |
| 929 | .init_stack = (stack), \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 930 | .init_stack_size = (stack_size), \ |
Andrew Boie | 1e06ffc | 2017-09-11 09:30:04 -0700 | [diff] [blame] | 931 | .init_entry = (k_thread_entry_t)entry, \ |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 932 | .init_p1 = (void *)p1, \ |
| 933 | .init_p2 = (void *)p2, \ |
| 934 | .init_p3 = (void *)p3, \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 935 | .init_prio = (prio), \ |
| 936 | .init_options = (options), \ |
| 937 | .init_delay = (delay), \ |
| 938 | .init_abort = (abort), \ |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 939 | .init_name = STRINGIFY(tname), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 940 | } |
| 941 | |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 942 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 943 | * INTERNAL_HIDDEN @endcond |
| 944 | */ |
| 945 | |
| 946 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 947 | * @brief Statically define and initialize a thread. |
| 948 | * |
| 949 | * The thread may be scheduled for immediate execution or a delayed start. |
| 950 | * |
| 951 | * Thread options are architecture-specific, and can include K_ESSENTIAL, |
| 952 | * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating |
| 953 | * them using "|" (the logical OR operator). |
| 954 | * |
| 955 | * The ID of the thread can be accessed using: |
| 956 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 957 | * @code extern const k_tid_t <name>; @endcode |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 958 | * |
| 959 | * @param name Name of the thread. |
| 960 | * @param stack_size Stack size in bytes. |
| 961 | * @param entry Thread entry function. |
| 962 | * @param p1 1st entry point parameter. |
| 963 | * @param p2 2nd entry point parameter. |
| 964 | * @param p3 3rd entry point parameter. |
| 965 | * @param prio Thread priority. |
| 966 | * @param options Thread options. |
| 967 | * @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] | 968 | * |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 969 | * @req K-THREAD-010 |
| 970 | * |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 971 | * @internal It has been observed that the x86 compiler by default aligns |
| 972 | * these _static_thread_data structures to 32-byte boundaries, thereby |
| 973 | * wasting space. To work around this, force a 4-byte alignment. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 974 | * |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 975 | */ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 976 | #define K_THREAD_DEFINE(name, stack_size, \ |
| 977 | entry, p1, p2, p3, \ |
| 978 | prio, options, delay) \ |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 979 | K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 980 | struct k_thread _k_thread_obj_##name; \ |
| 981 | Z_STRUCT_SECTION_ITERABLE(_static_thread_data, _k_thread_data_##name) =\ |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 982 | _THREAD_INITIALIZER(&_k_thread_obj_##name, \ |
| 983 | _k_thread_stack_##name, stack_size, \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 984 | entry, p1, p2, p3, prio, options, delay, \ |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 985 | NULL, name); \ |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 986 | const k_tid_t name = (k_tid_t)&_k_thread_obj_##name |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 987 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 988 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 989 | * @brief Get a thread's priority. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 990 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 991 | * This routine gets the priority of @a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 992 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 993 | * @param thread ID of thread whose priority is needed. |
| 994 | * |
| 995 | * @return Priority of @a thread. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 996 | * @req K-THREAD-009 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 997 | */ |
Andrew Boie | 76c04a2 | 2017-09-27 14:45:10 -0700 | [diff] [blame] | 998 | __syscall int k_thread_priority_get(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 999 | |
| 1000 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1001 | * @brief Set a thread's priority. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1002 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1003 | * This routine immediately changes the priority of @a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1004 | * |
| 1005 | * Rescheduling can occur immediately depending on the priority @a thread is |
| 1006 | * set to: |
| 1007 | * |
| 1008 | * - If its priority is raised above the priority of the caller of this |
| 1009 | * function, and the caller is preemptible, @a thread will be scheduled in. |
| 1010 | * |
| 1011 | * - If the caller operates on itself, it lowers its priority below that of |
| 1012 | * other threads in the system, and the caller is preemptible, the thread of |
| 1013 | * highest priority will be scheduled in. |
| 1014 | * |
| 1015 | * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to |
| 1016 | * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the |
| 1017 | * highest priority. |
| 1018 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1019 | * @param thread ID of thread whose priority is to be set. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1020 | * @param prio New priority. |
| 1021 | * |
| 1022 | * @warning Changing the priority of a thread currently involved in mutex |
| 1023 | * priority inheritance may result in undefined behavior. |
| 1024 | * |
| 1025 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1026 | * @req K-THREAD-008 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1027 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1028 | __syscall void k_thread_priority_set(k_tid_t thread, int prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1029 | |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 1030 | |
| 1031 | #ifdef CONFIG_SCHED_DEADLINE |
| 1032 | /** |
| 1033 | * @brief Set deadline expiration time for scheduler |
| 1034 | * |
| 1035 | * This sets the "deadline" expiration as a time delta from the |
| 1036 | * current time, in the same units used by k_cycle_get_32(). The |
| 1037 | * scheduler (when deadline scheduling is enabled) will choose the |
| 1038 | * next expiring thread when selecting between threads at the same |
| 1039 | * static priority. Threads at different priorities will be scheduled |
| 1040 | * according to their static priority. |
| 1041 | * |
| 1042 | * @note Deadlines that are negative (i.e. in the past) are still seen |
| 1043 | * as higher priority than others, even if the thread has "finished" |
| 1044 | * its work. If you don't want it scheduled anymore, you have to |
| 1045 | * reset the deadline into the future, block/pend the thread, or |
| 1046 | * modify its priority with k_thread_priority_set(). |
| 1047 | * |
| 1048 | * @note Despite the API naming, the scheduler makes no guarantees the |
| 1049 | * the thread WILL be scheduled within that deadline, nor does it take |
| 1050 | * extra metadata (like e.g. the "runtime" and "period" parameters in |
| 1051 | * Linux sched_setattr()) that allows the kernel to validate the |
| 1052 | * scheduling for achievability. Such features could be implemented |
| 1053 | * above this call, which is simply input to the priority selection |
| 1054 | * logic. |
| 1055 | * |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1056 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1057 | * @rst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1058 | * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project |
| 1059 | * configuration. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1060 | * @endrst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1061 | * |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 1062 | * @param thread A thread on which to set the deadline |
| 1063 | * @param deadline A time delta, in cycle units |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1064 | * |
| 1065 | * @req K-THREAD-007 |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 1066 | */ |
| 1067 | __syscall void k_thread_deadline_set(k_tid_t thread, int deadline); |
| 1068 | #endif |
| 1069 | |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1070 | #ifdef CONFIG_SCHED_CPU_MASK |
| 1071 | /** |
| 1072 | * @brief Sets all CPU enable masks to zero |
| 1073 | * |
| 1074 | * After this returns, the thread will no longer be schedulable on any |
| 1075 | * CPUs. The thread must not be currently runnable. |
| 1076 | * |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1077 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1078 | * @rst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1079 | * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project |
| 1080 | * configuration. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1081 | * @endrst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1082 | * |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1083 | * @param thread Thread to operate upon |
| 1084 | * @return Zero on success, otherwise error code |
| 1085 | */ |
| 1086 | int k_thread_cpu_mask_clear(k_tid_t thread); |
| 1087 | |
| 1088 | /** |
| 1089 | * @brief Sets all CPU enable masks to one |
| 1090 | * |
| 1091 | * After this returns, the thread will be schedulable on any CPU. The |
| 1092 | * thread must not be currently runnable. |
| 1093 | * |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1094 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1095 | * @rst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1096 | * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project |
| 1097 | * configuration. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1098 | * @endrst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1099 | * |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1100 | * @param thread Thread to operate upon |
| 1101 | * @return Zero on success, otherwise error code |
| 1102 | */ |
| 1103 | int k_thread_cpu_mask_enable_all(k_tid_t thread); |
| 1104 | |
| 1105 | /** |
| 1106 | * @brief Enable thread to run on specified CPU |
| 1107 | * |
| 1108 | * The thread must not be currently runnable. |
| 1109 | * |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1110 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1111 | * @rst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1112 | * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project |
| 1113 | * configuration. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1114 | * @endrst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1115 | * |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1116 | * @param thread Thread to operate upon |
| 1117 | * @param cpu CPU index |
| 1118 | * @return Zero on success, otherwise error code |
| 1119 | */ |
| 1120 | int k_thread_cpu_mask_enable(k_tid_t thread, int cpu); |
| 1121 | |
| 1122 | /** |
| 1123 | * @brief Prevent thread to run on specified CPU |
| 1124 | * |
| 1125 | * The thread must not be currently runnable. |
| 1126 | * |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1127 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1128 | * @rst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1129 | * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project |
| 1130 | * configuration. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1131 | * @endrst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1132 | * |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1133 | * @param thread Thread to operate upon |
| 1134 | * @param cpu CPU index |
| 1135 | * @return Zero on success, otherwise error code |
| 1136 | */ |
| 1137 | int k_thread_cpu_mask_disable(k_tid_t thread, int cpu); |
| 1138 | #endif |
| 1139 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1140 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1141 | * @brief Suspend a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1142 | * |
Andy Ross | 50d0942 | 2019-11-19 11:20:07 -0800 | [diff] [blame] | 1143 | * This routine prevents the kernel scheduler from making @a thread |
| 1144 | * the current thread. All other internal operations on @a thread are |
| 1145 | * still performed; for example, kernel objects it is waiting on are |
| 1146 | * still handed to it. Note that any existing timeouts |
| 1147 | * (e.g. k_sleep(), or a timeout argument to k_sem_take() et. al.) |
| 1148 | * will be canceled. On resume, the thread will begin running |
| 1149 | * immediately and return from the blocked call. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1150 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1151 | * If @a thread is already suspended, the routine has no effect. |
| 1152 | * |
| 1153 | * @param thread ID of thread to suspend. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1154 | * |
| 1155 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1156 | * @req K-THREAD-005 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1157 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1158 | __syscall void k_thread_suspend(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1159 | |
| 1160 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1161 | * @brief Resume a suspended thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1162 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1163 | * This routine allows the kernel scheduler to make @a thread the current |
| 1164 | * thread, when it is next eligible for that role. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1165 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1166 | * If @a thread is not currently suspended, the routine has no effect. |
| 1167 | * |
| 1168 | * @param thread ID of thread to resume. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1169 | * |
| 1170 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1171 | * @req K-THREAD-006 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1172 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1173 | __syscall void k_thread_resume(k_tid_t thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1174 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1175 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1176 | * @brief Set time-slicing period and scope. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1177 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1178 | * This routine specifies how the scheduler will perform time slicing of |
| 1179 | * preemptible threads. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1180 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1181 | * To enable time slicing, @a slice must be non-zero. The scheduler |
| 1182 | * ensures that no thread runs for more than the specified time limit |
| 1183 | * before other threads of that priority are given a chance to execute. |
| 1184 | * 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] | 1185 | * execute as long as desired without being preempted due to time slicing. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1186 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1187 | * 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] | 1188 | * execute. Once the scheduler selects a thread for execution, there is no |
| 1189 | * minimum guaranteed time the thread will execute before threads of greater or |
| 1190 | * equal priority are scheduled. |
| 1191 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1192 | * When the current thread is the only one of that priority eligible |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1193 | * for execution, this routine has no effect; the thread is immediately |
| 1194 | * rescheduled after the slice period expires. |
| 1195 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1196 | * To disable timeslicing, set both @a slice and @a prio to zero. |
| 1197 | * |
| 1198 | * @param slice Maximum time slice length (in milliseconds). |
| 1199 | * @param prio Highest thread priority level eligible for time slicing. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1200 | * |
| 1201 | * @return N/A |
| 1202 | */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 1203 | extern void k_sched_time_slice_set(s32_t slice, int prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1204 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 1205 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1206 | |
| 1207 | /** |
| 1208 | * @addtogroup isr_apis |
| 1209 | * @{ |
| 1210 | */ |
| 1211 | |
| 1212 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1213 | * @brief Determine if code is running at interrupt level. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1214 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1215 | * This routine allows the caller to customize its actions, depending on |
| 1216 | * whether it is a thread or an ISR. |
| 1217 | * |
| 1218 | * @note Can be called by ISRs. |
| 1219 | * |
Flavio Ceolin | 6a4a86e | 2018-12-17 12:40:22 -0800 | [diff] [blame] | 1220 | * @return false if invoked by a thread. |
| 1221 | * @return true if invoked by an ISR. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1222 | */ |
Flavio Ceolin | 6a4a86e | 2018-12-17 12:40:22 -0800 | [diff] [blame] | 1223 | extern bool k_is_in_isr(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1224 | |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1225 | /** |
| 1226 | * @brief Determine if code is running in a preemptible thread. |
| 1227 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1228 | * This routine allows the caller to customize its actions, depending on |
| 1229 | * whether it can be preempted by another thread. The routine returns a 'true' |
| 1230 | * value if all of the following conditions are met: |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1231 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1232 | * - The code is running in a thread, not at ISR. |
| 1233 | * - The thread's priority is in the preemptible range. |
| 1234 | * - The thread has not locked the scheduler. |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1235 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1236 | * @note Can be called by ISRs. |
| 1237 | * |
| 1238 | * @return 0 if invoked by an ISR or by a cooperative thread. |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1239 | * @return Non-zero if invoked by a preemptible thread. |
| 1240 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1241 | __syscall int k_is_preempt_thread(void); |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1242 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1243 | /** |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 1244 | * @} |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1245 | */ |
| 1246 | |
| 1247 | /** |
| 1248 | * @addtogroup thread_apis |
| 1249 | * @{ |
| 1250 | */ |
| 1251 | |
| 1252 | /** |
| 1253 | * @brief Lock the scheduler. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1254 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1255 | * This routine prevents the current thread from being preempted by another |
| 1256 | * thread by instructing the scheduler to treat it as a cooperative thread. |
| 1257 | * If the thread subsequently performs an operation that makes it unready, |
| 1258 | * it will be context switched out in the normal manner. When the thread |
| 1259 | * again becomes the current thread, its non-preemptible status is maintained. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1260 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1261 | * This routine can be called recursively. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1262 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1263 | * @note k_sched_lock() and k_sched_unlock() should normally be used |
| 1264 | * when the operation being performed can be safely interrupted by ISRs. |
| 1265 | * However, if the amount of processing involved is very small, better |
| 1266 | * performance may be obtained by using irq_lock() and irq_unlock(). |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1267 | * |
| 1268 | * @return N/A |
| 1269 | */ |
| 1270 | extern void k_sched_lock(void); |
| 1271 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1272 | /** |
| 1273 | * @brief Unlock the scheduler. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1274 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1275 | * This routine reverses the effect of a previous call to k_sched_lock(). |
| 1276 | * A thread must call the routine once for each time it called k_sched_lock() |
| 1277 | * before the thread becomes preemptible. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1278 | * |
| 1279 | * @return N/A |
| 1280 | */ |
| 1281 | extern void k_sched_unlock(void); |
| 1282 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1283 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1284 | * @brief Set current thread's custom data. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1285 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1286 | * This routine sets the custom data for the current thread to @ value. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1287 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1288 | * Custom data is not used by the kernel itself, and is freely available |
| 1289 | * for a thread to use as it sees fit. It can be used as a framework |
| 1290 | * upon which to build thread-local storage. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1291 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1292 | * @param value New custom data value. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1293 | * |
| 1294 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1295 | * |
| 1296 | * @req K-THREAD-016 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1297 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1298 | __syscall void k_thread_custom_data_set(void *value); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1299 | |
| 1300 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1301 | * @brief Get current thread's custom data. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1302 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1303 | * This routine returns the custom data for the current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1304 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1305 | * @return Current custom data value. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1306 | * @req K-THREAD-007 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1307 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1308 | __syscall void *k_thread_custom_data_get(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1309 | |
| 1310 | /** |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 1311 | * @brief Set current thread name |
| 1312 | * |
| 1313 | * Set the name of the thread to be used when THREAD_MONITOR is enabled for |
| 1314 | * tracing and debugging. |
| 1315 | * |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 1316 | * @param thread_id Thread to set name, or NULL to set the current thread |
| 1317 | * @param value Name string |
| 1318 | * @retval 0 on success |
| 1319 | * @retval -EFAULT Memory access error with supplied string |
| 1320 | * @retval -ENOSYS Thread name configuration option not enabled |
| 1321 | * @retval -EINVAL Thread name too long |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 1322 | */ |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 1323 | __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] | 1324 | |
| 1325 | /** |
| 1326 | * @brief Get thread name |
| 1327 | * |
| 1328 | * Get the name of a thread |
| 1329 | * |
| 1330 | * @param thread_id Thread ID |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 1331 | * @retval Thread name, or NULL if configuration not enabled |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 1332 | */ |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 1333 | const char *k_thread_name_get(k_tid_t thread_id); |
| 1334 | |
| 1335 | /** |
| 1336 | * @brief Copy the thread name into a supplied buffer |
| 1337 | * |
| 1338 | * @param thread_id Thread to obtain name information |
| 1339 | * @param buf Destination buffer |
David B. Kinder | 73896c0 | 2019-10-28 16:27:57 -0700 | [diff] [blame] | 1340 | * @param size Destination buffer size |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 1341 | * @retval -ENOSPC Destination buffer too small |
| 1342 | * @retval -EFAULT Memory access error |
| 1343 | * @retval -ENOSYS Thread name feature not enabled |
| 1344 | * @retval 0 Success |
| 1345 | */ |
| 1346 | __syscall int k_thread_name_copy(k_tid_t thread_id, char *buf, |
| 1347 | size_t size); |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 1348 | |
| 1349 | /** |
Pavlo Hamov | 8076c80 | 2019-07-31 12:43:54 +0300 | [diff] [blame] | 1350 | * @brief Get thread state string |
| 1351 | * |
| 1352 | * Get the human friendly thread state string |
| 1353 | * |
| 1354 | * @param thread_id Thread ID |
| 1355 | * @retval Thread state string, empty if no state flag is set |
| 1356 | */ |
| 1357 | const char *k_thread_state_str(k_tid_t thread_id); |
| 1358 | |
| 1359 | /** |
Andy Ross | cfe6203 | 2018-09-29 07:34:55 -0700 | [diff] [blame] | 1360 | * @} |
| 1361 | */ |
| 1362 | |
| 1363 | /** |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1364 | * @addtogroup clock_apis |
| 1365 | * @{ |
| 1366 | */ |
| 1367 | |
| 1368 | /** |
| 1369 | * @brief Generate null timeout delay. |
| 1370 | * |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1371 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1372 | * not to wait if the requested operation cannot be performed immediately. |
| 1373 | * |
| 1374 | * @return Timeout delay value. |
| 1375 | */ |
| 1376 | #define K_NO_WAIT 0 |
| 1377 | |
| 1378 | /** |
| 1379 | * @brief Generate timeout delay from milliseconds. |
| 1380 | * |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1381 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1382 | * to wait up to @a ms milliseconds to perform the requested operation. |
| 1383 | * |
| 1384 | * @param ms Duration in milliseconds. |
| 1385 | * |
| 1386 | * @return Timeout delay value. |
| 1387 | */ |
Johan Hedberg | 1447169 | 2016-11-13 10:52:15 +0200 | [diff] [blame] | 1388 | #define K_MSEC(ms) (ms) |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1389 | |
| 1390 | /** |
| 1391 | * @brief Generate timeout delay from seconds. |
| 1392 | * |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1393 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1394 | * to wait up to @a s seconds to perform the requested operation. |
| 1395 | * |
| 1396 | * @param s Duration in seconds. |
| 1397 | * |
| 1398 | * @return Timeout delay value. |
| 1399 | */ |
Johan Hedberg | 1447169 | 2016-11-13 10:52:15 +0200 | [diff] [blame] | 1400 | #define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC) |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1401 | |
| 1402 | /** |
| 1403 | * @brief Generate timeout delay from minutes. |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1404 | |
| 1405 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1406 | * to wait up to @a m minutes to perform the requested operation. |
| 1407 | * |
| 1408 | * @param m Duration in minutes. |
| 1409 | * |
| 1410 | * @return Timeout delay value. |
| 1411 | */ |
Johan Hedberg | 1447169 | 2016-11-13 10:52:15 +0200 | [diff] [blame] | 1412 | #define K_MINUTES(m) K_SECONDS((m) * 60) |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1413 | |
| 1414 | /** |
| 1415 | * @brief Generate timeout delay from hours. |
| 1416 | * |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1417 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1418 | * to wait up to @a h hours to perform the requested operation. |
| 1419 | * |
| 1420 | * @param h Duration in hours. |
| 1421 | * |
| 1422 | * @return Timeout delay value. |
| 1423 | */ |
Johan Hedberg | 1447169 | 2016-11-13 10:52:15 +0200 | [diff] [blame] | 1424 | #define K_HOURS(h) K_MINUTES((h) * 60) |
| 1425 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1426 | /** |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1427 | * @brief Generate infinite timeout delay. |
| 1428 | * |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1429 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1430 | * to wait as long as necessary to perform the requested operation. |
| 1431 | * |
| 1432 | * @return Timeout delay value. |
| 1433 | */ |
| 1434 | #define K_FOREVER (-1) |
| 1435 | |
| 1436 | /** |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 1437 | * @} |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1438 | */ |
| 1439 | |
| 1440 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1441 | * @cond INTERNAL_HIDDEN |
| 1442 | */ |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 1443 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1444 | struct k_timer { |
| 1445 | /* |
| 1446 | * _timeout structure must be first here if we want to use |
| 1447 | * dynamic timer allocation. timeout.node is used in the double-linked |
| 1448 | * list of free timers |
| 1449 | */ |
| 1450 | struct _timeout timeout; |
| 1451 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1452 | /* wait queue for the (single) thread waiting on this timer */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1453 | _wait_q_t wait_q; |
| 1454 | |
| 1455 | /* runs in ISR context */ |
Flavio Ceolin | 4b35dd2 | 2018-11-16 19:06:59 -0800 | [diff] [blame] | 1456 | void (*expiry_fn)(struct k_timer *timer); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1457 | |
| 1458 | /* runs in the context of the thread that calls k_timer_stop() */ |
Flavio Ceolin | 4b35dd2 | 2018-11-16 19:06:59 -0800 | [diff] [blame] | 1459 | void (*stop_fn)(struct k_timer *timer); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1460 | |
| 1461 | /* timer period */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 1462 | s32_t period; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1463 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1464 | /* timer status */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 1465 | u32_t status; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1466 | |
Benjamin Walsh | e4e98f9 | 2017-01-12 19:38:53 -0500 | [diff] [blame] | 1467 | /* user-specific data, also used to support legacy features */ |
| 1468 | void *user_data; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1469 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 1470 | _OBJECT_TRACING_NEXT_PTR(k_timer) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 1471 | _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1472 | }; |
| 1473 | |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 1474 | #define Z_TIMER_INITIALIZER(obj, expiry, stop) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1475 | { \ |
Krzysztof Chruscinski | be06327 | 2019-02-13 11:19:54 +0100 | [diff] [blame] | 1476 | .timeout = { \ |
| 1477 | .node = {},\ |
| 1478 | .dticks = 0, \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1479 | .fn = z_timer_expiration_handler \ |
Krzysztof Chruscinski | be06327 | 2019-02-13 11:19:54 +0100 | [diff] [blame] | 1480 | }, \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1481 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 1482 | .expiry_fn = expiry, \ |
| 1483 | .stop_fn = stop, \ |
Krzysztof Chruscinski | be06327 | 2019-02-13 11:19:54 +0100 | [diff] [blame] | 1484 | .period = 0, \ |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 1485 | .status = 0, \ |
Benjamin Walsh | e4e98f9 | 2017-01-12 19:38:53 -0500 | [diff] [blame] | 1486 | .user_data = 0, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 1487 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1488 | } |
| 1489 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 1490 | #define K_TIMER_INITIALIZER __DEPRECATED_MACRO Z_TIMER_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 1491 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1492 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1493 | * INTERNAL_HIDDEN @endcond |
| 1494 | */ |
| 1495 | |
| 1496 | /** |
| 1497 | * @defgroup timer_apis Timer APIs |
| 1498 | * @ingroup kernel_apis |
| 1499 | * @{ |
| 1500 | */ |
| 1501 | |
| 1502 | /** |
Allan Stephens | 5eceb85 | 2016-11-16 10:16:30 -0500 | [diff] [blame] | 1503 | * @typedef k_timer_expiry_t |
| 1504 | * @brief Timer expiry function type. |
| 1505 | * |
| 1506 | * A timer's expiry function is executed by the system clock interrupt handler |
| 1507 | * each time the timer expires. The expiry function is optional, and is only |
| 1508 | * invoked if the timer has been initialized with one. |
| 1509 | * |
| 1510 | * @param timer Address of timer. |
| 1511 | * |
| 1512 | * @return N/A |
| 1513 | */ |
| 1514 | typedef void (*k_timer_expiry_t)(struct k_timer *timer); |
| 1515 | |
| 1516 | /** |
| 1517 | * @typedef k_timer_stop_t |
| 1518 | * @brief Timer stop function type. |
| 1519 | * |
| 1520 | * A timer's stop function is executed if the timer is stopped prematurely. |
| 1521 | * The function runs in the context of the thread that stops the timer. |
| 1522 | * The stop function is optional, and is only invoked if the timer has been |
| 1523 | * initialized with one. |
| 1524 | * |
| 1525 | * @param timer Address of timer. |
| 1526 | * |
| 1527 | * @return N/A |
| 1528 | */ |
| 1529 | typedef void (*k_timer_stop_t)(struct k_timer *timer); |
| 1530 | |
| 1531 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1532 | * @brief Statically define and initialize a timer. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1533 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1534 | * 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] | 1535 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 1536 | * @code extern struct k_timer <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1537 | * |
| 1538 | * @param name Name of the timer variable. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1539 | * @param expiry_fn Function to invoke each time the timer expires. |
| 1540 | * @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] | 1541 | */ |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 1542 | #define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 1543 | Z_STRUCT_SECTION_ITERABLE(k_timer, name) = \ |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 1544 | Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1545 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1546 | /** |
| 1547 | * @brief Initialize a timer. |
| 1548 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1549 | * This routine initializes a timer, prior to its first use. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1550 | * |
| 1551 | * @param timer Address of timer. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1552 | * @param expiry_fn Function to invoke each time the timer expires. |
| 1553 | * @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] | 1554 | * |
| 1555 | * @return N/A |
| 1556 | */ |
| 1557 | extern void k_timer_init(struct k_timer *timer, |
Allan Stephens | 5eceb85 | 2016-11-16 10:16:30 -0500 | [diff] [blame] | 1558 | k_timer_expiry_t expiry_fn, |
| 1559 | k_timer_stop_t stop_fn); |
Andy Ross | 8d8b2ac | 2016-09-23 10:08:54 -0700 | [diff] [blame] | 1560 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1561 | /** |
| 1562 | * @brief Start a timer. |
| 1563 | * |
| 1564 | * This routine starts a timer, and resets its status to zero. The timer |
| 1565 | * begins counting down using the specified duration and period values. |
| 1566 | * |
| 1567 | * Attempting to start a timer that is already running is permitted. |
| 1568 | * The timer's status is reset to zero and the timer begins counting down |
| 1569 | * using the new duration and period values. |
| 1570 | * |
| 1571 | * @param timer Address of timer. |
| 1572 | * @param duration Initial timer duration (in milliseconds). |
| 1573 | * @param period Timer period (in milliseconds). |
| 1574 | * |
| 1575 | * @return N/A |
| 1576 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1577 | __syscall void k_timer_start(struct k_timer *timer, |
| 1578 | s32_t duration, s32_t period); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1579 | |
| 1580 | /** |
| 1581 | * @brief Stop a timer. |
| 1582 | * |
| 1583 | * This routine stops a running timer prematurely. The timer's stop function, |
| 1584 | * if one exists, is invoked by the caller. |
| 1585 | * |
| 1586 | * 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] | 1587 | * effect on the timer. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1588 | * |
Anas Nashif | 4fb12ae | 2017-02-01 20:06:55 -0500 | [diff] [blame] | 1589 | * @note Can be called by ISRs. The stop handler has to be callable from ISRs |
| 1590 | * if @a k_timer_stop is to be called from ISRs. |
| 1591 | * |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1592 | * @param timer Address of timer. |
| 1593 | * |
| 1594 | * @return N/A |
| 1595 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1596 | __syscall void k_timer_stop(struct k_timer *timer); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1597 | |
| 1598 | /** |
| 1599 | * @brief Read timer status. |
| 1600 | * |
| 1601 | * This routine reads the timer's status, which indicates the number of times |
| 1602 | * it has expired since its status was last read. |
| 1603 | * |
| 1604 | * Calling this routine resets the timer's status to zero. |
| 1605 | * |
| 1606 | * @param timer Address of timer. |
| 1607 | * |
| 1608 | * @return Timer status. |
| 1609 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1610 | __syscall u32_t k_timer_status_get(struct k_timer *timer); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1611 | |
| 1612 | /** |
| 1613 | * @brief Synchronize thread to timer expiration. |
| 1614 | * |
| 1615 | * This routine blocks the calling thread until the timer's status is non-zero |
| 1616 | * (indicating that it has expired at least once since it was last examined) |
| 1617 | * or the timer is stopped. If the timer status is already non-zero, |
| 1618 | * or the timer is already stopped, the caller continues without waiting. |
| 1619 | * |
| 1620 | * Calling this routine resets the timer's status to zero. |
| 1621 | * |
| 1622 | * This routine must not be used by interrupt handlers, since they are not |
| 1623 | * allowed to block. |
| 1624 | * |
| 1625 | * @param timer Address of timer. |
| 1626 | * |
| 1627 | * @return Timer status. |
| 1628 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1629 | __syscall u32_t k_timer_status_sync(struct k_timer *timer); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1630 | |
Andy Ross | 52e444b | 2018-09-28 09:06:37 -0700 | [diff] [blame] | 1631 | extern s32_t z_timeout_remaining(struct _timeout *timeout); |
| 1632 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1633 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1634 | * @brief Get time remaining before a timer next expires. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1635 | * |
| 1636 | * This routine computes the (approximate) time remaining before a running |
| 1637 | * timer next expires. If the timer is not running, it returns zero. |
| 1638 | * |
| 1639 | * @param timer Address of timer. |
| 1640 | * |
| 1641 | * @return Remaining time (in milliseconds). |
| 1642 | */ |
Flavio Ceolin | f1e5303 | 2018-12-04 16:03:13 -0800 | [diff] [blame] | 1643 | __syscall u32_t k_timer_remaining_get(struct k_timer *timer); |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1644 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1645 | 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] | 1646 | { |
Charles E. Youse | 0ad4022 | 2019-03-01 10:51:04 -0800 | [diff] [blame] | 1647 | const s32_t ticks = z_timeout_remaining(&timer->timeout); |
Andy Ross | 8892406 | 2019-10-03 11:43:10 -0700 | [diff] [blame] | 1648 | return (ticks > 0) ? (u32_t)k_ticks_to_ms_floor64(ticks) : 0U; |
Johan Hedberg | f99ad3f | 2016-12-09 10:39:49 +0200 | [diff] [blame] | 1649 | } |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1650 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1651 | /** |
Benjamin Walsh | e4e98f9 | 2017-01-12 19:38:53 -0500 | [diff] [blame] | 1652 | * @brief Associate user-specific data with a timer. |
| 1653 | * |
| 1654 | * This routine records the @a user_data with the @a timer, to be retrieved |
| 1655 | * later. |
| 1656 | * |
| 1657 | * It can be used e.g. in a timer handler shared across multiple subsystems to |
| 1658 | * retrieve data specific to the subsystem this timer is associated with. |
| 1659 | * |
| 1660 | * @param timer Address of timer. |
| 1661 | * @param user_data User data to associate with the timer. |
| 1662 | * |
| 1663 | * @return N/A |
| 1664 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1665 | __syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data); |
| 1666 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 1667 | /** |
| 1668 | * @internal |
| 1669 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1670 | 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] | 1671 | void *user_data) |
Benjamin Walsh | e4e98f9 | 2017-01-12 19:38:53 -0500 | [diff] [blame] | 1672 | { |
| 1673 | timer->user_data = user_data; |
| 1674 | } |
| 1675 | |
| 1676 | /** |
| 1677 | * @brief Retrieve the user-specific data from a timer. |
| 1678 | * |
| 1679 | * @param timer Address of timer. |
| 1680 | * |
| 1681 | * @return The user data. |
| 1682 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1683 | __syscall void *k_timer_user_data_get(struct k_timer *timer); |
| 1684 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1685 | 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] | 1686 | { |
| 1687 | return timer->user_data; |
| 1688 | } |
| 1689 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 1690 | /** @} */ |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1691 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1692 | /** |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1693 | * @addtogroup clock_apis |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1694 | * @{ |
| 1695 | */ |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1696 | |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1697 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1698 | * @brief Get system uptime. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1699 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1700 | * This routine returns the elapsed time since the system booted, |
| 1701 | * in milliseconds. |
| 1702 | * |
David B. Kinder | 00c41ea | 2019-06-10 11:13:33 -0700 | [diff] [blame] | 1703 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1704 | * @rst |
David B. Kinder | 00c41ea | 2019-06-10 11:13:33 -0700 | [diff] [blame] | 1705 | * While this function returns time in milliseconds, it does |
| 1706 | * not mean it has millisecond resolution. The actual resolution depends on |
Andy Ross | 669730f | 2019-06-11 11:18:20 -0700 | [diff] [blame] | 1707 | * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1708 | * @endrst |
Paul Sokolovsky | 65d51fd | 2019-02-04 22:44:50 +0300 | [diff] [blame] | 1709 | * |
| 1710 | * @return Current uptime in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1711 | */ |
Andrew Boie | a73d373 | 2017-10-08 12:23:55 -0700 | [diff] [blame] | 1712 | __syscall s64_t k_uptime_get(void); |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1713 | |
Ramesh Thomas | 89ffd44 | 2017-02-05 19:37:19 -0800 | [diff] [blame] | 1714 | /** |
| 1715 | * @brief Enable clock always on in tickless kernel |
| 1716 | * |
Andy Ross | 1db9f18 | 2019-06-25 10:09:45 -0700 | [diff] [blame] | 1717 | * Deprecated. This does nothing (it was always just a hint). This |
| 1718 | * functionality has been migrated to the SYSTEM_CLOCK_SLOPPY_IDLE |
| 1719 | * kconfig. |
Ramesh Thomas | 89ffd44 | 2017-02-05 19:37:19 -0800 | [diff] [blame] | 1720 | * |
| 1721 | * @retval prev_status Previous status of always on flag |
| 1722 | */ |
Andy Ross | 1db9f18 | 2019-06-25 10:09:45 -0700 | [diff] [blame] | 1723 | /* LCOV_EXCL_START */ |
| 1724 | __deprecated static inline int k_enable_sys_clock_always_on(void) |
| 1725 | { |
| 1726 | __ASSERT(IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE), |
| 1727 | "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead"); |
| 1728 | |
| 1729 | return !IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE); |
| 1730 | } |
| 1731 | /* LCOV_EXCL_STOP */ |
Ramesh Thomas | 89ffd44 | 2017-02-05 19:37:19 -0800 | [diff] [blame] | 1732 | |
| 1733 | /** |
| 1734 | * @brief Disable clock always on in tickless kernel |
| 1735 | * |
Andy Ross | 1db9f18 | 2019-06-25 10:09:45 -0700 | [diff] [blame] | 1736 | * Deprecated. This does nothing (it was always just a hint). This |
| 1737 | * functionality has been migrated to the SYS_CLOCK_SLOPPY_IDLE |
| 1738 | * kconfig. |
Ramesh Thomas | 89ffd44 | 2017-02-05 19:37:19 -0800 | [diff] [blame] | 1739 | */ |
Andy Ross | 1db9f18 | 2019-06-25 10:09:45 -0700 | [diff] [blame] | 1740 | /* LCOV_EXCL_START */ |
| 1741 | __deprecated static inline void k_disable_sys_clock_always_on(void) |
| 1742 | { |
| 1743 | __ASSERT(!IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE), |
| 1744 | "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead"); |
| 1745 | } |
| 1746 | /* LCOV_EXCL_STOP */ |
Ramesh Thomas | 89ffd44 | 2017-02-05 19:37:19 -0800 | [diff] [blame] | 1747 | |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1748 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1749 | * @brief Get system uptime (32-bit version). |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1750 | * |
Peter Bigot | a6067a3 | 2019-08-28 08:19:26 -0500 | [diff] [blame] | 1751 | * This routine returns the lower 32 bits of the system uptime in |
| 1752 | * milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1753 | * |
Peter Bigot | a6067a3 | 2019-08-28 08:19:26 -0500 | [diff] [blame] | 1754 | * Because correct conversion requires full precision of the system |
| 1755 | * clock there is no benefit to using this over k_uptime_get() unless |
| 1756 | * you know the application will never run long enough for the system |
| 1757 | * clock to approach 2^32 ticks. Calls to this function may involve |
| 1758 | * interrupt blocking and 64-bit math. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1759 | * |
David B. Kinder | 00c41ea | 2019-06-10 11:13:33 -0700 | [diff] [blame] | 1760 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1761 | * @rst |
David B. Kinder | 00c41ea | 2019-06-10 11:13:33 -0700 | [diff] [blame] | 1762 | * While this function returns time in milliseconds, it does |
| 1763 | * not mean it has millisecond resolution. The actual resolution depends on |
Andy Ross | 669730f | 2019-06-11 11:18:20 -0700 | [diff] [blame] | 1764 | * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1765 | * @endrst |
Paul Sokolovsky | 65d51fd | 2019-02-04 22:44:50 +0300 | [diff] [blame] | 1766 | * |
Peter Bigot | a6067a3 | 2019-08-28 08:19:26 -0500 | [diff] [blame] | 1767 | * @return The low 32 bits of the current uptime, in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1768 | */ |
Peter Bigot | a6067a3 | 2019-08-28 08:19:26 -0500 | [diff] [blame] | 1769 | static inline u32_t k_uptime_get_32(void) |
| 1770 | { |
| 1771 | return (u32_t)k_uptime_get(); |
| 1772 | } |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1773 | |
| 1774 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1775 | * @brief Get elapsed time. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1776 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1777 | * This routine computes the elapsed time between the current system uptime |
| 1778 | * and an earlier reference time, in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1779 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1780 | * @param reftime Pointer to a reference time, which is updated to the current |
| 1781 | * uptime upon return. |
| 1782 | * |
| 1783 | * @return Elapsed time. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1784 | */ |
Andy Ross | 987c0e5 | 2018-09-27 16:50:00 -0700 | [diff] [blame] | 1785 | static inline s64_t k_uptime_delta(s64_t *reftime) |
| 1786 | { |
| 1787 | s64_t uptime, delta; |
| 1788 | |
| 1789 | uptime = k_uptime_get(); |
| 1790 | delta = uptime - *reftime; |
| 1791 | *reftime = uptime; |
| 1792 | |
| 1793 | return delta; |
| 1794 | } |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1795 | |
| 1796 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1797 | * @brief Get elapsed time (32-bit version). |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1798 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1799 | * This routine computes the elapsed time between the current system uptime |
| 1800 | * and an earlier reference time, in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1801 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1802 | * This routine can be more efficient than k_uptime_delta(), as it reduces the |
| 1803 | * need for interrupt locking and 64-bit math. However, the 32-bit result |
| 1804 | * cannot hold an elapsed time larger than approximately 50 days, so the |
| 1805 | * caller must handle possible rollovers. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1806 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1807 | * @param reftime Pointer to a reference time, which is updated to the current |
| 1808 | * uptime upon return. |
| 1809 | * |
| 1810 | * @return Elapsed time. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1811 | */ |
Andy Ross | 987c0e5 | 2018-09-27 16:50:00 -0700 | [diff] [blame] | 1812 | static inline u32_t k_uptime_delta_32(s64_t *reftime) |
| 1813 | { |
| 1814 | return (u32_t)k_uptime_delta(reftime); |
| 1815 | } |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1816 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1817 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1818 | * @brief Read the hardware clock. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1819 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1820 | * This routine returns the current time, as measured by the system's hardware |
| 1821 | * clock. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1822 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1823 | * @return Current hardware clock up-counter (in cycles). |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1824 | */ |
Andrew Boie | 979b17f | 2019-10-03 15:20:41 -0700 | [diff] [blame] | 1825 | static inline u32_t k_cycle_get_32(void) |
| 1826 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 1827 | return arch_k_cycle_get_32(); |
Andrew Boie | 979b17f | 2019-10-03 15:20:41 -0700 | [diff] [blame] | 1828 | } |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1829 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1830 | /** |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 1831 | * @} |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1832 | */ |
| 1833 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1834 | /** |
| 1835 | * @cond INTERNAL_HIDDEN |
| 1836 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1837 | |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1838 | struct k_queue { |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1839 | sys_sflist_t data_q; |
Andy Ross | 603ea42 | 2018-07-25 13:01:54 -0700 | [diff] [blame] | 1840 | struct k_spinlock lock; |
Luiz Augusto von Dentz | 84db641 | 2017-07-13 12:43:59 +0300 | [diff] [blame] | 1841 | union { |
| 1842 | _wait_q_t wait_q; |
| 1843 | |
| 1844 | _POLL_EVENT; |
| 1845 | }; |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1846 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 1847 | _OBJECT_TRACING_NEXT_PTR(k_queue) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 1848 | _OBJECT_TRACING_LINKED_FLAG |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1849 | }; |
| 1850 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 1851 | #define _K_QUEUE_INITIALIZER(obj) \ |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1852 | { \ |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1853 | .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \ |
Stephanos Ioannidis | f628dcd | 2019-09-11 18:09:49 +0900 | [diff] [blame] | 1854 | .lock = { }, \ |
| 1855 | { \ |
| 1856 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
| 1857 | _POLL_EVENT_OBJ_INIT(obj) \ |
| 1858 | }, \ |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1859 | _OBJECT_TRACING_INIT \ |
| 1860 | } |
| 1861 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 1862 | #define K_QUEUE_INITIALIZER __DEPRECATED_MACRO _K_QUEUE_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 1863 | |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1864 | extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free); |
| 1865 | |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1866 | /** |
| 1867 | * INTERNAL_HIDDEN @endcond |
| 1868 | */ |
| 1869 | |
| 1870 | /** |
| 1871 | * @defgroup queue_apis Queue APIs |
| 1872 | * @ingroup kernel_apis |
| 1873 | * @{ |
| 1874 | */ |
| 1875 | |
| 1876 | /** |
| 1877 | * @brief Initialize a queue. |
| 1878 | * |
| 1879 | * This routine initializes a queue object, prior to its first use. |
| 1880 | * |
| 1881 | * @param queue Address of the queue. |
| 1882 | * |
| 1883 | * @return N/A |
| 1884 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1885 | __syscall void k_queue_init(struct k_queue *queue); |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1886 | |
| 1887 | /** |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 1888 | * @brief Cancel waiting on a queue. |
| 1889 | * |
| 1890 | * This routine causes first thread pending on @a queue, if any, to |
| 1891 | * 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] | 1892 | * If the queue is being waited on by k_poll(), it will return with |
| 1893 | * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent |
| 1894 | * k_queue_get() will return NULL). |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 1895 | * |
| 1896 | * @note Can be called by ISRs. |
| 1897 | * |
| 1898 | * @param queue Address of the queue. |
| 1899 | * |
| 1900 | * @return N/A |
| 1901 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1902 | __syscall void k_queue_cancel_wait(struct k_queue *queue); |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 1903 | |
| 1904 | /** |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1905 | * @brief Append an element to the end of a queue. |
| 1906 | * |
| 1907 | * 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] | 1908 | * aligned on a word boundary, and the first word of the item is reserved |
| 1909 | * for the kernel's use. |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1910 | * |
| 1911 | * @note Can be called by ISRs. |
| 1912 | * |
| 1913 | * @param queue Address of the queue. |
| 1914 | * @param data Address of the data item. |
| 1915 | * |
| 1916 | * @return N/A |
| 1917 | */ |
| 1918 | extern void k_queue_append(struct k_queue *queue, void *data); |
| 1919 | |
| 1920 | /** |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1921 | * @brief Append an element to a queue. |
| 1922 | * |
Andrew Boie | ac3dcc1 | 2019-04-01 12:28:03 -0700 | [diff] [blame] | 1923 | * This routine appends a data item to @a queue. There is an implicit memory |
| 1924 | * allocation to create an additional temporary bookkeeping data structure from |
| 1925 | * the calling thread's resource pool, which is automatically freed when the |
| 1926 | * item is removed. The data itself is not copied. |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1927 | * |
| 1928 | * @note Can be called by ISRs. |
| 1929 | * |
| 1930 | * @param queue Address of the queue. |
| 1931 | * @param data Address of the data item. |
| 1932 | * |
| 1933 | * @retval 0 on success |
| 1934 | * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool |
| 1935 | */ |
Adithya Baglody | 2a78b8d | 2018-10-25 12:09:04 +0530 | [diff] [blame] | 1936 | __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] | 1937 | |
| 1938 | /** |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1939 | * @brief Prepend an element to a queue. |
| 1940 | * |
| 1941 | * 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] | 1942 | * aligned on a word boundary, and the first word of the item is reserved |
| 1943 | * for the kernel's use. |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1944 | * |
| 1945 | * @note Can be called by ISRs. |
| 1946 | * |
| 1947 | * @param queue Address of the queue. |
| 1948 | * @param data Address of the data item. |
| 1949 | * |
| 1950 | * @return N/A |
| 1951 | */ |
| 1952 | extern void k_queue_prepend(struct k_queue *queue, void *data); |
| 1953 | |
| 1954 | /** |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1955 | * @brief Prepend an element to a queue. |
| 1956 | * |
Andrew Boie | ac3dcc1 | 2019-04-01 12:28:03 -0700 | [diff] [blame] | 1957 | * This routine prepends a data item to @a queue. There is an implicit memory |
| 1958 | * allocation to create an additional temporary bookkeeping data structure from |
| 1959 | * the calling thread's resource pool, which is automatically freed when the |
| 1960 | * item is removed. The data itself is not copied. |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1961 | * |
| 1962 | * @note Can be called by ISRs. |
| 1963 | * |
| 1964 | * @param queue Address of the queue. |
| 1965 | * @param data Address of the data item. |
| 1966 | * |
| 1967 | * @retval 0 on success |
| 1968 | * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool |
| 1969 | */ |
Adithya Baglody | 2a78b8d | 2018-10-25 12:09:04 +0530 | [diff] [blame] | 1970 | __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] | 1971 | |
| 1972 | /** |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1973 | * @brief Inserts an element to a queue. |
| 1974 | * |
| 1975 | * 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] | 1976 | * data item must be aligned on a word boundary, and the first word of |
| 1977 | * the item is reserved for the kernel's use. |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1978 | * |
| 1979 | * @note Can be called by ISRs. |
| 1980 | * |
| 1981 | * @param queue Address of the queue. |
| 1982 | * @param prev Address of the previous data item. |
| 1983 | * @param data Address of the data item. |
| 1984 | * |
| 1985 | * @return N/A |
| 1986 | */ |
| 1987 | extern void k_queue_insert(struct k_queue *queue, void *prev, void *data); |
| 1988 | |
| 1989 | /** |
| 1990 | * @brief Atomically append a list of elements to a queue. |
| 1991 | * |
| 1992 | * 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] | 1993 | * 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] | 1994 | * in each data item pointing to the next data item; the list must be |
| 1995 | * NULL-terminated. |
| 1996 | * |
| 1997 | * @note Can be called by ISRs. |
| 1998 | * |
| 1999 | * @param queue Address of the queue. |
| 2000 | * @param head Pointer to first node in singly-linked list. |
| 2001 | * @param tail Pointer to last node in singly-linked list. |
| 2002 | * |
| 2003 | * @return N/A |
| 2004 | */ |
| 2005 | extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail); |
| 2006 | |
| 2007 | /** |
| 2008 | * @brief Atomically add a list of elements to a queue. |
| 2009 | * |
| 2010 | * This routine adds a list of data items to @a queue in one operation. |
| 2011 | * The data items must be in a singly-linked list implemented using a |
| 2012 | * sys_slist_t object. Upon completion, the original list is empty. |
| 2013 | * |
| 2014 | * @note Can be called by ISRs. |
| 2015 | * |
| 2016 | * @param queue Address of the queue. |
| 2017 | * @param list Pointer to sys_slist_t object. |
| 2018 | * |
| 2019 | * @return N/A |
| 2020 | */ |
| 2021 | extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list); |
| 2022 | |
| 2023 | /** |
| 2024 | * @brief Get an element from a queue. |
| 2025 | * |
Nicolas Pitre | 659fa0d | 2019-05-21 22:13:01 -0400 | [diff] [blame] | 2026 | * This routine removes first data item from @a queue. The first word of the |
| 2027 | * data item is reserved for the kernel's use. |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2028 | * |
| 2029 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 2030 | * |
| 2031 | * @param queue Address of the queue. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 2032 | * @param timeout Non-negative waiting period to obtain a data item (in |
| 2033 | * milliseconds), or one of the special values K_NO_WAIT and |
| 2034 | * K_FOREVER. |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2035 | * |
| 2036 | * @return Address of the data item if successful; NULL if returned |
| 2037 | * without waiting, or waiting period timed out. |
| 2038 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2039 | __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] | 2040 | |
| 2041 | /** |
Luiz Augusto von Dentz | 50b9377 | 2017-07-03 16:52:45 +0300 | [diff] [blame] | 2042 | * @brief Remove an element from a queue. |
| 2043 | * |
Nicolas Pitre | 659fa0d | 2019-05-21 22:13:01 -0400 | [diff] [blame] | 2044 | * This routine removes data item from @a queue. The first word of the |
| 2045 | * 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] | 2046 | * rely on sys_slist_find_and_remove which is not a constant time operation. |
| 2047 | * |
| 2048 | * @note Can be called by ISRs |
| 2049 | * |
| 2050 | * @param queue Address of the queue. |
| 2051 | * @param data Address of the data item. |
| 2052 | * |
| 2053 | * @return true if data item was removed |
| 2054 | */ |
| 2055 | static inline bool k_queue_remove(struct k_queue *queue, void *data) |
| 2056 | { |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2057 | 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] | 2058 | } |
| 2059 | |
| 2060 | /** |
Dhananjay Gundapu Jayakrishnan | 24bfa40 | 2018-08-22 12:33:00 +0200 | [diff] [blame] | 2061 | * @brief Append an element to a queue only if it's not present already. |
| 2062 | * |
Nicolas Pitre | 659fa0d | 2019-05-21 22:13:01 -0400 | [diff] [blame] | 2063 | * This routine appends data item to @a queue. The first word of the data |
| 2064 | * 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] | 2065 | * relies on sys_slist_is_node_in_list which is not a constant time operation. |
| 2066 | * |
| 2067 | * @note Can be called by ISRs |
| 2068 | * |
| 2069 | * @param queue Address of the queue. |
| 2070 | * @param data Address of the data item. |
| 2071 | * |
| 2072 | * @return true if data item was added, false if not |
| 2073 | */ |
| 2074 | static inline bool k_queue_unique_append(struct k_queue *queue, void *data) |
| 2075 | { |
| 2076 | sys_sfnode_t *test; |
| 2077 | |
| 2078 | SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) { |
| 2079 | if (test == (sys_sfnode_t *) data) { |
| 2080 | return false; |
| 2081 | } |
| 2082 | } |
| 2083 | |
| 2084 | k_queue_append(queue, data); |
| 2085 | return true; |
| 2086 | } |
| 2087 | |
| 2088 | /** |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2089 | * @brief Query a queue to see if it has data available. |
| 2090 | * |
| 2091 | * Note that the data might be already gone by the time this function returns |
| 2092 | * if other threads are also trying to read from the queue. |
| 2093 | * |
| 2094 | * @note Can be called by ISRs. |
| 2095 | * |
| 2096 | * @param queue Address of the queue. |
| 2097 | * |
| 2098 | * @return Non-zero if the queue is empty. |
| 2099 | * @return 0 if data is available. |
| 2100 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2101 | __syscall int k_queue_is_empty(struct k_queue *queue); |
| 2102 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2103 | 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] | 2104 | { |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2105 | return (int)sys_sflist_is_empty(&queue->data_q); |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | /** |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2109 | * @brief Peek element at the head of queue. |
| 2110 | * |
| 2111 | * Return element from the head of queue without removing it. |
| 2112 | * |
| 2113 | * @param queue Address of the queue. |
| 2114 | * |
| 2115 | * @return Head element, or NULL if queue is empty. |
| 2116 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2117 | __syscall void *k_queue_peek_head(struct k_queue *queue); |
| 2118 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2119 | 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] | 2120 | { |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2121 | 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] | 2122 | } |
| 2123 | |
| 2124 | /** |
| 2125 | * @brief Peek element at the tail of queue. |
| 2126 | * |
| 2127 | * Return element from the tail of queue without removing it. |
| 2128 | * |
| 2129 | * @param queue Address of the queue. |
| 2130 | * |
| 2131 | * @return Tail element, or NULL if queue is empty. |
| 2132 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2133 | __syscall void *k_queue_peek_tail(struct k_queue *queue); |
| 2134 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2135 | 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] | 2136 | { |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2137 | 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] | 2138 | } |
| 2139 | |
| 2140 | /** |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2141 | * @brief Statically define and initialize a queue. |
| 2142 | * |
| 2143 | * The queue can be accessed outside the module where it is defined using: |
| 2144 | * |
| 2145 | * @code extern struct k_queue <name>; @endcode |
| 2146 | * |
| 2147 | * @param name Name of the queue. |
| 2148 | */ |
| 2149 | #define K_QUEUE_DEFINE(name) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 2150 | Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2151 | _K_QUEUE_INITIALIZER(name) |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2152 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 2153 | /** @} */ |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2154 | |
Wentong Wu | 5611e92 | 2019-06-20 23:51:27 +0800 | [diff] [blame] | 2155 | #ifdef CONFIG_USERSPACE |
| 2156 | /** |
| 2157 | * @brief futex structure |
| 2158 | * |
| 2159 | * A k_futex is a lightweight mutual exclusion primitive designed |
| 2160 | * to minimize kernel involvement. Uncontended operation relies |
| 2161 | * only on atomic access to shared memory. k_futex are tracked as |
| 2162 | * kernel objects and can live in user memory so any access bypass |
| 2163 | * the kernel object permission management mechanism. |
| 2164 | */ |
| 2165 | struct k_futex { |
| 2166 | atomic_t val; |
| 2167 | }; |
| 2168 | |
| 2169 | /** |
| 2170 | * @brief futex kernel data structure |
| 2171 | * |
| 2172 | * z_futex_data are the helper data structure for k_futex to complete |
| 2173 | * futex contended operation on kernel side, structure z_futex_data |
| 2174 | * of every futex object is invisible in user mode. |
| 2175 | */ |
| 2176 | struct z_futex_data { |
| 2177 | _wait_q_t wait_q; |
| 2178 | struct k_spinlock lock; |
| 2179 | }; |
| 2180 | |
| 2181 | #define Z_FUTEX_DATA_INITIALIZER(obj) \ |
| 2182 | { \ |
| 2183 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q) \ |
| 2184 | } |
| 2185 | |
| 2186 | /** |
| 2187 | * @defgroup futex_apis FUTEX APIs |
| 2188 | * @ingroup kernel_apis |
| 2189 | * @{ |
| 2190 | */ |
| 2191 | |
| 2192 | /** |
Wentong Wu | 5611e92 | 2019-06-20 23:51:27 +0800 | [diff] [blame] | 2193 | * @brief Pend the current thread on a futex |
| 2194 | * |
| 2195 | * Tests that the supplied futex contains the expected value, and if so, |
| 2196 | * goes to sleep until some other thread calls k_futex_wake() on it. |
| 2197 | * |
| 2198 | * @param futex Address of the futex. |
| 2199 | * @param expected Expected value of the futex, if it is different the caller |
| 2200 | * will not wait on it. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 2201 | * @param timeout Non-negative waiting period on the futex, in milliseconds, or |
| 2202 | * one of the special values K_NO_WAIT or K_FOREVER. |
Wentong Wu | 5611e92 | 2019-06-20 23:51:27 +0800 | [diff] [blame] | 2203 | * @retval -EACCES Caller does not have read access to futex address. |
| 2204 | * @retval -EAGAIN If the futex value did not match the expected parameter. |
| 2205 | * @retval -EINVAL Futex parameter address not recognized by the kernel. |
| 2206 | * @retval -ETIMEDOUT Thread woke up due to timeout and not a futex wakeup. |
| 2207 | * @retval 0 if the caller went to sleep and was woken up. The caller |
| 2208 | * should check the futex's value on wakeup to determine if it needs |
| 2209 | * to block again. |
| 2210 | */ |
| 2211 | __syscall int k_futex_wait(struct k_futex *futex, int expected, s32_t timeout); |
| 2212 | |
| 2213 | /** |
| 2214 | * @brief Wake one/all threads pending on a futex |
| 2215 | * |
| 2216 | * Wake up the highest priority thread pending on the supplied futex, or |
| 2217 | * wakeup all the threads pending on the supplied futex, and the behavior |
| 2218 | * depends on wake_all. |
| 2219 | * |
| 2220 | * @param futex Futex to wake up pending threads. |
| 2221 | * @param wake_all If true, wake up all pending threads; If false, |
| 2222 | * wakeup the highest priority thread. |
| 2223 | * @retval -EACCES Caller does not have access to the futex address. |
| 2224 | * @retval -EINVAL Futex parameter address not recognized by the kernel. |
| 2225 | * @retval Number of threads that were woken up. |
| 2226 | */ |
| 2227 | __syscall int k_futex_wake(struct k_futex *futex, bool wake_all); |
| 2228 | |
| 2229 | /** @} */ |
| 2230 | #endif |
| 2231 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2232 | struct k_fifo { |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2233 | struct k_queue _queue; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2234 | }; |
| 2235 | |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2236 | /** |
| 2237 | * @cond INTERNAL_HIDDEN |
| 2238 | */ |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 2239 | #define Z_FIFO_INITIALIZER(obj) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2240 | { \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2241 | ._queue = _K_QUEUE_INITIALIZER(obj._queue) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2242 | } |
| 2243 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 2244 | #define K_FIFO_INITIALIZER __DEPRECATED_MACRO Z_FIFO_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2245 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2246 | /** |
| 2247 | * INTERNAL_HIDDEN @endcond |
| 2248 | */ |
| 2249 | |
| 2250 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2251 | * @defgroup fifo_apis FIFO APIs |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2252 | * @ingroup kernel_apis |
| 2253 | * @{ |
| 2254 | */ |
| 2255 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2256 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2257 | * @brief Initialize a FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2258 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2259 | * This routine initializes a FIFO queue, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2260 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2261 | * @param fifo Address of the FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2262 | * |
| 2263 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2264 | * @req K-FIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2265 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2266 | #define k_fifo_init(fifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2267 | k_queue_init(&(fifo)->_queue) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2268 | |
| 2269 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2270 | * @brief Cancel waiting on a FIFO queue. |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 2271 | * |
| 2272 | * This routine causes first thread pending on @a fifo, if any, to |
| 2273 | * return from k_fifo_get() call with NULL value (as if timeout |
| 2274 | * expired). |
| 2275 | * |
| 2276 | * @note Can be called by ISRs. |
| 2277 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2278 | * @param fifo Address of the FIFO queue. |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 2279 | * |
| 2280 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2281 | * @req K-FIFO-001 |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 2282 | */ |
| 2283 | #define k_fifo_cancel_wait(fifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2284 | k_queue_cancel_wait(&(fifo)->_queue) |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 2285 | |
| 2286 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2287 | * @brief Add an element to a FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2288 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2289 | * 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] | 2290 | * aligned on a word boundary, and the first word of the item is reserved |
| 2291 | * for the kernel's use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2292 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2293 | * @note Can be called by ISRs. |
| 2294 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2295 | * @param fifo Address of the FIFO. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2296 | * @param data Address of the data item. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2297 | * |
| 2298 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2299 | * @req K-FIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2300 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2301 | #define k_fifo_put(fifo, data) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2302 | k_queue_append(&(fifo)->_queue, data) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2303 | |
| 2304 | /** |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2305 | * @brief Add an element to a FIFO queue. |
| 2306 | * |
Andrew Boie | ac3dcc1 | 2019-04-01 12:28:03 -0700 | [diff] [blame] | 2307 | * This routine adds a data item to @a fifo. There is an implicit memory |
| 2308 | * allocation to create an additional temporary bookkeeping data structure from |
| 2309 | * the calling thread's resource pool, which is automatically freed when the |
| 2310 | * item is removed. The data itself is not copied. |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2311 | * |
| 2312 | * @note Can be called by ISRs. |
| 2313 | * |
| 2314 | * @param fifo Address of the FIFO. |
| 2315 | * @param data Address of the data item. |
| 2316 | * |
| 2317 | * @retval 0 on success |
| 2318 | * @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] | 2319 | * @req K-FIFO-001 |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2320 | */ |
| 2321 | #define k_fifo_alloc_put(fifo, data) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2322 | k_queue_alloc_append(&(fifo)->_queue, data) |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2323 | |
| 2324 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2325 | * @brief Atomically add a list of elements to a FIFO. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2326 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2327 | * 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] | 2328 | * 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] | 2329 | * each data item pointing to the next data item; the list must be |
| 2330 | * NULL-terminated. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2331 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2332 | * @note Can be called by ISRs. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2333 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2334 | * @param fifo Address of the FIFO queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2335 | * @param head Pointer to first node in singly-linked list. |
| 2336 | * @param tail Pointer to last node in singly-linked list. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2337 | * |
| 2338 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2339 | * @req K-FIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2340 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2341 | #define k_fifo_put_list(fifo, head, tail) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2342 | k_queue_append_list(&(fifo)->_queue, head, tail) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2343 | |
| 2344 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2345 | * @brief Atomically add a list of elements to a FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2346 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2347 | * This routine adds a list of data items to @a fifo in one operation. |
| 2348 | * The data items must be in a singly-linked list implemented using a |
| 2349 | * 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] | 2350 | * and must be re-initialized via sys_slist_init(). |
| 2351 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2352 | * @note Can be called by ISRs. |
| 2353 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2354 | * @param fifo Address of the FIFO queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2355 | * @param list Pointer to sys_slist_t object. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2356 | * |
| 2357 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2358 | * @req K-FIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2359 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2360 | #define k_fifo_put_slist(fifo, list) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2361 | k_queue_merge_slist(&(fifo)->_queue, list) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2362 | |
| 2363 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2364 | * @brief Get an element from a FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2365 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2366 | * 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] | 2367 | * 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] | 2368 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2369 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 2370 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2371 | * @param fifo Address of the FIFO queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2372 | * @param timeout Waiting period to obtain a data item (in milliseconds), |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2373 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 2374 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2375 | * @return Address of the data item if successful; NULL if returned |
| 2376 | * without waiting, or waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2377 | * @req K-FIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2378 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2379 | #define k_fifo_get(fifo, timeout) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2380 | k_queue_get(&(fifo)->_queue, timeout) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2381 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2382 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2383 | * @brief Query a FIFO queue to see if it has data available. |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2384 | * |
| 2385 | * 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] | 2386 | * if other threads is also trying to read from the FIFO. |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2387 | * |
| 2388 | * @note Can be called by ISRs. |
| 2389 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2390 | * @param fifo Address of the FIFO queue. |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2391 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2392 | * @return Non-zero if the FIFO queue is empty. |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2393 | * @return 0 if data is available. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2394 | * @req K-FIFO-001 |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2395 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2396 | #define k_fifo_is_empty(fifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2397 | k_queue_is_empty(&(fifo)->_queue) |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2398 | |
| 2399 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2400 | * @brief Peek element at the head of a FIFO queue. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2401 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2402 | * 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] | 2403 | * 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] | 2404 | * on each iteration of processing, a head container will be peeked, |
| 2405 | * 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] | 2406 | * it will be completely remove from the FIFO queue. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2407 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2408 | * @param fifo Address of the FIFO queue. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2409 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2410 | * @return Head element, or NULL if the FIFO queue is empty. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2411 | * @req K-FIFO-001 |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2412 | */ |
| 2413 | #define k_fifo_peek_head(fifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2414 | k_queue_peek_head(&(fifo)->_queue) |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2415 | |
| 2416 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2417 | * @brief Peek element at the tail of FIFO queue. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2418 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2419 | * Return element from the tail of FIFO queue (without removing it). A usecase |
| 2420 | * for this is if elements of the FIFO queue are themselves containers. Then |
| 2421 | * 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] | 2422 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2423 | * @param fifo Address of the FIFO queue. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2424 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2425 | * @return Tail element, or NULL if a FIFO queue is empty. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2426 | * @req K-FIFO-001 |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2427 | */ |
| 2428 | #define k_fifo_peek_tail(fifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2429 | k_queue_peek_tail(&(fifo)->_queue) |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2430 | |
| 2431 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2432 | * @brief Statically define and initialize a FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2433 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2434 | * 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] | 2435 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 2436 | * @code extern struct k_fifo <name>; @endcode |
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 | * @param name Name of the FIFO queue. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2439 | * @req K-FIFO-002 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2440 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2441 | #define K_FIFO_DEFINE(name) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 2442 | Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \ |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 2443 | Z_FIFO_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2444 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 2445 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2446 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2447 | struct k_lifo { |
Luiz Augusto von Dentz | 0dc4dd4 | 2017-02-21 15:49:52 +0200 | [diff] [blame] | 2448 | struct k_queue _queue; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2449 | }; |
| 2450 | |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2451 | /** |
| 2452 | * @cond INTERNAL_HIDDEN |
| 2453 | */ |
| 2454 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2455 | #define _K_LIFO_INITIALIZER(obj) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2456 | { \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2457 | ._queue = _K_QUEUE_INITIALIZER(obj._queue) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2458 | } |
| 2459 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 2460 | #define K_LIFO_INITIALIZER __DEPRECATED_MACRO _K_LIFO_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2461 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2462 | /** |
| 2463 | * INTERNAL_HIDDEN @endcond |
| 2464 | */ |
| 2465 | |
| 2466 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2467 | * @defgroup lifo_apis LIFO APIs |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2468 | * @ingroup kernel_apis |
| 2469 | * @{ |
| 2470 | */ |
| 2471 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2472 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2473 | * @brief Initialize a LIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2474 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2475 | * This routine initializes a LIFO queue object, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2476 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2477 | * @param lifo Address of the LIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2478 | * |
| 2479 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2480 | * @req K-LIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2481 | */ |
Luiz Augusto von Dentz | 0dc4dd4 | 2017-02-21 15:49:52 +0200 | [diff] [blame] | 2482 | #define k_lifo_init(lifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2483 | k_queue_init(&(lifo)->_queue) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2484 | |
| 2485 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2486 | * @brief Add an element to a LIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2487 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2488 | * 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] | 2489 | * 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] | 2490 | * reserved for the kernel's use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2491 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2492 | * @note Can be called by ISRs. |
| 2493 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2494 | * @param lifo Address of the LIFO queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2495 | * @param data Address of the data item. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2496 | * |
| 2497 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2498 | * @req K-LIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2499 | */ |
Luiz Augusto von Dentz | 0dc4dd4 | 2017-02-21 15:49:52 +0200 | [diff] [blame] | 2500 | #define k_lifo_put(lifo, data) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2501 | k_queue_prepend(&(lifo)->_queue, data) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2502 | |
| 2503 | /** |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2504 | * @brief Add an element to a LIFO queue. |
| 2505 | * |
Andrew Boie | ac3dcc1 | 2019-04-01 12:28:03 -0700 | [diff] [blame] | 2506 | * This routine adds a data item to @a lifo. There is an implicit memory |
| 2507 | * allocation to create an additional temporary bookkeeping data structure from |
| 2508 | * the calling thread's resource pool, which is automatically freed when the |
| 2509 | * item is removed. The data itself is not copied. |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2510 | * |
| 2511 | * @note Can be called by ISRs. |
| 2512 | * |
| 2513 | * @param lifo Address of the LIFO. |
| 2514 | * @param data Address of the data item. |
| 2515 | * |
| 2516 | * @retval 0 on success |
| 2517 | * @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] | 2518 | * @req K-LIFO-001 |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2519 | */ |
| 2520 | #define k_lifo_alloc_put(lifo, data) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2521 | k_queue_alloc_prepend(&(lifo)->_queue, data) |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2522 | |
| 2523 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2524 | * @brief Get an element from a LIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2525 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2526 | * 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] | 2527 | * 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] | 2528 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2529 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 2530 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2531 | * @param lifo Address of the LIFO queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2532 | * @param timeout Waiting period to obtain a data item (in milliseconds), |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2533 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 2534 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2535 | * @return Address of the data item if successful; NULL if returned |
| 2536 | * without waiting, or waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2537 | * @req K-LIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2538 | */ |
Luiz Augusto von Dentz | 0dc4dd4 | 2017-02-21 15:49:52 +0200 | [diff] [blame] | 2539 | #define k_lifo_get(lifo, timeout) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2540 | k_queue_get(&(lifo)->_queue, timeout) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2541 | |
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 | * @brief Statically define and initialize a LIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2544 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2545 | * 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] | 2546 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 2547 | * @code extern struct k_lifo <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2548 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2549 | * @param name Name of the fifo. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2550 | * @req K-LIFO-002 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2551 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2552 | #define K_LIFO_DEFINE(name) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 2553 | Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2554 | _K_LIFO_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2555 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 2556 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2557 | |
| 2558 | /** |
| 2559 | * @cond INTERNAL_HIDDEN |
| 2560 | */ |
Adithya Baglody | 28080d3 | 2018-10-15 11:48:51 +0530 | [diff] [blame] | 2561 | #define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2562 | |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2563 | typedef uintptr_t stack_data_t; |
| 2564 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2565 | struct k_stack { |
| 2566 | _wait_q_t wait_q; |
Andy Ross | f0933d0 | 2018-07-26 10:23:02 -0700 | [diff] [blame] | 2567 | struct k_spinlock lock; |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2568 | stack_data_t *base, *next, *top; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2569 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 2570 | _OBJECT_TRACING_NEXT_PTR(k_stack) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 2571 | _OBJECT_TRACING_LINKED_FLAG |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2572 | u8_t flags; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2573 | }; |
| 2574 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2575 | #define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2576 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2577 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2578 | .base = stack_buffer, \ |
| 2579 | .next = stack_buffer, \ |
| 2580 | .top = stack_buffer + stack_num_entries, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 2581 | _OBJECT_TRACING_INIT \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2582 | } |
| 2583 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 2584 | #define K_STACK_INITIALIZER __DEPRECATED_MACRO _K_STACK_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2585 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2586 | /** |
| 2587 | * INTERNAL_HIDDEN @endcond |
| 2588 | */ |
| 2589 | |
| 2590 | /** |
| 2591 | * @defgroup stack_apis Stack APIs |
| 2592 | * @ingroup kernel_apis |
| 2593 | * @{ |
| 2594 | */ |
| 2595 | |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2596 | /** |
| 2597 | * @brief Initialize a stack. |
| 2598 | * |
| 2599 | * This routine initializes a stack object, prior to its first use. |
| 2600 | * |
| 2601 | * @param stack Address of the stack. |
| 2602 | * @param buffer Address of array used to hold stacked values. |
| 2603 | * @param num_entries Maximum number of values that can be stacked. |
| 2604 | * |
| 2605 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2606 | * @req K-STACK-001 |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2607 | */ |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2608 | void k_stack_init(struct k_stack *stack, |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2609 | stack_data_t *buffer, u32_t num_entries); |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2610 | |
| 2611 | |
| 2612 | /** |
| 2613 | * @brief Initialize a stack. |
| 2614 | * |
| 2615 | * This routine initializes a stack object, prior to its first use. Internal |
| 2616 | * buffers will be allocated from the calling thread's resource pool. |
| 2617 | * This memory will be released if k_stack_cleanup() is called, or |
| 2618 | * userspace is enabled and the stack object loses all references to it. |
| 2619 | * |
| 2620 | * @param stack Address of the stack. |
| 2621 | * @param num_entries Maximum number of values that can be stacked. |
| 2622 | * |
| 2623 | * @return -ENOMEM if memory couldn't be allocated |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2624 | * @req K-STACK-001 |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2625 | */ |
| 2626 | |
Adithya Baglody | 28080d3 | 2018-10-15 11:48:51 +0530 | [diff] [blame] | 2627 | __syscall s32_t k_stack_alloc_init(struct k_stack *stack, |
| 2628 | u32_t num_entries); |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2629 | |
| 2630 | /** |
| 2631 | * @brief Release a stack's allocated buffer |
| 2632 | * |
| 2633 | * If a stack object was given a dynamically allocated buffer via |
| 2634 | * k_stack_alloc_init(), this will free it. This function does nothing |
| 2635 | * if the buffer wasn't dynamically allocated. |
| 2636 | * |
| 2637 | * @param stack Address of the stack. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2638 | * @req K-STACK-001 |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2639 | */ |
| 2640 | void k_stack_cleanup(struct k_stack *stack); |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2641 | |
| 2642 | /** |
| 2643 | * @brief Push an element onto a stack. |
| 2644 | * |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2645 | * 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] | 2646 | * |
| 2647 | * @note Can be called by ISRs. |
| 2648 | * |
| 2649 | * @param stack Address of the stack. |
| 2650 | * @param data Value to push onto the stack. |
| 2651 | * |
| 2652 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2653 | * @req K-STACK-001 |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2654 | */ |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2655 | __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] | 2656 | |
| 2657 | /** |
| 2658 | * @brief Pop an element from a stack. |
| 2659 | * |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2660 | * This routine removes a stack_data_t value from @a stack in a "last in, |
| 2661 | * first out" manner and stores the value in @a data. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2662 | * |
| 2663 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 2664 | * |
| 2665 | * @param stack Address of the stack. |
| 2666 | * @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] | 2667 | * @param timeout Non-negative waiting period to obtain a value (in |
| 2668 | * milliseconds), or one of the special values K_NO_WAIT and |
| 2669 | * K_FOREVER. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2670 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2671 | * @retval 0 Element popped from stack. |
| 2672 | * @retval -EBUSY Returned without waiting. |
| 2673 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2674 | * @req K-STACK-001 |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2675 | */ |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 2676 | __syscall int k_stack_pop(struct k_stack *stack, stack_data_t *data, |
| 2677 | s32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2678 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2679 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2680 | * @brief Statically define and initialize a stack |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2681 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2682 | * 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] | 2683 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 2684 | * @code extern struct k_stack <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2685 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2686 | * @param name Name of the stack. |
| 2687 | * @param stack_num_entries Maximum number of values that can be stacked. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2688 | * @req K-STACK-002 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2689 | */ |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 2690 | #define K_STACK_DEFINE(name, stack_num_entries) \ |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2691 | stack_data_t __noinit \ |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 2692 | _k_stack_buf_##name[stack_num_entries]; \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 2693 | Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2694 | _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \ |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 2695 | stack_num_entries) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2696 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 2697 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2698 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2699 | struct k_work; |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 2700 | struct k_work_poll; |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2701 | |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 2702 | /* private, used by k_poll and k_work_poll */ |
Piotr Zięcik | 1c4177d | 2019-08-27 12:19:26 +0200 | [diff] [blame] | 2703 | typedef int (*_poller_cb_t)(struct k_poll_event *event, u32_t state); |
| 2704 | struct _poller { |
| 2705 | volatile bool is_polling; |
| 2706 | struct k_thread *thread; |
| 2707 | _poller_cb_t cb; |
| 2708 | }; |
| 2709 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2710 | /** |
Anas Nashif | 29f37f0 | 2019-01-21 14:30:35 -0500 | [diff] [blame] | 2711 | * @addtogroup thread_apis |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2712 | * @{ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2713 | */ |
| 2714 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2715 | /** |
| 2716 | * @typedef k_work_handler_t |
| 2717 | * @brief Work item handler function type. |
| 2718 | * |
| 2719 | * A work item's handler function is executed by a workqueue's thread |
| 2720 | * when the work item is processed by the workqueue. |
| 2721 | * |
| 2722 | * @param work Address of the work item. |
| 2723 | * |
| 2724 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2725 | * @req K-WORK-001 |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2726 | */ |
| 2727 | typedef void (*k_work_handler_t)(struct k_work *work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2728 | |
| 2729 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2730 | * @cond INTERNAL_HIDDEN |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2731 | */ |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2732 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2733 | struct k_work_q { |
Luiz Augusto von Dentz | adb581b | 2017-07-03 19:09:44 +0300 | [diff] [blame] | 2734 | struct k_queue queue; |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 2735 | struct k_thread thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2736 | }; |
| 2737 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2738 | enum { |
Iván Briano | 9c7b5ea | 2016-10-04 18:11:05 -0300 | [diff] [blame] | 2739 | K_WORK_STATE_PENDING, /* Work item pending state */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2740 | }; |
| 2741 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2742 | struct k_work { |
Luiz Augusto von Dentz | adb581b | 2017-07-03 19:09:44 +0300 | [diff] [blame] | 2743 | void *_reserved; /* Used by k_queue implementation. */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2744 | k_work_handler_t handler; |
| 2745 | atomic_t flags[1]; |
| 2746 | }; |
| 2747 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2748 | struct k_delayed_work { |
| 2749 | struct k_work work; |
| 2750 | struct _timeout timeout; |
| 2751 | struct k_work_q *work_q; |
| 2752 | }; |
| 2753 | |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 2754 | struct k_work_poll { |
| 2755 | struct k_work work; |
| 2756 | struct _poller poller; |
| 2757 | struct k_poll_event *events; |
| 2758 | int num_events; |
| 2759 | k_work_handler_t real_handler; |
| 2760 | struct _timeout timeout; |
| 2761 | int poll_result; |
| 2762 | }; |
| 2763 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2764 | extern struct k_work_q k_sys_work_q; |
| 2765 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2766 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2767 | * INTERNAL_HIDDEN @endcond |
| 2768 | */ |
| 2769 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2770 | #define Z_WORK_INITIALIZER(work_handler) \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2771 | { \ |
| 2772 | ._reserved = NULL, \ |
| 2773 | .handler = work_handler, \ |
| 2774 | .flags = { 0 } \ |
| 2775 | } |
| 2776 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 2777 | #define K_WORK_INITIALIZER __DEPRECATED_MACRO Z_WORK_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2778 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2779 | /** |
| 2780 | * @brief Initialize a statically-defined work item. |
| 2781 | * |
| 2782 | * This macro can be used to initialize a statically-defined workqueue work |
| 2783 | * item, prior to its first use. For example, |
| 2784 | * |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2785 | * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2786 | * |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2787 | * @param work Symbol name for work item object |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2788 | * @param work_handler Function to invoke each time work item is processed. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2789 | * @req K-WORK-002 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2790 | */ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2791 | #define K_WORK_DEFINE(work, work_handler) \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2792 | struct k_work work = Z_WORK_INITIALIZER(work_handler) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2793 | |
| 2794 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2795 | * @brief Initialize a work item. |
| 2796 | * |
| 2797 | * This routine initializes a workqueue work item, prior to its first use. |
| 2798 | * |
| 2799 | * @param work Address of work item. |
| 2800 | * @param handler Function to invoke each time work item is processed. |
| 2801 | * |
| 2802 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2803 | * @req K-WORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2804 | */ |
| 2805 | static inline void k_work_init(struct k_work *work, k_work_handler_t handler) |
| 2806 | { |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2807 | *work = (struct k_work)Z_WORK_INITIALIZER(handler); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2808 | } |
| 2809 | |
| 2810 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2811 | * @brief Submit a work item. |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 2812 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2813 | * This routine submits work item @a work to be processed by workqueue |
| 2814 | * @a work_q. If the work item is already pending in the workqueue's queue |
| 2815 | * as a result of an earlier submission, this routine has no effect on the |
| 2816 | * work item. If the work item has already been processed, or is currently |
| 2817 | * being processed, its work is considered complete and the work item can be |
| 2818 | * resubmitted. |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 2819 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2820 | * @warning |
| 2821 | * A submitted work item must not be modified until it has been processed |
| 2822 | * by the workqueue. |
| 2823 | * |
| 2824 | * @note Can be called by ISRs. |
| 2825 | * |
| 2826 | * @param work_q Address of workqueue. |
| 2827 | * @param work Address of work item. |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 2828 | * |
| 2829 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2830 | * @req K-WORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2831 | */ |
| 2832 | static inline void k_work_submit_to_queue(struct k_work_q *work_q, |
| 2833 | struct k_work *work) |
| 2834 | { |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 2835 | 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] | 2836 | k_queue_append(&work_q->queue, work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2837 | } |
| 2838 | } |
| 2839 | |
| 2840 | /** |
Andrew Boie | 2b1d54e | 2018-11-12 14:25:19 -0800 | [diff] [blame] | 2841 | * @brief Submit a work item to a user mode workqueue |
| 2842 | * |
David B. Kinder | 06d7835 | 2018-12-17 14:32:40 -0800 | [diff] [blame] | 2843 | * 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] | 2844 | * memory allocation is made from the caller's resource pool which is freed |
| 2845 | * once the worker thread consumes the k_work item. The workqueue |
| 2846 | * thread must have memory access to the k_work item being submitted. The caller |
| 2847 | * must have permission granted on the work_q parameter's queue object. |
| 2848 | * |
| 2849 | * Otherwise this works the same as k_work_submit_to_queue(). |
| 2850 | * |
| 2851 | * @note Can be called by ISRs. |
| 2852 | * |
| 2853 | * @param work_q Address of workqueue. |
| 2854 | * @param work Address of work item. |
| 2855 | * |
| 2856 | * @retval -EBUSY if the work item was already in some workqueue |
| 2857 | * @retval -ENOMEM if no memory for thread resource pool allocation |
| 2858 | * @retval 0 Success |
| 2859 | * @req K-WORK-001 |
| 2860 | */ |
| 2861 | static inline int k_work_submit_to_user_queue(struct k_work_q *work_q, |
| 2862 | struct k_work *work) |
| 2863 | { |
| 2864 | int ret = -EBUSY; |
| 2865 | |
| 2866 | if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) { |
| 2867 | ret = k_queue_alloc_append(&work_q->queue, work); |
| 2868 | |
| 2869 | /* Couldn't insert into the queue. Clear the pending bit |
| 2870 | * so the work item can be submitted again |
| 2871 | */ |
Flavio Ceolin | 76b3518 | 2018-12-16 12:48:29 -0800 | [diff] [blame] | 2872 | if (ret != 0) { |
Andrew Boie | 2b1d54e | 2018-11-12 14:25:19 -0800 | [diff] [blame] | 2873 | atomic_clear_bit(work->flags, K_WORK_STATE_PENDING); |
| 2874 | } |
| 2875 | } |
| 2876 | |
| 2877 | return ret; |
| 2878 | } |
| 2879 | |
| 2880 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2881 | * @brief Check if a work item is pending. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2882 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2883 | * This routine indicates if work item @a work is pending in a workqueue's |
| 2884 | * queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2885 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2886 | * @note Can be called by ISRs. |
| 2887 | * |
| 2888 | * @param work Address of work item. |
| 2889 | * |
Flavio Ceolin | 82ef4f8 | 2018-11-21 18:12:34 -0800 | [diff] [blame] | 2890 | * @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] | 2891 | * @req K-WORK-001 |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 2892 | */ |
Flavio Ceolin | 82ef4f8 | 2018-11-21 18:12:34 -0800 | [diff] [blame] | 2893 | static inline bool k_work_pending(struct k_work *work) |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 2894 | { |
Iván Briano | 9c7b5ea | 2016-10-04 18:11:05 -0300 | [diff] [blame] | 2895 | return atomic_test_bit(work->flags, K_WORK_STATE_PENDING); |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 2896 | } |
| 2897 | |
| 2898 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2899 | * @brief Start a workqueue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2900 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2901 | * This routine starts workqueue @a work_q. The workqueue spawns its work |
| 2902 | * processing thread, which runs forever. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2903 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2904 | * @param work_q Address of workqueue. |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 2905 | * @param stack Pointer to work queue thread's stack space, as defined by |
| 2906 | * K_THREAD_STACK_DEFINE() |
| 2907 | * @param stack_size Size of the work queue thread's stack (in bytes), which |
| 2908 | * should either be the same constant passed to |
| 2909 | * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF(). |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2910 | * @param prio Priority of the work queue's thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2911 | * |
| 2912 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2913 | * @req K-WORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2914 | */ |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 2915 | extern void k_work_q_start(struct k_work_q *work_q, |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 2916 | k_thread_stack_t *stack, |
Benjamin Walsh | 669360d | 2016-11-14 16:46:14 -0500 | [diff] [blame] | 2917 | size_t stack_size, int prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2918 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2919 | /** |
Andrew Boie | 2b1d54e | 2018-11-12 14:25:19 -0800 | [diff] [blame] | 2920 | * @brief Start a workqueue in user mode |
| 2921 | * |
| 2922 | * This works identically to k_work_q_start() except it is callable from user |
| 2923 | * mode, and the worker thread created will run in user mode. |
| 2924 | * The caller must have permissions granted on both the work_q parameter's |
| 2925 | * thread and queue objects, and the same restrictions on priority apply as |
| 2926 | * k_thread_create(). |
| 2927 | * |
| 2928 | * @param work_q Address of workqueue. |
| 2929 | * @param stack Pointer to work queue thread's stack space, as defined by |
| 2930 | * K_THREAD_STACK_DEFINE() |
| 2931 | * @param stack_size Size of the work queue thread's stack (in bytes), which |
| 2932 | * should either be the same constant passed to |
| 2933 | * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF(). |
| 2934 | * @param prio Priority of the work queue's thread. |
| 2935 | * |
| 2936 | * @return N/A |
| 2937 | * @req K-WORK-001 |
| 2938 | */ |
| 2939 | extern void k_work_q_user_start(struct k_work_q *work_q, |
| 2940 | k_thread_stack_t *stack, |
| 2941 | size_t stack_size, int prio); |
| 2942 | |
| 2943 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2944 | * @brief Initialize a delayed work item. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2945 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2946 | * This routine initializes a workqueue delayed work item, prior to |
| 2947 | * its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2948 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2949 | * @param work Address of delayed work item. |
| 2950 | * @param handler Function to invoke each time work item is processed. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2951 | * |
| 2952 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2953 | * @req K-DWORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2954 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 2955 | extern void k_delayed_work_init(struct k_delayed_work *work, |
| 2956 | k_work_handler_t handler); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2957 | |
| 2958 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2959 | * @brief Submit a delayed work item. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2960 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2961 | * This routine schedules work item @a work to be processed by workqueue |
| 2962 | * @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] | 2963 | * 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] | 2964 | * Only when the countdown completes is the work item actually submitted to |
| 2965 | * the workqueue and becomes pending. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2966 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2967 | * Submitting a previously submitted delayed work item that is still |
Andy Ross | 03c1d28 | 2018-02-13 12:13:25 -0800 | [diff] [blame] | 2968 | * counting down cancels the existing submission and restarts the |
| 2969 | * countdown using the new delay. Note that this behavior is |
| 2970 | * inherently subject to race conditions with the pre-existing |
| 2971 | * timeouts and work queue, so care must be taken to synchronize such |
| 2972 | * resubmissions externally. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2973 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2974 | * @warning |
| 2975 | * A delayed work item must not be modified until it has been processed |
| 2976 | * by the workqueue. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2977 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2978 | * @note Can be called by ISRs. |
| 2979 | * |
| 2980 | * @param work_q Address of workqueue. |
| 2981 | * @param work Address of delayed work item. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 2982 | * @param delay Non-negative delay before submitting the work item (in |
| 2983 | * milliseconds). |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2984 | * |
| 2985 | * @retval 0 Work item countdown started. |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2986 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 2987 | * @retval -EADDRINUSE Work item is pending on a different workqueue. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2988 | * @req K-DWORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2989 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 2990 | extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q, |
| 2991 | struct k_delayed_work *work, |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 2992 | s32_t delay); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2993 | |
| 2994 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2995 | * @brief Cancel a delayed work item. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2996 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2997 | * This routine cancels the submission of delayed work item @a work. |
David B. Kinder | 8b986d7 | 2017-04-18 15:56:26 -0700 | [diff] [blame] | 2998 | * 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] | 2999 | * underway. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3000 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3001 | * @note Can be called by ISRs. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3002 | * |
Andy Ross | d7ae2a8 | 2019-03-08 08:51:13 -0800 | [diff] [blame] | 3003 | * @note The result of calling this on a k_delayed_work item that has |
| 3004 | * not been submitted (i.e. before the return of the |
| 3005 | * k_delayed_work_submit_to_queue() call) is undefined. |
| 3006 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3007 | * @param work Address of delayed work item. |
| 3008 | * |
David B. Kinder | 8b986d7 | 2017-04-18 15:56:26 -0700 | [diff] [blame] | 3009 | * @retval 0 Work item countdown canceled. |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3010 | * @retval -EINVAL Work item is being processed or has completed its work. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3011 | * @req K-DWORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3012 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 3013 | extern int k_delayed_work_cancel(struct k_delayed_work *work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3014 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3015 | /** |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3016 | * @brief Submit a work item to the system workqueue. |
| 3017 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3018 | * This routine submits work item @a work to be processed by the system |
| 3019 | * workqueue. If the work item is already pending in the workqueue's queue |
| 3020 | * as a result of an earlier submission, this routine has no effect on the |
| 3021 | * work item. If the work item has already been processed, or is currently |
| 3022 | * being processed, its work is considered complete and the work item can be |
| 3023 | * resubmitted. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3024 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3025 | * @warning |
| 3026 | * Work items submitted to the system workqueue should avoid using handlers |
| 3027 | * that block or yield since this may prevent the system workqueue from |
| 3028 | * processing other work items in a timely manner. |
| 3029 | * |
| 3030 | * @note Can be called by ISRs. |
| 3031 | * |
| 3032 | * @param work Address of work item. |
| 3033 | * |
| 3034 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3035 | * @req K-WORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3036 | */ |
| 3037 | static inline void k_work_submit(struct k_work *work) |
| 3038 | { |
| 3039 | k_work_submit_to_queue(&k_sys_work_q, work); |
| 3040 | } |
| 3041 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3042 | /** |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3043 | * @brief Submit a delayed work item to the system workqueue. |
| 3044 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3045 | * This routine schedules work item @a work to be processed by the system |
| 3046 | * workqueue after a delay of @a delay milliseconds. The routine initiates |
David B. Kinder | 8b986d7 | 2017-04-18 15:56:26 -0700 | [diff] [blame] | 3047 | * 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] | 3048 | * Only when the countdown completes is the work item actually submitted to |
| 3049 | * the workqueue and becomes pending. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3050 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3051 | * Submitting a previously submitted delayed work item that is still |
| 3052 | * counting down cancels the existing submission and restarts the countdown |
| 3053 | * using the new delay. If the work item is currently pending on the |
| 3054 | * workqueue's queue because the countdown has completed it is too late to |
| 3055 | * resubmit the item, and resubmission fails without impacting the work item. |
| 3056 | * If the work item has already been processed, or is currently being processed, |
| 3057 | * its work is considered complete and the work item can be resubmitted. |
| 3058 | * |
| 3059 | * @warning |
| 3060 | * Work items submitted to the system workqueue should avoid using handlers |
| 3061 | * that block or yield since this may prevent the system workqueue from |
| 3062 | * processing other work items in a timely manner. |
| 3063 | * |
| 3064 | * @note Can be called by ISRs. |
| 3065 | * |
| 3066 | * @param work Address of delayed work item. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3067 | * @param delay Non-negative delay before submitting the work item (in |
| 3068 | * milliseconds). |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3069 | * |
| 3070 | * @retval 0 Work item countdown started. |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3071 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 3072 | * @retval -EADDRINUSE Work item is pending on a different workqueue. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3073 | * @req K-DWORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3074 | */ |
| 3075 | static inline int k_delayed_work_submit(struct k_delayed_work *work, |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3076 | s32_t delay) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3077 | { |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 3078 | 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] | 3079 | } |
| 3080 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3081 | /** |
Johan Hedberg | c8201b2 | 2016-12-09 10:42:22 +0200 | [diff] [blame] | 3082 | * @brief Get time remaining before a delayed work gets scheduled. |
| 3083 | * |
| 3084 | * This routine computes the (approximate) time remaining before a |
| 3085 | * 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] | 3086 | * scheduled, it returns zero. |
Johan Hedberg | c8201b2 | 2016-12-09 10:42:22 +0200 | [diff] [blame] | 3087 | * |
| 3088 | * @param work Delayed work item. |
| 3089 | * |
| 3090 | * @return Remaining time (in milliseconds). |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3091 | * @req K-DWORK-001 |
Johan Hedberg | c8201b2 | 2016-12-09 10:42:22 +0200 | [diff] [blame] | 3092 | */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3093 | 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] | 3094 | { |
Andy Ross | 8892406 | 2019-10-03 11:43:10 -0700 | [diff] [blame] | 3095 | return k_ticks_to_ms_floor64(z_timeout_remaining(&work->timeout)); |
Johan Hedberg | c8201b2 | 2016-12-09 10:42:22 +0200 | [diff] [blame] | 3096 | } |
| 3097 | |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 3098 | /** |
| 3099 | * @brief Initialize a triggered work item. |
| 3100 | * |
| 3101 | * This routine initializes a workqueue triggered work item, prior to |
| 3102 | * its first use. |
| 3103 | * |
| 3104 | * @param work Address of triggered work item. |
| 3105 | * @param handler Function to invoke each time work item is processed. |
| 3106 | * |
| 3107 | * @return N/A |
| 3108 | */ |
| 3109 | extern void k_work_poll_init(struct k_work_poll *work, |
| 3110 | k_work_handler_t handler); |
| 3111 | |
| 3112 | /** |
| 3113 | * @brief Submit a triggered work item. |
| 3114 | * |
| 3115 | * This routine schedules work item @a work to be processed by workqueue |
| 3116 | * @a work_q when one of the given @a events is signaled. The routine |
| 3117 | * initiates internal poller for the work item and then returns to the caller. |
| 3118 | * Only when one of the watched events happen the work item is actually |
| 3119 | * submitted to the workqueue and becomes pending. |
| 3120 | * |
| 3121 | * Submitting a previously submitted triggered work item that is still |
| 3122 | * waiting for the event cancels the existing submission and reschedules it |
| 3123 | * 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] | 3124 | * 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] | 3125 | * so care must be taken to synchronize such resubmissions externally. |
| 3126 | * |
| 3127 | * @note Can be called by ISRs. |
| 3128 | * |
| 3129 | * @warning |
| 3130 | * Provided array of events as well as a triggered work item must be placed |
| 3131 | * in persistent memory (valid until work handler execution or work |
| 3132 | * cancellation) and cannot be modified after submission. |
| 3133 | * |
| 3134 | * @param work_q Address of workqueue. |
| 3135 | * @param work Address of delayed work item. |
| 3136 | * @param events An array of pointers to events which trigger the work. |
| 3137 | * @param num_events The number of events in the array. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3138 | * @param timeout Non-negative timeout after which the work will be scheduled |
| 3139 | * for execution even if not triggered. |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 3140 | * |
| 3141 | * |
| 3142 | * @retval 0 Work item started watching for events. |
| 3143 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 3144 | * @retval -EADDRINUSE Work item is pending on a different workqueue. |
| 3145 | */ |
| 3146 | extern int k_work_poll_submit_to_queue(struct k_work_q *work_q, |
| 3147 | struct k_work_poll *work, |
| 3148 | struct k_poll_event *events, |
| 3149 | int num_events, |
| 3150 | s32_t timeout); |
| 3151 | |
| 3152 | /** |
| 3153 | * @brief Submit a triggered work item to the system workqueue. |
| 3154 | * |
| 3155 | * This routine schedules work item @a work to be processed by system |
| 3156 | * workqueue when one of the given @a events is signaled. The routine |
| 3157 | * initiates internal poller for the work item and then returns to the caller. |
| 3158 | * Only when one of the watched events happen the work item is actually |
| 3159 | * submitted to the workqueue and becomes pending. |
| 3160 | * |
| 3161 | * Submitting a previously submitted triggered work item that is still |
| 3162 | * waiting for the event cancels the existing submission and reschedules it |
| 3163 | * 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] | 3164 | * 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] | 3165 | * so care must be taken to synchronize such resubmissions externally. |
| 3166 | * |
| 3167 | * @note Can be called by ISRs. |
| 3168 | * |
| 3169 | * @warning |
| 3170 | * Provided array of events as well as a triggered work item must not be |
| 3171 | * modified until the item has been processed by the workqueue. |
| 3172 | * |
| 3173 | * @param work Address of delayed work item. |
| 3174 | * @param events An array of pointers to events which trigger the work. |
| 3175 | * @param num_events The number of events in the array. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3176 | * @param timeout Non-negative timeout after which the work will be scheduled |
| 3177 | * for execution even if not triggered. |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 3178 | * |
| 3179 | * @retval 0 Work item started watching for events. |
| 3180 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 3181 | * @retval -EADDRINUSE Work item is pending on a different workqueue. |
| 3182 | */ |
| 3183 | static inline int k_work_poll_submit(struct k_work_poll *work, |
| 3184 | struct k_poll_event *events, |
| 3185 | int num_events, |
| 3186 | s32_t timeout) |
| 3187 | { |
| 3188 | return k_work_poll_submit_to_queue(&k_sys_work_q, work, |
| 3189 | events, num_events, timeout); |
| 3190 | } |
| 3191 | |
| 3192 | /** |
| 3193 | * @brief Cancel a triggered work item. |
| 3194 | * |
| 3195 | * This routine cancels the submission of triggered work item @a work. |
| 3196 | * A triggered work item can only be canceled if no event triggered work |
| 3197 | * submission. |
| 3198 | * |
| 3199 | * @note Can be called by ISRs. |
| 3200 | * |
| 3201 | * @param work Address of delayed work item. |
| 3202 | * |
David B. Kinder | 73896c0 | 2019-10-28 16:27:57 -0700 | [diff] [blame] | 3203 | * @retval 0 Work item canceled. |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 3204 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 3205 | */ |
| 3206 | extern int k_work_poll_cancel(struct k_work_poll *work); |
| 3207 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 3208 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3209 | /** |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3210 | * @defgroup mutex_apis Mutex APIs |
| 3211 | * @ingroup kernel_apis |
| 3212 | * @{ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3213 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3214 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3215 | /** |
| 3216 | * Mutex Structure |
| 3217 | * @ingroup mutex_apis |
| 3218 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3219 | struct k_mutex { |
| 3220 | _wait_q_t wait_q; |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3221 | /** Mutex owner */ |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 3222 | struct k_thread *owner; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3223 | u32_t lock_count; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3224 | int owner_orig_prio; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3225 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 3226 | _OBJECT_TRACING_NEXT_PTR(k_mutex) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 3227 | _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3228 | }; |
| 3229 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3230 | /** |
| 3231 | * @cond INTERNAL_HIDDEN |
| 3232 | */ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3233 | #define _K_MUTEX_INITIALIZER(obj) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3234 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3235 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3236 | .owner = NULL, \ |
| 3237 | .lock_count = 0, \ |
| 3238 | .owner_orig_prio = K_LOWEST_THREAD_PRIO, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 3239 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3240 | } |
| 3241 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 3242 | #define K_MUTEX_INITIALIZER __DEPRECATED_MACRO _K_MUTEX_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3243 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3244 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3245 | * INTERNAL_HIDDEN @endcond |
| 3246 | */ |
| 3247 | |
| 3248 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3249 | * @brief Statically define and initialize a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3250 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3251 | * 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] | 3252 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 3253 | * @code extern struct k_mutex <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3254 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3255 | * @param name Name of the mutex. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3256 | * @req K-MUTEX-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3257 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3258 | #define K_MUTEX_DEFINE(name) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 3259 | Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3260 | _K_MUTEX_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3261 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3262 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3263 | * @brief Initialize a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3264 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3265 | * This routine initializes a mutex object, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3266 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3267 | * Upon completion, the mutex is available and does not have an owner. |
| 3268 | * |
| 3269 | * @param mutex Address of the mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3270 | * |
| 3271 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3272 | * @req K-MUTEX-002 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3273 | */ |
Andrew Boie | 2f7519b | 2017-09-29 03:33:06 -0700 | [diff] [blame] | 3274 | __syscall void k_mutex_init(struct k_mutex *mutex); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3275 | |
| 3276 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3277 | * @brief Lock a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3278 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3279 | * This routine locks @a mutex. If the mutex is locked by another thread, |
| 3280 | * the calling thread waits until the mutex becomes available or until |
| 3281 | * a timeout occurs. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3282 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3283 | * A thread is permitted to lock a mutex it has already locked. The operation |
| 3284 | * completes immediately and the lock count is increased by 1. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3285 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3286 | * @param mutex Address of the mutex. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3287 | * @param timeout Non-negative waiting period to lock the mutex (in |
| 3288 | * milliseconds), or one of the special values K_NO_WAIT and |
| 3289 | * K_FOREVER. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3290 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3291 | * @retval 0 Mutex locked. |
| 3292 | * @retval -EBUSY Returned without waiting. |
| 3293 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3294 | * @req K-MUTEX-002 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3295 | */ |
Andrew Boie | 2f7519b | 2017-09-29 03:33:06 -0700 | [diff] [blame] | 3296 | __syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3297 | |
| 3298 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3299 | * @brief Unlock a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3300 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3301 | * This routine unlocks @a mutex. The mutex must already be locked by the |
| 3302 | * calling thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3303 | * |
| 3304 | * 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] | 3305 | * 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] | 3306 | * thread. |
| 3307 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3308 | * @param mutex Address of the mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3309 | * |
| 3310 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3311 | * @req K-MUTEX-002 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3312 | */ |
Andrew Boie | 2f7519b | 2017-09-29 03:33:06 -0700 | [diff] [blame] | 3313 | __syscall void k_mutex_unlock(struct k_mutex *mutex); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3314 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3315 | /** |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 3316 | * @} |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3317 | */ |
| 3318 | |
| 3319 | /** |
| 3320 | * @cond INTERNAL_HIDDEN |
| 3321 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3322 | |
| 3323 | struct k_sem { |
| 3324 | _wait_q_t wait_q; |
Adithya Baglody | 4b06621 | 2018-10-16 11:59:12 +0530 | [diff] [blame] | 3325 | u32_t count; |
| 3326 | u32_t limit; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 3327 | _POLL_EVENT; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3328 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 3329 | _OBJECT_TRACING_NEXT_PTR(k_sem) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 3330 | _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3331 | }; |
| 3332 | |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 3333 | #define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3334 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3335 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3336 | .count = initial_count, \ |
| 3337 | .limit = count_limit, \ |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 3338 | _POLL_EVENT_OBJ_INIT(obj) \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 3339 | _OBJECT_TRACING_INIT \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3340 | } |
| 3341 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 3342 | #define K_SEM_INITIALIZER __DEPRECATED_MACRO Z_SEM_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3343 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3344 | /** |
| 3345 | * INTERNAL_HIDDEN @endcond |
| 3346 | */ |
| 3347 | |
| 3348 | /** |
| 3349 | * @defgroup semaphore_apis Semaphore APIs |
| 3350 | * @ingroup kernel_apis |
| 3351 | * @{ |
| 3352 | */ |
| 3353 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3354 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3355 | * @brief Initialize a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3356 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3357 | * This routine initializes a semaphore object, prior to its first use. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3358 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3359 | * @param sem Address of the semaphore. |
| 3360 | * @param initial_count Initial semaphore count. |
| 3361 | * @param limit Maximum permitted semaphore count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3362 | * |
| 3363 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3364 | * @req K-SEM-001 |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3365 | */ |
Andrew Boie | 9928023 | 2017-09-29 14:17:47 -0700 | [diff] [blame] | 3366 | __syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count, |
| 3367 | unsigned int limit); |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3368 | |
| 3369 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3370 | * @brief Take a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3371 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3372 | * This routine takes @a sem. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3373 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3374 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 3375 | * |
| 3376 | * @param sem Address of the semaphore. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3377 | * @param timeout Non-negative waiting period to take the semaphore (in |
| 3378 | * milliseconds), or one of the special values K_NO_WAIT and |
| 3379 | * K_FOREVER. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3380 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3381 | * @retval 0 Semaphore taken. |
| 3382 | * @retval -EBUSY Returned without waiting. |
| 3383 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3384 | * @req K-SEM-001 |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3385 | */ |
Andrew Boie | 9928023 | 2017-09-29 14:17:47 -0700 | [diff] [blame] | 3386 | __syscall int k_sem_take(struct k_sem *sem, s32_t timeout); |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3387 | |
| 3388 | /** |
| 3389 | * @brief Give a semaphore. |
| 3390 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3391 | * This routine gives @a sem, unless the semaphore is already at its maximum |
| 3392 | * permitted count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3393 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3394 | * @note Can be called by ISRs. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3395 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3396 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3397 | * |
| 3398 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3399 | * @req K-SEM-001 |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3400 | */ |
Andrew Boie | 9928023 | 2017-09-29 14:17:47 -0700 | [diff] [blame] | 3401 | __syscall void k_sem_give(struct k_sem *sem); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3402 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3403 | /** |
| 3404 | * @brief Reset a semaphore's count to zero. |
| 3405 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3406 | * This routine sets the count of @a sem to zero. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3407 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3408 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3409 | * |
| 3410 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3411 | * @req K-SEM-001 |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3412 | */ |
Andrew Boie | 990bf16 | 2017-10-03 12:36:49 -0700 | [diff] [blame] | 3413 | __syscall void k_sem_reset(struct k_sem *sem); |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 3414 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 3415 | /** |
| 3416 | * @internal |
| 3417 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3418 | static inline void z_impl_k_sem_reset(struct k_sem *sem) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3419 | { |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 3420 | sem->count = 0U; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3421 | } |
| 3422 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3423 | /** |
| 3424 | * @brief Get a semaphore's count. |
| 3425 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3426 | * This routine returns the current count of @a sem. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3427 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3428 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3429 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3430 | * @return Current semaphore count. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3431 | * @req K-SEM-001 |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3432 | */ |
Andrew Boie | 990bf16 | 2017-10-03 12:36:49 -0700 | [diff] [blame] | 3433 | __syscall unsigned int k_sem_count_get(struct k_sem *sem); |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 3434 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 3435 | /** |
| 3436 | * @internal |
| 3437 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3438 | 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] | 3439 | { |
| 3440 | return sem->count; |
| 3441 | } |
| 3442 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3443 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3444 | * @brief Statically define and initialize a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3445 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3446 | * 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] | 3447 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 3448 | * @code extern struct k_sem <name>; @endcode |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3449 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3450 | * @param name Name of the semaphore. |
| 3451 | * @param initial_count Initial semaphore count. |
| 3452 | * @param count_limit Maximum permitted semaphore count. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3453 | * @req K-SEM-002 |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3454 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3455 | #define K_SEM_DEFINE(name, initial_count, count_limit) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 3456 | Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \ |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 3457 | Z_SEM_INITIALIZER(name, initial_count, count_limit); \ |
Rajavardhan Gundi | 68040c8 | 2018-04-27 10:15:15 +0530 | [diff] [blame] | 3458 | BUILD_ASSERT(((count_limit) != 0) && \ |
| 3459 | ((initial_count) <= (count_limit))); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3460 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 3461 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3462 | |
| 3463 | /** |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3464 | * @defgroup msgq_apis Message Queue APIs |
| 3465 | * @ingroup kernel_apis |
| 3466 | * @{ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3467 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3468 | |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3469 | /** |
| 3470 | * @brief Message Queue Structure |
| 3471 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3472 | struct k_msgq { |
| 3473 | _wait_q_t wait_q; |
Andy Ross | be03dbd | 2018-07-26 10:23:02 -0700 | [diff] [blame] | 3474 | struct k_spinlock lock; |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 3475 | size_t msg_size; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3476 | u32_t max_msgs; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3477 | char *buffer_start; |
| 3478 | char *buffer_end; |
| 3479 | char *read_ptr; |
| 3480 | char *write_ptr; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3481 | u32_t used_msgs; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3482 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 3483 | _OBJECT_TRACING_NEXT_PTR(k_msgq) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 3484 | _OBJECT_TRACING_LINKED_FLAG |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 3485 | u8_t flags; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3486 | }; |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3487 | /** |
| 3488 | * @cond INTERNAL_HIDDEN |
| 3489 | */ |
| 3490 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3491 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3492 | #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] | 3493 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3494 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3495 | .msg_size = q_msg_size, \ |
Charles E. Youse | 6d01f67 | 2019-03-18 10:27:34 -0700 | [diff] [blame] | 3496 | .max_msgs = q_max_msgs, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3497 | .buffer_start = q_buffer, \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3498 | .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3499 | .read_ptr = q_buffer, \ |
| 3500 | .write_ptr = q_buffer, \ |
| 3501 | .used_msgs = 0, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 3502 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3503 | } |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 3504 | #define K_MSGQ_INITIALIZER __DEPRECATED_MACRO _K_MSGQ_INITIALIZER |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3505 | /** |
| 3506 | * INTERNAL_HIDDEN @endcond |
| 3507 | */ |
| 3508 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3509 | |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 3510 | #define K_MSGQ_FLAG_ALLOC BIT(0) |
| 3511 | |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3512 | /** |
| 3513 | * @brief Message Queue Attributes |
| 3514 | */ |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 3515 | struct k_msgq_attrs { |
| 3516 | size_t msg_size; |
| 3517 | u32_t max_msgs; |
| 3518 | u32_t used_msgs; |
| 3519 | }; |
| 3520 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3521 | |
| 3522 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3523 | * @brief Statically define and initialize a message queue. |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3524 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3525 | * The message queue's ring buffer contains space for @a q_max_msgs messages, |
| 3526 | * 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] | 3527 | * @a q_align -byte boundary, which must be a power of 2. To ensure that each |
| 3528 | * message is similarly aligned to this boundary, @a q_msg_size must also be |
| 3529 | * a multiple of @a q_align. |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3530 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3531 | * The message queue can be accessed outside the module where it is defined |
| 3532 | * using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3533 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 3534 | * @code extern struct k_msgq <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3535 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3536 | * @param q_name Name of the message queue. |
| 3537 | * @param q_msg_size Message size (in bytes). |
| 3538 | * @param q_max_msgs Maximum number of messages that can be queued. |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 3539 | * @param q_align Alignment of the message queue's ring buffer. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3540 | * |
| 3541 | * @req K-MSGQ-001 |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3542 | */ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 3543 | #define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \ |
| 3544 | static char __noinit __aligned(q_align) \ |
| 3545 | _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \ |
| 3546 | Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \ |
| 3547 | _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3548 | q_msg_size, q_max_msgs) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3549 | |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3550 | /** |
| 3551 | * @brief Initialize a message queue. |
| 3552 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3553 | * This routine initializes a message queue object, prior to its first use. |
| 3554 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 3555 | * The message queue's ring buffer must contain space for @a max_msgs messages, |
| 3556 | * each of which is @a msg_size bytes long. The buffer must be aligned to an |
| 3557 | * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure |
| 3558 | * that each message is similarly aligned to this boundary, @a q_msg_size |
| 3559 | * must also be a multiple of N. |
| 3560 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3561 | * @param q Address of the message queue. |
| 3562 | * @param buffer Pointer to ring buffer that holds queued messages. |
| 3563 | * @param msg_size Message size (in bytes). |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3564 | * @param max_msgs Maximum number of messages that can be queued. |
| 3565 | * |
| 3566 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3567 | * @req K-MSGQ-002 |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3568 | */ |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 3569 | void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size, |
| 3570 | u32_t max_msgs); |
| 3571 | |
| 3572 | /** |
| 3573 | * @brief Initialize a message queue. |
| 3574 | * |
| 3575 | * This routine initializes a message queue object, prior to its first use, |
| 3576 | * allocating its internal ring buffer from the calling thread's resource |
| 3577 | * pool. |
| 3578 | * |
| 3579 | * Memory allocated for the ring buffer can be released by calling |
| 3580 | * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses |
| 3581 | * all of its references. |
| 3582 | * |
| 3583 | * @param q Address of the message queue. |
| 3584 | * @param msg_size Message size (in bytes). |
| 3585 | * @param max_msgs Maximum number of messages that can be queued. |
| 3586 | * |
| 3587 | * @return 0 on success, -ENOMEM if there was insufficient memory in the |
| 3588 | * thread's resource pool, or -EINVAL if the size parameters cause |
| 3589 | * an integer overflow. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3590 | * @req K-MSGQ-002 |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 3591 | */ |
| 3592 | __syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size, |
| 3593 | u32_t max_msgs); |
| 3594 | |
| 3595 | |
| 3596 | void k_msgq_cleanup(struct k_msgq *q); |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3597 | |
| 3598 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3599 | * @brief Send a message to a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3600 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3601 | * This routine sends a message to message queue @a q. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3602 | * |
Benjamin Walsh | 8215ce1 | 2016-11-09 19:45:19 -0500 | [diff] [blame] | 3603 | * @note Can be called by ISRs. |
| 3604 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3605 | * @param q Address of the message queue. |
| 3606 | * @param data Pointer to the message. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3607 | * @param timeout Non-negative waiting period to add the message (in |
| 3608 | * milliseconds), or one of the special values K_NO_WAIT and |
| 3609 | * K_FOREVER. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3610 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3611 | * @retval 0 Message sent. |
| 3612 | * @retval -ENOMSG Returned without waiting or queue purged. |
| 3613 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3614 | * @req K-MSGQ-002 |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3615 | */ |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 3616 | __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] | 3617 | |
| 3618 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3619 | * @brief Receive a message from a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3620 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3621 | * This routine receives a message from message queue @a q in a "first in, |
| 3622 | * first out" manner. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3623 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3624 | * @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] | 3625 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3626 | * @param q Address of the message queue. |
| 3627 | * @param data Address of area to hold the received message. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3628 | * @param timeout Non-negative waiting period to receive the message (in |
| 3629 | * milliseconds), or one of the special values K_NO_WAIT and |
| 3630 | * K_FOREVER. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3631 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3632 | * @retval 0 Message received. |
| 3633 | * @retval -ENOMSG Returned without waiting. |
| 3634 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3635 | * @req K-MSGQ-002 |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3636 | */ |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 3637 | __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] | 3638 | |
| 3639 | /** |
Sathish Kuttan | 3efd8e1 | 2018-11-09 21:03:10 -0800 | [diff] [blame] | 3640 | * @brief Peek/read a message from a message queue. |
| 3641 | * |
| 3642 | * This routine reads a message from message queue @a q in a "first in, |
| 3643 | * first out" manner and leaves the message in the queue. |
| 3644 | * |
| 3645 | * @note Can be called by ISRs. |
| 3646 | * |
| 3647 | * @param q Address of the message queue. |
| 3648 | * @param data Address of area to hold the message read from the queue. |
| 3649 | * |
| 3650 | * @retval 0 Message read. |
| 3651 | * @retval -ENOMSG Returned when the queue has no message. |
| 3652 | * @req K-MSGQ-002 |
| 3653 | */ |
| 3654 | __syscall int k_msgq_peek(struct k_msgq *q, void *data); |
| 3655 | |
| 3656 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3657 | * @brief Purge a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3658 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3659 | * This routine discards all unreceived messages in a message queue's ring |
| 3660 | * buffer. Any threads that are blocked waiting to send a message to the |
| 3661 | * message queue are unblocked and see an -ENOMSG error code. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3662 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3663 | * @param q Address of the message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3664 | * |
| 3665 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3666 | * @req K-MSGQ-002 |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3667 | */ |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 3668 | __syscall void k_msgq_purge(struct k_msgq *q); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3669 | |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 3670 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3671 | * @brief Get the amount of free space in a message queue. |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 3672 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3673 | * This routine returns the number of unused entries in a message queue's |
| 3674 | * ring buffer. |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 3675 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3676 | * @param q Address of the message queue. |
| 3677 | * |
| 3678 | * @return Number of unused ring buffer entries. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3679 | * @req K-MSGQ-002 |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 3680 | */ |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 3681 | __syscall u32_t k_msgq_num_free_get(struct k_msgq *q); |
| 3682 | |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 3683 | /** |
| 3684 | * @brief Get basic attributes of a message queue. |
| 3685 | * |
| 3686 | * This routine fetches basic attributes of message queue into attr argument. |
| 3687 | * |
| 3688 | * @param q Address of the message queue. |
| 3689 | * @param attrs pointer to message queue attribute structure. |
| 3690 | * |
| 3691 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3692 | * @req K-MSGQ-003 |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 3693 | */ |
| 3694 | __syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs); |
| 3695 | |
| 3696 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3697 | 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] | 3698 | { |
| 3699 | return q->max_msgs - q->used_msgs; |
| 3700 | } |
| 3701 | |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3702 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3703 | * @brief Get the number of messages in a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3704 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3705 | * 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] | 3706 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3707 | * @param q Address of the message queue. |
| 3708 | * |
| 3709 | * @return Number of messages. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3710 | * @req K-MSGQ-002 |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3711 | */ |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 3712 | __syscall u32_t k_msgq_num_used_get(struct k_msgq *q); |
| 3713 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3714 | 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] | 3715 | { |
| 3716 | return q->used_msgs; |
| 3717 | } |
| 3718 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 3719 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3720 | |
| 3721 | /** |
| 3722 | * @defgroup mem_pool_apis Memory Pool APIs |
| 3723 | * @ingroup kernel_apis |
| 3724 | * @{ |
| 3725 | */ |
| 3726 | |
Andy Ross | 73cb958 | 2017-05-09 10:42:39 -0700 | [diff] [blame] | 3727 | /* Note on sizing: the use of a 20 bit field for block means that, |
| 3728 | * assuming a reasonable minimum block size of 16 bytes, we're limited |
| 3729 | * to 16M of memory managed by a single pool. Long term it would be |
| 3730 | * good to move to a variable bit size based on configuration. |
| 3731 | */ |
| 3732 | struct k_mem_block_id { |
| 3733 | u32_t pool : 8; |
| 3734 | u32_t level : 4; |
| 3735 | u32_t block : 20; |
| 3736 | }; |
| 3737 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3738 | struct k_mem_block { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3739 | void *data; |
Andy Ross | 73cb958 | 2017-05-09 10:42:39 -0700 | [diff] [blame] | 3740 | struct k_mem_block_id id; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3741 | }; |
| 3742 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 3743 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3744 | |
| 3745 | /** |
| 3746 | * @defgroup mailbox_apis Mailbox APIs |
| 3747 | * @ingroup kernel_apis |
| 3748 | * @{ |
| 3749 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3750 | |
| 3751 | struct k_mbox_msg { |
| 3752 | /** internal use only - needed for legacy API support */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3753 | u32_t _mailbox; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3754 | /** size of message (in bytes) */ |
Peter Mitsis | d93078c | 2016-10-14 12:59:37 -0400 | [diff] [blame] | 3755 | size_t size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3756 | /** application-defined information value */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3757 | u32_t info; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3758 | /** sender's message data buffer */ |
| 3759 | void *tx_data; |
| 3760 | /** internal use only - needed for legacy API support */ |
| 3761 | void *_rx_data; |
| 3762 | /** message data block descriptor */ |
| 3763 | struct k_mem_block tx_block; |
| 3764 | /** source thread id */ |
| 3765 | k_tid_t rx_source_thread; |
| 3766 | /** target thread id */ |
| 3767 | k_tid_t tx_target_thread; |
| 3768 | /** internal use only - thread waiting on send (may be a dummy) */ |
| 3769 | k_tid_t _syncing_thread; |
| 3770 | #if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0) |
| 3771 | /** internal use only - semaphore used during asynchronous send */ |
| 3772 | struct k_sem *_async_sem; |
| 3773 | #endif |
| 3774 | }; |
| 3775 | |
| 3776 | struct k_mbox { |
| 3777 | _wait_q_t tx_msg_queue; |
| 3778 | _wait_q_t rx_msg_queue; |
Andy Ross | 9eeb6b8 | 2018-07-25 15:06:24 -0700 | [diff] [blame] | 3779 | struct k_spinlock lock; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3780 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 3781 | _OBJECT_TRACING_NEXT_PTR(k_mbox) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 3782 | _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3783 | }; |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3784 | /** |
| 3785 | * @cond INTERNAL_HIDDEN |
| 3786 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3787 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3788 | #define _K_MBOX_INITIALIZER(obj) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3789 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3790 | .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \ |
| 3791 | .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 3792 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3793 | } |
| 3794 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 3795 | #define K_MBOX_INITIALIZER __DEPRECATED_MACRO _K_MBOX_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3796 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3797 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3798 | * INTERNAL_HIDDEN @endcond |
| 3799 | */ |
| 3800 | |
| 3801 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3802 | * @brief Statically define and initialize a mailbox. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3803 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3804 | * 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] | 3805 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 3806 | * @code extern struct k_mbox <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3807 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3808 | * @param name Name of the mailbox. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3809 | * @req K-MBOX-001 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3810 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3811 | #define K_MBOX_DEFINE(name) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 3812 | Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3813 | _K_MBOX_INITIALIZER(name) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3814 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3815 | /** |
| 3816 | * @brief Initialize a mailbox. |
| 3817 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3818 | * This routine initializes a mailbox object, prior to its first use. |
| 3819 | * |
| 3820 | * @param mbox Address of the mailbox. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3821 | * |
| 3822 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3823 | * @req K-MBOX-002 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3824 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3825 | extern void k_mbox_init(struct k_mbox *mbox); |
| 3826 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3827 | /** |
| 3828 | * @brief Send a mailbox message in a synchronous manner. |
| 3829 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3830 | * This routine sends a message to @a mbox and waits for a receiver to both |
| 3831 | * receive and process it. The message data may be in a buffer, in a memory |
| 3832 | * pool block, or non-existent (i.e. an empty message). |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3833 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3834 | * @param mbox Address of the mailbox. |
| 3835 | * @param tx_msg Address of the transmit message descriptor. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3836 | * @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] | 3837 | * milliseconds), or one of the special values K_NO_WAIT |
| 3838 | * and K_FOREVER. Once the message has been received, |
| 3839 | * this routine waits as long as necessary for the message |
| 3840 | * to be completely processed. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3841 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3842 | * @retval 0 Message sent. |
| 3843 | * @retval -ENOMSG Returned without waiting. |
| 3844 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3845 | * @req K-MBOX-002 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3846 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 3847 | 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] | 3848 | s32_t timeout); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3849 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3850 | /** |
| 3851 | * @brief Send a mailbox message in an asynchronous manner. |
| 3852 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3853 | * This routine sends a message to @a mbox without waiting for a receiver |
| 3854 | * to process it. The message data may be in a buffer, in a memory pool block, |
| 3855 | * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem |
| 3856 | * will be given when the message has been both received and completely |
| 3857 | * processed by the receiver. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3858 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3859 | * @param mbox Address of the mailbox. |
| 3860 | * @param tx_msg Address of the transmit message descriptor. |
| 3861 | * @param sem Address of a semaphore, or NULL if none is needed. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3862 | * |
| 3863 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3864 | * @req K-MBOX-002 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3865 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 3866 | 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] | 3867 | struct k_sem *sem); |
| 3868 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3869 | /** |
| 3870 | * @brief Receive a mailbox message. |
| 3871 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3872 | * This routine receives a message from @a mbox, then optionally retrieves |
| 3873 | * its data and disposes of the message. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3874 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3875 | * @param mbox Address of the mailbox. |
| 3876 | * @param rx_msg Address of the receive message descriptor. |
| 3877 | * @param buffer Address of the buffer to receive data, or NULL to defer data |
| 3878 | * retrieval and message disposal until later. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3879 | * @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] | 3880 | * milliseconds), or one of the special values K_NO_WAIT |
| 3881 | * and K_FOREVER. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3882 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3883 | * @retval 0 Message received. |
| 3884 | * @retval -ENOMSG Returned without waiting. |
| 3885 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3886 | * @req K-MBOX-002 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3887 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 3888 | 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] | 3889 | void *buffer, s32_t timeout); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3890 | |
| 3891 | /** |
| 3892 | * @brief Retrieve mailbox message data into a buffer. |
| 3893 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3894 | * This routine completes the processing of a received message by retrieving |
| 3895 | * its data into a buffer, then disposing of the message. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3896 | * |
| 3897 | * Alternatively, this routine can be used to dispose of a received message |
| 3898 | * without retrieving its data. |
| 3899 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3900 | * @param rx_msg Address of the receive message descriptor. |
| 3901 | * @param buffer Address of the buffer to receive data, or NULL to discard |
| 3902 | * the data. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3903 | * |
| 3904 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3905 | * @req K-MBOX-002 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3906 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 3907 | 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] | 3908 | |
| 3909 | /** |
| 3910 | * @brief Retrieve mailbox message data into a memory pool block. |
| 3911 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3912 | * This routine completes the processing of a received message by retrieving |
| 3913 | * its data into a memory pool block, then disposing of the message. |
| 3914 | * The memory pool block that results from successful retrieval must be |
| 3915 | * returned to the pool once the data has been processed, even in cases |
| 3916 | * where zero bytes of data are retrieved. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3917 | * |
| 3918 | * Alternatively, this routine can be used to dispose of a received message |
| 3919 | * without retrieving its data. In this case there is no need to return a |
| 3920 | * memory pool block to the pool. |
| 3921 | * |
| 3922 | * This routine allocates a new memory pool block for the data only if the |
| 3923 | * data is not already in one. If a new block cannot be allocated, the routine |
| 3924 | * returns a failure code and the received message is left unchanged. This |
| 3925 | * permits the caller to reattempt data retrieval at a later time or to dispose |
| 3926 | * of the received message without retrieving its data. |
| 3927 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3928 | * @param rx_msg Address of a receive message descriptor. |
| 3929 | * @param pool Address of memory pool, or NULL to discard data. |
| 3930 | * @param block Address of the area to hold memory pool block info. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3931 | * @param timeout Non-negative waiting period to wait for a memory pool block |
| 3932 | * (in milliseconds), or one of the special values K_NO_WAIT |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3933 | * and K_FOREVER. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3934 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3935 | * @retval 0 Data retrieved. |
| 3936 | * @retval -ENOMEM Returned without waiting. |
| 3937 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3938 | * @req K-MBOX-002 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3939 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 3940 | 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] | 3941 | struct k_mem_pool *pool, |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3942 | struct k_mem_block *block, s32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3943 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 3944 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3945 | |
| 3946 | /** |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3947 | * @defgroup pipe_apis Pipe APIs |
| 3948 | * @ingroup kernel_apis |
| 3949 | * @{ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3950 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3951 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3952 | /** Pipe Structure */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3953 | struct k_pipe { |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3954 | unsigned char *buffer; /**< Pipe buffer: may be NULL */ |
| 3955 | size_t size; /**< Buffer size */ |
| 3956 | size_t bytes_used; /**< # bytes used in buffer */ |
| 3957 | size_t read_index; /**< Where in buffer to read from */ |
| 3958 | size_t write_index; /**< Where in buffer to write */ |
Andy Ross | f582b55 | 2019-02-05 16:10:18 -0800 | [diff] [blame] | 3959 | struct k_spinlock lock; /**< Synchronization lock */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3960 | |
| 3961 | struct { |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3962 | _wait_q_t readers; /**< Reader wait queue */ |
| 3963 | _wait_q_t writers; /**< Writer wait queue */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3964 | } wait_q; |
| 3965 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 3966 | _OBJECT_TRACING_NEXT_PTR(k_pipe) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 3967 | _OBJECT_TRACING_LINKED_FLAG |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3968 | u8_t flags; /**< Flags */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3969 | }; |
| 3970 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3971 | /** |
| 3972 | * @cond INTERNAL_HIDDEN |
| 3973 | */ |
| 3974 | #define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */ |
| 3975 | |
Krzysztof Chruscinski | be06327 | 2019-02-13 11:19:54 +0100 | [diff] [blame] | 3976 | #define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \ |
| 3977 | { \ |
| 3978 | .buffer = pipe_buffer, \ |
| 3979 | .size = pipe_buffer_size, \ |
| 3980 | .bytes_used = 0, \ |
| 3981 | .read_index = 0, \ |
| 3982 | .write_index = 0, \ |
| 3983 | .lock = {}, \ |
| 3984 | .wait_q = { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3985 | .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \ |
| 3986 | .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \ |
Krzysztof Chruscinski | be06327 | 2019-02-13 11:19:54 +0100 | [diff] [blame] | 3987 | }, \ |
| 3988 | _OBJECT_TRACING_INIT \ |
| 3989 | .flags = 0 \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3990 | } |
| 3991 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 3992 | #define K_PIPE_INITIALIZER __DEPRECATED_MACRO _K_PIPE_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3993 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3994 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3995 | * INTERNAL_HIDDEN @endcond |
| 3996 | */ |
| 3997 | |
| 3998 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3999 | * @brief Statically define and initialize a pipe. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4000 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4001 | * 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] | 4002 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 4003 | * @code extern struct k_pipe <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4004 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4005 | * @param name Name of the pipe. |
| 4006 | * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes), |
| 4007 | * or zero if no ring buffer is used. |
| 4008 | * @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] | 4009 | * |
| 4010 | * @req K-PIPE-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4011 | */ |
Andrew Boie | 44fe812 | 2018-04-12 17:38:12 -0700 | [diff] [blame] | 4012 | #define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \ |
Andrew Boie | 41f6011 | 2019-01-31 15:53:24 -0800 | [diff] [blame] | 4013 | static unsigned char __noinit __aligned(pipe_align) \ |
Andrew Boie | 44fe812 | 2018-04-12 17:38:12 -0700 | [diff] [blame] | 4014 | _k_pipe_buf_##name[pipe_buffer_size]; \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 4015 | Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 4016 | _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4017 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4018 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4019 | * @brief Initialize a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4020 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4021 | * This routine initializes a pipe object, prior to its first use. |
| 4022 | * |
| 4023 | * @param pipe Address of the pipe. |
| 4024 | * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer |
| 4025 | * is used. |
| 4026 | * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring |
| 4027 | * buffer is used. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4028 | * |
| 4029 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4030 | * @req K-PIPE-002 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4031 | */ |
Andrew Boie | 44fe812 | 2018-04-12 17:38:12 -0700 | [diff] [blame] | 4032 | void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size); |
| 4033 | |
| 4034 | /** |
| 4035 | * @brief Release a pipe's allocated buffer |
| 4036 | * |
| 4037 | * If a pipe object was given a dynamically allocated buffer via |
| 4038 | * k_pipe_alloc_init(), this will free it. This function does nothing |
| 4039 | * if the buffer wasn't dynamically allocated. |
| 4040 | * |
| 4041 | * @param pipe Address of the pipe. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4042 | * @req K-PIPE-002 |
Andrew Boie | 44fe812 | 2018-04-12 17:38:12 -0700 | [diff] [blame] | 4043 | */ |
| 4044 | void k_pipe_cleanup(struct k_pipe *pipe); |
| 4045 | |
| 4046 | /** |
| 4047 | * @brief Initialize a pipe and allocate a buffer for it |
| 4048 | * |
| 4049 | * Storage for the buffer region will be allocated from the calling thread's |
| 4050 | * resource pool. This memory will be released if k_pipe_cleanup() is called, |
| 4051 | * or userspace is enabled and the pipe object loses all references to it. |
| 4052 | * |
| 4053 | * This function should only be called on uninitialized pipe objects. |
| 4054 | * |
| 4055 | * @param pipe Address of the pipe. |
| 4056 | * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring |
| 4057 | * buffer is used. |
| 4058 | * @retval 0 on success |
David B. Kinder | fcbd8fb | 2018-05-23 12:06:24 -0700 | [diff] [blame] | 4059 | * @retval -ENOMEM if memory couldn't be allocated |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4060 | * @req K-PIPE-002 |
Andrew Boie | 44fe812 | 2018-04-12 17:38:12 -0700 | [diff] [blame] | 4061 | */ |
| 4062 | __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] | 4063 | |
| 4064 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4065 | * @brief Write data to a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4066 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4067 | * 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] | 4068 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4069 | * @param pipe Address of the pipe. |
| 4070 | * @param data Address of data to write. |
| 4071 | * @param bytes_to_write Size of data (in bytes). |
| 4072 | * @param bytes_written Address of area to hold the number of bytes written. |
| 4073 | * @param min_xfer Minimum number of bytes to write. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 4074 | * @param timeout Non-negative waiting period to wait for the data to be written |
| 4075 | * (in milliseconds), or one of the special values K_NO_WAIT |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4076 | * and K_FOREVER. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4077 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4078 | * @retval 0 At least @a min_xfer bytes of data were written. |
| 4079 | * @retval -EIO Returned without waiting; zero data bytes were written. |
| 4080 | * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4081 | * minus one data bytes were written. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4082 | * @req K-PIPE-002 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4083 | */ |
Andrew Boie | b9a0578 | 2017-09-29 16:05:32 -0700 | [diff] [blame] | 4084 | __syscall int k_pipe_put(struct k_pipe *pipe, void *data, |
| 4085 | size_t bytes_to_write, size_t *bytes_written, |
| 4086 | size_t min_xfer, s32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4087 | |
| 4088 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4089 | * @brief Read data from a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4090 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4091 | * 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] | 4092 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4093 | * @param pipe Address of the pipe. |
| 4094 | * @param data Address to place the data read from pipe. |
| 4095 | * @param bytes_to_read Maximum number of data bytes to read. |
| 4096 | * @param bytes_read Address of area to hold the number of bytes read. |
| 4097 | * @param min_xfer Minimum number of data bytes to read. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 4098 | * @param timeout Non-negative waiting period to wait for the data to be read |
| 4099 | * (in milliseconds), or one of the special values K_NO_WAIT |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4100 | * and K_FOREVER. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4101 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4102 | * @retval 0 At least @a min_xfer bytes of data were read. |
| 4103 | * @retval -EIO Returned without waiting; zero data bytes were read. |
| 4104 | * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4105 | * minus one data bytes were read. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4106 | * @req K-PIPE-002 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4107 | */ |
Andrew Boie | b9a0578 | 2017-09-29 16:05:32 -0700 | [diff] [blame] | 4108 | __syscall int k_pipe_get(struct k_pipe *pipe, void *data, |
| 4109 | size_t bytes_to_read, size_t *bytes_read, |
| 4110 | size_t min_xfer, s32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4111 | |
| 4112 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4113 | * @brief Write memory block to a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4114 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4115 | * This routine writes the data contained in a memory block to @a pipe. |
| 4116 | * 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] | 4117 | * 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] | 4118 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4119 | * @param pipe Address of the pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4120 | * @param block Memory block containing data to send |
| 4121 | * @param size Number of data bytes in memory block to send |
| 4122 | * @param sem Semaphore to signal upon completion (else NULL) |
| 4123 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4124 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4125 | * @req K-PIPE-002 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4126 | */ |
| 4127 | extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block, |
| 4128 | size_t size, struct k_sem *sem); |
| 4129 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4130 | /** @} */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4131 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4132 | /** |
| 4133 | * @cond INTERNAL_HIDDEN |
| 4134 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4135 | |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4136 | struct k_mem_slab { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4137 | _wait_q_t wait_q; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4138 | u32_t num_blocks; |
Peter Mitsis | fb02d57 | 2016-10-13 16:55:45 -0400 | [diff] [blame] | 4139 | size_t block_size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4140 | char *buffer; |
| 4141 | char *free_list; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4142 | u32_t num_used; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4143 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 4144 | _OBJECT_TRACING_NEXT_PTR(k_mem_slab) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 4145 | _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4146 | }; |
| 4147 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 4148 | #define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4149 | slab_num_blocks) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4150 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4151 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4152 | .num_blocks = slab_num_blocks, \ |
| 4153 | .block_size = slab_block_size, \ |
| 4154 | .buffer = slab_buffer, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4155 | .free_list = NULL, \ |
| 4156 | .num_used = 0, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 4157 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4158 | } |
| 4159 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 4160 | #define K_MEM_SLAB_INITIALIZER __DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 4161 | |
| 4162 | |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 4163 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4164 | * INTERNAL_HIDDEN @endcond |
| 4165 | */ |
| 4166 | |
| 4167 | /** |
| 4168 | * @defgroup mem_slab_apis Memory Slab APIs |
| 4169 | * @ingroup kernel_apis |
| 4170 | * @{ |
| 4171 | */ |
| 4172 | |
| 4173 | /** |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 4174 | * @brief Statically define and initialize a memory slab. |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 4175 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 4176 | * The memory slab's buffer contains @a slab_num_blocks memory blocks |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4177 | * 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] | 4178 | * @a slab_align -byte boundary. To ensure that each memory block is similarly |
| 4179 | * 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] | 4180 | * @a slab_align. |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 4181 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 4182 | * 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] | 4183 | * using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4184 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 4185 | * @code extern struct k_mem_slab <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4186 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4187 | * @param name Name of the memory slab. |
| 4188 | * @param slab_block_size Size of each memory block (in bytes). |
| 4189 | * @param slab_num_blocks Number memory blocks. |
| 4190 | * @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] | 4191 | * @req K-MSLAB-001 |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 4192 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4193 | #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] | 4194 | char __noinit __aligned(WB_UP(slab_align)) \ |
| 4195 | _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] | 4196 | Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 4197 | _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \ |
Nicolas Pitre | 46cd5a0 | 2019-05-21 21:40:38 -0400 | [diff] [blame] | 4198 | WB_UP(slab_block_size), slab_num_blocks) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4199 | |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4200 | /** |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4201 | * @brief Initialize a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4202 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4203 | * Initializes a memory slab, prior to its first use. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4204 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 4205 | * The memory slab's buffer contains @a slab_num_blocks memory blocks |
| 4206 | * 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] | 4207 | * N-byte boundary matching a word boundary, where N is a power of 2 |
| 4208 | * (i.e. 4 on 32-bit systems, 8, 16, ...). |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 4209 | * To ensure that each memory block is similarly aligned to this boundary, |
| 4210 | * @a slab_block_size must also be a multiple of N. |
| 4211 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4212 | * @param slab Address of the memory slab. |
| 4213 | * @param buffer Pointer to buffer used for the memory blocks. |
| 4214 | * @param block_size Size of each memory block (in bytes). |
| 4215 | * @param num_blocks Number of memory blocks. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4216 | * |
| 4217 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4218 | * @req K-MSLAB-002 |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4219 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4220 | 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] | 4221 | size_t block_size, u32_t num_blocks); |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4222 | |
| 4223 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4224 | * @brief Allocate memory from a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4225 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4226 | * This routine allocates a memory block from a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4227 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4228 | * @param slab Address of the memory slab. |
| 4229 | * @param mem Pointer to block address area. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 4230 | * @param timeout Non-negative waiting period to wait for operation to complete |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4231 | * (in milliseconds). Use K_NO_WAIT to return without waiting, |
| 4232 | * or K_FOREVER to wait as long as necessary. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4233 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4234 | * @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] | 4235 | * is set to the starting address of the memory block. |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4236 | * @retval -ENOMEM Returned without waiting. |
| 4237 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4238 | * @req K-MSLAB-002 |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4239 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4240 | 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] | 4241 | s32_t timeout); |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4242 | |
| 4243 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4244 | * @brief Free memory allocated from a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4245 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4246 | * This routine releases a previously allocated memory block back to its |
| 4247 | * associated memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4248 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4249 | * @param slab Address of the memory slab. |
| 4250 | * @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] | 4251 | * |
| 4252 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4253 | * @req K-MSLAB-002 |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4254 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4255 | 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] | 4256 | |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4257 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4258 | * @brief Get the number of used blocks in a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4259 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4260 | * This routine gets the number of memory blocks that are currently |
| 4261 | * allocated in @a slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4262 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4263 | * @param slab Address of the memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4264 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4265 | * @return Number of allocated memory blocks. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4266 | * @req K-MSLAB-002 |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4267 | */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4268 | 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] | 4269 | { |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4270 | return slab->num_used; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4271 | } |
| 4272 | |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4273 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4274 | * @brief Get the number of unused blocks in a memory slab. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4275 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4276 | * This routine gets the number of memory blocks that are currently |
| 4277 | * unallocated in @a slab. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4278 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4279 | * @param slab Address of the memory slab. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4280 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4281 | * @return Number of unallocated memory blocks. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4282 | * @req K-MSLAB-002 |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4283 | */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4284 | 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] | 4285 | { |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4286 | return slab->num_blocks - slab->num_used; |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4287 | } |
| 4288 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4289 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4290 | |
| 4291 | /** |
| 4292 | * @cond INTERNAL_HIDDEN |
| 4293 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4294 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 4295 | struct k_mem_pool { |
Andrew Boie | aa6de29 | 2018-03-06 17:12:37 -0800 | [diff] [blame] | 4296 | struct sys_mem_pool_base base; |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 4297 | _wait_q_t wait_q; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4298 | }; |
| 4299 | |
Dmitriy Korovkin | 0741467 | 2016-11-03 13:35:42 -0400 | [diff] [blame] | 4300 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4301 | * INTERNAL_HIDDEN @endcond |
Dmitriy Korovkin | 0741467 | 2016-11-03 13:35:42 -0400 | [diff] [blame] | 4302 | */ |
| 4303 | |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 4304 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4305 | * @addtogroup mem_pool_apis |
| 4306 | * @{ |
| 4307 | */ |
| 4308 | |
| 4309 | /** |
| 4310 | * @brief Statically define and initialize a memory pool. |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 4311 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4312 | * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes |
| 4313 | * long. The memory pool allows blocks to be repeatedly partitioned into |
| 4314 | * 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] | 4315 | * to a @a align -byte boundary. |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 4316 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4317 | * If the pool is to be accessed outside the module where it is defined, it |
| 4318 | * can be declared via |
| 4319 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 4320 | * @code extern struct k_mem_pool <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4321 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4322 | * @param name Name of the memory pool. |
Andy Ross | 73cb958 | 2017-05-09 10:42:39 -0700 | [diff] [blame] | 4323 | * @param minsz Size of the smallest blocks in the pool (in bytes). |
| 4324 | * @param maxsz Size of the largest blocks in the pool (in bytes). |
| 4325 | * @param nmax Number of maximum sized blocks in the pool. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4326 | * @param align Alignment of the pool's buffer (power of 2). |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4327 | * @req K-MPOOL-001 |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 4328 | */ |
Andy Ross | 73cb958 | 2017-05-09 10:42:39 -0700 | [diff] [blame] | 4329 | #define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \ |
Nicolas Pitre | cf97437 | 2019-06-26 11:32:58 -0400 | [diff] [blame] | 4330 | char __aligned(WB_UP(align)) _mpool_buf_##name[WB_UP(maxsz) * nmax \ |
Andy Ross | 73cb958 | 2017-05-09 10:42:39 -0700 | [diff] [blame] | 4331 | + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4332 | 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] | 4333 | Z_STRUCT_SECTION_ITERABLE(k_mem_pool, name) = { \ |
Andrew Boie | aa6de29 | 2018-03-06 17:12:37 -0800 | [diff] [blame] | 4334 | .base = { \ |
| 4335 | .buf = _mpool_buf_##name, \ |
Nicolas Pitre | cf97437 | 2019-06-26 11:32:58 -0400 | [diff] [blame] | 4336 | .max_sz = WB_UP(maxsz), \ |
Andrew Boie | aa6de29 | 2018-03-06 17:12:37 -0800 | [diff] [blame] | 4337 | .n_max = nmax, \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4338 | .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \ |
Andrew Boie | aa6de29 | 2018-03-06 17:12:37 -0800 | [diff] [blame] | 4339 | .levels = _mpool_lvls_##name, \ |
| 4340 | .flags = SYS_MEM_POOL_KERNEL \ |
| 4341 | } \ |
Johann Fischer | 223a2b9 | 2019-07-04 15:55:20 +0200 | [diff] [blame] | 4342 | }; \ |
Nicolas Pitre | b2a022b | 2019-09-26 16:36:40 -0400 | [diff] [blame] | 4343 | BUILD_ASSERT(WB_UP(maxsz) >= _MPOOL_MINBLK) |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 4344 | |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4345 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4346 | * @brief Allocate memory from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4347 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4348 | * This routine allocates a memory block from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4349 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4350 | * @param pool Address of the memory pool. |
| 4351 | * @param block Pointer to block descriptor for the allocated memory. |
| 4352 | * @param size Amount of memory to allocate (in bytes). |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 4353 | * @param timeout Non-negative waiting period to wait for operation to complete |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4354 | * (in milliseconds). Use K_NO_WAIT to return without waiting, |
| 4355 | * or K_FOREVER to wait as long as necessary. |
| 4356 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4357 | * @retval 0 Memory allocated. The @a data field of the block descriptor |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4358 | * is set to the starting address of the memory block. |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4359 | * @retval -ENOMEM Returned without waiting. |
| 4360 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4361 | * @req K-MPOOL-002 |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4362 | */ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 4363 | 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] | 4364 | size_t size, s32_t timeout); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4365 | |
| 4366 | /** |
Andrew Boie | a2480bd | 2018-04-12 16:59:02 -0700 | [diff] [blame] | 4367 | * @brief Allocate memory from a memory pool with malloc() semantics |
| 4368 | * |
| 4369 | * Such memory must be released using k_free(). |
| 4370 | * |
| 4371 | * @param pool Address of the memory pool. |
| 4372 | * @param size Amount of memory to allocate (in bytes). |
| 4373 | * @return Address of the allocated memory if successful, otherwise NULL |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4374 | * @req K-MPOOL-002 |
Andrew Boie | a2480bd | 2018-04-12 16:59:02 -0700 | [diff] [blame] | 4375 | */ |
| 4376 | extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size); |
| 4377 | |
| 4378 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4379 | * @brief Free memory allocated from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4380 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4381 | * This routine releases a previously allocated memory block back to its |
| 4382 | * memory pool. |
| 4383 | * |
| 4384 | * @param block Pointer to block descriptor for the allocated memory. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4385 | * |
| 4386 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4387 | * @req K-MPOOL-002 |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4388 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4389 | extern void k_mem_pool_free(struct k_mem_block *block); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4390 | |
| 4391 | /** |
Johan Hedberg | 7d887cb | 2018-01-11 20:45:27 +0200 | [diff] [blame] | 4392 | * @brief Free memory allocated from a memory pool. |
| 4393 | * |
| 4394 | * This routine releases a previously allocated memory block back to its |
| 4395 | * memory pool |
| 4396 | * |
| 4397 | * @param id Memory block identifier. |
| 4398 | * |
| 4399 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4400 | * @req K-MPOOL-002 |
Johan Hedberg | 7d887cb | 2018-01-11 20:45:27 +0200 | [diff] [blame] | 4401 | */ |
| 4402 | extern void k_mem_pool_free_id(struct k_mem_block_id *id); |
| 4403 | |
| 4404 | /** |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4405 | * @} |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4406 | */ |
| 4407 | |
| 4408 | /** |
| 4409 | * @defgroup heap_apis Heap Memory Pool APIs |
| 4410 | * @ingroup kernel_apis |
| 4411 | * @{ |
| 4412 | */ |
| 4413 | |
| 4414 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4415 | * @brief Allocate memory from heap. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4416 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4417 | * This routine provides traditional malloc() semantics. Memory is |
Allan Stephens | 480a131 | 2016-10-13 15:44:48 -0500 | [diff] [blame] | 4418 | * allocated from the heap memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4419 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4420 | * @param size Amount of memory requested (in bytes). |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4421 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4422 | * @return Address of the allocated memory if successful; otherwise NULL. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4423 | * @req K-HEAP-001 |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4424 | */ |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 4425 | extern void *k_malloc(size_t size); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4426 | |
| 4427 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4428 | * @brief Free memory allocated from heap. |
Allan Stephens | 480a131 | 2016-10-13 15:44:48 -0500 | [diff] [blame] | 4429 | * |
| 4430 | * This routine provides traditional free() semantics. The memory being |
Andrew Boie | a2480bd | 2018-04-12 16:59:02 -0700 | [diff] [blame] | 4431 | * returned must have been allocated from the heap memory pool or |
| 4432 | * k_mem_pool_malloc(). |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4433 | * |
Anas Nashif | 345fdd5 | 2016-12-20 08:36:04 -0500 | [diff] [blame] | 4434 | * If @a ptr is NULL, no operation is performed. |
| 4435 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4436 | * @param ptr Pointer to previously allocated memory. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4437 | * |
| 4438 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4439 | * @req K-HEAP-001 |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4440 | */ |
| 4441 | extern void k_free(void *ptr); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4442 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4443 | /** |
Andrew Boie | 7f95e83 | 2017-11-08 14:40:01 -0800 | [diff] [blame] | 4444 | * @brief Allocate memory from heap, array style |
| 4445 | * |
| 4446 | * This routine provides traditional calloc() semantics. Memory is |
| 4447 | * allocated from the heap memory pool and zeroed. |
| 4448 | * |
| 4449 | * @param nmemb Number of elements in the requested array |
| 4450 | * @param size Size of each array element (in bytes). |
| 4451 | * |
| 4452 | * @return Address of the allocated memory if successful; otherwise NULL. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4453 | * @req K-HEAP-001 |
Andrew Boie | 7f95e83 | 2017-11-08 14:40:01 -0800 | [diff] [blame] | 4454 | */ |
| 4455 | extern void *k_calloc(size_t nmemb, size_t size); |
| 4456 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4457 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4458 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4459 | /* polling API - PRIVATE */ |
| 4460 | |
Benjamin Walsh | b017986 | 2017-02-02 16:39:57 -0500 | [diff] [blame] | 4461 | #ifdef CONFIG_POLL |
Flavio Ceolin | 6fdc56d | 2018-09-18 12:32:27 -0700 | [diff] [blame] | 4462 | #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] | 4463 | #else |
Flavio Ceolin | 6fdc56d | 2018-09-18 12:32:27 -0700 | [diff] [blame] | 4464 | #define _INIT_OBJ_POLL_EVENT(obj) do { } while (false) |
Benjamin Walsh | b017986 | 2017-02-02 16:39:57 -0500 | [diff] [blame] | 4465 | #endif |
| 4466 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4467 | /* private - types bit positions */ |
| 4468 | enum _poll_types_bits { |
| 4469 | /* can be used to ignore an event */ |
| 4470 | _POLL_TYPE_IGNORE, |
| 4471 | |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 4472 | /* to be signaled by k_poll_signal_raise() */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4473 | _POLL_TYPE_SIGNAL, |
| 4474 | |
| 4475 | /* semaphore availability */ |
| 4476 | _POLL_TYPE_SEM_AVAILABLE, |
| 4477 | |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 4478 | /* queue/fifo/lifo data availability */ |
| 4479 | _POLL_TYPE_DATA_AVAILABLE, |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4480 | |
| 4481 | _POLL_NUM_TYPES |
| 4482 | }; |
| 4483 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4484 | #define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1)) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4485 | |
| 4486 | /* private - states bit positions */ |
| 4487 | enum _poll_states_bits { |
| 4488 | /* default state when creating event */ |
| 4489 | _POLL_STATE_NOT_READY, |
| 4490 | |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 4491 | /* signaled by k_poll_signal_raise() */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4492 | _POLL_STATE_SIGNALED, |
| 4493 | |
| 4494 | /* semaphore is available */ |
| 4495 | _POLL_STATE_SEM_AVAILABLE, |
| 4496 | |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 4497 | /* data is available to read on queue/fifo/lifo */ |
| 4498 | _POLL_STATE_DATA_AVAILABLE, |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4499 | |
Paul Sokolovsky | 45c0b20 | 2018-08-21 23:29:11 +0300 | [diff] [blame] | 4500 | /* queue/fifo/lifo wait was cancelled */ |
| 4501 | _POLL_STATE_CANCELLED, |
| 4502 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4503 | _POLL_NUM_STATES |
| 4504 | }; |
| 4505 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4506 | #define Z_POLL_STATE_BIT(state) (1 << ((state) - 1)) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4507 | |
| 4508 | #define _POLL_EVENT_NUM_UNUSED_BITS \ |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4509 | (32 - (0 \ |
| 4510 | + 8 /* tag */ \ |
| 4511 | + _POLL_NUM_TYPES \ |
| 4512 | + _POLL_NUM_STATES \ |
| 4513 | + 1 /* modes */ \ |
| 4514 | )) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4515 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4516 | /* end of polling API - PRIVATE */ |
| 4517 | |
| 4518 | |
| 4519 | /** |
| 4520 | * @defgroup poll_apis Async polling APIs |
| 4521 | * @ingroup kernel_apis |
| 4522 | * @{ |
| 4523 | */ |
| 4524 | |
| 4525 | /* Public polling API */ |
| 4526 | |
| 4527 | /* public - values for k_poll_event.type bitfield */ |
| 4528 | #define K_POLL_TYPE_IGNORE 0 |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4529 | #define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL) |
| 4530 | #define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE) |
| 4531 | #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] | 4532 | #define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4533 | |
| 4534 | /* public - polling modes */ |
| 4535 | enum k_poll_modes { |
| 4536 | /* polling thread does not take ownership of objects when available */ |
| 4537 | K_POLL_MODE_NOTIFY_ONLY = 0, |
| 4538 | |
| 4539 | K_POLL_NUM_MODES |
| 4540 | }; |
| 4541 | |
| 4542 | /* public - values for k_poll_event.state bitfield */ |
| 4543 | #define K_POLL_STATE_NOT_READY 0 |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4544 | #define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED) |
| 4545 | #define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE) |
| 4546 | #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] | 4547 | #define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4548 | #define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4549 | |
| 4550 | /* public - poll signal object */ |
| 4551 | struct k_poll_signal { |
| 4552 | /* PRIVATE - DO NOT TOUCH */ |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 4553 | sys_dlist_t poll_events; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4554 | |
| 4555 | /* |
| 4556 | * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until |
| 4557 | * user resets it to 0. |
| 4558 | */ |
| 4559 | unsigned int signaled; |
| 4560 | |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 4561 | /* custom result value passed to k_poll_signal_raise() if needed */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4562 | int result; |
| 4563 | }; |
| 4564 | |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 4565 | #define K_POLL_SIGNAL_INITIALIZER(obj) \ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4566 | { \ |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 4567 | .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4568 | .signaled = 0, \ |
| 4569 | .result = 0, \ |
| 4570 | } |
| 4571 | |
| 4572 | struct k_poll_event { |
| 4573 | /* PRIVATE - DO NOT TOUCH */ |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 4574 | sys_dnode_t _node; |
| 4575 | |
| 4576 | /* PRIVATE - DO NOT TOUCH */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4577 | struct _poller *poller; |
| 4578 | |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4579 | /* optional user-specified tag, opaque, untouched by the API */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4580 | u32_t tag:8; |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4581 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4582 | /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4583 | u32_t type:_POLL_NUM_TYPES; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4584 | |
| 4585 | /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4586 | u32_t state:_POLL_NUM_STATES; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4587 | |
| 4588 | /* mode of operation, from enum k_poll_modes */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4589 | u32_t mode:1; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4590 | |
| 4591 | /* unused bits in 32-bit word */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4592 | u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4593 | |
| 4594 | /* per-type data */ |
| 4595 | union { |
| 4596 | void *obj; |
| 4597 | struct k_poll_signal *signal; |
| 4598 | struct k_sem *sem; |
| 4599 | struct k_fifo *fifo; |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 4600 | struct k_queue *queue; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4601 | }; |
| 4602 | }; |
| 4603 | |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4604 | #define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4605 | { \ |
| 4606 | .poller = NULL, \ |
| 4607 | .type = event_type, \ |
| 4608 | .state = K_POLL_STATE_NOT_READY, \ |
| 4609 | .mode = event_mode, \ |
| 4610 | .unused = 0, \ |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4611 | { .obj = event_obj }, \ |
| 4612 | } |
| 4613 | |
| 4614 | #define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \ |
| 4615 | event_tag) \ |
| 4616 | { \ |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4617 | .tag = event_tag, \ |
Markus Fuchs | be21d3f | 2019-10-09 21:31:25 +0200 | [diff] [blame] | 4618 | .type = event_type, \ |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4619 | .state = K_POLL_STATE_NOT_READY, \ |
| 4620 | .mode = event_mode, \ |
| 4621 | .unused = 0, \ |
| 4622 | { .obj = event_obj }, \ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4623 | } |
| 4624 | |
| 4625 | /** |
| 4626 | * @brief Initialize one struct k_poll_event instance |
| 4627 | * |
| 4628 | * After this routine is called on a poll event, the event it ready to be |
| 4629 | * placed in an event array to be passed to k_poll(). |
| 4630 | * |
| 4631 | * @param event The event to initialize. |
| 4632 | * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx |
| 4633 | * values. Only values that apply to the same object being polled |
| 4634 | * can be used together. Choosing K_POLL_TYPE_IGNORE disables the |
| 4635 | * event. |
Paul Sokolovsky | cfef979 | 2017-07-18 11:53:06 +0300 | [diff] [blame] | 4636 | * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY. |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4637 | * @param obj Kernel object or poll signal. |
| 4638 | * |
| 4639 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4640 | * @req K-POLL-001 |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4641 | */ |
| 4642 | |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4643 | 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] | 4644 | int mode, void *obj); |
| 4645 | |
| 4646 | /** |
| 4647 | * @brief Wait for one or many of multiple poll events to occur |
| 4648 | * |
| 4649 | * This routine allows a thread to wait concurrently for one or many of |
| 4650 | * multiple poll events to have occurred. Such events can be a kernel object |
| 4651 | * being available, like a semaphore, or a poll signal event. |
| 4652 | * |
| 4653 | * When an event notifies that a kernel object is available, the kernel object |
| 4654 | * is not "given" to the thread calling k_poll(): it merely signals the fact |
| 4655 | * that the object was available when the k_poll() call was in effect. Also, |
| 4656 | * all threads trying to acquire an object the regular way, i.e. by pending on |
| 4657 | * the object, have precedence over the thread polling on the object. This |
| 4658 | * means that the polling thread will never get the poll event on an object |
| 4659 | * until the object becomes available and its pend queue is empty. For this |
| 4660 | * reason, the k_poll() call is more effective when the objects being polled |
| 4661 | * only have one thread, the polling thread, trying to acquire them. |
| 4662 | * |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 4663 | * When k_poll() returns 0, the caller should loop on all the events that were |
| 4664 | * passed to k_poll() and check the state field for the values that were |
| 4665 | * expected and take the associated actions. |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4666 | * |
| 4667 | * Before being reused for another call to k_poll(), the user has to reset the |
| 4668 | * state field to K_POLL_STATE_NOT_READY. |
| 4669 | * |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4670 | * When called from user mode, a temporary memory allocation is required from |
| 4671 | * the caller's resource pool. |
| 4672 | * |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4673 | * @param events An array of pointers to events to be polled for. |
| 4674 | * @param num_events The number of events in the array. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 4675 | * @param timeout Non-negative waiting period for an event to be ready (in |
| 4676 | * milliseconds), or one of the special values K_NO_WAIT and |
| 4677 | * K_FOREVER. |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4678 | * |
| 4679 | * @retval 0 One or more events are ready. |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4680 | * @retval -EAGAIN Waiting period timed out. |
Paul Sokolovsky | 45c0b20 | 2018-08-21 23:29:11 +0300 | [diff] [blame] | 4681 | * @retval -EINTR Polling has been interrupted, e.g. with |
| 4682 | * k_queue_cancel_wait(). All output events are still set and valid, |
| 4683 | * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other |
| 4684 | * words, -EINTR status means that at least one of output events is |
| 4685 | * K_POLL_STATE_CANCELLED. |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4686 | * @retval -ENOMEM Thread resource pool insufficient memory (user mode only) |
| 4687 | * @retval -EINVAL Bad parameters (user mode only) |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4688 | * @req K-POLL-001 |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4689 | */ |
| 4690 | |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4691 | __syscall int k_poll(struct k_poll_event *events, int num_events, |
| 4692 | s32_t timeout); |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4693 | |
| 4694 | /** |
Benjamin Walsh | a304f16 | 2017-02-02 16:46:09 -0500 | [diff] [blame] | 4695 | * @brief Initialize a poll signal object. |
| 4696 | * |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 4697 | * 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] | 4698 | * |
| 4699 | * @param signal A poll signal. |
| 4700 | * |
| 4701 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4702 | * @req K-POLL-001 |
Benjamin Walsh | a304f16 | 2017-02-02 16:46:09 -0500 | [diff] [blame] | 4703 | */ |
| 4704 | |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4705 | __syscall void k_poll_signal_init(struct k_poll_signal *signal); |
| 4706 | |
| 4707 | /* |
| 4708 | * @brief Reset a poll signal object's state to unsignaled. |
| 4709 | * |
| 4710 | * @param signal A poll signal object |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4711 | * @req K-POLL-001 |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4712 | */ |
| 4713 | __syscall void k_poll_signal_reset(struct k_poll_signal *signal); |
| 4714 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4715 | 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] | 4716 | { |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 4717 | signal->signaled = 0U; |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4718 | } |
| 4719 | |
| 4720 | /** |
David B. Kinder | fcbd8fb | 2018-05-23 12:06:24 -0700 | [diff] [blame] | 4721 | * @brief Fetch the signaled state and result value of a poll signal |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4722 | * |
| 4723 | * @param signal A poll signal object |
| 4724 | * @param signaled An integer buffer which will be written nonzero if the |
| 4725 | * object was signaled |
| 4726 | * @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] | 4727 | * result value if the object was signaled, or an undefined |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4728 | * value if it was not. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4729 | * @req K-POLL-001 |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4730 | */ |
| 4731 | __syscall void k_poll_signal_check(struct k_poll_signal *signal, |
| 4732 | unsigned int *signaled, int *result); |
Benjamin Walsh | a304f16 | 2017-02-02 16:46:09 -0500 | [diff] [blame] | 4733 | |
| 4734 | /** |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4735 | * @brief Signal a poll signal object. |
| 4736 | * |
| 4737 | * This routine makes ready a poll signal, which is basically a poll event of |
| 4738 | * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be |
| 4739 | * made ready to run. A @a result value can be specified. |
| 4740 | * |
| 4741 | * The poll signal contains a 'signaled' field that, when set by |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 4742 | * 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] | 4743 | * k_poll_signal_reset(). It thus has to be reset by the user before being |
| 4744 | * passed again to k_poll() or k_poll() will consider it being signaled, and |
| 4745 | * will return immediately. |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4746 | * |
Peter A. Bigot | 773bd98 | 2019-04-30 07:06:39 -0500 | [diff] [blame] | 4747 | * @note The result is stored and the 'signaled' field is set even if |
| 4748 | * this function returns an error indicating that an expiring poll was |
| 4749 | * not notified. The next k_poll() will detect the missed raise. |
| 4750 | * |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4751 | * @param signal A poll signal. |
| 4752 | * @param result The value to store in the result field of the signal. |
| 4753 | * |
| 4754 | * @retval 0 The signal was delivered successfully. |
| 4755 | * @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] | 4756 | * @req K-POLL-001 |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4757 | */ |
| 4758 | |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 4759 | __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] | 4760 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 4761 | /** |
| 4762 | * @internal |
| 4763 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4764 | 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] | 4765 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4766 | /** @} */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4767 | |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 4768 | /** |
Anas Nashif | 30c3cff | 2019-01-22 08:18:13 -0500 | [diff] [blame] | 4769 | * @defgroup cpu_idle_apis CPU Idling APIs |
| 4770 | * @ingroup kernel_apis |
| 4771 | * @{ |
| 4772 | */ |
Anas Nashif | 30c3cff | 2019-01-22 08:18:13 -0500 | [diff] [blame] | 4773 | /** |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 4774 | * @brief Make the CPU idle. |
| 4775 | * |
| 4776 | * This function makes the CPU idle until an event wakes it up. |
| 4777 | * |
| 4778 | * In a regular system, the idle thread should be the only thread responsible |
| 4779 | * for making the CPU idle and triggering any type of power management. |
| 4780 | * However, in some more constrained systems, such as a single-threaded system, |
| 4781 | * the only thread would be responsible for this if needed. |
| 4782 | * |
| 4783 | * @return N/A |
Anas Nashif | 30c3cff | 2019-01-22 08:18:13 -0500 | [diff] [blame] | 4784 | * @req K-CPU-IDLE-001 |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 4785 | */ |
Andrew Boie | 07525a3 | 2019-09-21 16:17:23 -0700 | [diff] [blame] | 4786 | static inline void k_cpu_idle(void) |
| 4787 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4788 | arch_cpu_idle(); |
Andrew Boie | 07525a3 | 2019-09-21 16:17:23 -0700 | [diff] [blame] | 4789 | } |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 4790 | |
| 4791 | /** |
| 4792 | * @brief Make the CPU idle in an atomic fashion. |
| 4793 | * |
| 4794 | * Similar to k_cpu_idle(), but called with interrupts locked if operations |
| 4795 | * must be done atomically before making the CPU idle. |
| 4796 | * |
| 4797 | * @param key Interrupt locking key obtained from irq_lock(). |
| 4798 | * |
| 4799 | * @return N/A |
Anas Nashif | 30c3cff | 2019-01-22 08:18:13 -0500 | [diff] [blame] | 4800 | * @req K-CPU-IDLE-002 |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 4801 | */ |
Andrew Boie | 07525a3 | 2019-09-21 16:17:23 -0700 | [diff] [blame] | 4802 | static inline void k_cpu_atomic_idle(unsigned int key) |
| 4803 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4804 | arch_cpu_atomic_idle(key); |
Andrew Boie | 07525a3 | 2019-09-21 16:17:23 -0700 | [diff] [blame] | 4805 | } |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 4806 | |
Anas Nashif | 30c3cff | 2019-01-22 08:18:13 -0500 | [diff] [blame] | 4807 | /** |
| 4808 | * @} |
| 4809 | */ |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 4810 | |
| 4811 | /** |
| 4812 | * @internal |
| 4813 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4814 | extern void z_sys_power_save_idle_exit(s32_t ticks); |
Andrew Boie | 350f88d | 2017-01-18 13:13:45 -0800 | [diff] [blame] | 4815 | |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4816 | #ifdef ARCH_EXCEPT |
Ioannis Glaropoulos | df02923 | 2019-10-07 11:24:36 +0200 | [diff] [blame] | 4817 | /* This architecture has direct support for triggering a CPU exception */ |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4818 | #define z_except_reason(reason) ARCH_EXCEPT(reason) |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4819 | #else |
| 4820 | |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4821 | /* NOTE: This is the implementation for arches that do not implement |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4822 | * ARCH_EXCEPT() to generate a real CPU exception. |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4823 | * |
| 4824 | * We won't have a real exception frame to determine the PC value when |
| 4825 | * the oops occurred, so print file and line number before we jump into |
| 4826 | * the fatal error handler. |
| 4827 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4828 | #define z_except_reason(reason) do { \ |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4829 | printk("@ %s:%d:\n", __FILE__, __LINE__); \ |
Andrew Boie | 5623637 | 2019-07-15 15:22:29 -0700 | [diff] [blame] | 4830 | z_fatal_error(reason, NULL); \ |
Flavio Ceolin | 6fdc56d | 2018-09-18 12:32:27 -0700 | [diff] [blame] | 4831 | } while (false) |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4832 | |
| 4833 | #endif /* _ARCH__EXCEPT */ |
| 4834 | |
| 4835 | /** |
| 4836 | * @brief Fatally terminate a thread |
| 4837 | * |
| 4838 | * This should be called when a thread has encountered an unrecoverable |
| 4839 | * runtime condition and needs to terminate. What this ultimately |
| 4840 | * means is determined by the _fatal_error_handler() implementation, which |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 4841 | * will be called will reason code K_ERR_KERNEL_OOPS. |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4842 | * |
| 4843 | * If this is called from ISR context, the default system fatal error handler |
| 4844 | * will treat it as an unrecoverable system error, just like k_panic(). |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4845 | * @req K-MISC-003 |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4846 | */ |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 4847 | #define k_oops() z_except_reason(K_ERR_KERNEL_OOPS) |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4848 | |
| 4849 | /** |
| 4850 | * @brief Fatally terminate the system |
| 4851 | * |
| 4852 | * This should be called when the Zephyr kernel has encountered an |
| 4853 | * unrecoverable runtime condition and needs to terminate. What this ultimately |
| 4854 | * means is determined by the _fatal_error_handler() implementation, which |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 4855 | * will be called will reason code K_ERR_KERNEL_PANIC. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4856 | * @req K-MISC-004 |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4857 | */ |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 4858 | #define k_panic() z_except_reason(K_ERR_KERNEL_PANIC) |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4859 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4860 | /* |
| 4861 | * private APIs that are utilized by one or more public APIs |
| 4862 | */ |
| 4863 | |
Stephanos Ioannidis | 2d74604 | 2019-10-25 00:08:21 +0900 | [diff] [blame] | 4864 | /** |
| 4865 | * @internal |
| 4866 | */ |
| 4867 | extern void z_init_thread_base(struct _thread_base *thread_base, |
| 4868 | int priority, u32_t initial_state, |
| 4869 | unsigned int options); |
| 4870 | |
Benjamin Walsh | b12a8e0 | 2016-12-14 15:24:12 -0500 | [diff] [blame] | 4871 | #ifdef CONFIG_MULTITHREADING |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 4872 | /** |
| 4873 | * @internal |
| 4874 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4875 | extern void z_init_static_threads(void); |
Benjamin Walsh | b12a8e0 | 2016-12-14 15:24:12 -0500 | [diff] [blame] | 4876 | #else |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 4877 | /** |
| 4878 | * @internal |
| 4879 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4880 | #define z_init_static_threads() do { } while (false) |
Benjamin Walsh | b12a8e0 | 2016-12-14 15:24:12 -0500 | [diff] [blame] | 4881 | #endif |
| 4882 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 4883 | /** |
| 4884 | * @internal |
| 4885 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4886 | extern bool z_is_thread_essential(void); |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 4887 | /** |
| 4888 | * @internal |
| 4889 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4890 | extern void z_timer_expiration_handler(struct _timeout *t); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4891 | |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4892 | /* arch/cpu.h may declare an architecture or platform-specific macro |
| 4893 | * for properly declaring stacks, compatible with MMU/MPU constraints if |
| 4894 | * enabled |
| 4895 | */ |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 4896 | |
| 4897 | /** |
| 4898 | * @brief Obtain an extern reference to a stack |
| 4899 | * |
| 4900 | * This macro properly brings the symbol of a thread stack declared |
| 4901 | * elsewhere into scope. |
| 4902 | * |
| 4903 | * @param sym Thread stack symbol name |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4904 | * @req K-MISC-005 |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 4905 | */ |
| 4906 | #define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[] |
| 4907 | |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4908 | #ifdef ARCH_THREAD_STACK_DEFINE |
| 4909 | #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] | 4910 | #define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \ |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4911 | ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) |
| 4912 | #define K_THREAD_STACK_LEN(size) ARCH_THREAD_STACK_LEN(size) |
| 4913 | #define K_THREAD_STACK_MEMBER(sym, size) ARCH_THREAD_STACK_MEMBER(sym, size) |
| 4914 | #define K_THREAD_STACK_SIZEOF(sym) ARCH_THREAD_STACK_SIZEOF(sym) |
| 4915 | #define K_THREAD_STACK_RESERVED ARCH_THREAD_STACK_RESERVED |
Andrew Boie | 4e5c093 | 2019-04-04 12:05:28 -0700 | [diff] [blame] | 4916 | static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym) |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 4917 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4918 | return ARCH_THREAD_STACK_BUFFER(sym); |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 4919 | } |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4920 | #else |
| 4921 | /** |
| 4922 | * @brief Declare a toplevel thread stack memory region |
| 4923 | * |
| 4924 | * This declares a region of memory suitable for use as a thread's stack. |
| 4925 | * |
| 4926 | * This is the generic, historical definition. Align to STACK_ALIGN and put in |
| 4927 | * 'noinit' section so that it isn't zeroed at boot |
| 4928 | * |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 4929 | * 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] | 4930 | * k_thread_create(), but should otherwise not be manipulated. If the buffer |
Andrew Boie | 4e5c093 | 2019-04-04 12:05:28 -0700 | [diff] [blame] | 4931 | * inside needs to be examined, examine thread->stack_info for the associated |
| 4932 | * thread object to obtain the boundaries. |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4933 | * |
| 4934 | * It is legal to precede this definition with the 'static' keyword. |
| 4935 | * |
| 4936 | * It is NOT legal to take the sizeof(sym) and pass that to the stackSize |
| 4937 | * parameter of k_thread_create(), it may not be the same as the |
| 4938 | * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead. |
| 4939 | * |
Andrew Boie | e2d7791 | 2018-05-30 09:45:35 -0700 | [diff] [blame] | 4940 | * Some arches may round the size of the usable stack region up to satisfy |
| 4941 | * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned |
| 4942 | * size. |
| 4943 | * |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4944 | * @param sym Thread stack symbol name |
| 4945 | * @param size Size of the stack memory region |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4946 | * @req K-TSTACK-001 |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4947 | */ |
| 4948 | #define K_THREAD_STACK_DEFINE(sym, size) \ |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 4949 | struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size] |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4950 | |
| 4951 | /** |
Rajavardhan Gundi | d4dd928 | 2018-06-27 13:26:20 +0530 | [diff] [blame] | 4952 | * @brief Calculate size of stacks to be allocated in a stack array |
| 4953 | * |
| 4954 | * This macro calculates the size to be allocated for the stacks |
| 4955 | * inside a stack array. It accepts the indicated "size" as a parameter |
| 4956 | * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer |
| 4957 | * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used. |
| 4958 | * |
| 4959 | * @param size Size of the stack memory region |
| 4960 | * @req K-TSTACK-001 |
| 4961 | */ |
| 4962 | #define K_THREAD_STACK_LEN(size) (size) |
| 4963 | |
| 4964 | /** |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4965 | * @brief Declare a toplevel array of thread stack memory regions |
| 4966 | * |
| 4967 | * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE |
| 4968 | * definition for additional details and constraints. |
| 4969 | * |
| 4970 | * This is the generic, historical definition. Align to STACK_ALIGN and put in |
| 4971 | * 'noinit' section so that it isn't zeroed at boot |
| 4972 | * |
| 4973 | * @param sym Thread stack symbol name |
| 4974 | * @param nmemb Number of stacks to declare |
| 4975 | * @param size Size of the stack memory region |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4976 | * @req K-TSTACK-001 |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4977 | */ |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4978 | #define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \ |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 4979 | struct _k_thread_stack_element __noinit \ |
Rajavardhan Gundi | d4dd928 | 2018-06-27 13:26:20 +0530 | [diff] [blame] | 4980 | __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)] |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4981 | |
| 4982 | /** |
| 4983 | * @brief Declare an embedded stack memory region |
| 4984 | * |
| 4985 | * Used for stacks embedded within other data structures. Use is highly |
| 4986 | * discouraged but in some cases necessary. For memory protection scenarios, |
| 4987 | * it is very important that any RAM preceding this member not be writable |
| 4988 | * by threads else a stack overflow will lead to silent corruption. In other |
| 4989 | * words, the containing data structure should live in RAM owned by the kernel. |
| 4990 | * |
| 4991 | * @param sym Thread stack symbol name |
| 4992 | * @param size Size of the stack memory region |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4993 | * @req K-TSTACK-001 |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4994 | */ |
| 4995 | #define K_THREAD_STACK_MEMBER(sym, size) \ |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 4996 | struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size] |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4997 | |
| 4998 | /** |
| 4999 | * @brief Return the size in bytes of a stack memory region |
| 5000 | * |
| 5001 | * Convenience macro for passing the desired stack size to k_thread_create() |
| 5002 | * since the underlying implementation may actually create something larger |
| 5003 | * (for instance a guard area). |
| 5004 | * |
Andrew Boie | e2d7791 | 2018-05-30 09:45:35 -0700 | [diff] [blame] | 5005 | * The value returned here is not guaranteed to match the 'size' parameter |
| 5006 | * passed to K_THREAD_STACK_DEFINE and may be larger. |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5007 | * |
| 5008 | * @param sym Stack memory symbol |
| 5009 | * @return Size of the stack |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5010 | * @req K-TSTACK-001 |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5011 | */ |
| 5012 | #define K_THREAD_STACK_SIZEOF(sym) sizeof(sym) |
| 5013 | |
Andrew Boie | 575abc0 | 2019-03-19 10:42:24 -0700 | [diff] [blame] | 5014 | |
| 5015 | /** |
| 5016 | * @brief Indicate how much additional memory is reserved for stack objects |
| 5017 | * |
| 5018 | * Any given stack declaration may have additional memory in it for guard |
| 5019 | * areas or supervisor mode stacks. This macro indicates how much space |
| 5020 | * is reserved for this. The memory reserved may not be contiguous within |
| 5021 | * the stack object, and does not account for additional space used due to |
| 5022 | * enforce alignment. |
| 5023 | */ |
| 5024 | #define K_THREAD_STACK_RESERVED 0 |
| 5025 | |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5026 | /** |
| 5027 | * @brief Get a pointer to the physical stack buffer |
| 5028 | * |
Andrew Boie | 4e5c093 | 2019-04-04 12:05:28 -0700 | [diff] [blame] | 5029 | * This macro is deprecated. If a stack buffer needs to be examined, the |
| 5030 | * bounds should be obtained from the associated thread's stack_info struct. |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5031 | * |
| 5032 | * @param sym Declared stack symbol name |
| 5033 | * @return The buffer itself, a char * |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5034 | * @req K-TSTACK-001 |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5035 | */ |
Andrew Boie | 4e5c093 | 2019-04-04 12:05:28 -0700 | [diff] [blame] | 5036 | static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym) |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 5037 | { |
| 5038 | return (char *)sym; |
| 5039 | } |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5040 | |
| 5041 | #endif /* _ARCH_DECLARE_STACK */ |
| 5042 | |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5043 | /** |
| 5044 | * @defgroup mem_domain_apis Memory domain APIs |
| 5045 | * @ingroup kernel_apis |
| 5046 | * @{ |
| 5047 | */ |
| 5048 | |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5049 | /** |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5050 | * @def K_MEM_PARTITION_DEFINE |
| 5051 | * @brief Used to declare a memory partition |
| 5052 | * @req K-MP-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5053 | */ |
| 5054 | #ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK |
| 5055 | #define K_MEM_PARTITION_DEFINE(name, start, size, attr) \ |
| 5056 | _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \ |
Andrew Boie | 41f6011 | 2019-01-31 15:53:24 -0800 | [diff] [blame] | 5057 | struct k_mem_partition name =\ |
Nicolas Pitre | 58d839b | 2019-05-21 11:32:21 -0400 | [diff] [blame] | 5058 | { (uintptr_t)start, size, attr} |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5059 | #else |
| 5060 | #define K_MEM_PARTITION_DEFINE(name, start, size, attr) \ |
Andrew Boie | 41f6011 | 2019-01-31 15:53:24 -0800 | [diff] [blame] | 5061 | struct k_mem_partition name =\ |
Nicolas Pitre | 58d839b | 2019-05-21 11:32:21 -0400 | [diff] [blame] | 5062 | { (uintptr_t)start, size, attr} |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5063 | #endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */ |
| 5064 | |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5065 | /* memory partition */ |
| 5066 | struct k_mem_partition { |
| 5067 | /* start address of memory partition */ |
Nicolas Pitre | 58d839b | 2019-05-21 11:32:21 -0400 | [diff] [blame] | 5068 | uintptr_t start; |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5069 | /* size of memory partition */ |
| 5070 | u32_t size; |
Ioannis Glaropoulos | 39bf24a | 2018-11-27 15:45:36 +0100 | [diff] [blame] | 5071 | #if defined(CONFIG_MEMORY_PROTECTION) |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5072 | /* attribute of memory partition */ |
Adithya Baglody | 83bedcc | 2017-10-06 15:43:11 +0530 | [diff] [blame] | 5073 | k_mem_partition_attr_t attr; |
Ioannis Glaropoulos | 39bf24a | 2018-11-27 15:45:36 +0100 | [diff] [blame] | 5074 | #endif /* CONFIG_MEMORY_PROTECTION */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5075 | }; |
| 5076 | |
Ioannis Glaropoulos | 12c0244 | 2018-09-25 16:05:24 +0200 | [diff] [blame] | 5077 | /* memory domain |
Adithya Baglody | 3a6d72e | 2018-02-13 16:44:44 +0530 | [diff] [blame] | 5078 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5079 | struct k_mem_domain { |
Adithya Baglody | 83bedcc | 2017-10-06 15:43:11 +0530 | [diff] [blame] | 5080 | #ifdef CONFIG_USERSPACE |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5081 | /* partitions in the domain */ |
| 5082 | struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS]; |
Adithya Baglody | 83bedcc | 2017-10-06 15:43:11 +0530 | [diff] [blame] | 5083 | #endif /* CONFIG_USERSPACE */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5084 | /* domain q */ |
| 5085 | sys_dlist_t mem_domain_q; |
Leandro Pereira | 08de658 | 2018-02-28 14:22:57 -0800 | [diff] [blame] | 5086 | /* number of partitions in the domain */ |
| 5087 | u8_t num_partitions; |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5088 | }; |
Adithya Baglody | 83bedcc | 2017-10-06 15:43:11 +0530 | [diff] [blame] | 5089 | |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5090 | |
| 5091 | /** |
| 5092 | * @brief Initialize a memory domain. |
| 5093 | * |
| 5094 | * Initialize a memory domain with given name and memory partitions. |
| 5095 | * |
Andrew Boie | fddd550 | 2019-07-30 18:07:32 -0700 | [diff] [blame] | 5096 | * See documentation for k_mem_domain_add_partition() for details about |
| 5097 | * partition constraints. |
| 5098 | * |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5099 | * @param domain The memory domain to be initialized. |
| 5100 | * @param num_parts The number of array items of "parts" parameter. |
| 5101 | * @param parts An array of pointers to the memory partitions. Can be NULL |
| 5102 | * if num_parts is zero. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5103 | * @req K-MD-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5104 | */ |
Leandro Pereira | 08de658 | 2018-02-28 14:22:57 -0800 | [diff] [blame] | 5105 | 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] | 5106 | struct k_mem_partition *parts[]); |
| 5107 | /** |
| 5108 | * @brief Destroy a memory domain. |
| 5109 | * |
| 5110 | * Destroy a memory domain. |
| 5111 | * |
| 5112 | * @param domain The memory domain to be destroyed. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5113 | * @req K-MD-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5114 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5115 | extern void k_mem_domain_destroy(struct k_mem_domain *domain); |
| 5116 | |
| 5117 | /** |
| 5118 | * @brief Add a memory partition into a memory domain. |
| 5119 | * |
Andrew Boie | fddd550 | 2019-07-30 18:07:32 -0700 | [diff] [blame] | 5120 | * Add a memory partition into a memory domain. Partitions must conform to |
| 5121 | * the following constraints: |
| 5122 | * |
| 5123 | * - Partition bounds must be within system RAM boundaries on MMU-based |
| 5124 | * systems. |
| 5125 | * - Partitions in the same memory domain may not overlap each other. |
| 5126 | * - Partitions must not be defined which expose private kernel |
| 5127 | * data structures or kernel objects. |
| 5128 | * - The starting address alignment, and the partition size must conform to |
| 5129 | * the constraints of the underlying memory management hardware, which |
| 5130 | * varies per architecture. |
| 5131 | * - Memory domain partitions are only intended to control access to memory |
| 5132 | * from user mode threads. |
| 5133 | * |
| 5134 | * Violating these constraints may lead to CPU exceptions or undefined |
| 5135 | * behavior. |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5136 | * |
| 5137 | * @param domain The memory domain to be added a memory partition. |
| 5138 | * @param part The memory partition to be added |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5139 | * @req K-MD-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5140 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5141 | extern void k_mem_domain_add_partition(struct k_mem_domain *domain, |
| 5142 | struct k_mem_partition *part); |
| 5143 | |
| 5144 | /** |
| 5145 | * @brief Remove a memory partition from a memory domain. |
| 5146 | * |
| 5147 | * Remove a memory partition from a memory domain. |
| 5148 | * |
| 5149 | * @param domain The memory domain to be removed a memory partition. |
| 5150 | * @param part The memory partition to be removed |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5151 | * @req K-MD-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5152 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5153 | extern void k_mem_domain_remove_partition(struct k_mem_domain *domain, |
| 5154 | struct k_mem_partition *part); |
| 5155 | |
| 5156 | /** |
| 5157 | * @brief Add a thread into a memory domain. |
| 5158 | * |
| 5159 | * Add a thread into a memory domain. |
| 5160 | * |
| 5161 | * @param domain The memory domain that the thread is going to be added into. |
| 5162 | * @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] | 5163 | * |
| 5164 | * @req K-MD-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5165 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5166 | extern void k_mem_domain_add_thread(struct k_mem_domain *domain, |
| 5167 | k_tid_t thread); |
| 5168 | |
| 5169 | /** |
| 5170 | * @brief Remove a thread from its memory domain. |
| 5171 | * |
| 5172 | * Remove a thread from its memory domain. |
| 5173 | * |
| 5174 | * @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] | 5175 | * @req K-MD-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5176 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5177 | extern void k_mem_domain_remove_thread(k_tid_t thread); |
| 5178 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 5179 | /** @} */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5180 | |
Andrew Boie | 756f907 | 2017-10-10 16:01:49 -0700 | [diff] [blame] | 5181 | /** |
| 5182 | * @brief Emit a character buffer to the console device |
| 5183 | * |
| 5184 | * @param c String of characters to print |
| 5185 | * @param n The length of the string |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5186 | * |
| 5187 | * @req K-MISC-006 |
Andrew Boie | 756f907 | 2017-10-10 16:01:49 -0700 | [diff] [blame] | 5188 | */ |
| 5189 | __syscall void k_str_out(char *c, size_t n); |
| 5190 | |
Ioannis Glaropoulos | a6cb8b0 | 2019-05-09 21:55:10 +0200 | [diff] [blame] | 5191 | /** |
| 5192 | * @brief Disable preservation of floating point context information. |
| 5193 | * |
| 5194 | * This routine informs the kernel that the specified thread |
| 5195 | * will no longer be using the floating point registers. |
| 5196 | * |
| 5197 | * @warning |
| 5198 | * Some architectures apply restrictions on how the disabling of floating |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 5199 | * point preservation may be requested, see arch_float_disable. |
Ioannis Glaropoulos | a6cb8b0 | 2019-05-09 21:55:10 +0200 | [diff] [blame] | 5200 | * |
| 5201 | * @warning |
| 5202 | * This routine should only be used to disable floating point support for |
| 5203 | * a thread that currently has such support enabled. |
| 5204 | * |
| 5205 | * @param thread ID of thread. |
| 5206 | * |
| 5207 | * @retval 0 On success. |
| 5208 | * @retval -ENOSYS If the floating point disabling is not implemented. |
| 5209 | * -EINVAL If the floating point disabling could not be performed. |
| 5210 | */ |
| 5211 | __syscall int k_float_disable(struct k_thread *thread); |
| 5212 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 5213 | #ifdef __cplusplus |
| 5214 | } |
| 5215 | #endif |
| 5216 | |
Anas Nashif | 10291a0 | 2019-06-25 12:25:12 -0400 | [diff] [blame] | 5217 | #include <debug/tracing.h> |
Andrew Boie | fa94ee7 | 2017-09-28 16:54:35 -0700 | [diff] [blame] | 5218 | #include <syscalls/kernel.h> |
| 5219 | |
Benjamin Walsh | dfa7ce5 | 2017-01-22 17:06:05 -0500 | [diff] [blame] | 5220 | #endif /* !_ASMLANGUAGE */ |
| 5221 | |
Flavio Ceolin | 67ca176 | 2018-09-14 10:43:44 -0700 | [diff] [blame] | 5222 | #endif /* ZEPHYR_INCLUDE_KERNEL_H_ */ |