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 | }; |
Daniel Leung | 7476a6e | 2019-11-25 13:58:40 -0800 | [diff] [blame^] | 371 | |
| 372 | /** |
| 373 | * @typedef k_thread_stack_t |
| 374 | * @brief Typedef of struct _k_thread_stack_element |
| 375 | * |
| 376 | * @see _k_thread_stack_element |
| 377 | */ |
Andrew Boie | bca15da | 2017-10-15 14:17:48 -0700 | [diff] [blame] | 378 | |
Andrew Boie | 1e06ffc | 2017-09-11 09:30:04 -0700 | [diff] [blame] | 379 | /** |
| 380 | * @typedef k_thread_entry_t |
| 381 | * @brief Thread entry point function type. |
| 382 | * |
| 383 | * A thread's entry point function is invoked when the thread starts executing. |
| 384 | * Up to 3 argument values can be passed to the function. |
| 385 | * |
| 386 | * The thread terminates execution permanently if the entry point function |
| 387 | * returns. The thread is responsible for releasing any shared resources |
| 388 | * it may own (such as mutexes and dynamically allocated memory), prior to |
| 389 | * returning. |
| 390 | * |
| 391 | * @param p1 First argument. |
| 392 | * @param p2 Second argument. |
| 393 | * @param p3 Third argument. |
| 394 | * |
| 395 | * @return N/A |
| 396 | */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 397 | |
| 398 | #ifdef CONFIG_THREAD_MONITOR |
| 399 | struct __thread_entry { |
Andrew Boie | 1e06ffc | 2017-09-11 09:30:04 -0700 | [diff] [blame] | 400 | k_thread_entry_t pEntry; |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 401 | void *parameter1; |
| 402 | void *parameter2; |
| 403 | void *parameter3; |
| 404 | }; |
| 405 | #endif |
| 406 | |
| 407 | /* can be used for creating 'dummy' threads, e.g. for pending on objects */ |
| 408 | struct _thread_base { |
| 409 | |
| 410 | /* this thread's entry in a ready/wait queue */ |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 411 | union { |
Peter A. Bigot | 82ad0d2 | 2019-01-03 23:49:28 -0600 | [diff] [blame] | 412 | sys_dnode_t qnode_dlist; |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 413 | struct rbnode qnode_rb; |
| 414 | }; |
| 415 | |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 416 | /* wait queue on which the thread is pended (needed only for |
| 417 | * trees, not dumb lists) |
| 418 | */ |
| 419 | _wait_q_t *pended_on; |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 420 | |
| 421 | /* user facing 'thread options'; values defined in include/kernel.h */ |
| 422 | u8_t user_options; |
| 423 | |
| 424 | /* thread state */ |
| 425 | u8_t thread_state; |
| 426 | |
| 427 | /* |
| 428 | * scheduler lock count and thread priority |
| 429 | * |
| 430 | * These two fields control the preemptibility of a thread. |
| 431 | * |
| 432 | * When the scheduler is locked, sched_locked is decremented, which |
| 433 | * means that the scheduler is locked for values from 0xff to 0x01. A |
| 434 | * thread is coop if its prio is negative, thus 0x80 to 0xff when |
| 435 | * looked at the value as unsigned. |
| 436 | * |
| 437 | * By putting them end-to-end, this means that a thread is |
| 438 | * non-preemptible if the bundled value is greater than or equal to |
| 439 | * 0x0080. |
| 440 | */ |
| 441 | union { |
| 442 | struct { |
| 443 | #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 444 | u8_t sched_locked; |
| 445 | s8_t prio; |
| 446 | #else /* LITTLE and PDP */ |
| 447 | s8_t prio; |
| 448 | u8_t sched_locked; |
| 449 | #endif |
| 450 | }; |
| 451 | u16_t preempt; |
| 452 | }; |
| 453 | |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 454 | #ifdef CONFIG_SCHED_DEADLINE |
| 455 | int prio_deadline; |
| 456 | #endif |
| 457 | |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 458 | u32_t order_key; |
| 459 | |
Andy Ross | 2724fd1 | 2018-01-29 14:55:20 -0800 | [diff] [blame] | 460 | #ifdef CONFIG_SMP |
| 461 | /* True for the per-CPU idle threads */ |
| 462 | u8_t is_idle; |
| 463 | |
Andy Ross | 2724fd1 | 2018-01-29 14:55:20 -0800 | [diff] [blame] | 464 | /* CPU index on which thread was last run */ |
| 465 | u8_t cpu; |
Andy Ross | 15c4007 | 2018-04-12 12:50:05 -0700 | [diff] [blame] | 466 | |
| 467 | /* Recursive count of irq_lock() calls */ |
| 468 | u8_t global_lock_count; |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 469 | |
| 470 | #endif |
| 471 | |
| 472 | #ifdef CONFIG_SCHED_CPU_MASK |
| 473 | /* "May run on" bits for each CPU */ |
| 474 | u8_t cpu_mask; |
Andy Ross | 2724fd1 | 2018-01-29 14:55:20 -0800 | [diff] [blame] | 475 | #endif |
| 476 | |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 477 | /* data returned by APIs */ |
| 478 | void *swap_data; |
| 479 | |
| 480 | #ifdef CONFIG_SYS_CLOCK_EXISTS |
| 481 | /* this thread's entry in a timeout queue */ |
| 482 | struct _timeout timeout; |
| 483 | #endif |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 484 | }; |
| 485 | |
| 486 | typedef struct _thread_base _thread_base_t; |
| 487 | |
| 488 | #if defined(CONFIG_THREAD_STACK_INFO) |
| 489 | /* Contains the stack information of a thread */ |
| 490 | struct _thread_stack_info { |
Andrew Boie | 4e5c093 | 2019-04-04 12:05:28 -0700 | [diff] [blame] | 491 | /* Stack start - Represents the start address of the thread-writable |
| 492 | * stack area. |
Andrew Boie | b85ac3e | 2018-06-01 12:15:13 -0700 | [diff] [blame] | 493 | */ |
Nicolas Pitre | 58d839b | 2019-05-21 11:32:21 -0400 | [diff] [blame] | 494 | uintptr_t start; |
Andrew Boie | b85ac3e | 2018-06-01 12:15:13 -0700 | [diff] [blame] | 495 | |
| 496 | /* Stack Size - Thread writable stack buffer size. Represents |
| 497 | * the size of the actual area, starting from the start member, |
| 498 | * that should be writable by the thread |
| 499 | */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 500 | u32_t size; |
| 501 | }; |
Andrew Boie | 41c68ec | 2017-05-11 15:38:20 -0700 | [diff] [blame] | 502 | |
| 503 | typedef struct _thread_stack_info _thread_stack_info_t; |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 504 | #endif /* CONFIG_THREAD_STACK_INFO */ |
| 505 | |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 506 | #if defined(CONFIG_USERSPACE) |
| 507 | struct _mem_domain_info { |
| 508 | /* memory domain queue node */ |
| 509 | sys_dnode_t mem_domain_q_node; |
| 510 | /* memory domain of the thread */ |
| 511 | struct k_mem_domain *mem_domain; |
| 512 | }; |
| 513 | |
| 514 | #endif /* CONFIG_USERSPACE */ |
| 515 | |
Daniel Leung | fc18243 | 2018-08-16 15:42:28 -0700 | [diff] [blame] | 516 | #ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA |
| 517 | struct _thread_userspace_local_data { |
| 518 | int errno_var; |
| 519 | }; |
| 520 | #endif |
| 521 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 522 | /** |
| 523 | * @ingroup thread_apis |
| 524 | * Thread Structure |
| 525 | */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 526 | struct k_thread { |
| 527 | |
| 528 | struct _thread_base base; |
| 529 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 530 | /** defined by the architecture, but all archs need these */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 531 | struct _callee_saved callee_saved; |
| 532 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 533 | /** static thread init data */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 534 | void *init_data; |
| 535 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 536 | /** |
| 537 | * abort function |
| 538 | * @req K-THREAD-002 |
| 539 | * */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 540 | void (*fn_abort)(void); |
| 541 | |
| 542 | #if defined(CONFIG_THREAD_MONITOR) |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 543 | /** thread entry and parameters description */ |
Andrew Boie | 2dd91ec | 2018-06-06 08:45:01 -0700 | [diff] [blame] | 544 | struct __thread_entry entry; |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 545 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 546 | /** next item in list of all threads */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 547 | struct k_thread *next_thread; |
| 548 | #endif |
| 549 | |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 550 | #if defined(CONFIG_THREAD_NAME) |
| 551 | /* Thread name */ |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 552 | char name[CONFIG_THREAD_MAX_NAME_LEN]; |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 553 | #endif |
| 554 | |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 555 | #ifdef CONFIG_THREAD_CUSTOM_DATA |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 556 | /** crude thread-local storage */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 557 | void *custom_data; |
| 558 | #endif |
| 559 | |
Daniel Leung | fc18243 | 2018-08-16 15:42:28 -0700 | [diff] [blame] | 560 | #ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA |
| 561 | struct _thread_userspace_local_data *userspace_local_data; |
| 562 | #endif |
| 563 | |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 564 | #ifdef CONFIG_ERRNO |
Daniel Leung | fc18243 | 2018-08-16 15:42:28 -0700 | [diff] [blame] | 565 | #ifndef CONFIG_USERSPACE |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 566 | /** per-thread errno variable */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 567 | int errno_var; |
| 568 | #endif |
Andrew Boie | 7f4d006 | 2018-07-19 11:09:33 -0700 | [diff] [blame] | 569 | #endif |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 570 | |
| 571 | #if defined(CONFIG_THREAD_STACK_INFO) |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 572 | /** Stack Info */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 573 | struct _thread_stack_info stack_info; |
| 574 | #endif /* CONFIG_THREAD_STACK_INFO */ |
| 575 | |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 576 | #if defined(CONFIG_USERSPACE) |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 577 | /** memory domain info of the thread */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 578 | struct _mem_domain_info mem_domain_info; |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 579 | /** Base address of thread stack */ |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 580 | k_thread_stack_t *stack_obj; |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 581 | #endif /* CONFIG_USERSPACE */ |
| 582 | |
Andy Ross | 042d8ec | 2017-12-09 08:37:20 -0800 | [diff] [blame] | 583 | #if defined(CONFIG_USE_SWITCH) |
| 584 | /* When using __switch() a few previously arch-specific items |
| 585 | * become part of the core OS |
| 586 | */ |
| 587 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 588 | /** z_swap() return value */ |
Andy Ross | 042d8ec | 2017-12-09 08:37:20 -0800 | [diff] [blame] | 589 | int swap_retval; |
| 590 | |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 591 | /** Context handle returned via arch_switch() */ |
Andy Ross | 042d8ec | 2017-12-09 08:37:20 -0800 | [diff] [blame] | 592 | void *switch_handle; |
| 593 | #endif |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 594 | /** resource pool */ |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 595 | struct k_mem_pool *resource_pool; |
Andy Ross | 042d8ec | 2017-12-09 08:37:20 -0800 | [diff] [blame] | 596 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 597 | /** arch-specifics: must always be at the end */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 598 | struct _thread_arch arch; |
| 599 | }; |
| 600 | |
| 601 | typedef struct k_thread _thread_t; |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 602 | typedef struct k_thread *k_tid_t; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 603 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 604 | enum execution_context_types { |
| 605 | K_ISR = 0, |
| 606 | K_COOP_THREAD, |
| 607 | K_PREEMPT_THREAD, |
| 608 | }; |
| 609 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 610 | /** |
Anas Nashif | 4bcb294 | 2019-01-23 23:06:29 -0500 | [diff] [blame] | 611 | * @addtogroup thread_apis |
Carles Cufi | cb0cf9f | 2017-01-10 10:57:38 +0100 | [diff] [blame] | 612 | * @{ |
| 613 | */ |
Ramakrishna Pallala | 110b8e4 | 2018-04-27 12:55:43 +0530 | [diff] [blame] | 614 | typedef void (*k_thread_user_cb_t)(const struct k_thread *thread, |
| 615 | void *user_data); |
Carles Cufi | cb0cf9f | 2017-01-10 10:57:38 +0100 | [diff] [blame] | 616 | |
| 617 | /** |
Ramakrishna Pallala | 110b8e4 | 2018-04-27 12:55:43 +0530 | [diff] [blame] | 618 | * @brief Iterate over all the threads in the system. |
| 619 | * |
| 620 | * This routine iterates over all the threads in the system and |
| 621 | * calls the user_cb function for each thread. |
| 622 | * |
| 623 | * @param user_cb Pointer to the user callback function. |
| 624 | * @param user_data Pointer to user data. |
| 625 | * |
| 626 | * @note CONFIG_THREAD_MONITOR must be set for this function |
| 627 | * to be effective. Also this API uses irq_lock to protect the |
| 628 | * _kernel.threads list which means creation of new threads and |
| 629 | * terminations of existing threads are blocked until this |
| 630 | * API returns. |
| 631 | * |
| 632 | * @return N/A |
| 633 | */ |
| 634 | extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data); |
| 635 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 636 | /** @} */ |
Carles Cufi | cb0cf9f | 2017-01-10 10:57:38 +0100 | [diff] [blame] | 637 | |
| 638 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 639 | * @defgroup thread_apis Thread APIs |
| 640 | * @ingroup kernel_apis |
| 641 | * @{ |
| 642 | */ |
| 643 | |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 644 | #endif /* !_ASMLANGUAGE */ |
| 645 | |
| 646 | |
| 647 | /* |
| 648 | * Thread user options. May be needed by assembly code. Common part uses low |
| 649 | * bits, arch-specific use high bits. |
| 650 | */ |
| 651 | |
Anas Nashif | a541e93 | 2018-05-24 11:19:16 -0500 | [diff] [blame] | 652 | /** |
| 653 | * @brief system thread that must not abort |
| 654 | * @req K-THREAD-000 |
| 655 | * */ |
Flavio Ceolin | 8aec087 | 2018-08-15 11:52:00 -0700 | [diff] [blame] | 656 | #define K_ESSENTIAL (BIT(0)) |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 657 | |
| 658 | #if defined(CONFIG_FP_SHARING) |
Anas Nashif | a541e93 | 2018-05-24 11:19:16 -0500 | [diff] [blame] | 659 | /** |
| 660 | * @brief thread uses floating point registers |
| 661 | */ |
Flavio Ceolin | 8aec087 | 2018-08-15 11:52:00 -0700 | [diff] [blame] | 662 | #define K_FP_REGS (BIT(1)) |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 663 | #endif |
| 664 | |
Anas Nashif | a541e93 | 2018-05-24 11:19:16 -0500 | [diff] [blame] | 665 | /** |
| 666 | * @brief user mode thread |
| 667 | * |
| 668 | * This thread has dropped from supervisor mode to user mode and consequently |
Andrew Boie | 5cfa5dc | 2017-08-30 14:17:44 -0700 | [diff] [blame] | 669 | * has additional restrictions |
| 670 | */ |
Flavio Ceolin | 8aec087 | 2018-08-15 11:52:00 -0700 | [diff] [blame] | 671 | #define K_USER (BIT(2)) |
Andrew Boie | 5cfa5dc | 2017-08-30 14:17:44 -0700 | [diff] [blame] | 672 | |
Anas Nashif | a541e93 | 2018-05-24 11:19:16 -0500 | [diff] [blame] | 673 | /** |
| 674 | * @brief Inherit Permissions |
| 675 | * |
| 676 | * @details |
| 677 | * Indicates that the thread being created should inherit all kernel object |
Andrew Boie | 47f8fd1 | 2017-10-05 11:11:02 -0700 | [diff] [blame] | 678 | * permissions from the thread that created it. No effect if CONFIG_USERSPACE |
| 679 | * is not enabled. |
| 680 | */ |
Flavio Ceolin | 8aec087 | 2018-08-15 11:52:00 -0700 | [diff] [blame] | 681 | #define K_INHERIT_PERMS (BIT(3)) |
Andrew Boie | 47f8fd1 | 2017-10-05 11:11:02 -0700 | [diff] [blame] | 682 | |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 683 | #ifdef CONFIG_X86 |
| 684 | /* x86 Bitmask definitions for threads user options */ |
| 685 | |
| 686 | #if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE) |
| 687 | /* thread uses SSEx (and also FP) registers */ |
Flavio Ceolin | 8aec087 | 2018-08-15 11:52:00 -0700 | [diff] [blame] | 688 | #define K_SSE_REGS (BIT(7)) |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 689 | #endif |
| 690 | #endif |
| 691 | |
| 692 | /* end - thread options */ |
| 693 | |
| 694 | #if !defined(_ASMLANGUAGE) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 695 | /** |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 696 | * @brief Create a thread. |
| 697 | * |
| 698 | * This routine initializes a thread, then schedules it for execution. |
| 699 | * |
| 700 | * The new thread may be scheduled for immediate execution or a delayed start. |
| 701 | * If the newly spawned thread does not have a delayed start the kernel |
| 702 | * scheduler may preempt the current thread to allow the new thread to |
| 703 | * execute. |
| 704 | * |
| 705 | * Thread options are architecture-specific, and can include K_ESSENTIAL, |
| 706 | * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating |
| 707 | * them using "|" (the logical OR operator). |
| 708 | * |
| 709 | * Historically, users often would use the beginning of the stack memory region |
| 710 | * to store the struct k_thread data, although corruption will occur if the |
| 711 | * stack overflows this region and stack protection features may not detect this |
| 712 | * situation. |
| 713 | * |
| 714 | * @param new_thread Pointer to uninitialized struct k_thread |
| 715 | * @param stack Pointer to the stack space. |
| 716 | * @param stack_size Stack size in bytes. |
| 717 | * @param entry Thread entry function. |
| 718 | * @param p1 1st entry point parameter. |
| 719 | * @param p2 2nd entry point parameter. |
| 720 | * @param p3 3rd entry point parameter. |
| 721 | * @param prio Thread priority. |
| 722 | * @param options Thread options. |
| 723 | * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay). |
| 724 | * |
| 725 | * @return ID of new thread. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 726 | * |
| 727 | * @req K-THREAD-001 |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 728 | */ |
Andrew Boie | 662c345 | 2017-10-02 10:51:18 -0700 | [diff] [blame] | 729 | __syscall k_tid_t k_thread_create(struct k_thread *new_thread, |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 730 | k_thread_stack_t *stack, |
Andrew Boie | 662c345 | 2017-10-02 10:51:18 -0700 | [diff] [blame] | 731 | size_t stack_size, |
| 732 | k_thread_entry_t entry, |
| 733 | void *p1, void *p2, void *p3, |
| 734 | int prio, u32_t options, s32_t delay); |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 735 | |
Andrew Boie | 3f091b5 | 2017-08-30 14:34:14 -0700 | [diff] [blame] | 736 | /** |
| 737 | * @brief Drop a thread's privileges permanently to user mode |
| 738 | * |
| 739 | * @param entry Function to start executing from |
| 740 | * @param p1 1st entry point parameter |
| 741 | * @param p2 2nd entry point parameter |
| 742 | * @param p3 3rd entry point parameter |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 743 | * @req K-THREAD-003 |
Andrew Boie | 3f091b5 | 2017-08-30 14:34:14 -0700 | [diff] [blame] | 744 | */ |
| 745 | extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry, |
| 746 | void *p1, void *p2, |
| 747 | void *p3); |
Andrew Boie | 3f091b5 | 2017-08-30 14:34:14 -0700 | [diff] [blame] | 748 | |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 749 | /** |
Adithya Baglody | 392219e | 2019-01-02 14:40:39 +0530 | [diff] [blame] | 750 | * @brief Grant a thread access to a set of kernel objects |
Andrew Boie | e12857a | 2017-10-17 11:38:26 -0700 | [diff] [blame] | 751 | * |
| 752 | * This is a convenience function. For the provided thread, grant access to |
| 753 | * the remaining arguments, which must be pointers to kernel objects. |
Andrew Boie | e12857a | 2017-10-17 11:38:26 -0700 | [diff] [blame] | 754 | * |
| 755 | * The thread object must be initialized (i.e. running). The objects don't |
| 756 | * need to be. |
Adithya Baglody | 392219e | 2019-01-02 14:40:39 +0530 | [diff] [blame] | 757 | * Note that NULL shouldn't be passed as an argument. |
Andrew Boie | e12857a | 2017-10-17 11:38:26 -0700 | [diff] [blame] | 758 | * |
| 759 | * @param thread Thread to grant access to objects |
Adithya Baglody | 392219e | 2019-01-02 14:40:39 +0530 | [diff] [blame] | 760 | * @param ... list of kernel object pointers |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 761 | * @req K-THREAD-004 |
Andrew Boie | e12857a | 2017-10-17 11:38:26 -0700 | [diff] [blame] | 762 | */ |
Adithya Baglody | 392219e | 2019-01-02 14:40:39 +0530 | [diff] [blame] | 763 | #define k_thread_access_grant(thread, ...) \ |
| 764 | FOR_EACH_FIXED_ARG(k_object_access_grant, thread, __VA_ARGS__) |
Andrew Boie | e12857a | 2017-10-17 11:38:26 -0700 | [diff] [blame] | 765 | |
| 766 | /** |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 767 | * @brief Assign a resource memory pool to a thread |
| 768 | * |
| 769 | * By default, threads have no resource pool assigned unless their parent |
| 770 | * thread has a resource pool, in which case it is inherited. Multiple |
| 771 | * threads may be assigned to the same memory pool. |
| 772 | * |
| 773 | * Changing a thread's resource pool will not migrate allocations from the |
| 774 | * previous pool. |
| 775 | * |
| 776 | * @param thread Target thread to assign a memory pool for resource requests, |
| 777 | * or NULL if the thread should no longer have a memory pool. |
| 778 | * @param pool Memory pool to use for resources. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 779 | * @req K-THREAD-005 |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 780 | */ |
| 781 | static inline void k_thread_resource_pool_assign(struct k_thread *thread, |
| 782 | struct k_mem_pool *pool) |
| 783 | { |
| 784 | thread->resource_pool = pool; |
| 785 | } |
| 786 | |
| 787 | #if (CONFIG_HEAP_MEM_POOL_SIZE > 0) |
| 788 | /** |
| 789 | * @brief Assign the system heap as a thread's resource pool |
| 790 | * |
| 791 | * Similar to k_thread_resource_pool_assign(), but the thread will use |
| 792 | * the kernel heap to draw memory. |
| 793 | * |
| 794 | * Use with caution, as a malicious thread could perform DoS attacks on the |
| 795 | * kernel heap. |
| 796 | * |
| 797 | * @param thread Target thread to assign the system heap for resource requests |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 798 | * |
| 799 | * @req K-THREAD-004 |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 800 | */ |
| 801 | void k_thread_system_pool_assign(struct k_thread *thread); |
| 802 | #endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */ |
| 803 | |
| 804 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 805 | * @brief Put the current thread to sleep. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 806 | * |
Charles E. Youse | a567831 | 2019-05-09 16:46:46 -0700 | [diff] [blame] | 807 | * This routine puts the current thread to sleep for @a duration milliseconds. |
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 | * @param ms Number of milliseconds to sleep. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 810 | * |
Piotr Zięcik | 7700eb2 | 2018-10-25 17:45:08 +0200 | [diff] [blame] | 811 | * @return Zero if the requested time has elapsed or the number of milliseconds |
| 812 | * 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] | 813 | */ |
Charles E. Youse | a567831 | 2019-05-09 16:46:46 -0700 | [diff] [blame] | 814 | __syscall s32_t k_sleep(s32_t ms); |
| 815 | |
| 816 | /** |
| 817 | * @brief Put the current thread to sleep with microsecond resolution. |
| 818 | * |
| 819 | * This function is unlikely to work as expected without kernel tuning. |
| 820 | * In particular, because the lower bound on the duration of a sleep is |
| 821 | * the duration of a tick, CONFIG_SYS_CLOCK_TICKS_PER_SEC must be adjusted |
| 822 | * to achieve the resolution desired. The implications of doing this must |
| 823 | * be understood before attempting to use k_usleep(). Use with caution. |
| 824 | * |
| 825 | * @param us Number of microseconds to sleep. |
| 826 | * |
| 827 | * @return Zero if the requested time has elapsed or the number of microseconds |
| 828 | * left to sleep, if thread was woken up by \ref k_wakeup call. |
| 829 | */ |
| 830 | __syscall s32_t k_usleep(s32_t us); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 831 | |
| 832 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 833 | * @brief Cause the current thread to busy wait. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 834 | * |
| 835 | * 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] | 836 | * @a usec_to_wait microseconds. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 837 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 838 | * @return N/A |
| 839 | */ |
Andrew Boie | 42cfd4f | 2018-11-14 14:29:24 -0800 | [diff] [blame] | 840 | __syscall void k_busy_wait(u32_t usec_to_wait); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 841 | |
| 842 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 843 | * @brief Yield the current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 844 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 845 | * This routine causes the current thread to yield execution to another |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 846 | * 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] | 847 | * of the same or higher priority, the routine returns immediately. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 848 | * |
| 849 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 850 | * @req K-THREAD-015 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 851 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 852 | __syscall void k_yield(void); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 853 | |
| 854 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 855 | * @brief Wake up a sleeping thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 856 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 857 | * This routine prematurely wakes up @a thread from sleeping. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 858 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 859 | * If @a thread is not currently sleeping, the routine has no effect. |
| 860 | * |
| 861 | * @param thread ID of thread to wake. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 862 | * |
| 863 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 864 | * @req K-THREAD-014 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 865 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 866 | __syscall void k_wakeup(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 867 | |
| 868 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 869 | * @brief Get thread ID of the current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 870 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 871 | * @return ID of current thread. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 872 | * |
| 873 | * @req K-THREAD-013 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 874 | */ |
Andrew Boie | 76c04a2 | 2017-09-27 14:45:10 -0700 | [diff] [blame] | 875 | __syscall k_tid_t k_current_get(void); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 876 | |
| 877 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 878 | * @brief Abort a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 879 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 880 | * This routine permanently stops execution of @a thread. The thread is taken |
| 881 | * off all kernel queues it is part of (i.e. the ready queue, the timeout |
| 882 | * queue, or a kernel object wait queue). However, any kernel resources the |
| 883 | * thread might currently own (such as mutexes or memory blocks) are not |
| 884 | * released. It is the responsibility of the caller of this routine to ensure |
| 885 | * all necessary cleanup is performed. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 886 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 887 | * @param thread ID of thread to abort. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 888 | * |
| 889 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 890 | * @req K-THREAD-012 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 891 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 892 | __syscall void k_thread_abort(k_tid_t thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 893 | |
Andrew Boie | 7d627c5 | 2017-08-30 11:01:56 -0700 | [diff] [blame] | 894 | |
| 895 | /** |
| 896 | * @brief Start an inactive thread |
| 897 | * |
| 898 | * If a thread was created with K_FOREVER in the delay parameter, it will |
| 899 | * not be added to the scheduling queue until this function is called |
| 900 | * on it. |
| 901 | * |
| 902 | * @param thread thread to start |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 903 | * @req K-THREAD-011 |
Andrew Boie | 7d627c5 | 2017-08-30 11:01:56 -0700 | [diff] [blame] | 904 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 905 | __syscall void k_thread_start(k_tid_t thread); |
Andrew Boie | 7d627c5 | 2017-08-30 11:01:56 -0700 | [diff] [blame] | 906 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 907 | /** |
| 908 | * @cond INTERNAL_HIDDEN |
| 909 | */ |
| 910 | |
Benjamin Walsh | d211a52 | 2016-12-06 11:44:01 -0500 | [diff] [blame] | 911 | /* timeout has timed out and is not on _timeout_q anymore */ |
| 912 | #define _EXPIRED (-2) |
| 913 | |
Peter Mitsis | a04c0d7 | 2016-09-28 19:26:00 -0400 | [diff] [blame] | 914 | struct _static_thread_data { |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 915 | struct k_thread *init_thread; |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 916 | k_thread_stack_t *init_stack; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 917 | unsigned int init_stack_size; |
Andrew Boie | 1e06ffc | 2017-09-11 09:30:04 -0700 | [diff] [blame] | 918 | k_thread_entry_t init_entry; |
Allan Stephens | 7c5bffa | 2016-10-26 10:01:28 -0500 | [diff] [blame] | 919 | void *init_p1; |
| 920 | void *init_p2; |
| 921 | void *init_p3; |
| 922 | int init_prio; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 923 | u32_t init_options; |
| 924 | s32_t init_delay; |
Allan Stephens | 7c5bffa | 2016-10-26 10:01:28 -0500 | [diff] [blame] | 925 | void (*init_abort)(void); |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 926 | const char *init_name; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 927 | }; |
| 928 | |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 929 | #define _THREAD_INITIALIZER(thread, stack, stack_size, \ |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 930 | entry, p1, p2, p3, \ |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 931 | prio, options, delay, abort, tname) \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 932 | { \ |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 933 | .init_thread = (thread), \ |
| 934 | .init_stack = (stack), \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 935 | .init_stack_size = (stack_size), \ |
Andrew Boie | 1e06ffc | 2017-09-11 09:30:04 -0700 | [diff] [blame] | 936 | .init_entry = (k_thread_entry_t)entry, \ |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 937 | .init_p1 = (void *)p1, \ |
| 938 | .init_p2 = (void *)p2, \ |
| 939 | .init_p3 = (void *)p3, \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 940 | .init_prio = (prio), \ |
| 941 | .init_options = (options), \ |
| 942 | .init_delay = (delay), \ |
| 943 | .init_abort = (abort), \ |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 944 | .init_name = STRINGIFY(tname), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 945 | } |
| 946 | |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 947 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 948 | * INTERNAL_HIDDEN @endcond |
| 949 | */ |
| 950 | |
| 951 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 952 | * @brief Statically define and initialize a thread. |
| 953 | * |
| 954 | * The thread may be scheduled for immediate execution or a delayed start. |
| 955 | * |
| 956 | * Thread options are architecture-specific, and can include K_ESSENTIAL, |
| 957 | * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating |
| 958 | * them using "|" (the logical OR operator). |
| 959 | * |
| 960 | * The ID of the thread can be accessed using: |
| 961 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 962 | * @code extern const k_tid_t <name>; @endcode |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 963 | * |
| 964 | * @param name Name of the thread. |
| 965 | * @param stack_size Stack size in bytes. |
| 966 | * @param entry Thread entry function. |
| 967 | * @param p1 1st entry point parameter. |
| 968 | * @param p2 2nd entry point parameter. |
| 969 | * @param p3 3rd entry point parameter. |
| 970 | * @param prio Thread priority. |
| 971 | * @param options Thread options. |
| 972 | * @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] | 973 | * |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 974 | * @req K-THREAD-010 |
| 975 | * |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 976 | * @internal It has been observed that the x86 compiler by default aligns |
| 977 | * these _static_thread_data structures to 32-byte boundaries, thereby |
| 978 | * wasting space. To work around this, force a 4-byte alignment. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 979 | * |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 980 | */ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 981 | #define K_THREAD_DEFINE(name, stack_size, \ |
| 982 | entry, p1, p2, p3, \ |
| 983 | prio, options, delay) \ |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 984 | K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 985 | struct k_thread _k_thread_obj_##name; \ |
| 986 | Z_STRUCT_SECTION_ITERABLE(_static_thread_data, _k_thread_data_##name) =\ |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 987 | _THREAD_INITIALIZER(&_k_thread_obj_##name, \ |
| 988 | _k_thread_stack_##name, stack_size, \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 989 | entry, p1, p2, p3, prio, options, delay, \ |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 990 | NULL, name); \ |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 991 | const k_tid_t name = (k_tid_t)&_k_thread_obj_##name |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 992 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 993 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 994 | * @brief Get a thread's priority. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 995 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 996 | * This routine gets the priority of @a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 997 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 998 | * @param thread ID of thread whose priority is needed. |
| 999 | * |
| 1000 | * @return Priority of @a thread. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1001 | * @req K-THREAD-009 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1002 | */ |
Andrew Boie | 76c04a2 | 2017-09-27 14:45:10 -0700 | [diff] [blame] | 1003 | __syscall int k_thread_priority_get(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1004 | |
| 1005 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1006 | * @brief Set a thread's priority. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1007 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1008 | * This routine immediately changes the priority of @a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1009 | * |
| 1010 | * Rescheduling can occur immediately depending on the priority @a thread is |
| 1011 | * set to: |
| 1012 | * |
| 1013 | * - If its priority is raised above the priority of the caller of this |
| 1014 | * function, and the caller is preemptible, @a thread will be scheduled in. |
| 1015 | * |
| 1016 | * - If the caller operates on itself, it lowers its priority below that of |
| 1017 | * other threads in the system, and the caller is preemptible, the thread of |
| 1018 | * highest priority will be scheduled in. |
| 1019 | * |
| 1020 | * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to |
| 1021 | * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the |
| 1022 | * highest priority. |
| 1023 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1024 | * @param thread ID of thread whose priority is to be set. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1025 | * @param prio New priority. |
| 1026 | * |
| 1027 | * @warning Changing the priority of a thread currently involved in mutex |
| 1028 | * priority inheritance may result in undefined behavior. |
| 1029 | * |
| 1030 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1031 | * @req K-THREAD-008 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1032 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1033 | __syscall void k_thread_priority_set(k_tid_t thread, int prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1034 | |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 1035 | |
| 1036 | #ifdef CONFIG_SCHED_DEADLINE |
| 1037 | /** |
| 1038 | * @brief Set deadline expiration time for scheduler |
| 1039 | * |
| 1040 | * This sets the "deadline" expiration as a time delta from the |
| 1041 | * current time, in the same units used by k_cycle_get_32(). The |
| 1042 | * scheduler (when deadline scheduling is enabled) will choose the |
| 1043 | * next expiring thread when selecting between threads at the same |
| 1044 | * static priority. Threads at different priorities will be scheduled |
| 1045 | * according to their static priority. |
| 1046 | * |
| 1047 | * @note Deadlines that are negative (i.e. in the past) are still seen |
| 1048 | * as higher priority than others, even if the thread has "finished" |
| 1049 | * its work. If you don't want it scheduled anymore, you have to |
| 1050 | * reset the deadline into the future, block/pend the thread, or |
| 1051 | * modify its priority with k_thread_priority_set(). |
| 1052 | * |
| 1053 | * @note Despite the API naming, the scheduler makes no guarantees the |
| 1054 | * the thread WILL be scheduled within that deadline, nor does it take |
| 1055 | * extra metadata (like e.g. the "runtime" and "period" parameters in |
| 1056 | * Linux sched_setattr()) that allows the kernel to validate the |
| 1057 | * scheduling for achievability. Such features could be implemented |
| 1058 | * above this call, which is simply input to the priority selection |
| 1059 | * logic. |
| 1060 | * |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1061 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1062 | * @rst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1063 | * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project |
| 1064 | * configuration. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1065 | * @endrst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1066 | * |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 1067 | * @param thread A thread on which to set the deadline |
| 1068 | * @param deadline A time delta, in cycle units |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1069 | * |
| 1070 | * @req K-THREAD-007 |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 1071 | */ |
| 1072 | __syscall void k_thread_deadline_set(k_tid_t thread, int deadline); |
| 1073 | #endif |
| 1074 | |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1075 | #ifdef CONFIG_SCHED_CPU_MASK |
| 1076 | /** |
| 1077 | * @brief Sets all CPU enable masks to zero |
| 1078 | * |
| 1079 | * After this returns, the thread will no longer be schedulable on any |
| 1080 | * CPUs. The thread must not be currently runnable. |
| 1081 | * |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1082 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1083 | * @rst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1084 | * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project |
| 1085 | * configuration. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1086 | * @endrst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1087 | * |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1088 | * @param thread Thread to operate upon |
| 1089 | * @return Zero on success, otherwise error code |
| 1090 | */ |
| 1091 | int k_thread_cpu_mask_clear(k_tid_t thread); |
| 1092 | |
| 1093 | /** |
| 1094 | * @brief Sets all CPU enable masks to one |
| 1095 | * |
| 1096 | * After this returns, the thread will be schedulable on any CPU. The |
| 1097 | * thread must not be currently runnable. |
| 1098 | * |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1099 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1100 | * @rst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1101 | * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project |
| 1102 | * configuration. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1103 | * @endrst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1104 | * |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1105 | * @param thread Thread to operate upon |
| 1106 | * @return Zero on success, otherwise error code |
| 1107 | */ |
| 1108 | int k_thread_cpu_mask_enable_all(k_tid_t thread); |
| 1109 | |
| 1110 | /** |
| 1111 | * @brief Enable thread to run on specified CPU |
| 1112 | * |
| 1113 | * The thread must not be currently runnable. |
| 1114 | * |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1115 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1116 | * @rst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1117 | * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project |
| 1118 | * configuration. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1119 | * @endrst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1120 | * |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1121 | * @param thread Thread to operate upon |
| 1122 | * @param cpu CPU index |
| 1123 | * @return Zero on success, otherwise error code |
| 1124 | */ |
| 1125 | int k_thread_cpu_mask_enable(k_tid_t thread, int cpu); |
| 1126 | |
| 1127 | /** |
| 1128 | * @brief Prevent thread to run on specified CPU |
| 1129 | * |
| 1130 | * The thread must not be currently runnable. |
| 1131 | * |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1132 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1133 | * @rst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1134 | * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project |
| 1135 | * configuration. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1136 | * @endrst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1137 | * |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1138 | * @param thread Thread to operate upon |
| 1139 | * @param cpu CPU index |
| 1140 | * @return Zero on success, otherwise error code |
| 1141 | */ |
| 1142 | int k_thread_cpu_mask_disable(k_tid_t thread, int cpu); |
| 1143 | #endif |
| 1144 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1145 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1146 | * @brief Suspend a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1147 | * |
Andy Ross | 50d0942 | 2019-11-19 11:20:07 -0800 | [diff] [blame] | 1148 | * This routine prevents the kernel scheduler from making @a thread |
| 1149 | * the current thread. All other internal operations on @a thread are |
| 1150 | * still performed; for example, kernel objects it is waiting on are |
| 1151 | * still handed to it. Note that any existing timeouts |
| 1152 | * (e.g. k_sleep(), or a timeout argument to k_sem_take() et. al.) |
| 1153 | * will be canceled. On resume, the thread will begin running |
| 1154 | * immediately and return from the blocked call. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1155 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1156 | * If @a thread is already suspended, the routine has no effect. |
| 1157 | * |
| 1158 | * @param thread ID of thread to suspend. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1159 | * |
| 1160 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1161 | * @req K-THREAD-005 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1162 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1163 | __syscall void k_thread_suspend(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1164 | |
| 1165 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1166 | * @brief Resume a suspended thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1167 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1168 | * This routine allows the kernel scheduler to make @a thread the current |
| 1169 | * thread, when it is next eligible for that role. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1170 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1171 | * If @a thread is not currently suspended, the routine has no effect. |
| 1172 | * |
| 1173 | * @param thread ID of thread to resume. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1174 | * |
| 1175 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1176 | * @req K-THREAD-006 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1177 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1178 | __syscall void k_thread_resume(k_tid_t thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1179 | |
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 | * @brief Set time-slicing period and scope. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1182 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1183 | * This routine specifies how the scheduler will perform time slicing of |
| 1184 | * preemptible threads. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1185 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1186 | * To enable time slicing, @a slice must be non-zero. The scheduler |
| 1187 | * ensures that no thread runs for more than the specified time limit |
| 1188 | * before other threads of that priority are given a chance to execute. |
| 1189 | * 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] | 1190 | * execute as long as desired without being preempted due to time slicing. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1191 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1192 | * 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] | 1193 | * execute. Once the scheduler selects a thread for execution, there is no |
| 1194 | * minimum guaranteed time the thread will execute before threads of greater or |
| 1195 | * equal priority are scheduled. |
| 1196 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1197 | * When the current thread is the only one of that priority eligible |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1198 | * for execution, this routine has no effect; the thread is immediately |
| 1199 | * rescheduled after the slice period expires. |
| 1200 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1201 | * To disable timeslicing, set both @a slice and @a prio to zero. |
| 1202 | * |
| 1203 | * @param slice Maximum time slice length (in milliseconds). |
| 1204 | * @param prio Highest thread priority level eligible for time slicing. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1205 | * |
| 1206 | * @return N/A |
| 1207 | */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 1208 | extern void k_sched_time_slice_set(s32_t slice, int prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1209 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 1210 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1211 | |
| 1212 | /** |
| 1213 | * @addtogroup isr_apis |
| 1214 | * @{ |
| 1215 | */ |
| 1216 | |
| 1217 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1218 | * @brief Determine if code is running at interrupt level. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1219 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1220 | * This routine allows the caller to customize its actions, depending on |
| 1221 | * whether it is a thread or an ISR. |
| 1222 | * |
| 1223 | * @note Can be called by ISRs. |
| 1224 | * |
Flavio Ceolin | 6a4a86e | 2018-12-17 12:40:22 -0800 | [diff] [blame] | 1225 | * @return false if invoked by a thread. |
| 1226 | * @return true if invoked by an ISR. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1227 | */ |
Flavio Ceolin | 6a4a86e | 2018-12-17 12:40:22 -0800 | [diff] [blame] | 1228 | extern bool k_is_in_isr(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1229 | |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1230 | /** |
| 1231 | * @brief Determine if code is running in a preemptible thread. |
| 1232 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1233 | * This routine allows the caller to customize its actions, depending on |
| 1234 | * whether it can be preempted by another thread. The routine returns a 'true' |
| 1235 | * value if all of the following conditions are met: |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1236 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1237 | * - The code is running in a thread, not at ISR. |
| 1238 | * - The thread's priority is in the preemptible range. |
| 1239 | * - The thread has not locked the scheduler. |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1240 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1241 | * @note Can be called by ISRs. |
| 1242 | * |
| 1243 | * @return 0 if invoked by an ISR or by a cooperative thread. |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1244 | * @return Non-zero if invoked by a preemptible thread. |
| 1245 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1246 | __syscall int k_is_preempt_thread(void); |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1247 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1248 | /** |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 1249 | * @} |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1250 | */ |
| 1251 | |
| 1252 | /** |
| 1253 | * @addtogroup thread_apis |
| 1254 | * @{ |
| 1255 | */ |
| 1256 | |
| 1257 | /** |
| 1258 | * @brief Lock the scheduler. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1259 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1260 | * This routine prevents the current thread from being preempted by another |
| 1261 | * thread by instructing the scheduler to treat it as a cooperative thread. |
| 1262 | * If the thread subsequently performs an operation that makes it unready, |
| 1263 | * it will be context switched out in the normal manner. When the thread |
| 1264 | * again becomes the current thread, its non-preemptible status is maintained. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1265 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1266 | * This routine can be called recursively. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1267 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1268 | * @note k_sched_lock() and k_sched_unlock() should normally be used |
| 1269 | * when the operation being performed can be safely interrupted by ISRs. |
| 1270 | * However, if the amount of processing involved is very small, better |
| 1271 | * performance may be obtained by using irq_lock() and irq_unlock(). |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1272 | * |
| 1273 | * @return N/A |
| 1274 | */ |
| 1275 | extern void k_sched_lock(void); |
| 1276 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1277 | /** |
| 1278 | * @brief Unlock the scheduler. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1279 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1280 | * This routine reverses the effect of a previous call to k_sched_lock(). |
| 1281 | * A thread must call the routine once for each time it called k_sched_lock() |
| 1282 | * before the thread becomes preemptible. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1283 | * |
| 1284 | * @return N/A |
| 1285 | */ |
| 1286 | extern void k_sched_unlock(void); |
| 1287 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1288 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1289 | * @brief Set current thread's custom data. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1290 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1291 | * This routine sets the custom data for the current thread to @ value. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1292 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1293 | * Custom data is not used by the kernel itself, and is freely available |
| 1294 | * for a thread to use as it sees fit. It can be used as a framework |
| 1295 | * upon which to build thread-local storage. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1296 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1297 | * @param value New custom data value. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1298 | * |
| 1299 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1300 | * |
| 1301 | * @req K-THREAD-016 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1302 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1303 | __syscall void k_thread_custom_data_set(void *value); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1304 | |
| 1305 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1306 | * @brief Get current thread's custom data. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1307 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1308 | * This routine returns the custom data for the current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1309 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1310 | * @return Current custom data value. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1311 | * @req K-THREAD-007 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1312 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1313 | __syscall void *k_thread_custom_data_get(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1314 | |
| 1315 | /** |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 1316 | * @brief Set current thread name |
| 1317 | * |
| 1318 | * Set the name of the thread to be used when THREAD_MONITOR is enabled for |
| 1319 | * tracing and debugging. |
| 1320 | * |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 1321 | * @param thread_id Thread to set name, or NULL to set the current thread |
| 1322 | * @param value Name string |
| 1323 | * @retval 0 on success |
| 1324 | * @retval -EFAULT Memory access error with supplied string |
| 1325 | * @retval -ENOSYS Thread name configuration option not enabled |
| 1326 | * @retval -EINVAL Thread name too long |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 1327 | */ |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 1328 | __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] | 1329 | |
| 1330 | /** |
| 1331 | * @brief Get thread name |
| 1332 | * |
| 1333 | * Get the name of a thread |
| 1334 | * |
| 1335 | * @param thread_id Thread ID |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 1336 | * @retval Thread name, or NULL if configuration not enabled |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 1337 | */ |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 1338 | const char *k_thread_name_get(k_tid_t thread_id); |
| 1339 | |
| 1340 | /** |
| 1341 | * @brief Copy the thread name into a supplied buffer |
| 1342 | * |
| 1343 | * @param thread_id Thread to obtain name information |
| 1344 | * @param buf Destination buffer |
David B. Kinder | 73896c0 | 2019-10-28 16:27:57 -0700 | [diff] [blame] | 1345 | * @param size Destination buffer size |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 1346 | * @retval -ENOSPC Destination buffer too small |
| 1347 | * @retval -EFAULT Memory access error |
| 1348 | * @retval -ENOSYS Thread name feature not enabled |
| 1349 | * @retval 0 Success |
| 1350 | */ |
| 1351 | __syscall int k_thread_name_copy(k_tid_t thread_id, char *buf, |
| 1352 | size_t size); |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 1353 | |
| 1354 | /** |
Pavlo Hamov | 8076c80 | 2019-07-31 12:43:54 +0300 | [diff] [blame] | 1355 | * @brief Get thread state string |
| 1356 | * |
| 1357 | * Get the human friendly thread state string |
| 1358 | * |
| 1359 | * @param thread_id Thread ID |
| 1360 | * @retval Thread state string, empty if no state flag is set |
| 1361 | */ |
| 1362 | const char *k_thread_state_str(k_tid_t thread_id); |
| 1363 | |
| 1364 | /** |
Andy Ross | cfe6203 | 2018-09-29 07:34:55 -0700 | [diff] [blame] | 1365 | * @} |
| 1366 | */ |
| 1367 | |
| 1368 | /** |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1369 | * @addtogroup clock_apis |
| 1370 | * @{ |
| 1371 | */ |
| 1372 | |
| 1373 | /** |
| 1374 | * @brief Generate null timeout delay. |
| 1375 | * |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1376 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1377 | * not to wait if the requested operation cannot be performed immediately. |
| 1378 | * |
| 1379 | * @return Timeout delay value. |
| 1380 | */ |
| 1381 | #define K_NO_WAIT 0 |
| 1382 | |
| 1383 | /** |
| 1384 | * @brief Generate timeout delay from milliseconds. |
| 1385 | * |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1386 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1387 | * to wait up to @a ms milliseconds to perform the requested operation. |
| 1388 | * |
| 1389 | * @param ms Duration in milliseconds. |
| 1390 | * |
| 1391 | * @return Timeout delay value. |
| 1392 | */ |
Johan Hedberg | 1447169 | 2016-11-13 10:52:15 +0200 | [diff] [blame] | 1393 | #define K_MSEC(ms) (ms) |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1394 | |
| 1395 | /** |
| 1396 | * @brief Generate timeout delay from seconds. |
| 1397 | * |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1398 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1399 | * to wait up to @a s seconds to perform the requested operation. |
| 1400 | * |
| 1401 | * @param s Duration in seconds. |
| 1402 | * |
| 1403 | * @return Timeout delay value. |
| 1404 | */ |
Johan Hedberg | 1447169 | 2016-11-13 10:52:15 +0200 | [diff] [blame] | 1405 | #define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC) |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1406 | |
| 1407 | /** |
| 1408 | * @brief Generate timeout delay from minutes. |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1409 | |
| 1410 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1411 | * to wait up to @a m minutes to perform the requested operation. |
| 1412 | * |
| 1413 | * @param m Duration in minutes. |
| 1414 | * |
| 1415 | * @return Timeout delay value. |
| 1416 | */ |
Johan Hedberg | 1447169 | 2016-11-13 10:52:15 +0200 | [diff] [blame] | 1417 | #define K_MINUTES(m) K_SECONDS((m) * 60) |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1418 | |
| 1419 | /** |
| 1420 | * @brief Generate timeout delay from hours. |
| 1421 | * |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1422 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1423 | * to wait up to @a h hours to perform the requested operation. |
| 1424 | * |
| 1425 | * @param h Duration in hours. |
| 1426 | * |
| 1427 | * @return Timeout delay value. |
| 1428 | */ |
Johan Hedberg | 1447169 | 2016-11-13 10:52:15 +0200 | [diff] [blame] | 1429 | #define K_HOURS(h) K_MINUTES((h) * 60) |
| 1430 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1431 | /** |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1432 | * @brief Generate infinite timeout delay. |
| 1433 | * |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1434 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1435 | * to wait as long as necessary to perform the requested operation. |
| 1436 | * |
| 1437 | * @return Timeout delay value. |
| 1438 | */ |
| 1439 | #define K_FOREVER (-1) |
| 1440 | |
| 1441 | /** |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 1442 | * @} |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1443 | */ |
| 1444 | |
| 1445 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1446 | * @cond INTERNAL_HIDDEN |
| 1447 | */ |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 1448 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1449 | struct k_timer { |
| 1450 | /* |
| 1451 | * _timeout structure must be first here if we want to use |
| 1452 | * dynamic timer allocation. timeout.node is used in the double-linked |
| 1453 | * list of free timers |
| 1454 | */ |
| 1455 | struct _timeout timeout; |
| 1456 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1457 | /* wait queue for the (single) thread waiting on this timer */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1458 | _wait_q_t wait_q; |
| 1459 | |
| 1460 | /* runs in ISR context */ |
Flavio Ceolin | 4b35dd2 | 2018-11-16 19:06:59 -0800 | [diff] [blame] | 1461 | void (*expiry_fn)(struct k_timer *timer); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1462 | |
| 1463 | /* runs in the context of the thread that calls k_timer_stop() */ |
Flavio Ceolin | 4b35dd2 | 2018-11-16 19:06:59 -0800 | [diff] [blame] | 1464 | void (*stop_fn)(struct k_timer *timer); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1465 | |
| 1466 | /* timer period */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 1467 | s32_t period; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1468 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1469 | /* timer status */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 1470 | u32_t status; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1471 | |
Benjamin Walsh | e4e98f9 | 2017-01-12 19:38:53 -0500 | [diff] [blame] | 1472 | /* user-specific data, also used to support legacy features */ |
| 1473 | void *user_data; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1474 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 1475 | _OBJECT_TRACING_NEXT_PTR(k_timer) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 1476 | _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1477 | }; |
| 1478 | |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 1479 | #define Z_TIMER_INITIALIZER(obj, expiry, stop) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1480 | { \ |
Krzysztof Chruscinski | be06327 | 2019-02-13 11:19:54 +0100 | [diff] [blame] | 1481 | .timeout = { \ |
| 1482 | .node = {},\ |
| 1483 | .dticks = 0, \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1484 | .fn = z_timer_expiration_handler \ |
Krzysztof Chruscinski | be06327 | 2019-02-13 11:19:54 +0100 | [diff] [blame] | 1485 | }, \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1486 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 1487 | .expiry_fn = expiry, \ |
| 1488 | .stop_fn = stop, \ |
Krzysztof Chruscinski | be06327 | 2019-02-13 11:19:54 +0100 | [diff] [blame] | 1489 | .period = 0, \ |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 1490 | .status = 0, \ |
Benjamin Walsh | e4e98f9 | 2017-01-12 19:38:53 -0500 | [diff] [blame] | 1491 | .user_data = 0, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 1492 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1493 | } |
| 1494 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 1495 | #define K_TIMER_INITIALIZER __DEPRECATED_MACRO Z_TIMER_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 1496 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1497 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1498 | * INTERNAL_HIDDEN @endcond |
| 1499 | */ |
| 1500 | |
| 1501 | /** |
| 1502 | * @defgroup timer_apis Timer APIs |
| 1503 | * @ingroup kernel_apis |
| 1504 | * @{ |
| 1505 | */ |
| 1506 | |
| 1507 | /** |
Allan Stephens | 5eceb85 | 2016-11-16 10:16:30 -0500 | [diff] [blame] | 1508 | * @typedef k_timer_expiry_t |
| 1509 | * @brief Timer expiry function type. |
| 1510 | * |
| 1511 | * A timer's expiry function is executed by the system clock interrupt handler |
| 1512 | * each time the timer expires. The expiry function is optional, and is only |
| 1513 | * invoked if the timer has been initialized with one. |
| 1514 | * |
| 1515 | * @param timer Address of timer. |
| 1516 | * |
| 1517 | * @return N/A |
| 1518 | */ |
| 1519 | typedef void (*k_timer_expiry_t)(struct k_timer *timer); |
| 1520 | |
| 1521 | /** |
| 1522 | * @typedef k_timer_stop_t |
| 1523 | * @brief Timer stop function type. |
| 1524 | * |
| 1525 | * A timer's stop function is executed if the timer is stopped prematurely. |
| 1526 | * The function runs in the context of the thread that stops the timer. |
| 1527 | * The stop function is optional, and is only invoked if the timer has been |
| 1528 | * initialized with one. |
| 1529 | * |
| 1530 | * @param timer Address of timer. |
| 1531 | * |
| 1532 | * @return N/A |
| 1533 | */ |
| 1534 | typedef void (*k_timer_stop_t)(struct k_timer *timer); |
| 1535 | |
| 1536 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1537 | * @brief Statically define and initialize a timer. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1538 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1539 | * 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] | 1540 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 1541 | * @code extern struct k_timer <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1542 | * |
| 1543 | * @param name Name of the timer variable. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1544 | * @param expiry_fn Function to invoke each time the timer expires. |
| 1545 | * @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] | 1546 | */ |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 1547 | #define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 1548 | Z_STRUCT_SECTION_ITERABLE(k_timer, name) = \ |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 1549 | Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1550 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1551 | /** |
| 1552 | * @brief Initialize a timer. |
| 1553 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1554 | * This routine initializes a timer, prior to its first use. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1555 | * |
| 1556 | * @param timer Address of timer. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1557 | * @param expiry_fn Function to invoke each time the timer expires. |
| 1558 | * @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] | 1559 | * |
| 1560 | * @return N/A |
| 1561 | */ |
| 1562 | extern void k_timer_init(struct k_timer *timer, |
Allan Stephens | 5eceb85 | 2016-11-16 10:16:30 -0500 | [diff] [blame] | 1563 | k_timer_expiry_t expiry_fn, |
| 1564 | k_timer_stop_t stop_fn); |
Andy Ross | 8d8b2ac | 2016-09-23 10:08:54 -0700 | [diff] [blame] | 1565 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1566 | /** |
| 1567 | * @brief Start a timer. |
| 1568 | * |
| 1569 | * This routine starts a timer, and resets its status to zero. The timer |
| 1570 | * begins counting down using the specified duration and period values. |
| 1571 | * |
| 1572 | * Attempting to start a timer that is already running is permitted. |
| 1573 | * The timer's status is reset to zero and the timer begins counting down |
| 1574 | * using the new duration and period values. |
| 1575 | * |
| 1576 | * @param timer Address of timer. |
| 1577 | * @param duration Initial timer duration (in milliseconds). |
| 1578 | * @param period Timer period (in milliseconds). |
| 1579 | * |
| 1580 | * @return N/A |
| 1581 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1582 | __syscall void k_timer_start(struct k_timer *timer, |
| 1583 | s32_t duration, s32_t period); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1584 | |
| 1585 | /** |
| 1586 | * @brief Stop a timer. |
| 1587 | * |
| 1588 | * This routine stops a running timer prematurely. The timer's stop function, |
| 1589 | * if one exists, is invoked by the caller. |
| 1590 | * |
| 1591 | * 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] | 1592 | * effect on the timer. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1593 | * |
Anas Nashif | 4fb12ae | 2017-02-01 20:06:55 -0500 | [diff] [blame] | 1594 | * @note Can be called by ISRs. The stop handler has to be callable from ISRs |
| 1595 | * if @a k_timer_stop is to be called from ISRs. |
| 1596 | * |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1597 | * @param timer Address of timer. |
| 1598 | * |
| 1599 | * @return N/A |
| 1600 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1601 | __syscall void k_timer_stop(struct k_timer *timer); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1602 | |
| 1603 | /** |
| 1604 | * @brief Read timer status. |
| 1605 | * |
| 1606 | * This routine reads the timer's status, which indicates the number of times |
| 1607 | * it has expired since its status was last read. |
| 1608 | * |
| 1609 | * Calling this routine resets the timer's status to zero. |
| 1610 | * |
| 1611 | * @param timer Address of timer. |
| 1612 | * |
| 1613 | * @return Timer status. |
| 1614 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1615 | __syscall u32_t k_timer_status_get(struct k_timer *timer); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1616 | |
| 1617 | /** |
| 1618 | * @brief Synchronize thread to timer expiration. |
| 1619 | * |
| 1620 | * This routine blocks the calling thread until the timer's status is non-zero |
| 1621 | * (indicating that it has expired at least once since it was last examined) |
| 1622 | * or the timer is stopped. If the timer status is already non-zero, |
| 1623 | * or the timer is already stopped, the caller continues without waiting. |
| 1624 | * |
| 1625 | * Calling this routine resets the timer's status to zero. |
| 1626 | * |
| 1627 | * This routine must not be used by interrupt handlers, since they are not |
| 1628 | * allowed to block. |
| 1629 | * |
| 1630 | * @param timer Address of timer. |
| 1631 | * |
| 1632 | * @return Timer status. |
| 1633 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1634 | __syscall u32_t k_timer_status_sync(struct k_timer *timer); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1635 | |
Andy Ross | 52e444b | 2018-09-28 09:06:37 -0700 | [diff] [blame] | 1636 | extern s32_t z_timeout_remaining(struct _timeout *timeout); |
| 1637 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1638 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1639 | * @brief Get time remaining before a timer next expires. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1640 | * |
| 1641 | * This routine computes the (approximate) time remaining before a running |
| 1642 | * timer next expires. If the timer is not running, it returns zero. |
| 1643 | * |
| 1644 | * @param timer Address of timer. |
| 1645 | * |
| 1646 | * @return Remaining time (in milliseconds). |
| 1647 | */ |
Flavio Ceolin | f1e5303 | 2018-12-04 16:03:13 -0800 | [diff] [blame] | 1648 | __syscall u32_t k_timer_remaining_get(struct k_timer *timer); |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1649 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1650 | 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] | 1651 | { |
Charles E. Youse | 0ad4022 | 2019-03-01 10:51:04 -0800 | [diff] [blame] | 1652 | const s32_t ticks = z_timeout_remaining(&timer->timeout); |
Andy Ross | 8892406 | 2019-10-03 11:43:10 -0700 | [diff] [blame] | 1653 | return (ticks > 0) ? (u32_t)k_ticks_to_ms_floor64(ticks) : 0U; |
Johan Hedberg | f99ad3f | 2016-12-09 10:39:49 +0200 | [diff] [blame] | 1654 | } |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1655 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1656 | /** |
Benjamin Walsh | e4e98f9 | 2017-01-12 19:38:53 -0500 | [diff] [blame] | 1657 | * @brief Associate user-specific data with a timer. |
| 1658 | * |
| 1659 | * This routine records the @a user_data with the @a timer, to be retrieved |
| 1660 | * later. |
| 1661 | * |
| 1662 | * It can be used e.g. in a timer handler shared across multiple subsystems to |
| 1663 | * retrieve data specific to the subsystem this timer is associated with. |
| 1664 | * |
| 1665 | * @param timer Address of timer. |
| 1666 | * @param user_data User data to associate with the timer. |
| 1667 | * |
| 1668 | * @return N/A |
| 1669 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1670 | __syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data); |
| 1671 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 1672 | /** |
| 1673 | * @internal |
| 1674 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1675 | 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] | 1676 | void *user_data) |
Benjamin Walsh | e4e98f9 | 2017-01-12 19:38:53 -0500 | [diff] [blame] | 1677 | { |
| 1678 | timer->user_data = user_data; |
| 1679 | } |
| 1680 | |
| 1681 | /** |
| 1682 | * @brief Retrieve the user-specific data from a timer. |
| 1683 | * |
| 1684 | * @param timer Address of timer. |
| 1685 | * |
| 1686 | * @return The user data. |
| 1687 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1688 | __syscall void *k_timer_user_data_get(struct k_timer *timer); |
| 1689 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1690 | 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] | 1691 | { |
| 1692 | return timer->user_data; |
| 1693 | } |
| 1694 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 1695 | /** @} */ |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1696 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1697 | /** |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1698 | * @addtogroup clock_apis |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1699 | * @{ |
| 1700 | */ |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1701 | |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1702 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1703 | * @brief Get system uptime. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1704 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1705 | * This routine returns the elapsed time since the system booted, |
| 1706 | * in milliseconds. |
| 1707 | * |
David B. Kinder | 00c41ea | 2019-06-10 11:13:33 -0700 | [diff] [blame] | 1708 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1709 | * @rst |
David B. Kinder | 00c41ea | 2019-06-10 11:13:33 -0700 | [diff] [blame] | 1710 | * While this function returns time in milliseconds, it does |
| 1711 | * not mean it has millisecond resolution. The actual resolution depends on |
Andy Ross | 669730f | 2019-06-11 11:18:20 -0700 | [diff] [blame] | 1712 | * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1713 | * @endrst |
Paul Sokolovsky | 65d51fd | 2019-02-04 22:44:50 +0300 | [diff] [blame] | 1714 | * |
| 1715 | * @return Current uptime in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1716 | */ |
Andrew Boie | a73d373 | 2017-10-08 12:23:55 -0700 | [diff] [blame] | 1717 | __syscall s64_t k_uptime_get(void); |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1718 | |
Ramesh Thomas | 89ffd44 | 2017-02-05 19:37:19 -0800 | [diff] [blame] | 1719 | /** |
| 1720 | * @brief Enable clock always on in tickless kernel |
| 1721 | * |
Andy Ross | 1db9f18 | 2019-06-25 10:09:45 -0700 | [diff] [blame] | 1722 | * Deprecated. This does nothing (it was always just a hint). This |
| 1723 | * functionality has been migrated to the SYSTEM_CLOCK_SLOPPY_IDLE |
| 1724 | * kconfig. |
Ramesh Thomas | 89ffd44 | 2017-02-05 19:37:19 -0800 | [diff] [blame] | 1725 | * |
| 1726 | * @retval prev_status Previous status of always on flag |
| 1727 | */ |
Andy Ross | 1db9f18 | 2019-06-25 10:09:45 -0700 | [diff] [blame] | 1728 | /* LCOV_EXCL_START */ |
| 1729 | __deprecated static inline int k_enable_sys_clock_always_on(void) |
| 1730 | { |
| 1731 | __ASSERT(IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE), |
| 1732 | "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead"); |
| 1733 | |
| 1734 | return !IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE); |
| 1735 | } |
| 1736 | /* LCOV_EXCL_STOP */ |
Ramesh Thomas | 89ffd44 | 2017-02-05 19:37:19 -0800 | [diff] [blame] | 1737 | |
| 1738 | /** |
| 1739 | * @brief Disable clock always on in tickless kernel |
| 1740 | * |
Andy Ross | 1db9f18 | 2019-06-25 10:09:45 -0700 | [diff] [blame] | 1741 | * Deprecated. This does nothing (it was always just a hint). This |
| 1742 | * functionality has been migrated to the SYS_CLOCK_SLOPPY_IDLE |
| 1743 | * kconfig. |
Ramesh Thomas | 89ffd44 | 2017-02-05 19:37:19 -0800 | [diff] [blame] | 1744 | */ |
Andy Ross | 1db9f18 | 2019-06-25 10:09:45 -0700 | [diff] [blame] | 1745 | /* LCOV_EXCL_START */ |
| 1746 | __deprecated static inline void k_disable_sys_clock_always_on(void) |
| 1747 | { |
| 1748 | __ASSERT(!IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE), |
| 1749 | "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead"); |
| 1750 | } |
| 1751 | /* LCOV_EXCL_STOP */ |
Ramesh Thomas | 89ffd44 | 2017-02-05 19:37:19 -0800 | [diff] [blame] | 1752 | |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1753 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1754 | * @brief Get system uptime (32-bit version). |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1755 | * |
Peter Bigot | a6067a3 | 2019-08-28 08:19:26 -0500 | [diff] [blame] | 1756 | * This routine returns the lower 32 bits of the system uptime in |
| 1757 | * milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1758 | * |
Peter Bigot | a6067a3 | 2019-08-28 08:19:26 -0500 | [diff] [blame] | 1759 | * Because correct conversion requires full precision of the system |
| 1760 | * clock there is no benefit to using this over k_uptime_get() unless |
| 1761 | * you know the application will never run long enough for the system |
| 1762 | * clock to approach 2^32 ticks. Calls to this function may involve |
| 1763 | * interrupt blocking and 64-bit math. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1764 | * |
David B. Kinder | 00c41ea | 2019-06-10 11:13:33 -0700 | [diff] [blame] | 1765 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1766 | * @rst |
David B. Kinder | 00c41ea | 2019-06-10 11:13:33 -0700 | [diff] [blame] | 1767 | * While this function returns time in milliseconds, it does |
| 1768 | * not mean it has millisecond resolution. The actual resolution depends on |
Andy Ross | 669730f | 2019-06-11 11:18:20 -0700 | [diff] [blame] | 1769 | * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1770 | * @endrst |
Paul Sokolovsky | 65d51fd | 2019-02-04 22:44:50 +0300 | [diff] [blame] | 1771 | * |
Peter Bigot | a6067a3 | 2019-08-28 08:19:26 -0500 | [diff] [blame] | 1772 | * @return The low 32 bits of the current uptime, in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1773 | */ |
Peter Bigot | a6067a3 | 2019-08-28 08:19:26 -0500 | [diff] [blame] | 1774 | static inline u32_t k_uptime_get_32(void) |
| 1775 | { |
| 1776 | return (u32_t)k_uptime_get(); |
| 1777 | } |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1778 | |
| 1779 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1780 | * @brief Get elapsed time. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1781 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1782 | * This routine computes the elapsed time between the current system uptime |
| 1783 | * and an earlier reference time, in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1784 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1785 | * @param reftime Pointer to a reference time, which is updated to the current |
| 1786 | * uptime upon return. |
| 1787 | * |
| 1788 | * @return Elapsed time. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1789 | */ |
Andy Ross | 987c0e5 | 2018-09-27 16:50:00 -0700 | [diff] [blame] | 1790 | static inline s64_t k_uptime_delta(s64_t *reftime) |
| 1791 | { |
| 1792 | s64_t uptime, delta; |
| 1793 | |
| 1794 | uptime = k_uptime_get(); |
| 1795 | delta = uptime - *reftime; |
| 1796 | *reftime = uptime; |
| 1797 | |
| 1798 | return delta; |
| 1799 | } |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1800 | |
| 1801 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1802 | * @brief Get elapsed time (32-bit version). |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1803 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1804 | * This routine computes the elapsed time between the current system uptime |
| 1805 | * and an earlier reference time, in milliseconds. |
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 | * This routine can be more efficient than k_uptime_delta(), as it reduces the |
| 1808 | * need for interrupt locking and 64-bit math. However, the 32-bit result |
| 1809 | * cannot hold an elapsed time larger than approximately 50 days, so the |
| 1810 | * caller must handle possible rollovers. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1811 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1812 | * @param reftime Pointer to a reference time, which is updated to the current |
| 1813 | * uptime upon return. |
| 1814 | * |
| 1815 | * @return Elapsed time. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1816 | */ |
Andy Ross | 987c0e5 | 2018-09-27 16:50:00 -0700 | [diff] [blame] | 1817 | static inline u32_t k_uptime_delta_32(s64_t *reftime) |
| 1818 | { |
| 1819 | return (u32_t)k_uptime_delta(reftime); |
| 1820 | } |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1821 | |
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 | * @brief Read the hardware clock. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1824 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1825 | * This routine returns the current time, as measured by the system's hardware |
| 1826 | * clock. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1827 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1828 | * @return Current hardware clock up-counter (in cycles). |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1829 | */ |
Andrew Boie | 979b17f | 2019-10-03 15:20:41 -0700 | [diff] [blame] | 1830 | static inline u32_t k_cycle_get_32(void) |
| 1831 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 1832 | return arch_k_cycle_get_32(); |
Andrew Boie | 979b17f | 2019-10-03 15:20:41 -0700 | [diff] [blame] | 1833 | } |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1834 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1835 | /** |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 1836 | * @} |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1837 | */ |
| 1838 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1839 | /** |
| 1840 | * @cond INTERNAL_HIDDEN |
| 1841 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1842 | |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1843 | struct k_queue { |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1844 | sys_sflist_t data_q; |
Andy Ross | 603ea42 | 2018-07-25 13:01:54 -0700 | [diff] [blame] | 1845 | struct k_spinlock lock; |
Luiz Augusto von Dentz | 84db641 | 2017-07-13 12:43:59 +0300 | [diff] [blame] | 1846 | union { |
| 1847 | _wait_q_t wait_q; |
| 1848 | |
| 1849 | _POLL_EVENT; |
| 1850 | }; |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1851 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 1852 | _OBJECT_TRACING_NEXT_PTR(k_queue) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 1853 | _OBJECT_TRACING_LINKED_FLAG |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1854 | }; |
| 1855 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 1856 | #define _K_QUEUE_INITIALIZER(obj) \ |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1857 | { \ |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1858 | .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \ |
Stephanos Ioannidis | f628dcd | 2019-09-11 18:09:49 +0900 | [diff] [blame] | 1859 | .lock = { }, \ |
| 1860 | { \ |
| 1861 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
| 1862 | _POLL_EVENT_OBJ_INIT(obj) \ |
| 1863 | }, \ |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1864 | _OBJECT_TRACING_INIT \ |
| 1865 | } |
| 1866 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 1867 | #define K_QUEUE_INITIALIZER __DEPRECATED_MACRO _K_QUEUE_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 1868 | |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1869 | extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free); |
| 1870 | |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1871 | /** |
| 1872 | * INTERNAL_HIDDEN @endcond |
| 1873 | */ |
| 1874 | |
| 1875 | /** |
| 1876 | * @defgroup queue_apis Queue APIs |
| 1877 | * @ingroup kernel_apis |
| 1878 | * @{ |
| 1879 | */ |
| 1880 | |
| 1881 | /** |
| 1882 | * @brief Initialize a queue. |
| 1883 | * |
| 1884 | * This routine initializes a queue object, prior to its first use. |
| 1885 | * |
| 1886 | * @param queue Address of the queue. |
| 1887 | * |
| 1888 | * @return N/A |
| 1889 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1890 | __syscall void k_queue_init(struct k_queue *queue); |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1891 | |
| 1892 | /** |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 1893 | * @brief Cancel waiting on a queue. |
| 1894 | * |
| 1895 | * This routine causes first thread pending on @a queue, if any, to |
| 1896 | * 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] | 1897 | * If the queue is being waited on by k_poll(), it will return with |
| 1898 | * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent |
| 1899 | * k_queue_get() will return NULL). |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 1900 | * |
| 1901 | * @note Can be called by ISRs. |
| 1902 | * |
| 1903 | * @param queue Address of the queue. |
| 1904 | * |
| 1905 | * @return N/A |
| 1906 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1907 | __syscall void k_queue_cancel_wait(struct k_queue *queue); |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 1908 | |
| 1909 | /** |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1910 | * @brief Append an element to the end of a queue. |
| 1911 | * |
| 1912 | * 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] | 1913 | * aligned on a word boundary, and the first word of the item is reserved |
| 1914 | * for the kernel's use. |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1915 | * |
| 1916 | * @note Can be called by ISRs. |
| 1917 | * |
| 1918 | * @param queue Address of the queue. |
| 1919 | * @param data Address of the data item. |
| 1920 | * |
| 1921 | * @return N/A |
| 1922 | */ |
| 1923 | extern void k_queue_append(struct k_queue *queue, void *data); |
| 1924 | |
| 1925 | /** |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1926 | * @brief Append an element to a queue. |
| 1927 | * |
Andrew Boie | ac3dcc1 | 2019-04-01 12:28:03 -0700 | [diff] [blame] | 1928 | * This routine appends a data item to @a queue. There is an implicit memory |
| 1929 | * allocation to create an additional temporary bookkeeping data structure from |
| 1930 | * the calling thread's resource pool, which is automatically freed when the |
| 1931 | * item is removed. The data itself is not copied. |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1932 | * |
| 1933 | * @note Can be called by ISRs. |
| 1934 | * |
| 1935 | * @param queue Address of the queue. |
| 1936 | * @param data Address of the data item. |
| 1937 | * |
| 1938 | * @retval 0 on success |
| 1939 | * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool |
| 1940 | */ |
Adithya Baglody | 2a78b8d | 2018-10-25 12:09:04 +0530 | [diff] [blame] | 1941 | __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] | 1942 | |
| 1943 | /** |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1944 | * @brief Prepend an element to a queue. |
| 1945 | * |
| 1946 | * 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] | 1947 | * aligned on a word boundary, and the first word of the item is reserved |
| 1948 | * for the kernel's use. |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1949 | * |
| 1950 | * @note Can be called by ISRs. |
| 1951 | * |
| 1952 | * @param queue Address of the queue. |
| 1953 | * @param data Address of the data item. |
| 1954 | * |
| 1955 | * @return N/A |
| 1956 | */ |
| 1957 | extern void k_queue_prepend(struct k_queue *queue, void *data); |
| 1958 | |
| 1959 | /** |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1960 | * @brief Prepend an element to a queue. |
| 1961 | * |
Andrew Boie | ac3dcc1 | 2019-04-01 12:28:03 -0700 | [diff] [blame] | 1962 | * This routine prepends a data item to @a queue. There is an implicit memory |
| 1963 | * allocation to create an additional temporary bookkeeping data structure from |
| 1964 | * the calling thread's resource pool, which is automatically freed when the |
| 1965 | * item is removed. The data itself is not copied. |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1966 | * |
| 1967 | * @note Can be called by ISRs. |
| 1968 | * |
| 1969 | * @param queue Address of the queue. |
| 1970 | * @param data Address of the data item. |
| 1971 | * |
| 1972 | * @retval 0 on success |
| 1973 | * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool |
| 1974 | */ |
Adithya Baglody | 2a78b8d | 2018-10-25 12:09:04 +0530 | [diff] [blame] | 1975 | __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] | 1976 | |
| 1977 | /** |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1978 | * @brief Inserts an element to a queue. |
| 1979 | * |
| 1980 | * 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] | 1981 | * data item must be aligned on a word boundary, and the first word of |
| 1982 | * the item is reserved for the kernel's use. |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1983 | * |
| 1984 | * @note Can be called by ISRs. |
| 1985 | * |
| 1986 | * @param queue Address of the queue. |
| 1987 | * @param prev Address of the previous data item. |
| 1988 | * @param data Address of the data item. |
| 1989 | * |
| 1990 | * @return N/A |
| 1991 | */ |
| 1992 | extern void k_queue_insert(struct k_queue *queue, void *prev, void *data); |
| 1993 | |
| 1994 | /** |
| 1995 | * @brief Atomically append a list of elements to a queue. |
| 1996 | * |
| 1997 | * 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] | 1998 | * 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] | 1999 | * in each data item pointing to the next data item; the list must be |
| 2000 | * NULL-terminated. |
| 2001 | * |
| 2002 | * @note Can be called by ISRs. |
| 2003 | * |
| 2004 | * @param queue Address of the queue. |
| 2005 | * @param head Pointer to first node in singly-linked list. |
| 2006 | * @param tail Pointer to last node in singly-linked list. |
| 2007 | * |
| 2008 | * @return N/A |
| 2009 | */ |
| 2010 | extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail); |
| 2011 | |
| 2012 | /** |
| 2013 | * @brief Atomically add a list of elements to a queue. |
| 2014 | * |
| 2015 | * This routine adds a list of data items to @a queue in one operation. |
| 2016 | * The data items must be in a singly-linked list implemented using a |
| 2017 | * sys_slist_t object. Upon completion, the original list is empty. |
| 2018 | * |
| 2019 | * @note Can be called by ISRs. |
| 2020 | * |
| 2021 | * @param queue Address of the queue. |
| 2022 | * @param list Pointer to sys_slist_t object. |
| 2023 | * |
| 2024 | * @return N/A |
| 2025 | */ |
| 2026 | extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list); |
| 2027 | |
| 2028 | /** |
| 2029 | * @brief Get an element from a queue. |
| 2030 | * |
Nicolas Pitre | 659fa0d | 2019-05-21 22:13:01 -0400 | [diff] [blame] | 2031 | * This routine removes first data item from @a queue. The first word of the |
| 2032 | * data item is reserved for the kernel's use. |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2033 | * |
| 2034 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 2035 | * |
| 2036 | * @param queue Address of the queue. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 2037 | * @param timeout Non-negative waiting period to obtain a data item (in |
| 2038 | * milliseconds), or one of the special values K_NO_WAIT and |
| 2039 | * K_FOREVER. |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2040 | * |
| 2041 | * @return Address of the data item if successful; NULL if returned |
| 2042 | * without waiting, or waiting period timed out. |
| 2043 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2044 | __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] | 2045 | |
| 2046 | /** |
Luiz Augusto von Dentz | 50b9377 | 2017-07-03 16:52:45 +0300 | [diff] [blame] | 2047 | * @brief Remove an element from a queue. |
| 2048 | * |
Nicolas Pitre | 659fa0d | 2019-05-21 22:13:01 -0400 | [diff] [blame] | 2049 | * This routine removes data item from @a queue. The first word of the |
| 2050 | * 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] | 2051 | * rely on sys_slist_find_and_remove which is not a constant time operation. |
| 2052 | * |
| 2053 | * @note Can be called by ISRs |
| 2054 | * |
| 2055 | * @param queue Address of the queue. |
| 2056 | * @param data Address of the data item. |
| 2057 | * |
| 2058 | * @return true if data item was removed |
| 2059 | */ |
| 2060 | static inline bool k_queue_remove(struct k_queue *queue, void *data) |
| 2061 | { |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2062 | 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] | 2063 | } |
| 2064 | |
| 2065 | /** |
Dhananjay Gundapu Jayakrishnan | 24bfa40 | 2018-08-22 12:33:00 +0200 | [diff] [blame] | 2066 | * @brief Append an element to a queue only if it's not present already. |
| 2067 | * |
Nicolas Pitre | 659fa0d | 2019-05-21 22:13:01 -0400 | [diff] [blame] | 2068 | * This routine appends data item to @a queue. The first word of the data |
| 2069 | * 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] | 2070 | * relies on sys_slist_is_node_in_list which is not a constant time operation. |
| 2071 | * |
| 2072 | * @note Can be called by ISRs |
| 2073 | * |
| 2074 | * @param queue Address of the queue. |
| 2075 | * @param data Address of the data item. |
| 2076 | * |
| 2077 | * @return true if data item was added, false if not |
| 2078 | */ |
| 2079 | static inline bool k_queue_unique_append(struct k_queue *queue, void *data) |
| 2080 | { |
| 2081 | sys_sfnode_t *test; |
| 2082 | |
| 2083 | SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) { |
| 2084 | if (test == (sys_sfnode_t *) data) { |
| 2085 | return false; |
| 2086 | } |
| 2087 | } |
| 2088 | |
| 2089 | k_queue_append(queue, data); |
| 2090 | return true; |
| 2091 | } |
| 2092 | |
| 2093 | /** |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2094 | * @brief Query a queue to see if it has data available. |
| 2095 | * |
| 2096 | * Note that the data might be already gone by the time this function returns |
| 2097 | * if other threads are also trying to read from the queue. |
| 2098 | * |
| 2099 | * @note Can be called by ISRs. |
| 2100 | * |
| 2101 | * @param queue Address of the queue. |
| 2102 | * |
| 2103 | * @return Non-zero if the queue is empty. |
| 2104 | * @return 0 if data is available. |
| 2105 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2106 | __syscall int k_queue_is_empty(struct k_queue *queue); |
| 2107 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2108 | 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] | 2109 | { |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2110 | return (int)sys_sflist_is_empty(&queue->data_q); |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2111 | } |
| 2112 | |
| 2113 | /** |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2114 | * @brief Peek element at the head of queue. |
| 2115 | * |
| 2116 | * Return element from the head of queue without removing it. |
| 2117 | * |
| 2118 | * @param queue Address of the queue. |
| 2119 | * |
| 2120 | * @return Head element, or NULL if queue is empty. |
| 2121 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2122 | __syscall void *k_queue_peek_head(struct k_queue *queue); |
| 2123 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2124 | 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] | 2125 | { |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2126 | 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] | 2127 | } |
| 2128 | |
| 2129 | /** |
| 2130 | * @brief Peek element at the tail of queue. |
| 2131 | * |
| 2132 | * Return element from the tail of queue without removing it. |
| 2133 | * |
| 2134 | * @param queue Address of the queue. |
| 2135 | * |
| 2136 | * @return Tail element, or NULL if queue is empty. |
| 2137 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2138 | __syscall void *k_queue_peek_tail(struct k_queue *queue); |
| 2139 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2140 | 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] | 2141 | { |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2142 | 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] | 2143 | } |
| 2144 | |
| 2145 | /** |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2146 | * @brief Statically define and initialize a queue. |
| 2147 | * |
| 2148 | * The queue can be accessed outside the module where it is defined using: |
| 2149 | * |
| 2150 | * @code extern struct k_queue <name>; @endcode |
| 2151 | * |
| 2152 | * @param name Name of the queue. |
| 2153 | */ |
| 2154 | #define K_QUEUE_DEFINE(name) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 2155 | Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2156 | _K_QUEUE_INITIALIZER(name) |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2157 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 2158 | /** @} */ |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2159 | |
Wentong Wu | 5611e92 | 2019-06-20 23:51:27 +0800 | [diff] [blame] | 2160 | #ifdef CONFIG_USERSPACE |
| 2161 | /** |
| 2162 | * @brief futex structure |
| 2163 | * |
| 2164 | * A k_futex is a lightweight mutual exclusion primitive designed |
| 2165 | * to minimize kernel involvement. Uncontended operation relies |
| 2166 | * only on atomic access to shared memory. k_futex are tracked as |
| 2167 | * kernel objects and can live in user memory so any access bypass |
| 2168 | * the kernel object permission management mechanism. |
| 2169 | */ |
| 2170 | struct k_futex { |
| 2171 | atomic_t val; |
| 2172 | }; |
| 2173 | |
| 2174 | /** |
| 2175 | * @brief futex kernel data structure |
| 2176 | * |
| 2177 | * z_futex_data are the helper data structure for k_futex to complete |
| 2178 | * futex contended operation on kernel side, structure z_futex_data |
| 2179 | * of every futex object is invisible in user mode. |
| 2180 | */ |
| 2181 | struct z_futex_data { |
| 2182 | _wait_q_t wait_q; |
| 2183 | struct k_spinlock lock; |
| 2184 | }; |
| 2185 | |
| 2186 | #define Z_FUTEX_DATA_INITIALIZER(obj) \ |
| 2187 | { \ |
| 2188 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q) \ |
| 2189 | } |
| 2190 | |
| 2191 | /** |
| 2192 | * @defgroup futex_apis FUTEX APIs |
| 2193 | * @ingroup kernel_apis |
| 2194 | * @{ |
| 2195 | */ |
| 2196 | |
| 2197 | /** |
Wentong Wu | 5611e92 | 2019-06-20 23:51:27 +0800 | [diff] [blame] | 2198 | * @brief Pend the current thread on a futex |
| 2199 | * |
| 2200 | * Tests that the supplied futex contains the expected value, and if so, |
| 2201 | * goes to sleep until some other thread calls k_futex_wake() on it. |
| 2202 | * |
| 2203 | * @param futex Address of the futex. |
| 2204 | * @param expected Expected value of the futex, if it is different the caller |
| 2205 | * will not wait on it. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 2206 | * @param timeout Non-negative waiting period on the futex, in milliseconds, or |
| 2207 | * one of the special values K_NO_WAIT or K_FOREVER. |
Wentong Wu | 5611e92 | 2019-06-20 23:51:27 +0800 | [diff] [blame] | 2208 | * @retval -EACCES Caller does not have read access to futex address. |
| 2209 | * @retval -EAGAIN If the futex value did not match the expected parameter. |
| 2210 | * @retval -EINVAL Futex parameter address not recognized by the kernel. |
| 2211 | * @retval -ETIMEDOUT Thread woke up due to timeout and not a futex wakeup. |
| 2212 | * @retval 0 if the caller went to sleep and was woken up. The caller |
| 2213 | * should check the futex's value on wakeup to determine if it needs |
| 2214 | * to block again. |
| 2215 | */ |
| 2216 | __syscall int k_futex_wait(struct k_futex *futex, int expected, s32_t timeout); |
| 2217 | |
| 2218 | /** |
| 2219 | * @brief Wake one/all threads pending on a futex |
| 2220 | * |
| 2221 | * Wake up the highest priority thread pending on the supplied futex, or |
| 2222 | * wakeup all the threads pending on the supplied futex, and the behavior |
| 2223 | * depends on wake_all. |
| 2224 | * |
| 2225 | * @param futex Futex to wake up pending threads. |
| 2226 | * @param wake_all If true, wake up all pending threads; If false, |
| 2227 | * wakeup the highest priority thread. |
| 2228 | * @retval -EACCES Caller does not have access to the futex address. |
| 2229 | * @retval -EINVAL Futex parameter address not recognized by the kernel. |
| 2230 | * @retval Number of threads that were woken up. |
| 2231 | */ |
| 2232 | __syscall int k_futex_wake(struct k_futex *futex, bool wake_all); |
| 2233 | |
| 2234 | /** @} */ |
| 2235 | #endif |
| 2236 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2237 | struct k_fifo { |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2238 | struct k_queue _queue; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2239 | }; |
| 2240 | |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2241 | /** |
| 2242 | * @cond INTERNAL_HIDDEN |
| 2243 | */ |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 2244 | #define Z_FIFO_INITIALIZER(obj) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2245 | { \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2246 | ._queue = _K_QUEUE_INITIALIZER(obj._queue) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2247 | } |
| 2248 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 2249 | #define K_FIFO_INITIALIZER __DEPRECATED_MACRO Z_FIFO_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2250 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2251 | /** |
| 2252 | * INTERNAL_HIDDEN @endcond |
| 2253 | */ |
| 2254 | |
| 2255 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2256 | * @defgroup fifo_apis FIFO APIs |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2257 | * @ingroup kernel_apis |
| 2258 | * @{ |
| 2259 | */ |
| 2260 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2261 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2262 | * @brief Initialize a FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2263 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2264 | * This routine initializes a FIFO queue, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2265 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2266 | * @param fifo Address of the FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2267 | * |
| 2268 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2269 | * @req K-FIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2270 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2271 | #define k_fifo_init(fifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2272 | k_queue_init(&(fifo)->_queue) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2273 | |
| 2274 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2275 | * @brief Cancel waiting on a FIFO queue. |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 2276 | * |
| 2277 | * This routine causes first thread pending on @a fifo, if any, to |
| 2278 | * return from k_fifo_get() call with NULL value (as if timeout |
| 2279 | * expired). |
| 2280 | * |
| 2281 | * @note Can be called by ISRs. |
| 2282 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2283 | * @param fifo Address of the FIFO queue. |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 2284 | * |
| 2285 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2286 | * @req K-FIFO-001 |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 2287 | */ |
| 2288 | #define k_fifo_cancel_wait(fifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2289 | k_queue_cancel_wait(&(fifo)->_queue) |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 2290 | |
| 2291 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2292 | * @brief Add an element to a FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2293 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2294 | * 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] | 2295 | * aligned on a word boundary, and the first word of the item is reserved |
| 2296 | * for the kernel's use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2297 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2298 | * @note Can be called by ISRs. |
| 2299 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2300 | * @param fifo Address of the FIFO. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2301 | * @param data Address of the data item. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2302 | * |
| 2303 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2304 | * @req K-FIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2305 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2306 | #define k_fifo_put(fifo, data) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2307 | k_queue_append(&(fifo)->_queue, data) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2308 | |
| 2309 | /** |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2310 | * @brief Add an element to a FIFO queue. |
| 2311 | * |
Andrew Boie | ac3dcc1 | 2019-04-01 12:28:03 -0700 | [diff] [blame] | 2312 | * This routine adds a data item to @a fifo. There is an implicit memory |
| 2313 | * allocation to create an additional temporary bookkeeping data structure from |
| 2314 | * the calling thread's resource pool, which is automatically freed when the |
| 2315 | * item is removed. The data itself is not copied. |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2316 | * |
| 2317 | * @note Can be called by ISRs. |
| 2318 | * |
| 2319 | * @param fifo Address of the FIFO. |
| 2320 | * @param data Address of the data item. |
| 2321 | * |
| 2322 | * @retval 0 on success |
| 2323 | * @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] | 2324 | * @req K-FIFO-001 |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2325 | */ |
| 2326 | #define k_fifo_alloc_put(fifo, data) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2327 | k_queue_alloc_append(&(fifo)->_queue, data) |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2328 | |
| 2329 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2330 | * @brief Atomically add a list of elements to a FIFO. |
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 | * 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] | 2333 | * 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] | 2334 | * each data item pointing to the next data item; the list must be |
| 2335 | * NULL-terminated. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2336 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2337 | * @note Can be called by ISRs. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2338 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2339 | * @param fifo Address of the FIFO queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2340 | * @param head Pointer to first node in singly-linked list. |
| 2341 | * @param tail Pointer to last node in singly-linked list. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2342 | * |
| 2343 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2344 | * @req K-FIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2345 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2346 | #define k_fifo_put_list(fifo, head, tail) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2347 | k_queue_append_list(&(fifo)->_queue, head, tail) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2348 | |
| 2349 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2350 | * @brief Atomically add a list of elements to a FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2351 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2352 | * This routine adds a list of data items to @a fifo in one operation. |
| 2353 | * The data items must be in a singly-linked list implemented using a |
| 2354 | * 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] | 2355 | * and must be re-initialized via sys_slist_init(). |
| 2356 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2357 | * @note Can be called by ISRs. |
| 2358 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2359 | * @param fifo Address of the FIFO queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2360 | * @param list Pointer to sys_slist_t object. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2361 | * |
| 2362 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2363 | * @req K-FIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2364 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2365 | #define k_fifo_put_slist(fifo, list) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2366 | k_queue_merge_slist(&(fifo)->_queue, list) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2367 | |
| 2368 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2369 | * @brief Get an element from a FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2370 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2371 | * 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] | 2372 | * 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] | 2373 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2374 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 2375 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2376 | * @param fifo Address of the FIFO queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2377 | * @param timeout Waiting period to obtain a data item (in milliseconds), |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2378 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 2379 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2380 | * @return Address of the data item if successful; NULL if returned |
| 2381 | * without waiting, or waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2382 | * @req K-FIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2383 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2384 | #define k_fifo_get(fifo, timeout) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2385 | k_queue_get(&(fifo)->_queue, timeout) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2386 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2387 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2388 | * @brief Query a FIFO queue to see if it has data available. |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2389 | * |
| 2390 | * 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] | 2391 | * if other threads is also trying to read from the FIFO. |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2392 | * |
| 2393 | * @note Can be called by ISRs. |
| 2394 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2395 | * @param fifo Address of the FIFO queue. |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2396 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2397 | * @return Non-zero if the FIFO queue is empty. |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2398 | * @return 0 if data is available. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2399 | * @req K-FIFO-001 |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2400 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2401 | #define k_fifo_is_empty(fifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2402 | k_queue_is_empty(&(fifo)->_queue) |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2403 | |
| 2404 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2405 | * @brief Peek element at the head of a FIFO queue. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2406 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2407 | * 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] | 2408 | * 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] | 2409 | * on each iteration of processing, a head container will be peeked, |
| 2410 | * 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] | 2411 | * it will be completely remove from the FIFO queue. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2412 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2413 | * @param fifo Address of the FIFO queue. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2414 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2415 | * @return Head element, or NULL if the FIFO queue is empty. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2416 | * @req K-FIFO-001 |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2417 | */ |
| 2418 | #define k_fifo_peek_head(fifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2419 | k_queue_peek_head(&(fifo)->_queue) |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2420 | |
| 2421 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2422 | * @brief Peek element at the tail of FIFO queue. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2423 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2424 | * Return element from the tail of FIFO queue (without removing it). A usecase |
| 2425 | * for this is if elements of the FIFO queue are themselves containers. Then |
| 2426 | * 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] | 2427 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2428 | * @param fifo Address of the FIFO queue. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2429 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2430 | * @return Tail element, or NULL if a FIFO queue is empty. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2431 | * @req K-FIFO-001 |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2432 | */ |
| 2433 | #define k_fifo_peek_tail(fifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2434 | k_queue_peek_tail(&(fifo)->_queue) |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2435 | |
| 2436 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2437 | * @brief Statically define and initialize a FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2438 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2439 | * 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] | 2440 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 2441 | * @code extern struct k_fifo <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2442 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2443 | * @param name Name of the FIFO queue. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2444 | * @req K-FIFO-002 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2445 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2446 | #define K_FIFO_DEFINE(name) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 2447 | Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \ |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 2448 | Z_FIFO_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2449 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 2450 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2451 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2452 | struct k_lifo { |
Luiz Augusto von Dentz | 0dc4dd4 | 2017-02-21 15:49:52 +0200 | [diff] [blame] | 2453 | struct k_queue _queue; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2454 | }; |
| 2455 | |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2456 | /** |
| 2457 | * @cond INTERNAL_HIDDEN |
| 2458 | */ |
| 2459 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2460 | #define _K_LIFO_INITIALIZER(obj) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2461 | { \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2462 | ._queue = _K_QUEUE_INITIALIZER(obj._queue) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2463 | } |
| 2464 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 2465 | #define K_LIFO_INITIALIZER __DEPRECATED_MACRO _K_LIFO_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2466 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2467 | /** |
| 2468 | * INTERNAL_HIDDEN @endcond |
| 2469 | */ |
| 2470 | |
| 2471 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2472 | * @defgroup lifo_apis LIFO APIs |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2473 | * @ingroup kernel_apis |
| 2474 | * @{ |
| 2475 | */ |
| 2476 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2477 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2478 | * @brief Initialize a LIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2479 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2480 | * This routine initializes a LIFO queue object, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2481 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2482 | * @param lifo Address of the LIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2483 | * |
| 2484 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2485 | * @req K-LIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2486 | */ |
Luiz Augusto von Dentz | 0dc4dd4 | 2017-02-21 15:49:52 +0200 | [diff] [blame] | 2487 | #define k_lifo_init(lifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2488 | k_queue_init(&(lifo)->_queue) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2489 | |
| 2490 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2491 | * @brief Add an element to a LIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2492 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2493 | * 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] | 2494 | * 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] | 2495 | * reserved for the kernel's use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2496 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2497 | * @note Can be called by ISRs. |
| 2498 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2499 | * @param lifo Address of the LIFO queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2500 | * @param data Address of the data item. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2501 | * |
| 2502 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2503 | * @req K-LIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2504 | */ |
Luiz Augusto von Dentz | 0dc4dd4 | 2017-02-21 15:49:52 +0200 | [diff] [blame] | 2505 | #define k_lifo_put(lifo, data) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2506 | k_queue_prepend(&(lifo)->_queue, data) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2507 | |
| 2508 | /** |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2509 | * @brief Add an element to a LIFO queue. |
| 2510 | * |
Andrew Boie | ac3dcc1 | 2019-04-01 12:28:03 -0700 | [diff] [blame] | 2511 | * This routine adds a data item to @a lifo. There is an implicit memory |
| 2512 | * allocation to create an additional temporary bookkeeping data structure from |
| 2513 | * the calling thread's resource pool, which is automatically freed when the |
| 2514 | * item is removed. The data itself is not copied. |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2515 | * |
| 2516 | * @note Can be called by ISRs. |
| 2517 | * |
| 2518 | * @param lifo Address of the LIFO. |
| 2519 | * @param data Address of the data item. |
| 2520 | * |
| 2521 | * @retval 0 on success |
| 2522 | * @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] | 2523 | * @req K-LIFO-001 |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2524 | */ |
| 2525 | #define k_lifo_alloc_put(lifo, data) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2526 | k_queue_alloc_prepend(&(lifo)->_queue, data) |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2527 | |
| 2528 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2529 | * @brief Get an element from a LIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2530 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2531 | * 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] | 2532 | * 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] | 2533 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2534 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 2535 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2536 | * @param lifo Address of the LIFO queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2537 | * @param timeout Waiting period to obtain a data item (in milliseconds), |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2538 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 2539 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2540 | * @return Address of the data item if successful; NULL if returned |
| 2541 | * without waiting, or waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2542 | * @req K-LIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2543 | */ |
Luiz Augusto von Dentz | 0dc4dd4 | 2017-02-21 15:49:52 +0200 | [diff] [blame] | 2544 | #define k_lifo_get(lifo, timeout) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2545 | k_queue_get(&(lifo)->_queue, timeout) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2546 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2547 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2548 | * @brief Statically define and initialize a LIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2549 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2550 | * 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] | 2551 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 2552 | * @code extern struct k_lifo <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2553 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2554 | * @param name Name of the fifo. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2555 | * @req K-LIFO-002 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2556 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2557 | #define K_LIFO_DEFINE(name) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 2558 | Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2559 | _K_LIFO_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2560 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 2561 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2562 | |
| 2563 | /** |
| 2564 | * @cond INTERNAL_HIDDEN |
| 2565 | */ |
Adithya Baglody | 28080d3 | 2018-10-15 11:48:51 +0530 | [diff] [blame] | 2566 | #define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2567 | |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2568 | typedef uintptr_t stack_data_t; |
| 2569 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2570 | struct k_stack { |
| 2571 | _wait_q_t wait_q; |
Andy Ross | f0933d0 | 2018-07-26 10:23:02 -0700 | [diff] [blame] | 2572 | struct k_spinlock lock; |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2573 | stack_data_t *base, *next, *top; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2574 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 2575 | _OBJECT_TRACING_NEXT_PTR(k_stack) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 2576 | _OBJECT_TRACING_LINKED_FLAG |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2577 | u8_t flags; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2578 | }; |
| 2579 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2580 | #define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2581 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2582 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2583 | .base = stack_buffer, \ |
| 2584 | .next = stack_buffer, \ |
| 2585 | .top = stack_buffer + stack_num_entries, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 2586 | _OBJECT_TRACING_INIT \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2587 | } |
| 2588 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 2589 | #define K_STACK_INITIALIZER __DEPRECATED_MACRO _K_STACK_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2590 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2591 | /** |
| 2592 | * INTERNAL_HIDDEN @endcond |
| 2593 | */ |
| 2594 | |
| 2595 | /** |
| 2596 | * @defgroup stack_apis Stack APIs |
| 2597 | * @ingroup kernel_apis |
| 2598 | * @{ |
| 2599 | */ |
| 2600 | |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2601 | /** |
| 2602 | * @brief Initialize a stack. |
| 2603 | * |
| 2604 | * This routine initializes a stack object, prior to its first use. |
| 2605 | * |
| 2606 | * @param stack Address of the stack. |
| 2607 | * @param buffer Address of array used to hold stacked values. |
| 2608 | * @param num_entries Maximum number of values that can be stacked. |
| 2609 | * |
| 2610 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2611 | * @req K-STACK-001 |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2612 | */ |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2613 | void k_stack_init(struct k_stack *stack, |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2614 | stack_data_t *buffer, u32_t num_entries); |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2615 | |
| 2616 | |
| 2617 | /** |
| 2618 | * @brief Initialize a stack. |
| 2619 | * |
| 2620 | * This routine initializes a stack object, prior to its first use. Internal |
| 2621 | * buffers will be allocated from the calling thread's resource pool. |
| 2622 | * This memory will be released if k_stack_cleanup() is called, or |
| 2623 | * userspace is enabled and the stack object loses all references to it. |
| 2624 | * |
| 2625 | * @param stack Address of the stack. |
| 2626 | * @param num_entries Maximum number of values that can be stacked. |
| 2627 | * |
| 2628 | * @return -ENOMEM if memory couldn't be allocated |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2629 | * @req K-STACK-001 |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2630 | */ |
| 2631 | |
Adithya Baglody | 28080d3 | 2018-10-15 11:48:51 +0530 | [diff] [blame] | 2632 | __syscall s32_t k_stack_alloc_init(struct k_stack *stack, |
| 2633 | u32_t num_entries); |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2634 | |
| 2635 | /** |
| 2636 | * @brief Release a stack's allocated buffer |
| 2637 | * |
| 2638 | * If a stack object was given a dynamically allocated buffer via |
| 2639 | * k_stack_alloc_init(), this will free it. This function does nothing |
| 2640 | * if the buffer wasn't dynamically allocated. |
| 2641 | * |
| 2642 | * @param stack Address of the stack. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2643 | * @req K-STACK-001 |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2644 | */ |
| 2645 | void k_stack_cleanup(struct k_stack *stack); |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2646 | |
| 2647 | /** |
| 2648 | * @brief Push an element onto a stack. |
| 2649 | * |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2650 | * 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] | 2651 | * |
| 2652 | * @note Can be called by ISRs. |
| 2653 | * |
| 2654 | * @param stack Address of the stack. |
| 2655 | * @param data Value to push onto the stack. |
| 2656 | * |
| 2657 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2658 | * @req K-STACK-001 |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2659 | */ |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2660 | __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] | 2661 | |
| 2662 | /** |
| 2663 | * @brief Pop an element from a stack. |
| 2664 | * |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2665 | * This routine removes a stack_data_t value from @a stack in a "last in, |
| 2666 | * first out" manner and stores the value in @a data. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2667 | * |
| 2668 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 2669 | * |
| 2670 | * @param stack Address of the stack. |
| 2671 | * @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] | 2672 | * @param timeout Non-negative waiting period to obtain a value (in |
| 2673 | * milliseconds), or one of the special values K_NO_WAIT and |
| 2674 | * K_FOREVER. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2675 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2676 | * @retval 0 Element popped from stack. |
| 2677 | * @retval -EBUSY Returned without waiting. |
| 2678 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2679 | * @req K-STACK-001 |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2680 | */ |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 2681 | __syscall int k_stack_pop(struct k_stack *stack, stack_data_t *data, |
| 2682 | s32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2683 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2684 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2685 | * @brief Statically define and initialize a stack |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2686 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2687 | * 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] | 2688 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 2689 | * @code extern struct k_stack <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2690 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2691 | * @param name Name of the stack. |
| 2692 | * @param stack_num_entries Maximum number of values that can be stacked. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2693 | * @req K-STACK-002 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2694 | */ |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 2695 | #define K_STACK_DEFINE(name, stack_num_entries) \ |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2696 | stack_data_t __noinit \ |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 2697 | _k_stack_buf_##name[stack_num_entries]; \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 2698 | Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2699 | _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \ |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 2700 | stack_num_entries) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2701 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 2702 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2703 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2704 | struct k_work; |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 2705 | struct k_work_poll; |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2706 | |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 2707 | /* private, used by k_poll and k_work_poll */ |
Piotr Zięcik | 1c4177d | 2019-08-27 12:19:26 +0200 | [diff] [blame] | 2708 | typedef int (*_poller_cb_t)(struct k_poll_event *event, u32_t state); |
| 2709 | struct _poller { |
| 2710 | volatile bool is_polling; |
| 2711 | struct k_thread *thread; |
| 2712 | _poller_cb_t cb; |
| 2713 | }; |
| 2714 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2715 | /** |
Anas Nashif | 29f37f0 | 2019-01-21 14:30:35 -0500 | [diff] [blame] | 2716 | * @addtogroup thread_apis |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2717 | * @{ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2718 | */ |
| 2719 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2720 | /** |
| 2721 | * @typedef k_work_handler_t |
| 2722 | * @brief Work item handler function type. |
| 2723 | * |
| 2724 | * A work item's handler function is executed by a workqueue's thread |
| 2725 | * when the work item is processed by the workqueue. |
| 2726 | * |
| 2727 | * @param work Address of the work item. |
| 2728 | * |
| 2729 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2730 | * @req K-WORK-001 |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2731 | */ |
| 2732 | typedef void (*k_work_handler_t)(struct k_work *work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2733 | |
| 2734 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2735 | * @cond INTERNAL_HIDDEN |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2736 | */ |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2737 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2738 | struct k_work_q { |
Luiz Augusto von Dentz | adb581b | 2017-07-03 19:09:44 +0300 | [diff] [blame] | 2739 | struct k_queue queue; |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 2740 | struct k_thread thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2741 | }; |
| 2742 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2743 | enum { |
Iván Briano | 9c7b5ea | 2016-10-04 18:11:05 -0300 | [diff] [blame] | 2744 | K_WORK_STATE_PENDING, /* Work item pending state */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2745 | }; |
| 2746 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2747 | struct k_work { |
Luiz Augusto von Dentz | adb581b | 2017-07-03 19:09:44 +0300 | [diff] [blame] | 2748 | void *_reserved; /* Used by k_queue implementation. */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2749 | k_work_handler_t handler; |
| 2750 | atomic_t flags[1]; |
| 2751 | }; |
| 2752 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2753 | struct k_delayed_work { |
| 2754 | struct k_work work; |
| 2755 | struct _timeout timeout; |
| 2756 | struct k_work_q *work_q; |
| 2757 | }; |
| 2758 | |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 2759 | struct k_work_poll { |
| 2760 | struct k_work work; |
| 2761 | struct _poller poller; |
| 2762 | struct k_poll_event *events; |
| 2763 | int num_events; |
| 2764 | k_work_handler_t real_handler; |
| 2765 | struct _timeout timeout; |
| 2766 | int poll_result; |
| 2767 | }; |
| 2768 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2769 | extern struct k_work_q k_sys_work_q; |
| 2770 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2771 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2772 | * INTERNAL_HIDDEN @endcond |
| 2773 | */ |
| 2774 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2775 | #define Z_WORK_INITIALIZER(work_handler) \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2776 | { \ |
| 2777 | ._reserved = NULL, \ |
| 2778 | .handler = work_handler, \ |
| 2779 | .flags = { 0 } \ |
| 2780 | } |
| 2781 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 2782 | #define K_WORK_INITIALIZER __DEPRECATED_MACRO Z_WORK_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2783 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2784 | /** |
| 2785 | * @brief Initialize a statically-defined work item. |
| 2786 | * |
| 2787 | * This macro can be used to initialize a statically-defined workqueue work |
| 2788 | * item, prior to its first use. For example, |
| 2789 | * |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2790 | * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2791 | * |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2792 | * @param work Symbol name for work item object |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2793 | * @param work_handler Function to invoke each time work item is processed. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2794 | * @req K-WORK-002 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2795 | */ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2796 | #define K_WORK_DEFINE(work, work_handler) \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2797 | struct k_work work = Z_WORK_INITIALIZER(work_handler) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2798 | |
| 2799 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2800 | * @brief Initialize a work item. |
| 2801 | * |
| 2802 | * This routine initializes a workqueue work item, prior to its first use. |
| 2803 | * |
| 2804 | * @param work Address of work item. |
| 2805 | * @param handler Function to invoke each time work item is processed. |
| 2806 | * |
| 2807 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2808 | * @req K-WORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2809 | */ |
| 2810 | static inline void k_work_init(struct k_work *work, k_work_handler_t handler) |
| 2811 | { |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2812 | *work = (struct k_work)Z_WORK_INITIALIZER(handler); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2813 | } |
| 2814 | |
| 2815 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2816 | * @brief Submit a work item. |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 2817 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2818 | * This routine submits work item @a work to be processed by workqueue |
| 2819 | * @a work_q. If the work item is already pending in the workqueue's queue |
| 2820 | * as a result of an earlier submission, this routine has no effect on the |
| 2821 | * work item. If the work item has already been processed, or is currently |
| 2822 | * being processed, its work is considered complete and the work item can be |
| 2823 | * resubmitted. |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 2824 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2825 | * @warning |
| 2826 | * A submitted work item must not be modified until it has been processed |
| 2827 | * by the workqueue. |
| 2828 | * |
| 2829 | * @note Can be called by ISRs. |
| 2830 | * |
| 2831 | * @param work_q Address of workqueue. |
| 2832 | * @param work Address of work item. |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 2833 | * |
| 2834 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2835 | * @req K-WORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2836 | */ |
| 2837 | static inline void k_work_submit_to_queue(struct k_work_q *work_q, |
| 2838 | struct k_work *work) |
| 2839 | { |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 2840 | 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] | 2841 | k_queue_append(&work_q->queue, work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2842 | } |
| 2843 | } |
| 2844 | |
| 2845 | /** |
Andrew Boie | 2b1d54e | 2018-11-12 14:25:19 -0800 | [diff] [blame] | 2846 | * @brief Submit a work item to a user mode workqueue |
| 2847 | * |
David B. Kinder | 06d7835 | 2018-12-17 14:32:40 -0800 | [diff] [blame] | 2848 | * 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] | 2849 | * memory allocation is made from the caller's resource pool which is freed |
| 2850 | * once the worker thread consumes the k_work item. The workqueue |
| 2851 | * thread must have memory access to the k_work item being submitted. The caller |
| 2852 | * must have permission granted on the work_q parameter's queue object. |
| 2853 | * |
| 2854 | * Otherwise this works the same as k_work_submit_to_queue(). |
| 2855 | * |
| 2856 | * @note Can be called by ISRs. |
| 2857 | * |
| 2858 | * @param work_q Address of workqueue. |
| 2859 | * @param work Address of work item. |
| 2860 | * |
| 2861 | * @retval -EBUSY if the work item was already in some workqueue |
| 2862 | * @retval -ENOMEM if no memory for thread resource pool allocation |
| 2863 | * @retval 0 Success |
| 2864 | * @req K-WORK-001 |
| 2865 | */ |
| 2866 | static inline int k_work_submit_to_user_queue(struct k_work_q *work_q, |
| 2867 | struct k_work *work) |
| 2868 | { |
| 2869 | int ret = -EBUSY; |
| 2870 | |
| 2871 | if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) { |
| 2872 | ret = k_queue_alloc_append(&work_q->queue, work); |
| 2873 | |
| 2874 | /* Couldn't insert into the queue. Clear the pending bit |
| 2875 | * so the work item can be submitted again |
| 2876 | */ |
Flavio Ceolin | 76b3518 | 2018-12-16 12:48:29 -0800 | [diff] [blame] | 2877 | if (ret != 0) { |
Andrew Boie | 2b1d54e | 2018-11-12 14:25:19 -0800 | [diff] [blame] | 2878 | atomic_clear_bit(work->flags, K_WORK_STATE_PENDING); |
| 2879 | } |
| 2880 | } |
| 2881 | |
| 2882 | return ret; |
| 2883 | } |
| 2884 | |
| 2885 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2886 | * @brief Check if a work item is pending. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2887 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2888 | * This routine indicates if work item @a work is pending in a workqueue's |
| 2889 | * queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2890 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2891 | * @note Can be called by ISRs. |
| 2892 | * |
| 2893 | * @param work Address of work item. |
| 2894 | * |
Flavio Ceolin | 82ef4f8 | 2018-11-21 18:12:34 -0800 | [diff] [blame] | 2895 | * @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] | 2896 | * @req K-WORK-001 |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 2897 | */ |
Flavio Ceolin | 82ef4f8 | 2018-11-21 18:12:34 -0800 | [diff] [blame] | 2898 | static inline bool k_work_pending(struct k_work *work) |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 2899 | { |
Iván Briano | 9c7b5ea | 2016-10-04 18:11:05 -0300 | [diff] [blame] | 2900 | return atomic_test_bit(work->flags, K_WORK_STATE_PENDING); |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 2901 | } |
| 2902 | |
| 2903 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2904 | * @brief Start a workqueue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2905 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2906 | * This routine starts workqueue @a work_q. The workqueue spawns its work |
| 2907 | * processing thread, which runs forever. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2908 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2909 | * @param work_q Address of workqueue. |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 2910 | * @param stack Pointer to work queue thread's stack space, as defined by |
| 2911 | * K_THREAD_STACK_DEFINE() |
| 2912 | * @param stack_size Size of the work queue thread's stack (in bytes), which |
| 2913 | * should either be the same constant passed to |
| 2914 | * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF(). |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2915 | * @param prio Priority of the work queue's thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2916 | * |
| 2917 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2918 | * @req K-WORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2919 | */ |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 2920 | extern void k_work_q_start(struct k_work_q *work_q, |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 2921 | k_thread_stack_t *stack, |
Benjamin Walsh | 669360d | 2016-11-14 16:46:14 -0500 | [diff] [blame] | 2922 | size_t stack_size, int prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2923 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2924 | /** |
Andrew Boie | 2b1d54e | 2018-11-12 14:25:19 -0800 | [diff] [blame] | 2925 | * @brief Start a workqueue in user mode |
| 2926 | * |
| 2927 | * This works identically to k_work_q_start() except it is callable from user |
| 2928 | * mode, and the worker thread created will run in user mode. |
| 2929 | * The caller must have permissions granted on both the work_q parameter's |
| 2930 | * thread and queue objects, and the same restrictions on priority apply as |
| 2931 | * k_thread_create(). |
| 2932 | * |
| 2933 | * @param work_q Address of workqueue. |
| 2934 | * @param stack Pointer to work queue thread's stack space, as defined by |
| 2935 | * K_THREAD_STACK_DEFINE() |
| 2936 | * @param stack_size Size of the work queue thread's stack (in bytes), which |
| 2937 | * should either be the same constant passed to |
| 2938 | * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF(). |
| 2939 | * @param prio Priority of the work queue's thread. |
| 2940 | * |
| 2941 | * @return N/A |
| 2942 | * @req K-WORK-001 |
| 2943 | */ |
| 2944 | extern void k_work_q_user_start(struct k_work_q *work_q, |
| 2945 | k_thread_stack_t *stack, |
| 2946 | size_t stack_size, int prio); |
| 2947 | |
| 2948 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2949 | * @brief Initialize a delayed work item. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2950 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2951 | * This routine initializes a workqueue delayed work item, prior to |
| 2952 | * its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2953 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2954 | * @param work Address of delayed work item. |
| 2955 | * @param handler Function to invoke each time work item is processed. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2956 | * |
| 2957 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2958 | * @req K-DWORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2959 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 2960 | extern void k_delayed_work_init(struct k_delayed_work *work, |
| 2961 | k_work_handler_t handler); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2962 | |
| 2963 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2964 | * @brief Submit a delayed work item. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2965 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2966 | * This routine schedules work item @a work to be processed by workqueue |
| 2967 | * @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] | 2968 | * 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] | 2969 | * Only when the countdown completes is the work item actually submitted to |
| 2970 | * the workqueue and becomes pending. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2971 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2972 | * Submitting a previously submitted delayed work item that is still |
Andy Ross | 03c1d28 | 2018-02-13 12:13:25 -0800 | [diff] [blame] | 2973 | * counting down cancels the existing submission and restarts the |
| 2974 | * countdown using the new delay. Note that this behavior is |
| 2975 | * inherently subject to race conditions with the pre-existing |
| 2976 | * timeouts and work queue, so care must be taken to synchronize such |
| 2977 | * resubmissions externally. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2978 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2979 | * @warning |
| 2980 | * A delayed work item must not be modified until it has been processed |
| 2981 | * by the workqueue. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2982 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2983 | * @note Can be called by ISRs. |
| 2984 | * |
| 2985 | * @param work_q Address of workqueue. |
| 2986 | * @param work Address of delayed work item. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 2987 | * @param delay Non-negative delay before submitting the work item (in |
| 2988 | * milliseconds). |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2989 | * |
| 2990 | * @retval 0 Work item countdown started. |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2991 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 2992 | * @retval -EADDRINUSE Work item is pending on a different workqueue. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2993 | * @req K-DWORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2994 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 2995 | extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q, |
| 2996 | struct k_delayed_work *work, |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 2997 | s32_t delay); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2998 | |
| 2999 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3000 | * @brief Cancel a delayed work item. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3001 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3002 | * This routine cancels the submission of delayed work item @a work. |
David B. Kinder | 8b986d7 | 2017-04-18 15:56:26 -0700 | [diff] [blame] | 3003 | * 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] | 3004 | * underway. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3005 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3006 | * @note Can be called by ISRs. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3007 | * |
Andy Ross | d7ae2a8 | 2019-03-08 08:51:13 -0800 | [diff] [blame] | 3008 | * @note The result of calling this on a k_delayed_work item that has |
| 3009 | * not been submitted (i.e. before the return of the |
| 3010 | * k_delayed_work_submit_to_queue() call) is undefined. |
| 3011 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3012 | * @param work Address of delayed work item. |
| 3013 | * |
David B. Kinder | 8b986d7 | 2017-04-18 15:56:26 -0700 | [diff] [blame] | 3014 | * @retval 0 Work item countdown canceled. |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3015 | * @retval -EINVAL Work item is being processed or has completed its work. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3016 | * @req K-DWORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3017 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 3018 | extern int k_delayed_work_cancel(struct k_delayed_work *work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3019 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3020 | /** |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3021 | * @brief Submit a work item to the system workqueue. |
| 3022 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3023 | * This routine submits work item @a work to be processed by the system |
| 3024 | * workqueue. If the work item is already pending in the workqueue's queue |
| 3025 | * as a result of an earlier submission, this routine has no effect on the |
| 3026 | * work item. If the work item has already been processed, or is currently |
| 3027 | * being processed, its work is considered complete and the work item can be |
| 3028 | * resubmitted. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3029 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3030 | * @warning |
| 3031 | * Work items submitted to the system workqueue should avoid using handlers |
| 3032 | * that block or yield since this may prevent the system workqueue from |
| 3033 | * processing other work items in a timely manner. |
| 3034 | * |
| 3035 | * @note Can be called by ISRs. |
| 3036 | * |
| 3037 | * @param work Address of work item. |
| 3038 | * |
| 3039 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3040 | * @req K-WORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3041 | */ |
| 3042 | static inline void k_work_submit(struct k_work *work) |
| 3043 | { |
| 3044 | k_work_submit_to_queue(&k_sys_work_q, work); |
| 3045 | } |
| 3046 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3047 | /** |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3048 | * @brief Submit a delayed work item to the system workqueue. |
| 3049 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3050 | * This routine schedules work item @a work to be processed by the system |
| 3051 | * workqueue after a delay of @a delay milliseconds. The routine initiates |
David B. Kinder | 8b986d7 | 2017-04-18 15:56:26 -0700 | [diff] [blame] | 3052 | * 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] | 3053 | * Only when the countdown completes is the work item actually submitted to |
| 3054 | * the workqueue and becomes pending. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3055 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3056 | * Submitting a previously submitted delayed work item that is still |
| 3057 | * counting down cancels the existing submission and restarts the countdown |
| 3058 | * using the new delay. If the work item is currently pending on the |
| 3059 | * workqueue's queue because the countdown has completed it is too late to |
| 3060 | * resubmit the item, and resubmission fails without impacting the work item. |
| 3061 | * If the work item has already been processed, or is currently being processed, |
| 3062 | * its work is considered complete and the work item can be resubmitted. |
| 3063 | * |
| 3064 | * @warning |
| 3065 | * Work items submitted to the system workqueue should avoid using handlers |
| 3066 | * that block or yield since this may prevent the system workqueue from |
| 3067 | * processing other work items in a timely manner. |
| 3068 | * |
| 3069 | * @note Can be called by ISRs. |
| 3070 | * |
| 3071 | * @param work Address of delayed work item. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3072 | * @param delay Non-negative delay before submitting the work item (in |
| 3073 | * milliseconds). |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3074 | * |
| 3075 | * @retval 0 Work item countdown started. |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3076 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 3077 | * @retval -EADDRINUSE Work item is pending on a different workqueue. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3078 | * @req K-DWORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3079 | */ |
| 3080 | static inline int k_delayed_work_submit(struct k_delayed_work *work, |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3081 | s32_t delay) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3082 | { |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 3083 | 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] | 3084 | } |
| 3085 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3086 | /** |
Johan Hedberg | c8201b2 | 2016-12-09 10:42:22 +0200 | [diff] [blame] | 3087 | * @brief Get time remaining before a delayed work gets scheduled. |
| 3088 | * |
| 3089 | * This routine computes the (approximate) time remaining before a |
| 3090 | * 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] | 3091 | * scheduled, it returns zero. |
Johan Hedberg | c8201b2 | 2016-12-09 10:42:22 +0200 | [diff] [blame] | 3092 | * |
| 3093 | * @param work Delayed work item. |
| 3094 | * |
| 3095 | * @return Remaining time (in milliseconds). |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3096 | * @req K-DWORK-001 |
Johan Hedberg | c8201b2 | 2016-12-09 10:42:22 +0200 | [diff] [blame] | 3097 | */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3098 | 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] | 3099 | { |
Andy Ross | 8892406 | 2019-10-03 11:43:10 -0700 | [diff] [blame] | 3100 | return k_ticks_to_ms_floor64(z_timeout_remaining(&work->timeout)); |
Johan Hedberg | c8201b2 | 2016-12-09 10:42:22 +0200 | [diff] [blame] | 3101 | } |
| 3102 | |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 3103 | /** |
| 3104 | * @brief Initialize a triggered work item. |
| 3105 | * |
| 3106 | * This routine initializes a workqueue triggered work item, prior to |
| 3107 | * its first use. |
| 3108 | * |
| 3109 | * @param work Address of triggered work item. |
| 3110 | * @param handler Function to invoke each time work item is processed. |
| 3111 | * |
| 3112 | * @return N/A |
| 3113 | */ |
| 3114 | extern void k_work_poll_init(struct k_work_poll *work, |
| 3115 | k_work_handler_t handler); |
| 3116 | |
| 3117 | /** |
| 3118 | * @brief Submit a triggered work item. |
| 3119 | * |
| 3120 | * This routine schedules work item @a work to be processed by workqueue |
| 3121 | * @a work_q when one of the given @a events is signaled. The routine |
| 3122 | * initiates internal poller for the work item and then returns to the caller. |
| 3123 | * Only when one of the watched events happen the work item is actually |
| 3124 | * submitted to the workqueue and becomes pending. |
| 3125 | * |
| 3126 | * Submitting a previously submitted triggered work item that is still |
| 3127 | * waiting for the event cancels the existing submission and reschedules it |
| 3128 | * 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] | 3129 | * 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] | 3130 | * so care must be taken to synchronize such resubmissions externally. |
| 3131 | * |
| 3132 | * @note Can be called by ISRs. |
| 3133 | * |
| 3134 | * @warning |
| 3135 | * Provided array of events as well as a triggered work item must be placed |
| 3136 | * in persistent memory (valid until work handler execution or work |
| 3137 | * cancellation) and cannot be modified after submission. |
| 3138 | * |
| 3139 | * @param work_q Address of workqueue. |
| 3140 | * @param work Address of delayed work item. |
| 3141 | * @param events An array of pointers to events which trigger the work. |
| 3142 | * @param num_events The number of events in the array. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3143 | * @param timeout Non-negative timeout after which the work will be scheduled |
| 3144 | * for execution even if not triggered. |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 3145 | * |
| 3146 | * |
| 3147 | * @retval 0 Work item started watching for events. |
| 3148 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 3149 | * @retval -EADDRINUSE Work item is pending on a different workqueue. |
| 3150 | */ |
| 3151 | extern int k_work_poll_submit_to_queue(struct k_work_q *work_q, |
| 3152 | struct k_work_poll *work, |
| 3153 | struct k_poll_event *events, |
| 3154 | int num_events, |
| 3155 | s32_t timeout); |
| 3156 | |
| 3157 | /** |
| 3158 | * @brief Submit a triggered work item to the system workqueue. |
| 3159 | * |
| 3160 | * This routine schedules work item @a work to be processed by system |
| 3161 | * workqueue when one of the given @a events is signaled. The routine |
| 3162 | * initiates internal poller for the work item and then returns to the caller. |
| 3163 | * Only when one of the watched events happen the work item is actually |
| 3164 | * submitted to the workqueue and becomes pending. |
| 3165 | * |
| 3166 | * Submitting a previously submitted triggered work item that is still |
| 3167 | * waiting for the event cancels the existing submission and reschedules it |
| 3168 | * 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] | 3169 | * 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] | 3170 | * so care must be taken to synchronize such resubmissions externally. |
| 3171 | * |
| 3172 | * @note Can be called by ISRs. |
| 3173 | * |
| 3174 | * @warning |
| 3175 | * Provided array of events as well as a triggered work item must not be |
| 3176 | * modified until the item has been processed by the workqueue. |
| 3177 | * |
| 3178 | * @param work Address of delayed work item. |
| 3179 | * @param events An array of pointers to events which trigger the work. |
| 3180 | * @param num_events The number of events in the array. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3181 | * @param timeout Non-negative timeout after which the work will be scheduled |
| 3182 | * for execution even if not triggered. |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 3183 | * |
| 3184 | * @retval 0 Work item started watching for events. |
| 3185 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 3186 | * @retval -EADDRINUSE Work item is pending on a different workqueue. |
| 3187 | */ |
| 3188 | static inline int k_work_poll_submit(struct k_work_poll *work, |
| 3189 | struct k_poll_event *events, |
| 3190 | int num_events, |
| 3191 | s32_t timeout) |
| 3192 | { |
| 3193 | return k_work_poll_submit_to_queue(&k_sys_work_q, work, |
| 3194 | events, num_events, timeout); |
| 3195 | } |
| 3196 | |
| 3197 | /** |
| 3198 | * @brief Cancel a triggered work item. |
| 3199 | * |
| 3200 | * This routine cancels the submission of triggered work item @a work. |
| 3201 | * A triggered work item can only be canceled if no event triggered work |
| 3202 | * submission. |
| 3203 | * |
| 3204 | * @note Can be called by ISRs. |
| 3205 | * |
| 3206 | * @param work Address of delayed work item. |
| 3207 | * |
David B. Kinder | 73896c0 | 2019-10-28 16:27:57 -0700 | [diff] [blame] | 3208 | * @retval 0 Work item canceled. |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 3209 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 3210 | */ |
| 3211 | extern int k_work_poll_cancel(struct k_work_poll *work); |
| 3212 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 3213 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3214 | /** |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3215 | * @defgroup mutex_apis Mutex APIs |
| 3216 | * @ingroup kernel_apis |
| 3217 | * @{ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3218 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3219 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3220 | /** |
| 3221 | * Mutex Structure |
| 3222 | * @ingroup mutex_apis |
| 3223 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3224 | struct k_mutex { |
| 3225 | _wait_q_t wait_q; |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3226 | /** Mutex owner */ |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 3227 | struct k_thread *owner; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3228 | u32_t lock_count; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3229 | int owner_orig_prio; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3230 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 3231 | _OBJECT_TRACING_NEXT_PTR(k_mutex) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 3232 | _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3233 | }; |
| 3234 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3235 | /** |
| 3236 | * @cond INTERNAL_HIDDEN |
| 3237 | */ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3238 | #define _K_MUTEX_INITIALIZER(obj) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3239 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3240 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3241 | .owner = NULL, \ |
| 3242 | .lock_count = 0, \ |
| 3243 | .owner_orig_prio = K_LOWEST_THREAD_PRIO, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 3244 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3245 | } |
| 3246 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 3247 | #define K_MUTEX_INITIALIZER __DEPRECATED_MACRO _K_MUTEX_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3248 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3249 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3250 | * INTERNAL_HIDDEN @endcond |
| 3251 | */ |
| 3252 | |
| 3253 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3254 | * @brief Statically define and initialize a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3255 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3256 | * 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] | 3257 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 3258 | * @code extern struct k_mutex <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3259 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3260 | * @param name Name of the mutex. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3261 | * @req K-MUTEX-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3262 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3263 | #define K_MUTEX_DEFINE(name) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 3264 | Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3265 | _K_MUTEX_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3266 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3267 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3268 | * @brief Initialize a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3269 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3270 | * This routine initializes a mutex object, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3271 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3272 | * Upon completion, the mutex is available and does not have an owner. |
| 3273 | * |
| 3274 | * @param mutex Address of the mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3275 | * |
| 3276 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3277 | * @req K-MUTEX-002 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3278 | */ |
Andrew Boie | 2f7519b | 2017-09-29 03:33:06 -0700 | [diff] [blame] | 3279 | __syscall void k_mutex_init(struct k_mutex *mutex); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3280 | |
| 3281 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3282 | * @brief Lock a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3283 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3284 | * This routine locks @a mutex. If the mutex is locked by another thread, |
| 3285 | * the calling thread waits until the mutex becomes available or until |
| 3286 | * a timeout occurs. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3287 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3288 | * A thread is permitted to lock a mutex it has already locked. The operation |
| 3289 | * completes immediately and the lock count is increased by 1. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3290 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3291 | * @param mutex Address of the mutex. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3292 | * @param timeout Non-negative waiting period to lock the mutex (in |
| 3293 | * milliseconds), or one of the special values K_NO_WAIT and |
| 3294 | * K_FOREVER. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3295 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3296 | * @retval 0 Mutex locked. |
| 3297 | * @retval -EBUSY Returned without waiting. |
| 3298 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3299 | * @req K-MUTEX-002 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3300 | */ |
Andrew Boie | 2f7519b | 2017-09-29 03:33:06 -0700 | [diff] [blame] | 3301 | __syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3302 | |
| 3303 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3304 | * @brief Unlock a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3305 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3306 | * This routine unlocks @a mutex. The mutex must already be locked by the |
| 3307 | * calling thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3308 | * |
| 3309 | * 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] | 3310 | * 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] | 3311 | * thread. |
| 3312 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3313 | * @param mutex Address of the mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3314 | * |
| 3315 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3316 | * @req K-MUTEX-002 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3317 | */ |
Andrew Boie | 2f7519b | 2017-09-29 03:33:06 -0700 | [diff] [blame] | 3318 | __syscall void k_mutex_unlock(struct k_mutex *mutex); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3319 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3320 | /** |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 3321 | * @} |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3322 | */ |
| 3323 | |
| 3324 | /** |
| 3325 | * @cond INTERNAL_HIDDEN |
| 3326 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3327 | |
| 3328 | struct k_sem { |
| 3329 | _wait_q_t wait_q; |
Adithya Baglody | 4b06621 | 2018-10-16 11:59:12 +0530 | [diff] [blame] | 3330 | u32_t count; |
| 3331 | u32_t limit; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 3332 | _POLL_EVENT; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3333 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 3334 | _OBJECT_TRACING_NEXT_PTR(k_sem) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 3335 | _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3336 | }; |
| 3337 | |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 3338 | #define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3339 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3340 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3341 | .count = initial_count, \ |
| 3342 | .limit = count_limit, \ |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 3343 | _POLL_EVENT_OBJ_INIT(obj) \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 3344 | _OBJECT_TRACING_INIT \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3345 | } |
| 3346 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 3347 | #define K_SEM_INITIALIZER __DEPRECATED_MACRO Z_SEM_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3348 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3349 | /** |
| 3350 | * INTERNAL_HIDDEN @endcond |
| 3351 | */ |
| 3352 | |
| 3353 | /** |
| 3354 | * @defgroup semaphore_apis Semaphore APIs |
| 3355 | * @ingroup kernel_apis |
| 3356 | * @{ |
| 3357 | */ |
| 3358 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3359 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3360 | * @brief Initialize a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3361 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3362 | * This routine initializes a semaphore object, prior to its first use. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3363 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3364 | * @param sem Address of the semaphore. |
| 3365 | * @param initial_count Initial semaphore count. |
| 3366 | * @param limit Maximum permitted semaphore count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3367 | * |
| 3368 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3369 | * @req K-SEM-001 |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3370 | */ |
Andrew Boie | 9928023 | 2017-09-29 14:17:47 -0700 | [diff] [blame] | 3371 | __syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count, |
| 3372 | unsigned int limit); |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3373 | |
| 3374 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3375 | * @brief Take a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3376 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3377 | * This routine takes @a sem. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3378 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3379 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 3380 | * |
| 3381 | * @param sem Address of the semaphore. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3382 | * @param timeout Non-negative waiting period to take the semaphore (in |
| 3383 | * milliseconds), or one of the special values K_NO_WAIT and |
| 3384 | * K_FOREVER. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3385 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3386 | * @retval 0 Semaphore taken. |
| 3387 | * @retval -EBUSY Returned without waiting. |
| 3388 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3389 | * @req K-SEM-001 |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3390 | */ |
Andrew Boie | 9928023 | 2017-09-29 14:17:47 -0700 | [diff] [blame] | 3391 | __syscall int k_sem_take(struct k_sem *sem, s32_t timeout); |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3392 | |
| 3393 | /** |
| 3394 | * @brief Give a semaphore. |
| 3395 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3396 | * This routine gives @a sem, unless the semaphore is already at its maximum |
| 3397 | * permitted count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3398 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3399 | * @note Can be called by ISRs. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3400 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3401 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3402 | * |
| 3403 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3404 | * @req K-SEM-001 |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3405 | */ |
Andrew Boie | 9928023 | 2017-09-29 14:17:47 -0700 | [diff] [blame] | 3406 | __syscall void k_sem_give(struct k_sem *sem); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3407 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3408 | /** |
| 3409 | * @brief Reset a semaphore's count to zero. |
| 3410 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3411 | * This routine sets the count of @a sem to zero. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3412 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3413 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3414 | * |
| 3415 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3416 | * @req K-SEM-001 |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3417 | */ |
Andrew Boie | 990bf16 | 2017-10-03 12:36:49 -0700 | [diff] [blame] | 3418 | __syscall void k_sem_reset(struct k_sem *sem); |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 3419 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 3420 | /** |
| 3421 | * @internal |
| 3422 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3423 | static inline void z_impl_k_sem_reset(struct k_sem *sem) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3424 | { |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 3425 | sem->count = 0U; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3426 | } |
| 3427 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3428 | /** |
| 3429 | * @brief Get a semaphore's count. |
| 3430 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3431 | * This routine returns the current count of @a sem. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3432 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3433 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3434 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3435 | * @return Current semaphore count. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3436 | * @req K-SEM-001 |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3437 | */ |
Andrew Boie | 990bf16 | 2017-10-03 12:36:49 -0700 | [diff] [blame] | 3438 | __syscall unsigned int k_sem_count_get(struct k_sem *sem); |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 3439 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 3440 | /** |
| 3441 | * @internal |
| 3442 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3443 | 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] | 3444 | { |
| 3445 | return sem->count; |
| 3446 | } |
| 3447 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3448 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3449 | * @brief Statically define and initialize a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3450 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3451 | * 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] | 3452 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 3453 | * @code extern struct k_sem <name>; @endcode |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3454 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3455 | * @param name Name of the semaphore. |
| 3456 | * @param initial_count Initial semaphore count. |
| 3457 | * @param count_limit Maximum permitted semaphore count. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3458 | * @req K-SEM-002 |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3459 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3460 | #define K_SEM_DEFINE(name, initial_count, count_limit) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 3461 | Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \ |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 3462 | Z_SEM_INITIALIZER(name, initial_count, count_limit); \ |
Rajavardhan Gundi | 68040c8 | 2018-04-27 10:15:15 +0530 | [diff] [blame] | 3463 | BUILD_ASSERT(((count_limit) != 0) && \ |
| 3464 | ((initial_count) <= (count_limit))); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3465 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 3466 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3467 | |
| 3468 | /** |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3469 | * @defgroup msgq_apis Message Queue APIs |
| 3470 | * @ingroup kernel_apis |
| 3471 | * @{ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3472 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3473 | |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3474 | /** |
| 3475 | * @brief Message Queue Structure |
| 3476 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3477 | struct k_msgq { |
| 3478 | _wait_q_t wait_q; |
Andy Ross | be03dbd | 2018-07-26 10:23:02 -0700 | [diff] [blame] | 3479 | struct k_spinlock lock; |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 3480 | size_t msg_size; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3481 | u32_t max_msgs; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3482 | char *buffer_start; |
| 3483 | char *buffer_end; |
| 3484 | char *read_ptr; |
| 3485 | char *write_ptr; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3486 | u32_t used_msgs; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3487 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 3488 | _OBJECT_TRACING_NEXT_PTR(k_msgq) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 3489 | _OBJECT_TRACING_LINKED_FLAG |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 3490 | u8_t flags; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3491 | }; |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3492 | /** |
| 3493 | * @cond INTERNAL_HIDDEN |
| 3494 | */ |
| 3495 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3496 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3497 | #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] | 3498 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3499 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3500 | .msg_size = q_msg_size, \ |
Charles E. Youse | 6d01f67 | 2019-03-18 10:27:34 -0700 | [diff] [blame] | 3501 | .max_msgs = q_max_msgs, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3502 | .buffer_start = q_buffer, \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3503 | .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3504 | .read_ptr = q_buffer, \ |
| 3505 | .write_ptr = q_buffer, \ |
| 3506 | .used_msgs = 0, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 3507 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3508 | } |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 3509 | #define K_MSGQ_INITIALIZER __DEPRECATED_MACRO _K_MSGQ_INITIALIZER |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3510 | /** |
| 3511 | * INTERNAL_HIDDEN @endcond |
| 3512 | */ |
| 3513 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3514 | |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 3515 | #define K_MSGQ_FLAG_ALLOC BIT(0) |
| 3516 | |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3517 | /** |
| 3518 | * @brief Message Queue Attributes |
| 3519 | */ |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 3520 | struct k_msgq_attrs { |
| 3521 | size_t msg_size; |
| 3522 | u32_t max_msgs; |
| 3523 | u32_t used_msgs; |
| 3524 | }; |
| 3525 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3526 | |
| 3527 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3528 | * @brief Statically define and initialize a message queue. |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3529 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3530 | * The message queue's ring buffer contains space for @a q_max_msgs messages, |
| 3531 | * 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] | 3532 | * @a q_align -byte boundary, which must be a power of 2. To ensure that each |
| 3533 | * message is similarly aligned to this boundary, @a q_msg_size must also be |
| 3534 | * a multiple of @a q_align. |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3535 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3536 | * The message queue can be accessed outside the module where it is defined |
| 3537 | * using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3538 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 3539 | * @code extern struct k_msgq <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3540 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3541 | * @param q_name Name of the message queue. |
| 3542 | * @param q_msg_size Message size (in bytes). |
| 3543 | * @param q_max_msgs Maximum number of messages that can be queued. |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 3544 | * @param q_align Alignment of the message queue's ring buffer. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3545 | * |
| 3546 | * @req K-MSGQ-001 |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3547 | */ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 3548 | #define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \ |
| 3549 | static char __noinit __aligned(q_align) \ |
| 3550 | _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \ |
| 3551 | Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \ |
| 3552 | _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3553 | q_msg_size, q_max_msgs) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3554 | |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3555 | /** |
| 3556 | * @brief Initialize a message queue. |
| 3557 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3558 | * This routine initializes a message queue object, prior to its first use. |
| 3559 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 3560 | * The message queue's ring buffer must contain space for @a max_msgs messages, |
| 3561 | * each of which is @a msg_size bytes long. The buffer must be aligned to an |
| 3562 | * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure |
| 3563 | * that each message is similarly aligned to this boundary, @a q_msg_size |
| 3564 | * must also be a multiple of N. |
| 3565 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3566 | * @param q Address of the message queue. |
| 3567 | * @param buffer Pointer to ring buffer that holds queued messages. |
| 3568 | * @param msg_size Message size (in bytes). |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3569 | * @param max_msgs Maximum number of messages that can be queued. |
| 3570 | * |
| 3571 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3572 | * @req K-MSGQ-002 |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3573 | */ |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 3574 | void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size, |
| 3575 | u32_t max_msgs); |
| 3576 | |
| 3577 | /** |
| 3578 | * @brief Initialize a message queue. |
| 3579 | * |
| 3580 | * This routine initializes a message queue object, prior to its first use, |
| 3581 | * allocating its internal ring buffer from the calling thread's resource |
| 3582 | * pool. |
| 3583 | * |
| 3584 | * Memory allocated for the ring buffer can be released by calling |
| 3585 | * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses |
| 3586 | * all of its references. |
| 3587 | * |
| 3588 | * @param q Address of the message queue. |
| 3589 | * @param msg_size Message size (in bytes). |
| 3590 | * @param max_msgs Maximum number of messages that can be queued. |
| 3591 | * |
| 3592 | * @return 0 on success, -ENOMEM if there was insufficient memory in the |
| 3593 | * thread's resource pool, or -EINVAL if the size parameters cause |
| 3594 | * an integer overflow. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3595 | * @req K-MSGQ-002 |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 3596 | */ |
| 3597 | __syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size, |
| 3598 | u32_t max_msgs); |
| 3599 | |
| 3600 | |
| 3601 | void k_msgq_cleanup(struct k_msgq *q); |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3602 | |
| 3603 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3604 | * @brief Send a message to a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3605 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3606 | * This routine sends a message to message queue @a q. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3607 | * |
Benjamin Walsh | 8215ce1 | 2016-11-09 19:45:19 -0500 | [diff] [blame] | 3608 | * @note Can be called by ISRs. |
| 3609 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3610 | * @param q Address of the message queue. |
| 3611 | * @param data Pointer to the message. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3612 | * @param timeout Non-negative waiting period to add the message (in |
| 3613 | * milliseconds), or one of the special values K_NO_WAIT and |
| 3614 | * K_FOREVER. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3615 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3616 | * @retval 0 Message sent. |
| 3617 | * @retval -ENOMSG Returned without waiting or queue purged. |
| 3618 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3619 | * @req K-MSGQ-002 |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3620 | */ |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 3621 | __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] | 3622 | |
| 3623 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3624 | * @brief Receive a message from a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3625 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3626 | * This routine receives a message from message queue @a q in a "first in, |
| 3627 | * first out" manner. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3628 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3629 | * @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] | 3630 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3631 | * @param q Address of the message queue. |
| 3632 | * @param data Address of area to hold the received message. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3633 | * @param timeout Non-negative waiting period to receive the message (in |
| 3634 | * milliseconds), or one of the special values K_NO_WAIT and |
| 3635 | * K_FOREVER. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3636 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3637 | * @retval 0 Message received. |
| 3638 | * @retval -ENOMSG Returned without waiting. |
| 3639 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3640 | * @req K-MSGQ-002 |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3641 | */ |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 3642 | __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] | 3643 | |
| 3644 | /** |
Sathish Kuttan | 3efd8e1 | 2018-11-09 21:03:10 -0800 | [diff] [blame] | 3645 | * @brief Peek/read a message from a message queue. |
| 3646 | * |
| 3647 | * This routine reads a message from message queue @a q in a "first in, |
| 3648 | * first out" manner and leaves the message in the queue. |
| 3649 | * |
| 3650 | * @note Can be called by ISRs. |
| 3651 | * |
| 3652 | * @param q Address of the message queue. |
| 3653 | * @param data Address of area to hold the message read from the queue. |
| 3654 | * |
| 3655 | * @retval 0 Message read. |
| 3656 | * @retval -ENOMSG Returned when the queue has no message. |
| 3657 | * @req K-MSGQ-002 |
| 3658 | */ |
| 3659 | __syscall int k_msgq_peek(struct k_msgq *q, void *data); |
| 3660 | |
| 3661 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3662 | * @brief Purge a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3663 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3664 | * This routine discards all unreceived messages in a message queue's ring |
| 3665 | * buffer. Any threads that are blocked waiting to send a message to the |
| 3666 | * message queue are unblocked and see an -ENOMSG error code. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3667 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3668 | * @param q Address of the message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3669 | * |
| 3670 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3671 | * @req K-MSGQ-002 |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3672 | */ |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 3673 | __syscall void k_msgq_purge(struct k_msgq *q); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3674 | |
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 | * @brief Get the amount of free space in a message queue. |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 3677 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3678 | * This routine returns the number of unused entries in a message queue's |
| 3679 | * ring buffer. |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 3680 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3681 | * @param q Address of the message queue. |
| 3682 | * |
| 3683 | * @return Number of unused ring buffer entries. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3684 | * @req K-MSGQ-002 |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 3685 | */ |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 3686 | __syscall u32_t k_msgq_num_free_get(struct k_msgq *q); |
| 3687 | |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 3688 | /** |
| 3689 | * @brief Get basic attributes of a message queue. |
| 3690 | * |
| 3691 | * This routine fetches basic attributes of message queue into attr argument. |
| 3692 | * |
| 3693 | * @param q Address of the message queue. |
| 3694 | * @param attrs pointer to message queue attribute structure. |
| 3695 | * |
| 3696 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3697 | * @req K-MSGQ-003 |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 3698 | */ |
| 3699 | __syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs); |
| 3700 | |
| 3701 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3702 | 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] | 3703 | { |
| 3704 | return q->max_msgs - q->used_msgs; |
| 3705 | } |
| 3706 | |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3707 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3708 | * @brief Get the number of messages in a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3709 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3710 | * 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] | 3711 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3712 | * @param q Address of the message queue. |
| 3713 | * |
| 3714 | * @return Number of messages. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3715 | * @req K-MSGQ-002 |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3716 | */ |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 3717 | __syscall u32_t k_msgq_num_used_get(struct k_msgq *q); |
| 3718 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3719 | 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] | 3720 | { |
| 3721 | return q->used_msgs; |
| 3722 | } |
| 3723 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 3724 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3725 | |
| 3726 | /** |
| 3727 | * @defgroup mem_pool_apis Memory Pool APIs |
| 3728 | * @ingroup kernel_apis |
| 3729 | * @{ |
| 3730 | */ |
| 3731 | |
Andy Ross | 73cb958 | 2017-05-09 10:42:39 -0700 | [diff] [blame] | 3732 | /* Note on sizing: the use of a 20 bit field for block means that, |
| 3733 | * assuming a reasonable minimum block size of 16 bytes, we're limited |
| 3734 | * to 16M of memory managed by a single pool. Long term it would be |
| 3735 | * good to move to a variable bit size based on configuration. |
| 3736 | */ |
| 3737 | struct k_mem_block_id { |
| 3738 | u32_t pool : 8; |
| 3739 | u32_t level : 4; |
| 3740 | u32_t block : 20; |
| 3741 | }; |
| 3742 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3743 | struct k_mem_block { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3744 | void *data; |
Andy Ross | 73cb958 | 2017-05-09 10:42:39 -0700 | [diff] [blame] | 3745 | struct k_mem_block_id id; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3746 | }; |
| 3747 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 3748 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3749 | |
| 3750 | /** |
| 3751 | * @defgroup mailbox_apis Mailbox APIs |
| 3752 | * @ingroup kernel_apis |
| 3753 | * @{ |
| 3754 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3755 | |
| 3756 | struct k_mbox_msg { |
| 3757 | /** internal use only - needed for legacy API support */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3758 | u32_t _mailbox; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3759 | /** size of message (in bytes) */ |
Peter Mitsis | d93078c | 2016-10-14 12:59:37 -0400 | [diff] [blame] | 3760 | size_t size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3761 | /** application-defined information value */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3762 | u32_t info; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3763 | /** sender's message data buffer */ |
| 3764 | void *tx_data; |
| 3765 | /** internal use only - needed for legacy API support */ |
| 3766 | void *_rx_data; |
| 3767 | /** message data block descriptor */ |
| 3768 | struct k_mem_block tx_block; |
| 3769 | /** source thread id */ |
| 3770 | k_tid_t rx_source_thread; |
| 3771 | /** target thread id */ |
| 3772 | k_tid_t tx_target_thread; |
| 3773 | /** internal use only - thread waiting on send (may be a dummy) */ |
| 3774 | k_tid_t _syncing_thread; |
| 3775 | #if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0) |
| 3776 | /** internal use only - semaphore used during asynchronous send */ |
| 3777 | struct k_sem *_async_sem; |
| 3778 | #endif |
| 3779 | }; |
| 3780 | |
| 3781 | struct k_mbox { |
| 3782 | _wait_q_t tx_msg_queue; |
| 3783 | _wait_q_t rx_msg_queue; |
Andy Ross | 9eeb6b8 | 2018-07-25 15:06:24 -0700 | [diff] [blame] | 3784 | struct k_spinlock lock; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3785 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 3786 | _OBJECT_TRACING_NEXT_PTR(k_mbox) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 3787 | _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3788 | }; |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3789 | /** |
| 3790 | * @cond INTERNAL_HIDDEN |
| 3791 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3792 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3793 | #define _K_MBOX_INITIALIZER(obj) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3794 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3795 | .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \ |
| 3796 | .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 3797 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3798 | } |
| 3799 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 3800 | #define K_MBOX_INITIALIZER __DEPRECATED_MACRO _K_MBOX_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3801 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3802 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3803 | * INTERNAL_HIDDEN @endcond |
| 3804 | */ |
| 3805 | |
| 3806 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3807 | * @brief Statically define and initialize a mailbox. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3808 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3809 | * 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] | 3810 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 3811 | * @code extern struct k_mbox <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3812 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3813 | * @param name Name of the mailbox. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3814 | * @req K-MBOX-001 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3815 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3816 | #define K_MBOX_DEFINE(name) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 3817 | Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3818 | _K_MBOX_INITIALIZER(name) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3819 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3820 | /** |
| 3821 | * @brief Initialize a mailbox. |
| 3822 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3823 | * This routine initializes a mailbox object, prior to its first use. |
| 3824 | * |
| 3825 | * @param mbox Address of the mailbox. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3826 | * |
| 3827 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3828 | * @req K-MBOX-002 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3829 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3830 | extern void k_mbox_init(struct k_mbox *mbox); |
| 3831 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3832 | /** |
| 3833 | * @brief Send a mailbox message in a synchronous manner. |
| 3834 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3835 | * This routine sends a message to @a mbox and waits for a receiver to both |
| 3836 | * receive and process it. The message data may be in a buffer, in a memory |
| 3837 | * pool block, or non-existent (i.e. an empty message). |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3838 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3839 | * @param mbox Address of the mailbox. |
| 3840 | * @param tx_msg Address of the transmit message descriptor. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3841 | * @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] | 3842 | * milliseconds), or one of the special values K_NO_WAIT |
| 3843 | * and K_FOREVER. Once the message has been received, |
| 3844 | * this routine waits as long as necessary for the message |
| 3845 | * to be completely processed. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3846 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3847 | * @retval 0 Message sent. |
| 3848 | * @retval -ENOMSG Returned without waiting. |
| 3849 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3850 | * @req K-MBOX-002 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3851 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 3852 | 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] | 3853 | s32_t timeout); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3854 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3855 | /** |
| 3856 | * @brief Send a mailbox message in an asynchronous manner. |
| 3857 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3858 | * This routine sends a message to @a mbox without waiting for a receiver |
| 3859 | * to process it. The message data may be in a buffer, in a memory pool block, |
| 3860 | * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem |
| 3861 | * will be given when the message has been both received and completely |
| 3862 | * processed by the receiver. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3863 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3864 | * @param mbox Address of the mailbox. |
| 3865 | * @param tx_msg Address of the transmit message descriptor. |
| 3866 | * @param sem Address of a semaphore, or NULL if none is needed. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3867 | * |
| 3868 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3869 | * @req K-MBOX-002 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3870 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 3871 | 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] | 3872 | struct k_sem *sem); |
| 3873 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3874 | /** |
| 3875 | * @brief Receive a mailbox message. |
| 3876 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3877 | * This routine receives a message from @a mbox, then optionally retrieves |
| 3878 | * its data and disposes of the message. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3879 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3880 | * @param mbox Address of the mailbox. |
| 3881 | * @param rx_msg Address of the receive message descriptor. |
| 3882 | * @param buffer Address of the buffer to receive data, or NULL to defer data |
| 3883 | * retrieval and message disposal until later. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3884 | * @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] | 3885 | * milliseconds), or one of the special values K_NO_WAIT |
| 3886 | * and K_FOREVER. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3887 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3888 | * @retval 0 Message received. |
| 3889 | * @retval -ENOMSG Returned without waiting. |
| 3890 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3891 | * @req K-MBOX-002 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3892 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 3893 | 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] | 3894 | void *buffer, s32_t timeout); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3895 | |
| 3896 | /** |
| 3897 | * @brief Retrieve mailbox message data into a buffer. |
| 3898 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3899 | * This routine completes the processing of a received message by retrieving |
| 3900 | * its data into a buffer, then disposing of the message. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3901 | * |
| 3902 | * Alternatively, this routine can be used to dispose of a received message |
| 3903 | * without retrieving its data. |
| 3904 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3905 | * @param rx_msg Address of the receive message descriptor. |
| 3906 | * @param buffer Address of the buffer to receive data, or NULL to discard |
| 3907 | * the data. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3908 | * |
| 3909 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3910 | * @req K-MBOX-002 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3911 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 3912 | 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] | 3913 | |
| 3914 | /** |
| 3915 | * @brief Retrieve mailbox message data into a memory pool block. |
| 3916 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3917 | * This routine completes the processing of a received message by retrieving |
| 3918 | * its data into a memory pool block, then disposing of the message. |
| 3919 | * The memory pool block that results from successful retrieval must be |
| 3920 | * returned to the pool once the data has been processed, even in cases |
| 3921 | * where zero bytes of data are retrieved. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3922 | * |
| 3923 | * Alternatively, this routine can be used to dispose of a received message |
| 3924 | * without retrieving its data. In this case there is no need to return a |
| 3925 | * memory pool block to the pool. |
| 3926 | * |
| 3927 | * This routine allocates a new memory pool block for the data only if the |
| 3928 | * data is not already in one. If a new block cannot be allocated, the routine |
| 3929 | * returns a failure code and the received message is left unchanged. This |
| 3930 | * permits the caller to reattempt data retrieval at a later time or to dispose |
| 3931 | * of the received message without retrieving its data. |
| 3932 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3933 | * @param rx_msg Address of a receive message descriptor. |
| 3934 | * @param pool Address of memory pool, or NULL to discard data. |
| 3935 | * @param block Address of the area to hold memory pool block info. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3936 | * @param timeout Non-negative waiting period to wait for a memory pool block |
| 3937 | * (in milliseconds), or one of the special values K_NO_WAIT |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3938 | * and K_FOREVER. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3939 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3940 | * @retval 0 Data retrieved. |
| 3941 | * @retval -ENOMEM Returned without waiting. |
| 3942 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3943 | * @req K-MBOX-002 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3944 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 3945 | 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] | 3946 | struct k_mem_pool *pool, |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3947 | struct k_mem_block *block, s32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3948 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 3949 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3950 | |
| 3951 | /** |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3952 | * @defgroup pipe_apis Pipe APIs |
| 3953 | * @ingroup kernel_apis |
| 3954 | * @{ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3955 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3956 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3957 | /** Pipe Structure */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3958 | struct k_pipe { |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3959 | unsigned char *buffer; /**< Pipe buffer: may be NULL */ |
| 3960 | size_t size; /**< Buffer size */ |
| 3961 | size_t bytes_used; /**< # bytes used in buffer */ |
| 3962 | size_t read_index; /**< Where in buffer to read from */ |
| 3963 | size_t write_index; /**< Where in buffer to write */ |
Andy Ross | f582b55 | 2019-02-05 16:10:18 -0800 | [diff] [blame] | 3964 | struct k_spinlock lock; /**< Synchronization lock */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3965 | |
| 3966 | struct { |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3967 | _wait_q_t readers; /**< Reader wait queue */ |
| 3968 | _wait_q_t writers; /**< Writer wait queue */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3969 | } wait_q; |
| 3970 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 3971 | _OBJECT_TRACING_NEXT_PTR(k_pipe) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 3972 | _OBJECT_TRACING_LINKED_FLAG |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3973 | u8_t flags; /**< Flags */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3974 | }; |
| 3975 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3976 | /** |
| 3977 | * @cond INTERNAL_HIDDEN |
| 3978 | */ |
| 3979 | #define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */ |
| 3980 | |
Krzysztof Chruscinski | be06327 | 2019-02-13 11:19:54 +0100 | [diff] [blame] | 3981 | #define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \ |
| 3982 | { \ |
| 3983 | .buffer = pipe_buffer, \ |
| 3984 | .size = pipe_buffer_size, \ |
| 3985 | .bytes_used = 0, \ |
| 3986 | .read_index = 0, \ |
| 3987 | .write_index = 0, \ |
| 3988 | .lock = {}, \ |
| 3989 | .wait_q = { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3990 | .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \ |
| 3991 | .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \ |
Krzysztof Chruscinski | be06327 | 2019-02-13 11:19:54 +0100 | [diff] [blame] | 3992 | }, \ |
| 3993 | _OBJECT_TRACING_INIT \ |
| 3994 | .flags = 0 \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3995 | } |
| 3996 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 3997 | #define K_PIPE_INITIALIZER __DEPRECATED_MACRO _K_PIPE_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3998 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3999 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4000 | * INTERNAL_HIDDEN @endcond |
| 4001 | */ |
| 4002 | |
| 4003 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4004 | * @brief Statically define and initialize a pipe. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4005 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4006 | * 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] | 4007 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 4008 | * @code extern struct k_pipe <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4009 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4010 | * @param name Name of the pipe. |
| 4011 | * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes), |
| 4012 | * or zero if no ring buffer is used. |
| 4013 | * @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] | 4014 | * |
| 4015 | * @req K-PIPE-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4016 | */ |
Andrew Boie | 44fe812 | 2018-04-12 17:38:12 -0700 | [diff] [blame] | 4017 | #define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \ |
Andrew Boie | 41f6011 | 2019-01-31 15:53:24 -0800 | [diff] [blame] | 4018 | static unsigned char __noinit __aligned(pipe_align) \ |
Andrew Boie | 44fe812 | 2018-04-12 17:38:12 -0700 | [diff] [blame] | 4019 | _k_pipe_buf_##name[pipe_buffer_size]; \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 4020 | Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 4021 | _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4022 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4023 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4024 | * @brief Initialize a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4025 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4026 | * This routine initializes a pipe object, prior to its first use. |
| 4027 | * |
| 4028 | * @param pipe Address of the pipe. |
| 4029 | * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer |
| 4030 | * is used. |
| 4031 | * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring |
| 4032 | * buffer is used. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4033 | * |
| 4034 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4035 | * @req K-PIPE-002 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4036 | */ |
Andrew Boie | 44fe812 | 2018-04-12 17:38:12 -0700 | [diff] [blame] | 4037 | void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size); |
| 4038 | |
| 4039 | /** |
| 4040 | * @brief Release a pipe's allocated buffer |
| 4041 | * |
| 4042 | * If a pipe object was given a dynamically allocated buffer via |
| 4043 | * k_pipe_alloc_init(), this will free it. This function does nothing |
| 4044 | * if the buffer wasn't dynamically allocated. |
| 4045 | * |
| 4046 | * @param pipe Address of the pipe. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4047 | * @req K-PIPE-002 |
Andrew Boie | 44fe812 | 2018-04-12 17:38:12 -0700 | [diff] [blame] | 4048 | */ |
| 4049 | void k_pipe_cleanup(struct k_pipe *pipe); |
| 4050 | |
| 4051 | /** |
| 4052 | * @brief Initialize a pipe and allocate a buffer for it |
| 4053 | * |
| 4054 | * Storage for the buffer region will be allocated from the calling thread's |
| 4055 | * resource pool. This memory will be released if k_pipe_cleanup() is called, |
| 4056 | * or userspace is enabled and the pipe object loses all references to it. |
| 4057 | * |
| 4058 | * This function should only be called on uninitialized pipe objects. |
| 4059 | * |
| 4060 | * @param pipe Address of the pipe. |
| 4061 | * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring |
| 4062 | * buffer is used. |
| 4063 | * @retval 0 on success |
David B. Kinder | fcbd8fb | 2018-05-23 12:06:24 -0700 | [diff] [blame] | 4064 | * @retval -ENOMEM if memory couldn't be allocated |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4065 | * @req K-PIPE-002 |
Andrew Boie | 44fe812 | 2018-04-12 17:38:12 -0700 | [diff] [blame] | 4066 | */ |
| 4067 | __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] | 4068 | |
| 4069 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4070 | * @brief Write data to a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4071 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4072 | * 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] | 4073 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4074 | * @param pipe Address of the pipe. |
| 4075 | * @param data Address of data to write. |
| 4076 | * @param bytes_to_write Size of data (in bytes). |
| 4077 | * @param bytes_written Address of area to hold the number of bytes written. |
| 4078 | * @param min_xfer Minimum number of bytes to write. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 4079 | * @param timeout Non-negative waiting period to wait for the data to be written |
| 4080 | * (in milliseconds), or one of the special values K_NO_WAIT |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4081 | * and K_FOREVER. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4082 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4083 | * @retval 0 At least @a min_xfer bytes of data were written. |
| 4084 | * @retval -EIO Returned without waiting; zero data bytes were written. |
| 4085 | * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4086 | * minus one data bytes were written. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4087 | * @req K-PIPE-002 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4088 | */ |
Andrew Boie | b9a0578 | 2017-09-29 16:05:32 -0700 | [diff] [blame] | 4089 | __syscall int k_pipe_put(struct k_pipe *pipe, void *data, |
| 4090 | size_t bytes_to_write, size_t *bytes_written, |
| 4091 | size_t min_xfer, s32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4092 | |
| 4093 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4094 | * @brief Read data from a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4095 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4096 | * 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] | 4097 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4098 | * @param pipe Address of the pipe. |
| 4099 | * @param data Address to place the data read from pipe. |
| 4100 | * @param bytes_to_read Maximum number of data bytes to read. |
| 4101 | * @param bytes_read Address of area to hold the number of bytes read. |
| 4102 | * @param min_xfer Minimum number of data bytes to read. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 4103 | * @param timeout Non-negative waiting period to wait for the data to be read |
| 4104 | * (in milliseconds), or one of the special values K_NO_WAIT |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4105 | * and K_FOREVER. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4106 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4107 | * @retval 0 At least @a min_xfer bytes of data were read. |
| 4108 | * @retval -EIO Returned without waiting; zero data bytes were read. |
| 4109 | * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4110 | * minus one data bytes were read. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4111 | * @req K-PIPE-002 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4112 | */ |
Andrew Boie | b9a0578 | 2017-09-29 16:05:32 -0700 | [diff] [blame] | 4113 | __syscall int k_pipe_get(struct k_pipe *pipe, void *data, |
| 4114 | size_t bytes_to_read, size_t *bytes_read, |
| 4115 | size_t min_xfer, s32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4116 | |
| 4117 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4118 | * @brief Write memory block to a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4119 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4120 | * This routine writes the data contained in a memory block to @a pipe. |
| 4121 | * 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] | 4122 | * 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] | 4123 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4124 | * @param pipe Address of the pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4125 | * @param block Memory block containing data to send |
| 4126 | * @param size Number of data bytes in memory block to send |
| 4127 | * @param sem Semaphore to signal upon completion (else NULL) |
| 4128 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4129 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4130 | * @req K-PIPE-002 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4131 | */ |
| 4132 | extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block, |
| 4133 | size_t size, struct k_sem *sem); |
| 4134 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4135 | /** @} */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4136 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4137 | /** |
| 4138 | * @cond INTERNAL_HIDDEN |
| 4139 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4140 | |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4141 | struct k_mem_slab { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4142 | _wait_q_t wait_q; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4143 | u32_t num_blocks; |
Peter Mitsis | fb02d57 | 2016-10-13 16:55:45 -0400 | [diff] [blame] | 4144 | size_t block_size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4145 | char *buffer; |
| 4146 | char *free_list; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4147 | u32_t num_used; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4148 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 4149 | _OBJECT_TRACING_NEXT_PTR(k_mem_slab) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 4150 | _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4151 | }; |
| 4152 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 4153 | #define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4154 | slab_num_blocks) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4155 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4156 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4157 | .num_blocks = slab_num_blocks, \ |
| 4158 | .block_size = slab_block_size, \ |
| 4159 | .buffer = slab_buffer, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4160 | .free_list = NULL, \ |
| 4161 | .num_used = 0, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 4162 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4163 | } |
| 4164 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 4165 | #define K_MEM_SLAB_INITIALIZER __DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 4166 | |
| 4167 | |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 4168 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4169 | * INTERNAL_HIDDEN @endcond |
| 4170 | */ |
| 4171 | |
| 4172 | /** |
| 4173 | * @defgroup mem_slab_apis Memory Slab APIs |
| 4174 | * @ingroup kernel_apis |
| 4175 | * @{ |
| 4176 | */ |
| 4177 | |
| 4178 | /** |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 4179 | * @brief Statically define and initialize a memory slab. |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 4180 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 4181 | * The memory slab's buffer contains @a slab_num_blocks memory blocks |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4182 | * 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] | 4183 | * @a slab_align -byte boundary. To ensure that each memory block is similarly |
| 4184 | * 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] | 4185 | * @a slab_align. |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 4186 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 4187 | * 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] | 4188 | * using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4189 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 4190 | * @code extern struct k_mem_slab <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4191 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4192 | * @param name Name of the memory slab. |
| 4193 | * @param slab_block_size Size of each memory block (in bytes). |
| 4194 | * @param slab_num_blocks Number memory blocks. |
| 4195 | * @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] | 4196 | * @req K-MSLAB-001 |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 4197 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4198 | #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] | 4199 | char __noinit __aligned(WB_UP(slab_align)) \ |
| 4200 | _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] | 4201 | Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 4202 | _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \ |
Nicolas Pitre | 46cd5a0 | 2019-05-21 21:40:38 -0400 | [diff] [blame] | 4203 | WB_UP(slab_block_size), slab_num_blocks) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4204 | |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4205 | /** |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4206 | * @brief Initialize a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4207 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4208 | * Initializes a memory slab, prior to its first use. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4209 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 4210 | * The memory slab's buffer contains @a slab_num_blocks memory blocks |
| 4211 | * 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] | 4212 | * N-byte boundary matching a word boundary, where N is a power of 2 |
| 4213 | * (i.e. 4 on 32-bit systems, 8, 16, ...). |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 4214 | * To ensure that each memory block is similarly aligned to this boundary, |
| 4215 | * @a slab_block_size must also be a multiple of N. |
| 4216 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4217 | * @param slab Address of the memory slab. |
| 4218 | * @param buffer Pointer to buffer used for the memory blocks. |
| 4219 | * @param block_size Size of each memory block (in bytes). |
| 4220 | * @param num_blocks Number of memory blocks. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4221 | * |
| 4222 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4223 | * @req K-MSLAB-002 |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4224 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4225 | 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] | 4226 | size_t block_size, u32_t num_blocks); |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4227 | |
| 4228 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4229 | * @brief Allocate memory from a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4230 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4231 | * This routine allocates a memory block from a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4232 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4233 | * @param slab Address of the memory slab. |
| 4234 | * @param mem Pointer to block address area. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 4235 | * @param timeout Non-negative waiting period to wait for operation to complete |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4236 | * (in milliseconds). Use K_NO_WAIT to return without waiting, |
| 4237 | * or K_FOREVER to wait as long as necessary. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4238 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4239 | * @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] | 4240 | * is set to the starting address of the memory block. |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4241 | * @retval -ENOMEM Returned without waiting. |
| 4242 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4243 | * @req K-MSLAB-002 |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4244 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4245 | 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] | 4246 | s32_t timeout); |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4247 | |
| 4248 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4249 | * @brief Free memory allocated from a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4250 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4251 | * This routine releases a previously allocated memory block back to its |
| 4252 | * associated memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4253 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4254 | * @param slab Address of the memory slab. |
| 4255 | * @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] | 4256 | * |
| 4257 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4258 | * @req K-MSLAB-002 |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4259 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4260 | 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] | 4261 | |
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 | * @brief Get the number of used blocks in a 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 | * This routine gets the number of memory blocks that are currently |
| 4266 | * allocated in @a slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4267 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4268 | * @param slab Address of the memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4269 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4270 | * @return Number of allocated memory blocks. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4271 | * @req K-MSLAB-002 |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4272 | */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4273 | 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] | 4274 | { |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4275 | return slab->num_used; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4276 | } |
| 4277 | |
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 | * @brief Get the number of unused blocks in a 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 | * This routine gets the number of memory blocks that are currently |
| 4282 | * unallocated in @a slab. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4283 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4284 | * @param slab Address of the memory slab. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4285 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4286 | * @return Number of unallocated memory blocks. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4287 | * @req K-MSLAB-002 |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4288 | */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4289 | 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] | 4290 | { |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4291 | return slab->num_blocks - slab->num_used; |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4292 | } |
| 4293 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4294 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4295 | |
| 4296 | /** |
| 4297 | * @cond INTERNAL_HIDDEN |
| 4298 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4299 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 4300 | struct k_mem_pool { |
Andrew Boie | aa6de29 | 2018-03-06 17:12:37 -0800 | [diff] [blame] | 4301 | struct sys_mem_pool_base base; |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 4302 | _wait_q_t wait_q; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4303 | }; |
| 4304 | |
Dmitriy Korovkin | 0741467 | 2016-11-03 13:35:42 -0400 | [diff] [blame] | 4305 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4306 | * INTERNAL_HIDDEN @endcond |
Dmitriy Korovkin | 0741467 | 2016-11-03 13:35:42 -0400 | [diff] [blame] | 4307 | */ |
| 4308 | |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 4309 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4310 | * @addtogroup mem_pool_apis |
| 4311 | * @{ |
| 4312 | */ |
| 4313 | |
| 4314 | /** |
| 4315 | * @brief Statically define and initialize a memory pool. |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 4316 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4317 | * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes |
| 4318 | * long. The memory pool allows blocks to be repeatedly partitioned into |
| 4319 | * 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] | 4320 | * to a @a align -byte boundary. |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 4321 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4322 | * If the pool is to be accessed outside the module where it is defined, it |
| 4323 | * can be declared via |
| 4324 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 4325 | * @code extern struct k_mem_pool <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4326 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4327 | * @param name Name of the memory pool. |
Andy Ross | 73cb958 | 2017-05-09 10:42:39 -0700 | [diff] [blame] | 4328 | * @param minsz Size of the smallest blocks in the pool (in bytes). |
| 4329 | * @param maxsz Size of the largest blocks in the pool (in bytes). |
| 4330 | * @param nmax Number of maximum sized blocks in the pool. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4331 | * @param align Alignment of the pool's buffer (power of 2). |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4332 | * @req K-MPOOL-001 |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 4333 | */ |
Andy Ross | 73cb958 | 2017-05-09 10:42:39 -0700 | [diff] [blame] | 4334 | #define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \ |
Nicolas Pitre | cf97437 | 2019-06-26 11:32:58 -0400 | [diff] [blame] | 4335 | char __aligned(WB_UP(align)) _mpool_buf_##name[WB_UP(maxsz) * nmax \ |
Andy Ross | 73cb958 | 2017-05-09 10:42:39 -0700 | [diff] [blame] | 4336 | + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4337 | 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] | 4338 | Z_STRUCT_SECTION_ITERABLE(k_mem_pool, name) = { \ |
Andrew Boie | aa6de29 | 2018-03-06 17:12:37 -0800 | [diff] [blame] | 4339 | .base = { \ |
| 4340 | .buf = _mpool_buf_##name, \ |
Nicolas Pitre | cf97437 | 2019-06-26 11:32:58 -0400 | [diff] [blame] | 4341 | .max_sz = WB_UP(maxsz), \ |
Andrew Boie | aa6de29 | 2018-03-06 17:12:37 -0800 | [diff] [blame] | 4342 | .n_max = nmax, \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4343 | .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \ |
Andrew Boie | aa6de29 | 2018-03-06 17:12:37 -0800 | [diff] [blame] | 4344 | .levels = _mpool_lvls_##name, \ |
| 4345 | .flags = SYS_MEM_POOL_KERNEL \ |
| 4346 | } \ |
Johann Fischer | 223a2b9 | 2019-07-04 15:55:20 +0200 | [diff] [blame] | 4347 | }; \ |
Nicolas Pitre | b2a022b | 2019-09-26 16:36:40 -0400 | [diff] [blame] | 4348 | BUILD_ASSERT(WB_UP(maxsz) >= _MPOOL_MINBLK) |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 4349 | |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4350 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4351 | * @brief Allocate memory from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4352 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4353 | * This routine allocates a memory block from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4354 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4355 | * @param pool Address of the memory pool. |
| 4356 | * @param block Pointer to block descriptor for the allocated memory. |
| 4357 | * @param size Amount of memory to allocate (in bytes). |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 4358 | * @param timeout Non-negative waiting period to wait for operation to complete |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4359 | * (in milliseconds). Use K_NO_WAIT to return without waiting, |
| 4360 | * or K_FOREVER to wait as long as necessary. |
| 4361 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4362 | * @retval 0 Memory allocated. The @a data field of the block descriptor |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4363 | * is set to the starting address of the memory block. |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4364 | * @retval -ENOMEM Returned without waiting. |
| 4365 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4366 | * @req K-MPOOL-002 |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4367 | */ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 4368 | 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] | 4369 | size_t size, s32_t timeout); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4370 | |
| 4371 | /** |
Andrew Boie | a2480bd | 2018-04-12 16:59:02 -0700 | [diff] [blame] | 4372 | * @brief Allocate memory from a memory pool with malloc() semantics |
| 4373 | * |
| 4374 | * Such memory must be released using k_free(). |
| 4375 | * |
| 4376 | * @param pool Address of the memory pool. |
| 4377 | * @param size Amount of memory to allocate (in bytes). |
| 4378 | * @return Address of the allocated memory if successful, otherwise NULL |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4379 | * @req K-MPOOL-002 |
Andrew Boie | a2480bd | 2018-04-12 16:59:02 -0700 | [diff] [blame] | 4380 | */ |
| 4381 | extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size); |
| 4382 | |
| 4383 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4384 | * @brief Free memory allocated from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4385 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4386 | * This routine releases a previously allocated memory block back to its |
| 4387 | * memory pool. |
| 4388 | * |
| 4389 | * @param block Pointer to block descriptor for the allocated memory. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4390 | * |
| 4391 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4392 | * @req K-MPOOL-002 |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4393 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4394 | extern void k_mem_pool_free(struct k_mem_block *block); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4395 | |
| 4396 | /** |
Johan Hedberg | 7d887cb | 2018-01-11 20:45:27 +0200 | [diff] [blame] | 4397 | * @brief Free memory allocated from a memory pool. |
| 4398 | * |
| 4399 | * This routine releases a previously allocated memory block back to its |
| 4400 | * memory pool |
| 4401 | * |
| 4402 | * @param id Memory block identifier. |
| 4403 | * |
| 4404 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4405 | * @req K-MPOOL-002 |
Johan Hedberg | 7d887cb | 2018-01-11 20:45:27 +0200 | [diff] [blame] | 4406 | */ |
| 4407 | extern void k_mem_pool_free_id(struct k_mem_block_id *id); |
| 4408 | |
| 4409 | /** |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4410 | * @} |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4411 | */ |
| 4412 | |
| 4413 | /** |
| 4414 | * @defgroup heap_apis Heap Memory Pool APIs |
| 4415 | * @ingroup kernel_apis |
| 4416 | * @{ |
| 4417 | */ |
| 4418 | |
| 4419 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4420 | * @brief Allocate memory from heap. |
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 | * This routine provides traditional malloc() semantics. Memory is |
Allan Stephens | 480a131 | 2016-10-13 15:44:48 -0500 | [diff] [blame] | 4423 | * allocated from the heap memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4424 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4425 | * @param size Amount of memory requested (in bytes). |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4426 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4427 | * @return Address of the allocated memory if successful; otherwise NULL. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4428 | * @req K-HEAP-001 |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4429 | */ |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 4430 | extern void *k_malloc(size_t size); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4431 | |
| 4432 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4433 | * @brief Free memory allocated from heap. |
Allan Stephens | 480a131 | 2016-10-13 15:44:48 -0500 | [diff] [blame] | 4434 | * |
| 4435 | * This routine provides traditional free() semantics. The memory being |
Andrew Boie | a2480bd | 2018-04-12 16:59:02 -0700 | [diff] [blame] | 4436 | * returned must have been allocated from the heap memory pool or |
| 4437 | * k_mem_pool_malloc(). |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4438 | * |
Anas Nashif | 345fdd5 | 2016-12-20 08:36:04 -0500 | [diff] [blame] | 4439 | * If @a ptr is NULL, no operation is performed. |
| 4440 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4441 | * @param ptr Pointer to previously allocated memory. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4442 | * |
| 4443 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4444 | * @req K-HEAP-001 |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4445 | */ |
| 4446 | extern void k_free(void *ptr); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4447 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4448 | /** |
Andrew Boie | 7f95e83 | 2017-11-08 14:40:01 -0800 | [diff] [blame] | 4449 | * @brief Allocate memory from heap, array style |
| 4450 | * |
| 4451 | * This routine provides traditional calloc() semantics. Memory is |
| 4452 | * allocated from the heap memory pool and zeroed. |
| 4453 | * |
| 4454 | * @param nmemb Number of elements in the requested array |
| 4455 | * @param size Size of each array element (in bytes). |
| 4456 | * |
| 4457 | * @return Address of the allocated memory if successful; otherwise NULL. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4458 | * @req K-HEAP-001 |
Andrew Boie | 7f95e83 | 2017-11-08 14:40:01 -0800 | [diff] [blame] | 4459 | */ |
| 4460 | extern void *k_calloc(size_t nmemb, size_t size); |
| 4461 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4462 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4463 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4464 | /* polling API - PRIVATE */ |
| 4465 | |
Benjamin Walsh | b017986 | 2017-02-02 16:39:57 -0500 | [diff] [blame] | 4466 | #ifdef CONFIG_POLL |
Flavio Ceolin | 6fdc56d | 2018-09-18 12:32:27 -0700 | [diff] [blame] | 4467 | #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] | 4468 | #else |
Flavio Ceolin | 6fdc56d | 2018-09-18 12:32:27 -0700 | [diff] [blame] | 4469 | #define _INIT_OBJ_POLL_EVENT(obj) do { } while (false) |
Benjamin Walsh | b017986 | 2017-02-02 16:39:57 -0500 | [diff] [blame] | 4470 | #endif |
| 4471 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4472 | /* private - types bit positions */ |
| 4473 | enum _poll_types_bits { |
| 4474 | /* can be used to ignore an event */ |
| 4475 | _POLL_TYPE_IGNORE, |
| 4476 | |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 4477 | /* to be signaled by k_poll_signal_raise() */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4478 | _POLL_TYPE_SIGNAL, |
| 4479 | |
| 4480 | /* semaphore availability */ |
| 4481 | _POLL_TYPE_SEM_AVAILABLE, |
| 4482 | |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 4483 | /* queue/fifo/lifo data availability */ |
| 4484 | _POLL_TYPE_DATA_AVAILABLE, |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4485 | |
| 4486 | _POLL_NUM_TYPES |
| 4487 | }; |
| 4488 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4489 | #define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1)) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4490 | |
| 4491 | /* private - states bit positions */ |
| 4492 | enum _poll_states_bits { |
| 4493 | /* default state when creating event */ |
| 4494 | _POLL_STATE_NOT_READY, |
| 4495 | |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 4496 | /* signaled by k_poll_signal_raise() */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4497 | _POLL_STATE_SIGNALED, |
| 4498 | |
| 4499 | /* semaphore is available */ |
| 4500 | _POLL_STATE_SEM_AVAILABLE, |
| 4501 | |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 4502 | /* data is available to read on queue/fifo/lifo */ |
| 4503 | _POLL_STATE_DATA_AVAILABLE, |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4504 | |
Paul Sokolovsky | 45c0b20 | 2018-08-21 23:29:11 +0300 | [diff] [blame] | 4505 | /* queue/fifo/lifo wait was cancelled */ |
| 4506 | _POLL_STATE_CANCELLED, |
| 4507 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4508 | _POLL_NUM_STATES |
| 4509 | }; |
| 4510 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4511 | #define Z_POLL_STATE_BIT(state) (1 << ((state) - 1)) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4512 | |
| 4513 | #define _POLL_EVENT_NUM_UNUSED_BITS \ |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4514 | (32 - (0 \ |
| 4515 | + 8 /* tag */ \ |
| 4516 | + _POLL_NUM_TYPES \ |
| 4517 | + _POLL_NUM_STATES \ |
| 4518 | + 1 /* modes */ \ |
| 4519 | )) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4520 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4521 | /* end of polling API - PRIVATE */ |
| 4522 | |
| 4523 | |
| 4524 | /** |
| 4525 | * @defgroup poll_apis Async polling APIs |
| 4526 | * @ingroup kernel_apis |
| 4527 | * @{ |
| 4528 | */ |
| 4529 | |
| 4530 | /* Public polling API */ |
| 4531 | |
| 4532 | /* public - values for k_poll_event.type bitfield */ |
| 4533 | #define K_POLL_TYPE_IGNORE 0 |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4534 | #define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL) |
| 4535 | #define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE) |
| 4536 | #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] | 4537 | #define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4538 | |
| 4539 | /* public - polling modes */ |
| 4540 | enum k_poll_modes { |
| 4541 | /* polling thread does not take ownership of objects when available */ |
| 4542 | K_POLL_MODE_NOTIFY_ONLY = 0, |
| 4543 | |
| 4544 | K_POLL_NUM_MODES |
| 4545 | }; |
| 4546 | |
| 4547 | /* public - values for k_poll_event.state bitfield */ |
| 4548 | #define K_POLL_STATE_NOT_READY 0 |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4549 | #define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED) |
| 4550 | #define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE) |
| 4551 | #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] | 4552 | #define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4553 | #define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4554 | |
| 4555 | /* public - poll signal object */ |
| 4556 | struct k_poll_signal { |
| 4557 | /* PRIVATE - DO NOT TOUCH */ |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 4558 | sys_dlist_t poll_events; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4559 | |
| 4560 | /* |
| 4561 | * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until |
| 4562 | * user resets it to 0. |
| 4563 | */ |
| 4564 | unsigned int signaled; |
| 4565 | |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 4566 | /* custom result value passed to k_poll_signal_raise() if needed */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4567 | int result; |
| 4568 | }; |
| 4569 | |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 4570 | #define K_POLL_SIGNAL_INITIALIZER(obj) \ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4571 | { \ |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 4572 | .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4573 | .signaled = 0, \ |
| 4574 | .result = 0, \ |
| 4575 | } |
| 4576 | |
| 4577 | struct k_poll_event { |
| 4578 | /* PRIVATE - DO NOT TOUCH */ |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 4579 | sys_dnode_t _node; |
| 4580 | |
| 4581 | /* PRIVATE - DO NOT TOUCH */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4582 | struct _poller *poller; |
| 4583 | |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4584 | /* optional user-specified tag, opaque, untouched by the API */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4585 | u32_t tag:8; |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4586 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4587 | /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4588 | u32_t type:_POLL_NUM_TYPES; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4589 | |
| 4590 | /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4591 | u32_t state:_POLL_NUM_STATES; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4592 | |
| 4593 | /* mode of operation, from enum k_poll_modes */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4594 | u32_t mode:1; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4595 | |
| 4596 | /* unused bits in 32-bit word */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4597 | u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4598 | |
| 4599 | /* per-type data */ |
| 4600 | union { |
| 4601 | void *obj; |
| 4602 | struct k_poll_signal *signal; |
| 4603 | struct k_sem *sem; |
| 4604 | struct k_fifo *fifo; |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 4605 | struct k_queue *queue; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4606 | }; |
| 4607 | }; |
| 4608 | |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4609 | #define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4610 | { \ |
| 4611 | .poller = NULL, \ |
| 4612 | .type = event_type, \ |
| 4613 | .state = K_POLL_STATE_NOT_READY, \ |
| 4614 | .mode = event_mode, \ |
| 4615 | .unused = 0, \ |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4616 | { .obj = event_obj }, \ |
| 4617 | } |
| 4618 | |
| 4619 | #define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \ |
| 4620 | event_tag) \ |
| 4621 | { \ |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4622 | .tag = event_tag, \ |
Markus Fuchs | be21d3f | 2019-10-09 21:31:25 +0200 | [diff] [blame] | 4623 | .type = event_type, \ |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4624 | .state = K_POLL_STATE_NOT_READY, \ |
| 4625 | .mode = event_mode, \ |
| 4626 | .unused = 0, \ |
| 4627 | { .obj = event_obj }, \ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4628 | } |
| 4629 | |
| 4630 | /** |
| 4631 | * @brief Initialize one struct k_poll_event instance |
| 4632 | * |
| 4633 | * After this routine is called on a poll event, the event it ready to be |
| 4634 | * placed in an event array to be passed to k_poll(). |
| 4635 | * |
| 4636 | * @param event The event to initialize. |
| 4637 | * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx |
| 4638 | * values. Only values that apply to the same object being polled |
| 4639 | * can be used together. Choosing K_POLL_TYPE_IGNORE disables the |
| 4640 | * event. |
Paul Sokolovsky | cfef979 | 2017-07-18 11:53:06 +0300 | [diff] [blame] | 4641 | * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY. |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4642 | * @param obj Kernel object or poll signal. |
| 4643 | * |
| 4644 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4645 | * @req K-POLL-001 |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4646 | */ |
| 4647 | |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4648 | 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] | 4649 | int mode, void *obj); |
| 4650 | |
| 4651 | /** |
| 4652 | * @brief Wait for one or many of multiple poll events to occur |
| 4653 | * |
| 4654 | * This routine allows a thread to wait concurrently for one or many of |
| 4655 | * multiple poll events to have occurred. Such events can be a kernel object |
| 4656 | * being available, like a semaphore, or a poll signal event. |
| 4657 | * |
| 4658 | * When an event notifies that a kernel object is available, the kernel object |
| 4659 | * is not "given" to the thread calling k_poll(): it merely signals the fact |
| 4660 | * that the object was available when the k_poll() call was in effect. Also, |
| 4661 | * all threads trying to acquire an object the regular way, i.e. by pending on |
| 4662 | * the object, have precedence over the thread polling on the object. This |
| 4663 | * means that the polling thread will never get the poll event on an object |
| 4664 | * until the object becomes available and its pend queue is empty. For this |
| 4665 | * reason, the k_poll() call is more effective when the objects being polled |
| 4666 | * only have one thread, the polling thread, trying to acquire them. |
| 4667 | * |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 4668 | * When k_poll() returns 0, the caller should loop on all the events that were |
| 4669 | * passed to k_poll() and check the state field for the values that were |
| 4670 | * expected and take the associated actions. |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4671 | * |
| 4672 | * Before being reused for another call to k_poll(), the user has to reset the |
| 4673 | * state field to K_POLL_STATE_NOT_READY. |
| 4674 | * |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4675 | * When called from user mode, a temporary memory allocation is required from |
| 4676 | * the caller's resource pool. |
| 4677 | * |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4678 | * @param events An array of pointers to events to be polled for. |
| 4679 | * @param num_events The number of events in the array. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 4680 | * @param timeout Non-negative waiting period for an event to be ready (in |
| 4681 | * milliseconds), or one of the special values K_NO_WAIT and |
| 4682 | * K_FOREVER. |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4683 | * |
| 4684 | * @retval 0 One or more events are ready. |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4685 | * @retval -EAGAIN Waiting period timed out. |
Paul Sokolovsky | 45c0b20 | 2018-08-21 23:29:11 +0300 | [diff] [blame] | 4686 | * @retval -EINTR Polling has been interrupted, e.g. with |
| 4687 | * k_queue_cancel_wait(). All output events are still set and valid, |
| 4688 | * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other |
| 4689 | * words, -EINTR status means that at least one of output events is |
| 4690 | * K_POLL_STATE_CANCELLED. |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4691 | * @retval -ENOMEM Thread resource pool insufficient memory (user mode only) |
| 4692 | * @retval -EINVAL Bad parameters (user mode only) |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4693 | * @req K-POLL-001 |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4694 | */ |
| 4695 | |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4696 | __syscall int k_poll(struct k_poll_event *events, int num_events, |
| 4697 | s32_t timeout); |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4698 | |
| 4699 | /** |
Benjamin Walsh | a304f16 | 2017-02-02 16:46:09 -0500 | [diff] [blame] | 4700 | * @brief Initialize a poll signal object. |
| 4701 | * |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 4702 | * 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] | 4703 | * |
| 4704 | * @param signal A poll signal. |
| 4705 | * |
| 4706 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4707 | * @req K-POLL-001 |
Benjamin Walsh | a304f16 | 2017-02-02 16:46:09 -0500 | [diff] [blame] | 4708 | */ |
| 4709 | |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4710 | __syscall void k_poll_signal_init(struct k_poll_signal *signal); |
| 4711 | |
| 4712 | /* |
| 4713 | * @brief Reset a poll signal object's state to unsignaled. |
| 4714 | * |
| 4715 | * @param signal A poll signal object |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4716 | * @req K-POLL-001 |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4717 | */ |
| 4718 | __syscall void k_poll_signal_reset(struct k_poll_signal *signal); |
| 4719 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4720 | 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] | 4721 | { |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 4722 | signal->signaled = 0U; |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4723 | } |
| 4724 | |
| 4725 | /** |
David B. Kinder | fcbd8fb | 2018-05-23 12:06:24 -0700 | [diff] [blame] | 4726 | * @brief Fetch the signaled state and result value of a poll signal |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4727 | * |
| 4728 | * @param signal A poll signal object |
| 4729 | * @param signaled An integer buffer which will be written nonzero if the |
| 4730 | * object was signaled |
| 4731 | * @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] | 4732 | * result value if the object was signaled, or an undefined |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4733 | * value if it was not. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4734 | * @req K-POLL-001 |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4735 | */ |
| 4736 | __syscall void k_poll_signal_check(struct k_poll_signal *signal, |
| 4737 | unsigned int *signaled, int *result); |
Benjamin Walsh | a304f16 | 2017-02-02 16:46:09 -0500 | [diff] [blame] | 4738 | |
| 4739 | /** |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4740 | * @brief Signal a poll signal object. |
| 4741 | * |
| 4742 | * This routine makes ready a poll signal, which is basically a poll event of |
| 4743 | * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be |
| 4744 | * made ready to run. A @a result value can be specified. |
| 4745 | * |
| 4746 | * The poll signal contains a 'signaled' field that, when set by |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 4747 | * 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] | 4748 | * k_poll_signal_reset(). It thus has to be reset by the user before being |
| 4749 | * passed again to k_poll() or k_poll() will consider it being signaled, and |
| 4750 | * will return immediately. |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4751 | * |
Peter A. Bigot | 773bd98 | 2019-04-30 07:06:39 -0500 | [diff] [blame] | 4752 | * @note The result is stored and the 'signaled' field is set even if |
| 4753 | * this function returns an error indicating that an expiring poll was |
| 4754 | * not notified. The next k_poll() will detect the missed raise. |
| 4755 | * |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4756 | * @param signal A poll signal. |
| 4757 | * @param result The value to store in the result field of the signal. |
| 4758 | * |
| 4759 | * @retval 0 The signal was delivered successfully. |
| 4760 | * @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] | 4761 | * @req K-POLL-001 |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4762 | */ |
| 4763 | |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 4764 | __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] | 4765 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 4766 | /** |
| 4767 | * @internal |
| 4768 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4769 | 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] | 4770 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4771 | /** @} */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4772 | |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 4773 | /** |
Anas Nashif | 30c3cff | 2019-01-22 08:18:13 -0500 | [diff] [blame] | 4774 | * @defgroup cpu_idle_apis CPU Idling APIs |
| 4775 | * @ingroup kernel_apis |
| 4776 | * @{ |
| 4777 | */ |
Anas Nashif | 30c3cff | 2019-01-22 08:18:13 -0500 | [diff] [blame] | 4778 | /** |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 4779 | * @brief Make the CPU idle. |
| 4780 | * |
| 4781 | * This function makes the CPU idle until an event wakes it up. |
| 4782 | * |
| 4783 | * In a regular system, the idle thread should be the only thread responsible |
| 4784 | * for making the CPU idle and triggering any type of power management. |
| 4785 | * However, in some more constrained systems, such as a single-threaded system, |
| 4786 | * the only thread would be responsible for this if needed. |
| 4787 | * |
| 4788 | * @return N/A |
Anas Nashif | 30c3cff | 2019-01-22 08:18:13 -0500 | [diff] [blame] | 4789 | * @req K-CPU-IDLE-001 |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 4790 | */ |
Andrew Boie | 07525a3 | 2019-09-21 16:17:23 -0700 | [diff] [blame] | 4791 | static inline void k_cpu_idle(void) |
| 4792 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4793 | arch_cpu_idle(); |
Andrew Boie | 07525a3 | 2019-09-21 16:17:23 -0700 | [diff] [blame] | 4794 | } |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 4795 | |
| 4796 | /** |
| 4797 | * @brief Make the CPU idle in an atomic fashion. |
| 4798 | * |
| 4799 | * Similar to k_cpu_idle(), but called with interrupts locked if operations |
| 4800 | * must be done atomically before making the CPU idle. |
| 4801 | * |
| 4802 | * @param key Interrupt locking key obtained from irq_lock(). |
| 4803 | * |
| 4804 | * @return N/A |
Anas Nashif | 30c3cff | 2019-01-22 08:18:13 -0500 | [diff] [blame] | 4805 | * @req K-CPU-IDLE-002 |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 4806 | */ |
Andrew Boie | 07525a3 | 2019-09-21 16:17:23 -0700 | [diff] [blame] | 4807 | static inline void k_cpu_atomic_idle(unsigned int key) |
| 4808 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4809 | arch_cpu_atomic_idle(key); |
Andrew Boie | 07525a3 | 2019-09-21 16:17:23 -0700 | [diff] [blame] | 4810 | } |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 4811 | |
Anas Nashif | 30c3cff | 2019-01-22 08:18:13 -0500 | [diff] [blame] | 4812 | /** |
| 4813 | * @} |
| 4814 | */ |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 4815 | |
| 4816 | /** |
| 4817 | * @internal |
| 4818 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4819 | extern void z_sys_power_save_idle_exit(s32_t ticks); |
Andrew Boie | 350f88d | 2017-01-18 13:13:45 -0800 | [diff] [blame] | 4820 | |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4821 | #ifdef ARCH_EXCEPT |
Ioannis Glaropoulos | df02923 | 2019-10-07 11:24:36 +0200 | [diff] [blame] | 4822 | /* This architecture has direct support for triggering a CPU exception */ |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4823 | #define z_except_reason(reason) ARCH_EXCEPT(reason) |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4824 | #else |
| 4825 | |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4826 | /* NOTE: This is the implementation for arches that do not implement |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4827 | * ARCH_EXCEPT() to generate a real CPU exception. |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4828 | * |
| 4829 | * We won't have a real exception frame to determine the PC value when |
| 4830 | * the oops occurred, so print file and line number before we jump into |
| 4831 | * the fatal error handler. |
| 4832 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4833 | #define z_except_reason(reason) do { \ |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4834 | printk("@ %s:%d:\n", __FILE__, __LINE__); \ |
Andrew Boie | 5623637 | 2019-07-15 15:22:29 -0700 | [diff] [blame] | 4835 | z_fatal_error(reason, NULL); \ |
Flavio Ceolin | 6fdc56d | 2018-09-18 12:32:27 -0700 | [diff] [blame] | 4836 | } while (false) |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4837 | |
| 4838 | #endif /* _ARCH__EXCEPT */ |
| 4839 | |
| 4840 | /** |
| 4841 | * @brief Fatally terminate a thread |
| 4842 | * |
| 4843 | * This should be called when a thread has encountered an unrecoverable |
| 4844 | * runtime condition and needs to terminate. What this ultimately |
| 4845 | * means is determined by the _fatal_error_handler() implementation, which |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 4846 | * will be called will reason code K_ERR_KERNEL_OOPS. |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4847 | * |
| 4848 | * If this is called from ISR context, the default system fatal error handler |
| 4849 | * will treat it as an unrecoverable system error, just like k_panic(). |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4850 | * @req K-MISC-003 |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4851 | */ |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 4852 | #define k_oops() z_except_reason(K_ERR_KERNEL_OOPS) |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4853 | |
| 4854 | /** |
| 4855 | * @brief Fatally terminate the system |
| 4856 | * |
| 4857 | * This should be called when the Zephyr kernel has encountered an |
| 4858 | * unrecoverable runtime condition and needs to terminate. What this ultimately |
| 4859 | * means is determined by the _fatal_error_handler() implementation, which |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 4860 | * will be called will reason code K_ERR_KERNEL_PANIC. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4861 | * @req K-MISC-004 |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4862 | */ |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 4863 | #define k_panic() z_except_reason(K_ERR_KERNEL_PANIC) |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4864 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4865 | /* |
| 4866 | * private APIs that are utilized by one or more public APIs |
| 4867 | */ |
| 4868 | |
Stephanos Ioannidis | 2d74604 | 2019-10-25 00:08:21 +0900 | [diff] [blame] | 4869 | /** |
| 4870 | * @internal |
| 4871 | */ |
| 4872 | extern void z_init_thread_base(struct _thread_base *thread_base, |
| 4873 | int priority, u32_t initial_state, |
| 4874 | unsigned int options); |
| 4875 | |
Benjamin Walsh | b12a8e0 | 2016-12-14 15:24:12 -0500 | [diff] [blame] | 4876 | #ifdef CONFIG_MULTITHREADING |
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 | extern void z_init_static_threads(void); |
Benjamin Walsh | b12a8e0 | 2016-12-14 15:24:12 -0500 | [diff] [blame] | 4881 | #else |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 4882 | /** |
| 4883 | * @internal |
| 4884 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4885 | #define z_init_static_threads() do { } while (false) |
Benjamin Walsh | b12a8e0 | 2016-12-14 15:24:12 -0500 | [diff] [blame] | 4886 | #endif |
| 4887 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 4888 | /** |
| 4889 | * @internal |
| 4890 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4891 | extern bool z_is_thread_essential(void); |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 4892 | /** |
| 4893 | * @internal |
| 4894 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4895 | extern void z_timer_expiration_handler(struct _timeout *t); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4896 | |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4897 | /* arch/cpu.h may declare an architecture or platform-specific macro |
| 4898 | * for properly declaring stacks, compatible with MMU/MPU constraints if |
| 4899 | * enabled |
| 4900 | */ |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 4901 | |
| 4902 | /** |
| 4903 | * @brief Obtain an extern reference to a stack |
| 4904 | * |
| 4905 | * This macro properly brings the symbol of a thread stack declared |
| 4906 | * elsewhere into scope. |
| 4907 | * |
| 4908 | * @param sym Thread stack symbol name |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4909 | * @req K-MISC-005 |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 4910 | */ |
| 4911 | #define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[] |
| 4912 | |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4913 | #ifdef ARCH_THREAD_STACK_DEFINE |
| 4914 | #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] | 4915 | #define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \ |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4916 | ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) |
| 4917 | #define K_THREAD_STACK_LEN(size) ARCH_THREAD_STACK_LEN(size) |
| 4918 | #define K_THREAD_STACK_MEMBER(sym, size) ARCH_THREAD_STACK_MEMBER(sym, size) |
| 4919 | #define K_THREAD_STACK_SIZEOF(sym) ARCH_THREAD_STACK_SIZEOF(sym) |
| 4920 | #define K_THREAD_STACK_RESERVED ARCH_THREAD_STACK_RESERVED |
Andrew Boie | 4e5c093 | 2019-04-04 12:05:28 -0700 | [diff] [blame] | 4921 | static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym) |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 4922 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4923 | return ARCH_THREAD_STACK_BUFFER(sym); |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 4924 | } |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4925 | #else |
| 4926 | /** |
| 4927 | * @brief Declare a toplevel thread stack memory region |
| 4928 | * |
| 4929 | * This declares a region of memory suitable for use as a thread's stack. |
| 4930 | * |
| 4931 | * This is the generic, historical definition. Align to STACK_ALIGN and put in |
| 4932 | * 'noinit' section so that it isn't zeroed at boot |
| 4933 | * |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 4934 | * 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] | 4935 | * k_thread_create(), but should otherwise not be manipulated. If the buffer |
Andrew Boie | 4e5c093 | 2019-04-04 12:05:28 -0700 | [diff] [blame] | 4936 | * inside needs to be examined, examine thread->stack_info for the associated |
| 4937 | * thread object to obtain the boundaries. |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4938 | * |
| 4939 | * It is legal to precede this definition with the 'static' keyword. |
| 4940 | * |
| 4941 | * It is NOT legal to take the sizeof(sym) and pass that to the stackSize |
| 4942 | * parameter of k_thread_create(), it may not be the same as the |
| 4943 | * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead. |
| 4944 | * |
Andrew Boie | e2d7791 | 2018-05-30 09:45:35 -0700 | [diff] [blame] | 4945 | * Some arches may round the size of the usable stack region up to satisfy |
| 4946 | * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned |
| 4947 | * size. |
| 4948 | * |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4949 | * @param sym Thread stack symbol name |
| 4950 | * @param size Size of the stack memory region |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4951 | * @req K-TSTACK-001 |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4952 | */ |
| 4953 | #define K_THREAD_STACK_DEFINE(sym, size) \ |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 4954 | struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size] |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4955 | |
| 4956 | /** |
Rajavardhan Gundi | d4dd928 | 2018-06-27 13:26:20 +0530 | [diff] [blame] | 4957 | * @brief Calculate size of stacks to be allocated in a stack array |
| 4958 | * |
| 4959 | * This macro calculates the size to be allocated for the stacks |
| 4960 | * inside a stack array. It accepts the indicated "size" as a parameter |
| 4961 | * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer |
| 4962 | * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used. |
| 4963 | * |
| 4964 | * @param size Size of the stack memory region |
| 4965 | * @req K-TSTACK-001 |
| 4966 | */ |
| 4967 | #define K_THREAD_STACK_LEN(size) (size) |
| 4968 | |
| 4969 | /** |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4970 | * @brief Declare a toplevel array of thread stack memory regions |
| 4971 | * |
| 4972 | * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE |
| 4973 | * definition for additional details and constraints. |
| 4974 | * |
| 4975 | * This is the generic, historical definition. Align to STACK_ALIGN and put in |
| 4976 | * 'noinit' section so that it isn't zeroed at boot |
| 4977 | * |
| 4978 | * @param sym Thread stack symbol name |
| 4979 | * @param nmemb Number of stacks to declare |
| 4980 | * @param size Size of the stack memory region |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4981 | * @req K-TSTACK-001 |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4982 | */ |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4983 | #define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \ |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 4984 | struct _k_thread_stack_element __noinit \ |
Rajavardhan Gundi | d4dd928 | 2018-06-27 13:26:20 +0530 | [diff] [blame] | 4985 | __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)] |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4986 | |
| 4987 | /** |
| 4988 | * @brief Declare an embedded stack memory region |
| 4989 | * |
| 4990 | * Used for stacks embedded within other data structures. Use is highly |
| 4991 | * discouraged but in some cases necessary. For memory protection scenarios, |
| 4992 | * it is very important that any RAM preceding this member not be writable |
| 4993 | * by threads else a stack overflow will lead to silent corruption. In other |
| 4994 | * words, the containing data structure should live in RAM owned by the kernel. |
| 4995 | * |
| 4996 | * @param sym Thread stack symbol name |
| 4997 | * @param size Size of the stack memory region |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4998 | * @req K-TSTACK-001 |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4999 | */ |
| 5000 | #define K_THREAD_STACK_MEMBER(sym, size) \ |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 5001 | struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size] |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5002 | |
| 5003 | /** |
| 5004 | * @brief Return the size in bytes of a stack memory region |
| 5005 | * |
| 5006 | * Convenience macro for passing the desired stack size to k_thread_create() |
| 5007 | * since the underlying implementation may actually create something larger |
| 5008 | * (for instance a guard area). |
| 5009 | * |
Andrew Boie | e2d7791 | 2018-05-30 09:45:35 -0700 | [diff] [blame] | 5010 | * The value returned here is not guaranteed to match the 'size' parameter |
| 5011 | * passed to K_THREAD_STACK_DEFINE and may be larger. |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5012 | * |
| 5013 | * @param sym Stack memory symbol |
| 5014 | * @return Size of the stack |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5015 | * @req K-TSTACK-001 |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5016 | */ |
| 5017 | #define K_THREAD_STACK_SIZEOF(sym) sizeof(sym) |
| 5018 | |
Andrew Boie | 575abc0 | 2019-03-19 10:42:24 -0700 | [diff] [blame] | 5019 | |
| 5020 | /** |
| 5021 | * @brief Indicate how much additional memory is reserved for stack objects |
| 5022 | * |
| 5023 | * Any given stack declaration may have additional memory in it for guard |
| 5024 | * areas or supervisor mode stacks. This macro indicates how much space |
| 5025 | * is reserved for this. The memory reserved may not be contiguous within |
| 5026 | * the stack object, and does not account for additional space used due to |
| 5027 | * enforce alignment. |
| 5028 | */ |
| 5029 | #define K_THREAD_STACK_RESERVED 0 |
| 5030 | |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5031 | /** |
| 5032 | * @brief Get a pointer to the physical stack buffer |
| 5033 | * |
Andrew Boie | 4e5c093 | 2019-04-04 12:05:28 -0700 | [diff] [blame] | 5034 | * This macro is deprecated. If a stack buffer needs to be examined, the |
| 5035 | * bounds should be obtained from the associated thread's stack_info struct. |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5036 | * |
| 5037 | * @param sym Declared stack symbol name |
| 5038 | * @return The buffer itself, a char * |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5039 | * @req K-TSTACK-001 |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5040 | */ |
Andrew Boie | 4e5c093 | 2019-04-04 12:05:28 -0700 | [diff] [blame] | 5041 | static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym) |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 5042 | { |
| 5043 | return (char *)sym; |
| 5044 | } |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5045 | |
| 5046 | #endif /* _ARCH_DECLARE_STACK */ |
| 5047 | |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5048 | /** |
| 5049 | * @defgroup mem_domain_apis Memory domain APIs |
| 5050 | * @ingroup kernel_apis |
| 5051 | * @{ |
| 5052 | */ |
| 5053 | |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5054 | /** |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5055 | * @def K_MEM_PARTITION_DEFINE |
| 5056 | * @brief Used to declare a memory partition |
| 5057 | * @req K-MP-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5058 | */ |
| 5059 | #ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK |
| 5060 | #define K_MEM_PARTITION_DEFINE(name, start, size, attr) \ |
| 5061 | _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \ |
Andrew Boie | 41f6011 | 2019-01-31 15:53:24 -0800 | [diff] [blame] | 5062 | struct k_mem_partition name =\ |
Nicolas Pitre | 58d839b | 2019-05-21 11:32:21 -0400 | [diff] [blame] | 5063 | { (uintptr_t)start, size, attr} |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5064 | #else |
| 5065 | #define K_MEM_PARTITION_DEFINE(name, start, size, attr) \ |
Andrew Boie | 41f6011 | 2019-01-31 15:53:24 -0800 | [diff] [blame] | 5066 | struct k_mem_partition name =\ |
Nicolas Pitre | 58d839b | 2019-05-21 11:32:21 -0400 | [diff] [blame] | 5067 | { (uintptr_t)start, size, attr} |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5068 | #endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */ |
| 5069 | |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5070 | /* memory partition */ |
| 5071 | struct k_mem_partition { |
| 5072 | /* start address of memory partition */ |
Nicolas Pitre | 58d839b | 2019-05-21 11:32:21 -0400 | [diff] [blame] | 5073 | uintptr_t start; |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5074 | /* size of memory partition */ |
| 5075 | u32_t size; |
Ioannis Glaropoulos | 39bf24a | 2018-11-27 15:45:36 +0100 | [diff] [blame] | 5076 | #if defined(CONFIG_MEMORY_PROTECTION) |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5077 | /* attribute of memory partition */ |
Adithya Baglody | 83bedcc | 2017-10-06 15:43:11 +0530 | [diff] [blame] | 5078 | k_mem_partition_attr_t attr; |
Ioannis Glaropoulos | 39bf24a | 2018-11-27 15:45:36 +0100 | [diff] [blame] | 5079 | #endif /* CONFIG_MEMORY_PROTECTION */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5080 | }; |
| 5081 | |
Ioannis Glaropoulos | 12c0244 | 2018-09-25 16:05:24 +0200 | [diff] [blame] | 5082 | /* memory domain |
Adithya Baglody | 3a6d72e | 2018-02-13 16:44:44 +0530 | [diff] [blame] | 5083 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5084 | struct k_mem_domain { |
Adithya Baglody | 83bedcc | 2017-10-06 15:43:11 +0530 | [diff] [blame] | 5085 | #ifdef CONFIG_USERSPACE |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5086 | /* partitions in the domain */ |
| 5087 | struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS]; |
Adithya Baglody | 83bedcc | 2017-10-06 15:43:11 +0530 | [diff] [blame] | 5088 | #endif /* CONFIG_USERSPACE */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5089 | /* domain q */ |
| 5090 | sys_dlist_t mem_domain_q; |
Leandro Pereira | 08de658 | 2018-02-28 14:22:57 -0800 | [diff] [blame] | 5091 | /* number of partitions in the domain */ |
| 5092 | u8_t num_partitions; |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5093 | }; |
Adithya Baglody | 83bedcc | 2017-10-06 15:43:11 +0530 | [diff] [blame] | 5094 | |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5095 | |
| 5096 | /** |
| 5097 | * @brief Initialize a memory domain. |
| 5098 | * |
| 5099 | * Initialize a memory domain with given name and memory partitions. |
| 5100 | * |
Andrew Boie | fddd550 | 2019-07-30 18:07:32 -0700 | [diff] [blame] | 5101 | * See documentation for k_mem_domain_add_partition() for details about |
| 5102 | * partition constraints. |
| 5103 | * |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5104 | * @param domain The memory domain to be initialized. |
| 5105 | * @param num_parts The number of array items of "parts" parameter. |
| 5106 | * @param parts An array of pointers to the memory partitions. Can be NULL |
| 5107 | * if num_parts is zero. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5108 | * @req K-MD-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5109 | */ |
Leandro Pereira | 08de658 | 2018-02-28 14:22:57 -0800 | [diff] [blame] | 5110 | 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] | 5111 | struct k_mem_partition *parts[]); |
| 5112 | /** |
| 5113 | * @brief Destroy a memory domain. |
| 5114 | * |
| 5115 | * Destroy a memory domain. |
| 5116 | * |
| 5117 | * @param domain The memory domain to be destroyed. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5118 | * @req K-MD-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5119 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5120 | extern void k_mem_domain_destroy(struct k_mem_domain *domain); |
| 5121 | |
| 5122 | /** |
| 5123 | * @brief Add a memory partition into a memory domain. |
| 5124 | * |
Andrew Boie | fddd550 | 2019-07-30 18:07:32 -0700 | [diff] [blame] | 5125 | * Add a memory partition into a memory domain. Partitions must conform to |
| 5126 | * the following constraints: |
| 5127 | * |
| 5128 | * - Partition bounds must be within system RAM boundaries on MMU-based |
| 5129 | * systems. |
| 5130 | * - Partitions in the same memory domain may not overlap each other. |
| 5131 | * - Partitions must not be defined which expose private kernel |
| 5132 | * data structures or kernel objects. |
| 5133 | * - The starting address alignment, and the partition size must conform to |
| 5134 | * the constraints of the underlying memory management hardware, which |
| 5135 | * varies per architecture. |
| 5136 | * - Memory domain partitions are only intended to control access to memory |
| 5137 | * from user mode threads. |
| 5138 | * |
| 5139 | * Violating these constraints may lead to CPU exceptions or undefined |
| 5140 | * behavior. |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5141 | * |
| 5142 | * @param domain The memory domain to be added a memory partition. |
| 5143 | * @param part The memory partition to be added |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5144 | * @req K-MD-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5145 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5146 | extern void k_mem_domain_add_partition(struct k_mem_domain *domain, |
| 5147 | struct k_mem_partition *part); |
| 5148 | |
| 5149 | /** |
| 5150 | * @brief Remove a memory partition from a memory domain. |
| 5151 | * |
| 5152 | * Remove a memory partition from a memory domain. |
| 5153 | * |
| 5154 | * @param domain The memory domain to be removed a memory partition. |
| 5155 | * @param part The memory partition to be removed |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5156 | * @req K-MD-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5157 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5158 | extern void k_mem_domain_remove_partition(struct k_mem_domain *domain, |
| 5159 | struct k_mem_partition *part); |
| 5160 | |
| 5161 | /** |
| 5162 | * @brief Add a thread into a memory domain. |
| 5163 | * |
| 5164 | * Add a thread into a memory domain. |
| 5165 | * |
| 5166 | * @param domain The memory domain that the thread is going to be added into. |
| 5167 | * @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] | 5168 | * |
| 5169 | * @req K-MD-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5170 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5171 | extern void k_mem_domain_add_thread(struct k_mem_domain *domain, |
| 5172 | k_tid_t thread); |
| 5173 | |
| 5174 | /** |
| 5175 | * @brief Remove a thread from its memory domain. |
| 5176 | * |
| 5177 | * Remove a thread from its memory domain. |
| 5178 | * |
| 5179 | * @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] | 5180 | * @req K-MD-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5181 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5182 | extern void k_mem_domain_remove_thread(k_tid_t thread); |
| 5183 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 5184 | /** @} */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5185 | |
Andrew Boie | 756f907 | 2017-10-10 16:01:49 -0700 | [diff] [blame] | 5186 | /** |
| 5187 | * @brief Emit a character buffer to the console device |
| 5188 | * |
| 5189 | * @param c String of characters to print |
| 5190 | * @param n The length of the string |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5191 | * |
| 5192 | * @req K-MISC-006 |
Andrew Boie | 756f907 | 2017-10-10 16:01:49 -0700 | [diff] [blame] | 5193 | */ |
| 5194 | __syscall void k_str_out(char *c, size_t n); |
| 5195 | |
Ioannis Glaropoulos | a6cb8b0 | 2019-05-09 21:55:10 +0200 | [diff] [blame] | 5196 | /** |
| 5197 | * @brief Disable preservation of floating point context information. |
| 5198 | * |
| 5199 | * This routine informs the kernel that the specified thread |
| 5200 | * will no longer be using the floating point registers. |
| 5201 | * |
| 5202 | * @warning |
| 5203 | * Some architectures apply restrictions on how the disabling of floating |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 5204 | * point preservation may be requested, see arch_float_disable. |
Ioannis Glaropoulos | a6cb8b0 | 2019-05-09 21:55:10 +0200 | [diff] [blame] | 5205 | * |
| 5206 | * @warning |
| 5207 | * This routine should only be used to disable floating point support for |
| 5208 | * a thread that currently has such support enabled. |
| 5209 | * |
| 5210 | * @param thread ID of thread. |
| 5211 | * |
| 5212 | * @retval 0 On success. |
| 5213 | * @retval -ENOSYS If the floating point disabling is not implemented. |
| 5214 | * -EINVAL If the floating point disabling could not be performed. |
| 5215 | */ |
| 5216 | __syscall int k_float_disable(struct k_thread *thread); |
| 5217 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 5218 | #ifdef __cplusplus |
| 5219 | } |
| 5220 | #endif |
| 5221 | |
Anas Nashif | 10291a0 | 2019-06-25 12:25:12 -0400 | [diff] [blame] | 5222 | #include <debug/tracing.h> |
Andrew Boie | fa94ee7 | 2017-09-28 16:54:35 -0700 | [diff] [blame] | 5223 | #include <syscalls/kernel.h> |
| 5224 | |
Benjamin Walsh | dfa7ce5 | 2017-01-22 17:06:05 -0500 | [diff] [blame] | 5225 | #endif /* !_ASMLANGUAGE */ |
| 5226 | |
Flavio Ceolin | 67ca176 | 2018-09-14 10:43:44 -0700 | [diff] [blame] | 5227 | #endif /* ZEPHYR_INCLUDE_KERNEL_H_ */ |