Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, Wind River Systems, Inc. |
| 3 | * |
David B. Kinder | ac74d8b | 2017-01-18 17:01:01 -0800 | [diff] [blame] | 4 | * SPDX-License-Identifier: Apache-2.0 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * @file |
| 9 | * |
| 10 | * @brief Public kernel APIs. |
| 11 | */ |
| 12 | |
Flavio Ceolin | 67ca176 | 2018-09-14 10:43:44 -0700 | [diff] [blame] | 13 | #ifndef ZEPHYR_INCLUDE_KERNEL_H_ |
| 14 | #define ZEPHYR_INCLUDE_KERNEL_H_ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 15 | |
Benjamin Walsh | dfa7ce5 | 2017-01-22 17:06:05 -0500 | [diff] [blame] | 16 | #if !defined(_ASMLANGUAGE) |
Ioannis Glaropoulos | 92b8a41 | 2018-06-20 17:30:48 +0200 | [diff] [blame] | 17 | #include <kernel_includes.h> |
Kumar Gala | 8777ff1 | 2018-07-25 20:24:34 -0500 | [diff] [blame] | 18 | #include <errno.h> |
Flavio Ceolin | 6fdc56d | 2018-09-18 12:32:27 -0700 | [diff] [blame] | 19 | #include <stdbool.h> |
Stephanos Ioannidis | 33fbe00 | 2019-09-09 21:26:59 +0900 | [diff] [blame] | 20 | #include <toolchain.h> |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 21 | |
| 22 | #ifdef __cplusplus |
| 23 | extern "C" { |
| 24 | #endif |
| 25 | |
Anas Nashif | bbb157d | 2017-01-15 08:46:31 -0500 | [diff] [blame] | 26 | /** |
| 27 | * @brief Kernel APIs |
| 28 | * @defgroup kernel_apis Kernel APIs |
| 29 | * @{ |
| 30 | * @} |
| 31 | */ |
| 32 | |
Anas Nashif | 61f4b24 | 2016-11-18 10:53:59 -0500 | [diff] [blame] | 33 | #ifdef CONFIG_KERNEL_DEBUG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 34 | #define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__) |
| 35 | #else |
| 36 | #define K_DEBUG(fmt, ...) |
| 37 | #endif |
| 38 | |
Benjamin Walsh | 2f28041 | 2017-01-14 19:23:46 -0500 | [diff] [blame] | 39 | #if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED) |
| 40 | #define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES) |
| 41 | #define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1) |
| 42 | #elif defined(CONFIG_COOP_ENABLED) |
| 43 | #define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1) |
| 44 | #define _NUM_PREEMPT_PRIO (0) |
| 45 | #elif defined(CONFIG_PREEMPT_ENABLED) |
| 46 | #define _NUM_COOP_PRIO (0) |
| 47 | #define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1) |
| 48 | #else |
| 49 | #error "invalid configuration" |
| 50 | #endif |
| 51 | |
| 52 | #define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x))) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 53 | #define K_PRIO_PREEMPT(x) (x) |
| 54 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 55 | #define K_ANY NULL |
| 56 | #define K_END NULL |
| 57 | |
Benjamin Walsh | edb3570 | 2017-01-14 18:47:22 -0500 | [diff] [blame] | 58 | #if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 59 | #define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES) |
Benjamin Walsh | edb3570 | 2017-01-14 18:47:22 -0500 | [diff] [blame] | 60 | #elif defined(CONFIG_COOP_ENABLED) |
| 61 | #define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1) |
| 62 | #elif defined(CONFIG_PREEMPT_ENABLED) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 63 | #define K_HIGHEST_THREAD_PRIO 0 |
Benjamin Walsh | edb3570 | 2017-01-14 18:47:22 -0500 | [diff] [blame] | 64 | #else |
| 65 | #error "invalid configuration" |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 66 | #endif |
| 67 | |
Benjamin Walsh | 7fa3cd7 | 2017-01-14 18:49:11 -0500 | [diff] [blame] | 68 | #ifdef CONFIG_PREEMPT_ENABLED |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 69 | #define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES |
| 70 | #else |
| 71 | #define K_LOWEST_THREAD_PRIO -1 |
| 72 | #endif |
| 73 | |
Benjamin Walsh | fab8d92 | 2016-11-08 15:36:36 -0500 | [diff] [blame] | 74 | #define K_IDLE_PRIO K_LOWEST_THREAD_PRIO |
| 75 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 76 | #define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO) |
| 77 | #define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1) |
| 78 | |
Andy Ross | 225c74b | 2018-06-27 11:20:50 -0700 | [diff] [blame] | 79 | #ifdef CONFIG_WAITQ_SCALABLE |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 80 | |
| 81 | typedef struct { |
| 82 | struct _priq_rb waitq; |
| 83 | } _wait_q_t; |
| 84 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 85 | extern bool z_priq_rb_lessthan(struct rbnode *a, struct rbnode *b); |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 86 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 87 | #define Z_WAIT_Q_INIT(wait_q) { { { .lessthan_fn = z_priq_rb_lessthan } } } |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 88 | |
| 89 | #else |
| 90 | |
Andy Ross | ccf3bf7 | 2018-05-10 11:10:34 -0700 | [diff] [blame] | 91 | typedef struct { |
| 92 | sys_dlist_t waitq; |
| 93 | } _wait_q_t; |
| 94 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 95 | #define Z_WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) } |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 96 | |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 97 | #endif |
| 98 | |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 99 | #ifdef CONFIG_OBJECT_TRACING |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 100 | #define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next; |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 101 | #define _OBJECT_TRACING_LINKED_FLAG u8_t __linked; |
| 102 | #define _OBJECT_TRACING_INIT \ |
| 103 | .__next = NULL, \ |
| 104 | .__linked = 0, |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 105 | #else |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 106 | #define _OBJECT_TRACING_INIT |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 107 | #define _OBJECT_TRACING_NEXT_PTR(type) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 108 | #define _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 109 | #endif |
| 110 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 111 | #ifdef CONFIG_POLL |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 112 | #define _POLL_EVENT_OBJ_INIT(obj) \ |
| 113 | .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), |
| 114 | #define _POLL_EVENT sys_dlist_t poll_events |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 115 | #else |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 116 | #define _POLL_EVENT_OBJ_INIT(obj) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 117 | #define _POLL_EVENT |
| 118 | #endif |
| 119 | |
Benjamin Walsh | f6ca7de | 2016-11-08 10:36:50 -0500 | [diff] [blame] | 120 | struct k_thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 121 | struct k_mutex; |
| 122 | struct k_sem; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 123 | struct k_msgq; |
| 124 | struct k_mbox; |
| 125 | struct k_pipe; |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 126 | struct k_queue; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 127 | struct k_fifo; |
| 128 | struct k_lifo; |
| 129 | struct k_stack; |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 130 | struct k_mem_slab; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 131 | struct k_mem_pool; |
| 132 | struct k_timer; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 133 | struct k_poll_event; |
| 134 | struct k_poll_signal; |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 135 | struct k_mem_domain; |
| 136 | struct k_mem_partition; |
Wentong Wu | 5611e92 | 2019-06-20 23:51:27 +0800 | [diff] [blame] | 137 | struct k_futex; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 138 | |
Andrew Boie | 5bd891d | 2017-09-27 12:59:28 -0700 | [diff] [blame] | 139 | /* This enumeration needs to be kept in sync with the lists of kernel objects |
| 140 | * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str() |
| 141 | * function in kernel/userspace.c |
| 142 | */ |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 143 | enum k_objects { |
Andrew Boie | 7e3d3d7 | 2017-10-10 09:31:32 -0700 | [diff] [blame] | 144 | K_OBJ_ANY, |
| 145 | |
Leandro Pereira | c200367 | 2018-04-04 13:50:32 -0700 | [diff] [blame] | 146 | /** @cond |
| 147 | * Doxygen should ignore this build-time generated include file |
| 148 | * when genrating API documentation. Enumeration values are |
| 149 | * generated during build by gen_kobject_list.py. It includes |
| 150 | * basic kernel objects (e.g. pipes and mutexes) and driver types. |
| 151 | */ |
| 152 | #include <kobj-types-enum.h> |
| 153 | /** @endcond |
| 154 | */ |
Andrew Boie | 5bd891d | 2017-09-27 12:59:28 -0700 | [diff] [blame] | 155 | |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 156 | K_OBJ_LAST |
| 157 | }; |
Anas Nashif | 4bcb294 | 2019-01-23 23:06:29 -0500 | [diff] [blame] | 158 | /** |
| 159 | * @defgroup usermode_apis User Mode APIs |
| 160 | * @ingroup kernel_apis |
| 161 | * @{ |
| 162 | */ |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 163 | |
| 164 | #ifdef CONFIG_USERSPACE |
| 165 | /* Table generated by gperf, these objects are retrieved via |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 166 | * z_object_find() */ |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 167 | struct _k_object { |
| 168 | char *name; |
Andrew Boie | a811af3 | 2017-10-14 13:50:26 -0700 | [diff] [blame] | 169 | u8_t perms[CONFIG_MAX_THREAD_BYTES]; |
| 170 | u8_t type; |
| 171 | u8_t flags; |
Andrew Boie | bca15da | 2017-10-15 14:17:48 -0700 | [diff] [blame] | 172 | u32_t data; |
Andrew Boie | df55524 | 2018-05-25 07:28:54 -0700 | [diff] [blame] | 173 | } __packed __aligned(4); |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 174 | |
Andrew Boie | 877f82e | 2017-10-17 11:20:22 -0700 | [diff] [blame] | 175 | struct _k_object_assignment { |
| 176 | struct k_thread *thread; |
| 177 | void * const *objects; |
| 178 | }; |
| 179 | |
| 180 | /** |
| 181 | * @brief Grant a static thread access to a list of kernel objects |
| 182 | * |
| 183 | * For threads declared with K_THREAD_DEFINE(), grant the thread access to |
| 184 | * a set of kernel objects. These objects do not need to be in an initialized |
| 185 | * state. The permissions will be granted when the threads are initialized |
| 186 | * in the early boot sequence. |
| 187 | * |
| 188 | * All arguments beyond the first must be pointers to kernel objects. |
| 189 | * |
| 190 | * @param name_ Name of the thread, as passed to K_THREAD_DEFINE() |
| 191 | */ |
| 192 | #define K_THREAD_ACCESS_GRANT(name_, ...) \ |
| 193 | static void * const _CONCAT(_object_list_, name_)[] = \ |
| 194 | { __VA_ARGS__, NULL }; \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 195 | static const Z_STRUCT_SECTION_ITERABLE(_k_object_assignment, \ |
| 196 | _CONCAT(_object_access_, name_)) = \ |
Andrew Boie | 877f82e | 2017-10-17 11:20:22 -0700 | [diff] [blame] | 197 | { (&_k_thread_obj_ ## name_), \ |
| 198 | (_CONCAT(_object_list_, name_)) } |
| 199 | |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 200 | #define K_OBJ_FLAG_INITIALIZED BIT(0) |
Andrew Boie | 04caa67 | 2017-10-13 13:57:07 -0700 | [diff] [blame] | 201 | #define K_OBJ_FLAG_PUBLIC BIT(1) |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 202 | #define K_OBJ_FLAG_ALLOC BIT(2) |
Andrew Boie | 7875707 | 2019-07-23 13:29:30 -0700 | [diff] [blame] | 203 | #define K_OBJ_FLAG_DRIVER BIT(3) |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 204 | |
| 205 | /** |
| 206 | * Lookup a kernel object and init its metadata if it exists |
| 207 | * |
| 208 | * Calling this on an object will make it usable from userspace. |
| 209 | * Intended to be called as the last statement in kernel object init |
| 210 | * functions. |
| 211 | * |
Anas Nashif | 50e3acd | 2018-12-08 13:26:18 -0500 | [diff] [blame] | 212 | * @param obj Address of the kernel object |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 213 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 214 | void z_object_init(void *obj); |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 215 | #else |
Andrew Boie | c3d4e65 | 2019-06-28 14:19:16 -0700 | [diff] [blame] | 216 | /* LCOV_EXCL_START */ |
Andrew Boie | 877f82e | 2017-10-17 11:20:22 -0700 | [diff] [blame] | 217 | #define K_THREAD_ACCESS_GRANT(thread, ...) |
| 218 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 219 | /** |
| 220 | * @internal |
| 221 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 222 | static inline void z_object_init(void *obj) |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 223 | { |
| 224 | ARG_UNUSED(obj); |
| 225 | } |
| 226 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 227 | /** |
| 228 | * @internal |
| 229 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 230 | static inline void z_impl_k_object_access_grant(void *object, |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 231 | struct k_thread *thread) |
| 232 | { |
| 233 | ARG_UNUSED(object); |
| 234 | ARG_UNUSED(thread); |
| 235 | } |
| 236 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 237 | /** |
| 238 | * @internal |
| 239 | */ |
Andrew Boie | e9cfc54 | 2018-04-13 13:15:28 -0700 | [diff] [blame] | 240 | static inline void k_object_access_revoke(void *object, |
| 241 | struct k_thread *thread) |
Andrew Boie | a89bf01 | 2017-10-09 14:47:55 -0700 | [diff] [blame] | 242 | { |
| 243 | ARG_UNUSED(object); |
| 244 | ARG_UNUSED(thread); |
| 245 | } |
| 246 | |
Andrew Boie | e9cfc54 | 2018-04-13 13:15:28 -0700 | [diff] [blame] | 247 | /** |
| 248 | * @internal |
| 249 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 250 | static inline void z_impl_k_object_release(void *object) |
Andrew Boie | e9cfc54 | 2018-04-13 13:15:28 -0700 | [diff] [blame] | 251 | { |
| 252 | ARG_UNUSED(object); |
| 253 | } |
| 254 | |
Andrew Boie | 41bab6e | 2017-10-14 14:42:23 -0700 | [diff] [blame] | 255 | static inline void k_object_access_all_grant(void *object) |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 256 | { |
| 257 | ARG_UNUSED(object); |
| 258 | } |
Andrew Boie | c3d4e65 | 2019-06-28 14:19:16 -0700 | [diff] [blame] | 259 | /* LCOV_EXCL_STOP */ |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 260 | #endif /* !CONFIG_USERSPACE */ |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 261 | |
| 262 | /** |
Marti Bolivar | 67db616 | 2019-08-27 19:12:51 -0600 | [diff] [blame] | 263 | * Grant a thread access to a kernel object |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 264 | * |
| 265 | * The thread will be granted access to the object if the caller is from |
| 266 | * supervisor mode, or the caller is from user mode AND has permissions |
Andrew Boie | a89bf01 | 2017-10-09 14:47:55 -0700 | [diff] [blame] | 267 | * on both the object and the thread whose access is being granted. |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 268 | * |
| 269 | * @param object Address of kernel object |
| 270 | * @param thread Thread to grant access to the object |
| 271 | */ |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 272 | __syscall void k_object_access_grant(void *object, struct k_thread *thread); |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 273 | |
Andrew Boie | a89bf01 | 2017-10-09 14:47:55 -0700 | [diff] [blame] | 274 | /** |
Marti Bolivar | 67db616 | 2019-08-27 19:12:51 -0600 | [diff] [blame] | 275 | * Revoke a thread's access to a kernel object |
Andrew Boie | a89bf01 | 2017-10-09 14:47:55 -0700 | [diff] [blame] | 276 | * |
| 277 | * The thread will lose access to the object if the caller is from |
| 278 | * supervisor mode, or the caller is from user mode AND has permissions |
| 279 | * on both the object and the thread whose access is being revoked. |
| 280 | * |
| 281 | * @param object Address of kernel object |
| 282 | * @param thread Thread to remove access to the object |
| 283 | */ |
Andrew Boie | e9cfc54 | 2018-04-13 13:15:28 -0700 | [diff] [blame] | 284 | void k_object_access_revoke(void *object, struct k_thread *thread); |
| 285 | |
| 286 | |
| 287 | __syscall void k_object_release(void *object); |
Andrew Boie | 3b5ae80 | 2017-10-04 12:10:32 -0700 | [diff] [blame] | 288 | |
| 289 | /** |
Marti Bolivar | 67db616 | 2019-08-27 19:12:51 -0600 | [diff] [blame] | 290 | * Grant all present and future threads access to an object |
Andrew Boie | 3b5ae80 | 2017-10-04 12:10:32 -0700 | [diff] [blame] | 291 | * |
| 292 | * If the caller is from supervisor mode, or the caller is from user mode and |
| 293 | * have sufficient permissions on the object, then that object will have |
| 294 | * permissions granted to it for *all* current and future threads running in |
| 295 | * the system, effectively becoming a public kernel object. |
| 296 | * |
| 297 | * Use of this API should be avoided on systems that are running untrusted code |
| 298 | * as it is possible for such code to derive the addresses of kernel objects |
| 299 | * and perform unwanted operations on them. |
| 300 | * |
Andrew Boie | 04caa67 | 2017-10-13 13:57:07 -0700 | [diff] [blame] | 301 | * It is not possible to revoke permissions on public objects; once public, |
| 302 | * any thread may use it. |
| 303 | * |
Andrew Boie | 3b5ae80 | 2017-10-04 12:10:32 -0700 | [diff] [blame] | 304 | * @param object Address of kernel object |
| 305 | */ |
Andrew Boie | 41bab6e | 2017-10-14 14:42:23 -0700 | [diff] [blame] | 306 | void k_object_access_all_grant(void *object); |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 307 | |
Andrew Boie | 31bdfc0 | 2017-11-08 16:38:03 -0800 | [diff] [blame] | 308 | /** |
| 309 | * Allocate a kernel object of a designated type |
| 310 | * |
| 311 | * This will instantiate at runtime a kernel object of the specified type, |
| 312 | * returning a pointer to it. The object will be returned in an uninitialized |
| 313 | * state, with the calling thread being granted permission on it. The memory |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 314 | * for the object will be allocated out of the calling thread's resource pool. |
Andrew Boie | 31bdfc0 | 2017-11-08 16:38:03 -0800 | [diff] [blame] | 315 | * |
| 316 | * Currently, allocation of thread stacks is not supported. |
| 317 | * |
| 318 | * @param otype Requested kernel object type |
| 319 | * @return A pointer to the allocated kernel object, or NULL if memory wasn't |
| 320 | * available |
| 321 | */ |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 322 | __syscall void *k_object_alloc(enum k_objects otype); |
Andrew Boie | 31bdfc0 | 2017-11-08 16:38:03 -0800 | [diff] [blame] | 323 | |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 324 | #ifdef CONFIG_DYNAMIC_OBJECTS |
Andrew Boie | 31bdfc0 | 2017-11-08 16:38:03 -0800 | [diff] [blame] | 325 | /** |
| 326 | * Free a kernel object previously allocated with k_object_alloc() |
| 327 | * |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 328 | * This will return memory for a kernel object back to resource pool it was |
| 329 | * allocated from. Care must be exercised that the object will not be used |
| 330 | * during or after when this call is made. |
Andrew Boie | 31bdfc0 | 2017-11-08 16:38:03 -0800 | [diff] [blame] | 331 | * |
| 332 | * @param obj Pointer to the kernel object memory address. |
| 333 | */ |
| 334 | void k_object_free(void *obj); |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 335 | #else |
Andrew Boie | c3d4e65 | 2019-06-28 14:19:16 -0700 | [diff] [blame] | 336 | /* LCOV_EXCL_START */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 337 | static inline void *z_impl_k_object_alloc(enum k_objects otype) |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 338 | { |
Kumar Gala | 85699f7 | 2018-05-17 09:26:03 -0500 | [diff] [blame] | 339 | ARG_UNUSED(otype); |
| 340 | |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 341 | return NULL; |
| 342 | } |
| 343 | |
| 344 | static inline void k_obj_free(void *obj) |
| 345 | { |
| 346 | ARG_UNUSED(obj); |
| 347 | } |
Andrew Boie | c3d4e65 | 2019-06-28 14:19:16 -0700 | [diff] [blame] | 348 | /* LCOV_EXCL_STOP */ |
Andrew Boie | 31bdfc0 | 2017-11-08 16:38:03 -0800 | [diff] [blame] | 349 | #endif /* CONFIG_DYNAMIC_OBJECTS */ |
| 350 | |
Anas Nashif | 4bcb294 | 2019-01-23 23:06:29 -0500 | [diff] [blame] | 351 | /** @} */ |
| 352 | |
Andrew Boie | bca15da | 2017-10-15 14:17:48 -0700 | [diff] [blame] | 353 | /* Using typedef deliberately here, this is quite intended to be an opaque |
Andrew Boie | 4e5c093 | 2019-04-04 12:05:28 -0700 | [diff] [blame] | 354 | * type. |
Andrew Boie | bca15da | 2017-10-15 14:17:48 -0700 | [diff] [blame] | 355 | * |
| 356 | * The purpose of this data type is to clearly distinguish between the |
| 357 | * declared symbol for a stack (of type k_thread_stack_t) and the underlying |
| 358 | * buffer which composes the stack data actually used by the underlying |
Anas Nashif | f2cb20c | 2019-06-18 14:45:40 -0400 | [diff] [blame] | 359 | * thread; they cannot be used interchangeably as some arches precede the |
Andrew Boie | bca15da | 2017-10-15 14:17:48 -0700 | [diff] [blame] | 360 | * stack buffer region with guard areas that trigger a MPU or MMU fault |
| 361 | * if written to. |
| 362 | * |
| 363 | * APIs that want to work with the buffer inside should continue to use |
| 364 | * char *. |
| 365 | * |
| 366 | * Stacks should always be created with K_THREAD_STACK_DEFINE(). |
| 367 | */ |
| 368 | struct __packed _k_thread_stack_element { |
| 369 | char data; |
| 370 | }; |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 371 | typedef struct _k_thread_stack_element k_thread_stack_t; |
Andrew Boie | bca15da | 2017-10-15 14:17:48 -0700 | [diff] [blame] | 372 | |
Andrew Boie | 1e06ffc | 2017-09-11 09:30:04 -0700 | [diff] [blame] | 373 | /** |
| 374 | * @typedef k_thread_entry_t |
| 375 | * @brief Thread entry point function type. |
| 376 | * |
| 377 | * A thread's entry point function is invoked when the thread starts executing. |
| 378 | * Up to 3 argument values can be passed to the function. |
| 379 | * |
| 380 | * The thread terminates execution permanently if the entry point function |
| 381 | * returns. The thread is responsible for releasing any shared resources |
| 382 | * it may own (such as mutexes and dynamically allocated memory), prior to |
| 383 | * returning. |
| 384 | * |
| 385 | * @param p1 First argument. |
| 386 | * @param p2 Second argument. |
| 387 | * @param p3 Third argument. |
| 388 | * |
| 389 | * @return N/A |
| 390 | */ |
| 391 | typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3); |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 392 | |
| 393 | #ifdef CONFIG_THREAD_MONITOR |
| 394 | struct __thread_entry { |
Andrew Boie | 1e06ffc | 2017-09-11 09:30:04 -0700 | [diff] [blame] | 395 | k_thread_entry_t pEntry; |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 396 | void *parameter1; |
| 397 | void *parameter2; |
| 398 | void *parameter3; |
| 399 | }; |
| 400 | #endif |
| 401 | |
| 402 | /* can be used for creating 'dummy' threads, e.g. for pending on objects */ |
| 403 | struct _thread_base { |
| 404 | |
| 405 | /* this thread's entry in a ready/wait queue */ |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 406 | union { |
Peter A. Bigot | 82ad0d2 | 2019-01-03 23:49:28 -0600 | [diff] [blame] | 407 | sys_dnode_t qnode_dlist; |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 408 | struct rbnode qnode_rb; |
| 409 | }; |
| 410 | |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 411 | /* wait queue on which the thread is pended (needed only for |
| 412 | * trees, not dumb lists) |
| 413 | */ |
| 414 | _wait_q_t *pended_on; |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 415 | |
| 416 | /* user facing 'thread options'; values defined in include/kernel.h */ |
| 417 | u8_t user_options; |
| 418 | |
| 419 | /* thread state */ |
| 420 | u8_t thread_state; |
| 421 | |
| 422 | /* |
| 423 | * scheduler lock count and thread priority |
| 424 | * |
| 425 | * These two fields control the preemptibility of a thread. |
| 426 | * |
| 427 | * When the scheduler is locked, sched_locked is decremented, which |
| 428 | * means that the scheduler is locked for values from 0xff to 0x01. A |
| 429 | * thread is coop if its prio is negative, thus 0x80 to 0xff when |
| 430 | * looked at the value as unsigned. |
| 431 | * |
| 432 | * By putting them end-to-end, this means that a thread is |
| 433 | * non-preemptible if the bundled value is greater than or equal to |
| 434 | * 0x0080. |
| 435 | */ |
| 436 | union { |
| 437 | struct { |
| 438 | #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 439 | u8_t sched_locked; |
| 440 | s8_t prio; |
| 441 | #else /* LITTLE and PDP */ |
| 442 | s8_t prio; |
| 443 | u8_t sched_locked; |
| 444 | #endif |
| 445 | }; |
| 446 | u16_t preempt; |
| 447 | }; |
| 448 | |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 449 | #ifdef CONFIG_SCHED_DEADLINE |
| 450 | int prio_deadline; |
| 451 | #endif |
| 452 | |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 453 | u32_t order_key; |
| 454 | |
Andy Ross | 2724fd1 | 2018-01-29 14:55:20 -0800 | [diff] [blame] | 455 | #ifdef CONFIG_SMP |
| 456 | /* True for the per-CPU idle threads */ |
| 457 | u8_t is_idle; |
| 458 | |
Andy Ross | 2724fd1 | 2018-01-29 14:55:20 -0800 | [diff] [blame] | 459 | /* CPU index on which thread was last run */ |
| 460 | u8_t cpu; |
Andy Ross | 15c4007 | 2018-04-12 12:50:05 -0700 | [diff] [blame] | 461 | |
| 462 | /* Recursive count of irq_lock() calls */ |
| 463 | u8_t global_lock_count; |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 464 | |
| 465 | #endif |
| 466 | |
| 467 | #ifdef CONFIG_SCHED_CPU_MASK |
| 468 | /* "May run on" bits for each CPU */ |
| 469 | u8_t cpu_mask; |
Andy Ross | 2724fd1 | 2018-01-29 14:55:20 -0800 | [diff] [blame] | 470 | #endif |
| 471 | |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 472 | /* data returned by APIs */ |
| 473 | void *swap_data; |
| 474 | |
| 475 | #ifdef CONFIG_SYS_CLOCK_EXISTS |
| 476 | /* this thread's entry in a timeout queue */ |
| 477 | struct _timeout timeout; |
| 478 | #endif |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 479 | }; |
| 480 | |
| 481 | typedef struct _thread_base _thread_base_t; |
| 482 | |
| 483 | #if defined(CONFIG_THREAD_STACK_INFO) |
| 484 | /* Contains the stack information of a thread */ |
| 485 | struct _thread_stack_info { |
Andrew Boie | 4e5c093 | 2019-04-04 12:05:28 -0700 | [diff] [blame] | 486 | /* Stack start - Represents the start address of the thread-writable |
| 487 | * stack area. |
Andrew Boie | b85ac3e | 2018-06-01 12:15:13 -0700 | [diff] [blame] | 488 | */ |
Nicolas Pitre | 58d839b | 2019-05-21 11:32:21 -0400 | [diff] [blame] | 489 | uintptr_t start; |
Andrew Boie | b85ac3e | 2018-06-01 12:15:13 -0700 | [diff] [blame] | 490 | |
| 491 | /* Stack Size - Thread writable stack buffer size. Represents |
| 492 | * the size of the actual area, starting from the start member, |
| 493 | * that should be writable by the thread |
| 494 | */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 495 | u32_t size; |
| 496 | }; |
Andrew Boie | 41c68ec | 2017-05-11 15:38:20 -0700 | [diff] [blame] | 497 | |
| 498 | typedef struct _thread_stack_info _thread_stack_info_t; |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 499 | #endif /* CONFIG_THREAD_STACK_INFO */ |
| 500 | |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 501 | #if defined(CONFIG_USERSPACE) |
| 502 | struct _mem_domain_info { |
| 503 | /* memory domain queue node */ |
| 504 | sys_dnode_t mem_domain_q_node; |
| 505 | /* memory domain of the thread */ |
| 506 | struct k_mem_domain *mem_domain; |
| 507 | }; |
| 508 | |
| 509 | #endif /* CONFIG_USERSPACE */ |
| 510 | |
Daniel Leung | fc18243 | 2018-08-16 15:42:28 -0700 | [diff] [blame] | 511 | #ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA |
| 512 | struct _thread_userspace_local_data { |
| 513 | int errno_var; |
| 514 | }; |
| 515 | #endif |
| 516 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 517 | /** |
| 518 | * @ingroup thread_apis |
| 519 | * Thread Structure |
| 520 | */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 521 | struct k_thread { |
| 522 | |
| 523 | struct _thread_base base; |
| 524 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 525 | /** defined by the architecture, but all archs need these */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 526 | struct _callee_saved callee_saved; |
| 527 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 528 | /** static thread init data */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 529 | void *init_data; |
| 530 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 531 | /** |
| 532 | * abort function |
| 533 | * @req K-THREAD-002 |
| 534 | * */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 535 | void (*fn_abort)(void); |
| 536 | |
| 537 | #if defined(CONFIG_THREAD_MONITOR) |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 538 | /** thread entry and parameters description */ |
Andrew Boie | 2dd91ec | 2018-06-06 08:45:01 -0700 | [diff] [blame] | 539 | struct __thread_entry entry; |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 540 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 541 | /** next item in list of all threads */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 542 | struct k_thread *next_thread; |
| 543 | #endif |
| 544 | |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 545 | #if defined(CONFIG_THREAD_NAME) |
| 546 | /* Thread name */ |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 547 | char name[CONFIG_THREAD_MAX_NAME_LEN]; |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 548 | #endif |
| 549 | |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 550 | #ifdef CONFIG_THREAD_CUSTOM_DATA |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 551 | /** crude thread-local storage */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 552 | void *custom_data; |
| 553 | #endif |
| 554 | |
Daniel Leung | fc18243 | 2018-08-16 15:42:28 -0700 | [diff] [blame] | 555 | #ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA |
| 556 | struct _thread_userspace_local_data *userspace_local_data; |
| 557 | #endif |
| 558 | |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 559 | #ifdef CONFIG_ERRNO |
Daniel Leung | fc18243 | 2018-08-16 15:42:28 -0700 | [diff] [blame] | 560 | #ifndef CONFIG_USERSPACE |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 561 | /** per-thread errno variable */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 562 | int errno_var; |
| 563 | #endif |
Andrew Boie | 7f4d006 | 2018-07-19 11:09:33 -0700 | [diff] [blame] | 564 | #endif |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 565 | |
| 566 | #if defined(CONFIG_THREAD_STACK_INFO) |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 567 | /** Stack Info */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 568 | struct _thread_stack_info stack_info; |
| 569 | #endif /* CONFIG_THREAD_STACK_INFO */ |
| 570 | |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 571 | #if defined(CONFIG_USERSPACE) |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 572 | /** memory domain info of the thread */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 573 | struct _mem_domain_info mem_domain_info; |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 574 | /** Base address of thread stack */ |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 575 | k_thread_stack_t *stack_obj; |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 576 | #endif /* CONFIG_USERSPACE */ |
| 577 | |
Andy Ross | 042d8ec | 2017-12-09 08:37:20 -0800 | [diff] [blame] | 578 | #if defined(CONFIG_USE_SWITCH) |
| 579 | /* When using __switch() a few previously arch-specific items |
| 580 | * become part of the core OS |
| 581 | */ |
| 582 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 583 | /** z_swap() return value */ |
Andy Ross | 042d8ec | 2017-12-09 08:37:20 -0800 | [diff] [blame] | 584 | int swap_retval; |
| 585 | |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 586 | /** Context handle returned via arch_switch() */ |
Andy Ross | 042d8ec | 2017-12-09 08:37:20 -0800 | [diff] [blame] | 587 | void *switch_handle; |
| 588 | #endif |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 589 | /** resource pool */ |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 590 | struct k_mem_pool *resource_pool; |
Andy Ross | 042d8ec | 2017-12-09 08:37:20 -0800 | [diff] [blame] | 591 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 592 | /** arch-specifics: must always be at the end */ |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 593 | struct _thread_arch arch; |
| 594 | }; |
| 595 | |
| 596 | typedef struct k_thread _thread_t; |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 597 | typedef struct k_thread *k_tid_t; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 598 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 599 | enum execution_context_types { |
| 600 | K_ISR = 0, |
| 601 | K_COOP_THREAD, |
| 602 | K_PREEMPT_THREAD, |
| 603 | }; |
| 604 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 605 | /** |
Anas Nashif | 4bcb294 | 2019-01-23 23:06:29 -0500 | [diff] [blame] | 606 | * @addtogroup thread_apis |
Carles Cufi | cb0cf9f | 2017-01-10 10:57:38 +0100 | [diff] [blame] | 607 | * @{ |
| 608 | */ |
Ramakrishna Pallala | 110b8e4 | 2018-04-27 12:55:43 +0530 | [diff] [blame] | 609 | typedef void (*k_thread_user_cb_t)(const struct k_thread *thread, |
| 610 | void *user_data); |
Carles Cufi | cb0cf9f | 2017-01-10 10:57:38 +0100 | [diff] [blame] | 611 | |
| 612 | /** |
Ramakrishna Pallala | 110b8e4 | 2018-04-27 12:55:43 +0530 | [diff] [blame] | 613 | * @brief Iterate over all the threads in the system. |
| 614 | * |
| 615 | * This routine iterates over all the threads in the system and |
| 616 | * calls the user_cb function for each thread. |
| 617 | * |
| 618 | * @param user_cb Pointer to the user callback function. |
| 619 | * @param user_data Pointer to user data. |
| 620 | * |
| 621 | * @note CONFIG_THREAD_MONITOR must be set for this function |
| 622 | * to be effective. Also this API uses irq_lock to protect the |
| 623 | * _kernel.threads list which means creation of new threads and |
| 624 | * terminations of existing threads are blocked until this |
| 625 | * API returns. |
| 626 | * |
| 627 | * @return N/A |
| 628 | */ |
| 629 | extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data); |
| 630 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 631 | /** @} */ |
Carles Cufi | cb0cf9f | 2017-01-10 10:57:38 +0100 | [diff] [blame] | 632 | |
| 633 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 634 | * @defgroup thread_apis Thread APIs |
| 635 | * @ingroup kernel_apis |
| 636 | * @{ |
| 637 | */ |
| 638 | |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 639 | #endif /* !_ASMLANGUAGE */ |
| 640 | |
| 641 | |
| 642 | /* |
| 643 | * Thread user options. May be needed by assembly code. Common part uses low |
| 644 | * bits, arch-specific use high bits. |
| 645 | */ |
| 646 | |
Anas Nashif | a541e93 | 2018-05-24 11:19:16 -0500 | [diff] [blame] | 647 | /** |
| 648 | * @brief system thread that must not abort |
| 649 | * @req K-THREAD-000 |
| 650 | * */ |
Flavio Ceolin | 8aec087 | 2018-08-15 11:52:00 -0700 | [diff] [blame] | 651 | #define K_ESSENTIAL (BIT(0)) |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 652 | |
| 653 | #if defined(CONFIG_FP_SHARING) |
Anas Nashif | a541e93 | 2018-05-24 11:19:16 -0500 | [diff] [blame] | 654 | /** |
| 655 | * @brief thread uses floating point registers |
| 656 | */ |
Flavio Ceolin | 8aec087 | 2018-08-15 11:52:00 -0700 | [diff] [blame] | 657 | #define K_FP_REGS (BIT(1)) |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 658 | #endif |
| 659 | |
Anas Nashif | a541e93 | 2018-05-24 11:19:16 -0500 | [diff] [blame] | 660 | /** |
| 661 | * @brief user mode thread |
| 662 | * |
| 663 | * This thread has dropped from supervisor mode to user mode and consequently |
Andrew Boie | 5cfa5dc | 2017-08-30 14:17:44 -0700 | [diff] [blame] | 664 | * has additional restrictions |
| 665 | */ |
Flavio Ceolin | 8aec087 | 2018-08-15 11:52:00 -0700 | [diff] [blame] | 666 | #define K_USER (BIT(2)) |
Andrew Boie | 5cfa5dc | 2017-08-30 14:17:44 -0700 | [diff] [blame] | 667 | |
Anas Nashif | a541e93 | 2018-05-24 11:19:16 -0500 | [diff] [blame] | 668 | /** |
| 669 | * @brief Inherit Permissions |
| 670 | * |
| 671 | * @details |
| 672 | * Indicates that the thread being created should inherit all kernel object |
Andrew Boie | 47f8fd1 | 2017-10-05 11:11:02 -0700 | [diff] [blame] | 673 | * permissions from the thread that created it. No effect if CONFIG_USERSPACE |
| 674 | * is not enabled. |
| 675 | */ |
Flavio Ceolin | 8aec087 | 2018-08-15 11:52:00 -0700 | [diff] [blame] | 676 | #define K_INHERIT_PERMS (BIT(3)) |
Andrew Boie | 47f8fd1 | 2017-10-05 11:11:02 -0700 | [diff] [blame] | 677 | |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 678 | #ifdef CONFIG_X86 |
| 679 | /* x86 Bitmask definitions for threads user options */ |
| 680 | |
| 681 | #if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE) |
| 682 | /* thread uses SSEx (and also FP) registers */ |
Flavio Ceolin | 8aec087 | 2018-08-15 11:52:00 -0700 | [diff] [blame] | 683 | #define K_SSE_REGS (BIT(7)) |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 684 | #endif |
| 685 | #endif |
| 686 | |
| 687 | /* end - thread options */ |
| 688 | |
| 689 | #if !defined(_ASMLANGUAGE) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 690 | /** |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 691 | * @brief Create a thread. |
| 692 | * |
| 693 | * This routine initializes a thread, then schedules it for execution. |
| 694 | * |
| 695 | * The new thread may be scheduled for immediate execution or a delayed start. |
| 696 | * If the newly spawned thread does not have a delayed start the kernel |
| 697 | * scheduler may preempt the current thread to allow the new thread to |
| 698 | * execute. |
| 699 | * |
| 700 | * Thread options are architecture-specific, and can include K_ESSENTIAL, |
| 701 | * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating |
| 702 | * them using "|" (the logical OR operator). |
| 703 | * |
| 704 | * Historically, users often would use the beginning of the stack memory region |
| 705 | * to store the struct k_thread data, although corruption will occur if the |
| 706 | * stack overflows this region and stack protection features may not detect this |
| 707 | * situation. |
| 708 | * |
| 709 | * @param new_thread Pointer to uninitialized struct k_thread |
| 710 | * @param stack Pointer to the stack space. |
| 711 | * @param stack_size Stack size in bytes. |
| 712 | * @param entry Thread entry function. |
| 713 | * @param p1 1st entry point parameter. |
| 714 | * @param p2 2nd entry point parameter. |
| 715 | * @param p3 3rd entry point parameter. |
| 716 | * @param prio Thread priority. |
| 717 | * @param options Thread options. |
| 718 | * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay). |
| 719 | * |
| 720 | * @return ID of new thread. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 721 | * |
| 722 | * @req K-THREAD-001 |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 723 | */ |
Andrew Boie | 662c345 | 2017-10-02 10:51:18 -0700 | [diff] [blame] | 724 | __syscall k_tid_t k_thread_create(struct k_thread *new_thread, |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 725 | k_thread_stack_t *stack, |
Andrew Boie | 662c345 | 2017-10-02 10:51:18 -0700 | [diff] [blame] | 726 | size_t stack_size, |
| 727 | k_thread_entry_t entry, |
| 728 | void *p1, void *p2, void *p3, |
| 729 | int prio, u32_t options, s32_t delay); |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 730 | |
Andrew Boie | 3f091b5 | 2017-08-30 14:34:14 -0700 | [diff] [blame] | 731 | /** |
| 732 | * @brief Drop a thread's privileges permanently to user mode |
| 733 | * |
| 734 | * @param entry Function to start executing from |
| 735 | * @param p1 1st entry point parameter |
| 736 | * @param p2 2nd entry point parameter |
| 737 | * @param p3 3rd entry point parameter |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 738 | * @req K-THREAD-003 |
Andrew Boie | 3f091b5 | 2017-08-30 14:34:14 -0700 | [diff] [blame] | 739 | */ |
| 740 | extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry, |
| 741 | void *p1, void *p2, |
| 742 | void *p3); |
Andrew Boie | 3f091b5 | 2017-08-30 14:34:14 -0700 | [diff] [blame] | 743 | |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 744 | /** |
Adithya Baglody | 392219e | 2019-01-02 14:40:39 +0530 | [diff] [blame] | 745 | * @brief Grant a thread access to a set of kernel objects |
Andrew Boie | e12857a | 2017-10-17 11:38:26 -0700 | [diff] [blame] | 746 | * |
| 747 | * This is a convenience function. For the provided thread, grant access to |
| 748 | * the remaining arguments, which must be pointers to kernel objects. |
Andrew Boie | e12857a | 2017-10-17 11:38:26 -0700 | [diff] [blame] | 749 | * |
| 750 | * The thread object must be initialized (i.e. running). The objects don't |
| 751 | * need to be. |
Adithya Baglody | 392219e | 2019-01-02 14:40:39 +0530 | [diff] [blame] | 752 | * Note that NULL shouldn't be passed as an argument. |
Andrew Boie | e12857a | 2017-10-17 11:38:26 -0700 | [diff] [blame] | 753 | * |
| 754 | * @param thread Thread to grant access to objects |
Adithya Baglody | 392219e | 2019-01-02 14:40:39 +0530 | [diff] [blame] | 755 | * @param ... list of kernel object pointers |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 756 | * @req K-THREAD-004 |
Andrew Boie | e12857a | 2017-10-17 11:38:26 -0700 | [diff] [blame] | 757 | */ |
Adithya Baglody | 392219e | 2019-01-02 14:40:39 +0530 | [diff] [blame] | 758 | #define k_thread_access_grant(thread, ...) \ |
| 759 | FOR_EACH_FIXED_ARG(k_object_access_grant, thread, __VA_ARGS__) |
Andrew Boie | e12857a | 2017-10-17 11:38:26 -0700 | [diff] [blame] | 760 | |
| 761 | /** |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 762 | * @brief Assign a resource memory pool to a thread |
| 763 | * |
| 764 | * By default, threads have no resource pool assigned unless their parent |
| 765 | * thread has a resource pool, in which case it is inherited. Multiple |
| 766 | * threads may be assigned to the same memory pool. |
| 767 | * |
| 768 | * Changing a thread's resource pool will not migrate allocations from the |
| 769 | * previous pool. |
| 770 | * |
| 771 | * @param thread Target thread to assign a memory pool for resource requests, |
| 772 | * or NULL if the thread should no longer have a memory pool. |
| 773 | * @param pool Memory pool to use for resources. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 774 | * @req K-THREAD-005 |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 775 | */ |
| 776 | static inline void k_thread_resource_pool_assign(struct k_thread *thread, |
| 777 | struct k_mem_pool *pool) |
| 778 | { |
| 779 | thread->resource_pool = pool; |
| 780 | } |
| 781 | |
| 782 | #if (CONFIG_HEAP_MEM_POOL_SIZE > 0) |
| 783 | /** |
| 784 | * @brief Assign the system heap as a thread's resource pool |
| 785 | * |
| 786 | * Similar to k_thread_resource_pool_assign(), but the thread will use |
| 787 | * the kernel heap to draw memory. |
| 788 | * |
| 789 | * Use with caution, as a malicious thread could perform DoS attacks on the |
| 790 | * kernel heap. |
| 791 | * |
| 792 | * @param thread Target thread to assign the system heap for resource requests |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 793 | * |
| 794 | * @req K-THREAD-004 |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 795 | */ |
| 796 | void k_thread_system_pool_assign(struct k_thread *thread); |
| 797 | #endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */ |
| 798 | |
| 799 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 800 | * @brief Put the current thread to sleep. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 801 | * |
Charles E. Youse | a567831 | 2019-05-09 16:46:46 -0700 | [diff] [blame] | 802 | * This routine puts the current thread to sleep for @a duration milliseconds. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 803 | * |
Charles E. Youse | a567831 | 2019-05-09 16:46:46 -0700 | [diff] [blame] | 804 | * @param ms Number of milliseconds to sleep. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 805 | * |
Piotr Zięcik | 7700eb2 | 2018-10-25 17:45:08 +0200 | [diff] [blame] | 806 | * @return Zero if the requested time has elapsed or the number of milliseconds |
| 807 | * left to sleep, if thread was woken up by \ref k_wakeup call. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 808 | */ |
Charles E. Youse | a567831 | 2019-05-09 16:46:46 -0700 | [diff] [blame] | 809 | __syscall s32_t k_sleep(s32_t ms); |
| 810 | |
| 811 | /** |
| 812 | * @brief Put the current thread to sleep with microsecond resolution. |
| 813 | * |
| 814 | * This function is unlikely to work as expected without kernel tuning. |
| 815 | * In particular, because the lower bound on the duration of a sleep is |
| 816 | * the duration of a tick, CONFIG_SYS_CLOCK_TICKS_PER_SEC must be adjusted |
| 817 | * to achieve the resolution desired. The implications of doing this must |
| 818 | * be understood before attempting to use k_usleep(). Use with caution. |
| 819 | * |
| 820 | * @param us Number of microseconds to sleep. |
| 821 | * |
| 822 | * @return Zero if the requested time has elapsed or the number of microseconds |
| 823 | * left to sleep, if thread was woken up by \ref k_wakeup call. |
| 824 | */ |
| 825 | __syscall s32_t k_usleep(s32_t us); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 826 | |
| 827 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 828 | * @brief Cause the current thread to busy wait. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 829 | * |
| 830 | * This routine causes the current thread to execute a "do nothing" loop for |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 831 | * @a usec_to_wait microseconds. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 832 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 833 | * @return N/A |
| 834 | */ |
Andrew Boie | 42cfd4f | 2018-11-14 14:29:24 -0800 | [diff] [blame] | 835 | __syscall void k_busy_wait(u32_t usec_to_wait); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 836 | |
| 837 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 838 | * @brief Yield the current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 839 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 840 | * This routine causes the current thread to yield execution to another |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 841 | * thread of the same or higher priority. If there are no other ready threads |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 842 | * of the same or higher priority, the routine returns immediately. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 843 | * |
| 844 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 845 | * @req K-THREAD-015 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 846 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 847 | __syscall void k_yield(void); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 848 | |
| 849 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 850 | * @brief Wake up a sleeping thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 851 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 852 | * This routine prematurely wakes up @a thread from sleeping. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 853 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 854 | * If @a thread is not currently sleeping, the routine has no effect. |
| 855 | * |
| 856 | * @param thread ID of thread to wake. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 857 | * |
| 858 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 859 | * @req K-THREAD-014 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 860 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 861 | __syscall void k_wakeup(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 862 | |
| 863 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 864 | * @brief Get thread ID of the current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 865 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 866 | * @return ID of current thread. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 867 | * |
| 868 | * @req K-THREAD-013 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 869 | */ |
Andrew Boie | 76c04a2 | 2017-09-27 14:45:10 -0700 | [diff] [blame] | 870 | __syscall k_tid_t k_current_get(void); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 871 | |
| 872 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 873 | * @brief Abort a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 874 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 875 | * This routine permanently stops execution of @a thread. The thread is taken |
| 876 | * off all kernel queues it is part of (i.e. the ready queue, the timeout |
| 877 | * queue, or a kernel object wait queue). However, any kernel resources the |
| 878 | * thread might currently own (such as mutexes or memory blocks) are not |
| 879 | * released. It is the responsibility of the caller of this routine to ensure |
| 880 | * all necessary cleanup is performed. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 881 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 882 | * @param thread ID of thread to abort. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 883 | * |
| 884 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 885 | * @req K-THREAD-012 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 886 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 887 | __syscall void k_thread_abort(k_tid_t thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 888 | |
Andrew Boie | 7d627c5 | 2017-08-30 11:01:56 -0700 | [diff] [blame] | 889 | |
| 890 | /** |
| 891 | * @brief Start an inactive thread |
| 892 | * |
| 893 | * If a thread was created with K_FOREVER in the delay parameter, it will |
| 894 | * not be added to the scheduling queue until this function is called |
| 895 | * on it. |
| 896 | * |
| 897 | * @param thread thread to start |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 898 | * @req K-THREAD-011 |
Andrew Boie | 7d627c5 | 2017-08-30 11:01:56 -0700 | [diff] [blame] | 899 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 900 | __syscall void k_thread_start(k_tid_t thread); |
Andrew Boie | 7d627c5 | 2017-08-30 11:01:56 -0700 | [diff] [blame] | 901 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 902 | /** |
| 903 | * @cond INTERNAL_HIDDEN |
| 904 | */ |
| 905 | |
Benjamin Walsh | d211a52 | 2016-12-06 11:44:01 -0500 | [diff] [blame] | 906 | /* timeout has timed out and is not on _timeout_q anymore */ |
| 907 | #define _EXPIRED (-2) |
| 908 | |
Peter Mitsis | a04c0d7 | 2016-09-28 19:26:00 -0400 | [diff] [blame] | 909 | struct _static_thread_data { |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 910 | struct k_thread *init_thread; |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 911 | k_thread_stack_t *init_stack; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 912 | unsigned int init_stack_size; |
Andrew Boie | 1e06ffc | 2017-09-11 09:30:04 -0700 | [diff] [blame] | 913 | k_thread_entry_t init_entry; |
Allan Stephens | 7c5bffa | 2016-10-26 10:01:28 -0500 | [diff] [blame] | 914 | void *init_p1; |
| 915 | void *init_p2; |
| 916 | void *init_p3; |
| 917 | int init_prio; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 918 | u32_t init_options; |
| 919 | s32_t init_delay; |
Allan Stephens | 7c5bffa | 2016-10-26 10:01:28 -0500 | [diff] [blame] | 920 | void (*init_abort)(void); |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 921 | const char *init_name; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 922 | }; |
| 923 | |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 924 | #define _THREAD_INITIALIZER(thread, stack, stack_size, \ |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 925 | entry, p1, p2, p3, \ |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 926 | prio, options, delay, abort, tname) \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 927 | { \ |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 928 | .init_thread = (thread), \ |
| 929 | .init_stack = (stack), \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 930 | .init_stack_size = (stack_size), \ |
Andrew Boie | 1e06ffc | 2017-09-11 09:30:04 -0700 | [diff] [blame] | 931 | .init_entry = (k_thread_entry_t)entry, \ |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 932 | .init_p1 = (void *)p1, \ |
| 933 | .init_p2 = (void *)p2, \ |
| 934 | .init_p3 = (void *)p3, \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 935 | .init_prio = (prio), \ |
| 936 | .init_options = (options), \ |
| 937 | .init_delay = (delay), \ |
| 938 | .init_abort = (abort), \ |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 939 | .init_name = STRINGIFY(tname), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 940 | } |
| 941 | |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 942 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 943 | * INTERNAL_HIDDEN @endcond |
| 944 | */ |
| 945 | |
| 946 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 947 | * @brief Statically define and initialize a thread. |
| 948 | * |
| 949 | * The thread may be scheduled for immediate execution or a delayed start. |
| 950 | * |
| 951 | * Thread options are architecture-specific, and can include K_ESSENTIAL, |
| 952 | * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating |
| 953 | * them using "|" (the logical OR operator). |
| 954 | * |
| 955 | * The ID of the thread can be accessed using: |
| 956 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 957 | * @code extern const k_tid_t <name>; @endcode |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 958 | * |
| 959 | * @param name Name of the thread. |
| 960 | * @param stack_size Stack size in bytes. |
| 961 | * @param entry Thread entry function. |
| 962 | * @param p1 1st entry point parameter. |
| 963 | * @param p2 2nd entry point parameter. |
| 964 | * @param p3 3rd entry point parameter. |
| 965 | * @param prio Thread priority. |
| 966 | * @param options Thread options. |
| 967 | * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay). |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 968 | * |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 969 | * @req K-THREAD-010 |
| 970 | * |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 971 | * @internal It has been observed that the x86 compiler by default aligns |
| 972 | * these _static_thread_data structures to 32-byte boundaries, thereby |
| 973 | * wasting space. To work around this, force a 4-byte alignment. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 974 | * |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 975 | */ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 976 | #define K_THREAD_DEFINE(name, stack_size, \ |
| 977 | entry, p1, p2, p3, \ |
| 978 | prio, options, delay) \ |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 979 | K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 980 | struct k_thread _k_thread_obj_##name; \ |
| 981 | Z_STRUCT_SECTION_ITERABLE(_static_thread_data, _k_thread_data_##name) =\ |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 982 | _THREAD_INITIALIZER(&_k_thread_obj_##name, \ |
| 983 | _k_thread_stack_##name, stack_size, \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 984 | entry, p1, p2, p3, prio, options, delay, \ |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 985 | NULL, name); \ |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 986 | const k_tid_t name = (k_tid_t)&_k_thread_obj_##name |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 987 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 988 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 989 | * @brief Get a thread's priority. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 990 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 991 | * This routine gets the priority of @a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 992 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 993 | * @param thread ID of thread whose priority is needed. |
| 994 | * |
| 995 | * @return Priority of @a thread. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 996 | * @req K-THREAD-009 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 997 | */ |
Andrew Boie | 76c04a2 | 2017-09-27 14:45:10 -0700 | [diff] [blame] | 998 | __syscall int k_thread_priority_get(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 999 | |
| 1000 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1001 | * @brief Set a thread's priority. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1002 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1003 | * This routine immediately changes the priority of @a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1004 | * |
| 1005 | * Rescheduling can occur immediately depending on the priority @a thread is |
| 1006 | * set to: |
| 1007 | * |
| 1008 | * - If its priority is raised above the priority of the caller of this |
| 1009 | * function, and the caller is preemptible, @a thread will be scheduled in. |
| 1010 | * |
| 1011 | * - If the caller operates on itself, it lowers its priority below that of |
| 1012 | * other threads in the system, and the caller is preemptible, the thread of |
| 1013 | * highest priority will be scheduled in. |
| 1014 | * |
| 1015 | * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to |
| 1016 | * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the |
| 1017 | * highest priority. |
| 1018 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1019 | * @param thread ID of thread whose priority is to be set. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1020 | * @param prio New priority. |
| 1021 | * |
| 1022 | * @warning Changing the priority of a thread currently involved in mutex |
| 1023 | * priority inheritance may result in undefined behavior. |
| 1024 | * |
| 1025 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1026 | * @req K-THREAD-008 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1027 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1028 | __syscall void k_thread_priority_set(k_tid_t thread, int prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1029 | |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 1030 | |
| 1031 | #ifdef CONFIG_SCHED_DEADLINE |
| 1032 | /** |
| 1033 | * @brief Set deadline expiration time for scheduler |
| 1034 | * |
| 1035 | * This sets the "deadline" expiration as a time delta from the |
| 1036 | * current time, in the same units used by k_cycle_get_32(). The |
| 1037 | * scheduler (when deadline scheduling is enabled) will choose the |
| 1038 | * next expiring thread when selecting between threads at the same |
| 1039 | * static priority. Threads at different priorities will be scheduled |
| 1040 | * according to their static priority. |
| 1041 | * |
| 1042 | * @note Deadlines that are negative (i.e. in the past) are still seen |
| 1043 | * as higher priority than others, even if the thread has "finished" |
| 1044 | * its work. If you don't want it scheduled anymore, you have to |
| 1045 | * reset the deadline into the future, block/pend the thread, or |
| 1046 | * modify its priority with k_thread_priority_set(). |
| 1047 | * |
| 1048 | * @note Despite the API naming, the scheduler makes no guarantees the |
| 1049 | * the thread WILL be scheduled within that deadline, nor does it take |
| 1050 | * extra metadata (like e.g. the "runtime" and "period" parameters in |
| 1051 | * Linux sched_setattr()) that allows the kernel to validate the |
| 1052 | * scheduling for achievability. Such features could be implemented |
| 1053 | * above this call, which is simply input to the priority selection |
| 1054 | * logic. |
| 1055 | * |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1056 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1057 | * @rst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1058 | * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project |
| 1059 | * configuration. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1060 | * @endrst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1061 | * |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 1062 | * @param thread A thread on which to set the deadline |
| 1063 | * @param deadline A time delta, in cycle units |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1064 | * |
| 1065 | * @req K-THREAD-007 |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 1066 | */ |
| 1067 | __syscall void k_thread_deadline_set(k_tid_t thread, int deadline); |
| 1068 | #endif |
| 1069 | |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1070 | #ifdef CONFIG_SCHED_CPU_MASK |
| 1071 | /** |
| 1072 | * @brief Sets all CPU enable masks to zero |
| 1073 | * |
| 1074 | * After this returns, the thread will no longer be schedulable on any |
| 1075 | * CPUs. The thread must not be currently runnable. |
| 1076 | * |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1077 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1078 | * @rst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1079 | * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project |
| 1080 | * configuration. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1081 | * @endrst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1082 | * |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1083 | * @param thread Thread to operate upon |
| 1084 | * @return Zero on success, otherwise error code |
| 1085 | */ |
| 1086 | int k_thread_cpu_mask_clear(k_tid_t thread); |
| 1087 | |
| 1088 | /** |
| 1089 | * @brief Sets all CPU enable masks to one |
| 1090 | * |
| 1091 | * After this returns, the thread will be schedulable on any CPU. The |
| 1092 | * thread must not be currently runnable. |
| 1093 | * |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1094 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1095 | * @rst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1096 | * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project |
| 1097 | * configuration. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1098 | * @endrst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1099 | * |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1100 | * @param thread Thread to operate upon |
| 1101 | * @return Zero on success, otherwise error code |
| 1102 | */ |
| 1103 | int k_thread_cpu_mask_enable_all(k_tid_t thread); |
| 1104 | |
| 1105 | /** |
| 1106 | * @brief Enable thread to run on specified CPU |
| 1107 | * |
| 1108 | * The thread must not be currently runnable. |
| 1109 | * |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1110 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1111 | * @rst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1112 | * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project |
| 1113 | * configuration. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1114 | * @endrst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1115 | * |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1116 | * @param thread Thread to operate upon |
| 1117 | * @param cpu CPU index |
| 1118 | * @return Zero on success, otherwise error code |
| 1119 | */ |
| 1120 | int k_thread_cpu_mask_enable(k_tid_t thread, int cpu); |
| 1121 | |
| 1122 | /** |
| 1123 | * @brief Prevent thread to run on specified CPU |
| 1124 | * |
| 1125 | * The thread must not be currently runnable. |
| 1126 | * |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1127 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1128 | * @rst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1129 | * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project |
| 1130 | * configuration. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1131 | * @endrst |
Anas Nashif | 240c516 | 2019-06-10 12:25:50 -0400 | [diff] [blame] | 1132 | * |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1133 | * @param thread Thread to operate upon |
| 1134 | * @param cpu CPU index |
| 1135 | * @return Zero on success, otherwise error code |
| 1136 | */ |
| 1137 | int k_thread_cpu_mask_disable(k_tid_t thread, int cpu); |
| 1138 | #endif |
| 1139 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1140 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1141 | * @brief Suspend a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1142 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1143 | * This routine prevents the kernel scheduler from making @a thread the |
| 1144 | * current thread. All other internal operations on @a thread are still |
| 1145 | * performed; for example, any timeout it is waiting on keeps ticking, |
| 1146 | * kernel objects it is waiting on are still handed to it, etc. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1147 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1148 | * If @a thread is already suspended, the routine has no effect. |
| 1149 | * |
| 1150 | * @param thread ID of thread to suspend. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1151 | * |
| 1152 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1153 | * @req K-THREAD-005 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1154 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1155 | __syscall void k_thread_suspend(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1156 | |
| 1157 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1158 | * @brief Resume a suspended thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1159 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1160 | * This routine allows the kernel scheduler to make @a thread the current |
| 1161 | * thread, when it is next eligible for that role. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1162 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1163 | * If @a thread is not currently suspended, the routine has no effect. |
| 1164 | * |
| 1165 | * @param thread ID of thread to resume. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1166 | * |
| 1167 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1168 | * @req K-THREAD-006 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1169 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1170 | __syscall void k_thread_resume(k_tid_t thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1171 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1172 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1173 | * @brief Set time-slicing period and scope. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1174 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1175 | * This routine specifies how the scheduler will perform time slicing of |
| 1176 | * preemptible threads. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1177 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1178 | * To enable time slicing, @a slice must be non-zero. The scheduler |
| 1179 | * ensures that no thread runs for more than the specified time limit |
| 1180 | * before other threads of that priority are given a chance to execute. |
| 1181 | * 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] | 1182 | * execute as long as desired without being preempted due to time slicing. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1183 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1184 | * 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] | 1185 | * execute. Once the scheduler selects a thread for execution, there is no |
| 1186 | * minimum guaranteed time the thread will execute before threads of greater or |
| 1187 | * equal priority are scheduled. |
| 1188 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1189 | * When the current thread is the only one of that priority eligible |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1190 | * for execution, this routine has no effect; the thread is immediately |
| 1191 | * rescheduled after the slice period expires. |
| 1192 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1193 | * To disable timeslicing, set both @a slice and @a prio to zero. |
| 1194 | * |
| 1195 | * @param slice Maximum time slice length (in milliseconds). |
| 1196 | * @param prio Highest thread priority level eligible for time slicing. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1197 | * |
| 1198 | * @return N/A |
| 1199 | */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 1200 | extern void k_sched_time_slice_set(s32_t slice, int prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1201 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 1202 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1203 | |
| 1204 | /** |
| 1205 | * @addtogroup isr_apis |
| 1206 | * @{ |
| 1207 | */ |
| 1208 | |
| 1209 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1210 | * @brief Determine if code is running at interrupt level. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1211 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1212 | * This routine allows the caller to customize its actions, depending on |
| 1213 | * whether it is a thread or an ISR. |
| 1214 | * |
| 1215 | * @note Can be called by ISRs. |
| 1216 | * |
Flavio Ceolin | 6a4a86e | 2018-12-17 12:40:22 -0800 | [diff] [blame] | 1217 | * @return false if invoked by a thread. |
| 1218 | * @return true if invoked by an ISR. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1219 | */ |
Flavio Ceolin | 6a4a86e | 2018-12-17 12:40:22 -0800 | [diff] [blame] | 1220 | extern bool k_is_in_isr(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1221 | |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1222 | /** |
| 1223 | * @brief Determine if code is running in a preemptible thread. |
| 1224 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1225 | * This routine allows the caller to customize its actions, depending on |
| 1226 | * whether it can be preempted by another thread. The routine returns a 'true' |
| 1227 | * value if all of the following conditions are met: |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1228 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1229 | * - The code is running in a thread, not at ISR. |
| 1230 | * - The thread's priority is in the preemptible range. |
| 1231 | * - The thread has not locked the scheduler. |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1232 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1233 | * @note Can be called by ISRs. |
| 1234 | * |
| 1235 | * @return 0 if invoked by an ISR or by a cooperative thread. |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1236 | * @return Non-zero if invoked by a preemptible thread. |
| 1237 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1238 | __syscall int k_is_preempt_thread(void); |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1239 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1240 | /** |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 1241 | * @} |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1242 | */ |
| 1243 | |
| 1244 | /** |
| 1245 | * @addtogroup thread_apis |
| 1246 | * @{ |
| 1247 | */ |
| 1248 | |
| 1249 | /** |
| 1250 | * @brief Lock the scheduler. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1251 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1252 | * This routine prevents the current thread from being preempted by another |
| 1253 | * thread by instructing the scheduler to treat it as a cooperative thread. |
| 1254 | * If the thread subsequently performs an operation that makes it unready, |
| 1255 | * it will be context switched out in the normal manner. When the thread |
| 1256 | * again becomes the current thread, its non-preemptible status is maintained. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1257 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1258 | * This routine can be called recursively. |
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 | * @note k_sched_lock() and k_sched_unlock() should normally be used |
| 1261 | * when the operation being performed can be safely interrupted by ISRs. |
| 1262 | * However, if the amount of processing involved is very small, better |
| 1263 | * performance may be obtained by using irq_lock() and irq_unlock(). |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1264 | * |
| 1265 | * @return N/A |
| 1266 | */ |
| 1267 | extern void k_sched_lock(void); |
| 1268 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1269 | /** |
| 1270 | * @brief Unlock the scheduler. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1271 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1272 | * This routine reverses the effect of a previous call to k_sched_lock(). |
| 1273 | * A thread must call the routine once for each time it called k_sched_lock() |
| 1274 | * before the thread becomes preemptible. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 1275 | * |
| 1276 | * @return N/A |
| 1277 | */ |
| 1278 | extern void k_sched_unlock(void); |
| 1279 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1280 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1281 | * @brief Set current thread's custom data. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1282 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1283 | * This routine sets the custom data for the current thread to @ value. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1284 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1285 | * Custom data is not used by the kernel itself, and is freely available |
| 1286 | * for a thread to use as it sees fit. It can be used as a framework |
| 1287 | * upon which to build thread-local storage. |
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 | * @param value New custom data value. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1290 | * |
| 1291 | * @return N/A |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1292 | * |
| 1293 | * @req K-THREAD-016 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1294 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1295 | __syscall void k_thread_custom_data_set(void *value); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1296 | |
| 1297 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1298 | * @brief Get current thread's custom data. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1299 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1300 | * This routine returns the custom data for the current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1301 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1302 | * @return Current custom data value. |
Anas Nashif | 47420d0 | 2018-05-24 14:20:56 -0400 | [diff] [blame] | 1303 | * @req K-THREAD-007 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1304 | */ |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1305 | __syscall void *k_thread_custom_data_get(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1306 | |
| 1307 | /** |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 1308 | * @brief Set current thread name |
| 1309 | * |
| 1310 | * Set the name of the thread to be used when THREAD_MONITOR is enabled for |
| 1311 | * tracing and debugging. |
| 1312 | * |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 1313 | * @param thread_id Thread to set name, or NULL to set the current thread |
| 1314 | * @param value Name string |
| 1315 | * @retval 0 on success |
| 1316 | * @retval -EFAULT Memory access error with supplied string |
| 1317 | * @retval -ENOSYS Thread name configuration option not enabled |
| 1318 | * @retval -EINVAL Thread name too long |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 1319 | */ |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 1320 | __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] | 1321 | |
| 1322 | /** |
| 1323 | * @brief Get thread name |
| 1324 | * |
| 1325 | * Get the name of a thread |
| 1326 | * |
| 1327 | * @param thread_id Thread ID |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 1328 | * @retval Thread name, or NULL if configuration not enabled |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 1329 | */ |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 1330 | const char *k_thread_name_get(k_tid_t thread_id); |
| 1331 | |
| 1332 | /** |
| 1333 | * @brief Copy the thread name into a supplied buffer |
| 1334 | * |
| 1335 | * @param thread_id Thread to obtain name information |
| 1336 | * @param buf Destination buffer |
David B. Kinder | 73896c0 | 2019-10-28 16:27:57 -0700 | [diff] [blame] | 1337 | * @param size Destination buffer size |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 1338 | * @retval -ENOSPC Destination buffer too small |
| 1339 | * @retval -EFAULT Memory access error |
| 1340 | * @retval -ENOSYS Thread name feature not enabled |
| 1341 | * @retval 0 Success |
| 1342 | */ |
| 1343 | __syscall int k_thread_name_copy(k_tid_t thread_id, char *buf, |
| 1344 | size_t size); |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 1345 | |
| 1346 | /** |
Pavlo Hamov | 8076c80 | 2019-07-31 12:43:54 +0300 | [diff] [blame] | 1347 | * @brief Get thread state string |
| 1348 | * |
| 1349 | * Get the human friendly thread state string |
| 1350 | * |
| 1351 | * @param thread_id Thread ID |
| 1352 | * @retval Thread state string, empty if no state flag is set |
| 1353 | */ |
| 1354 | const char *k_thread_state_str(k_tid_t thread_id); |
| 1355 | |
| 1356 | /** |
Andy Ross | cfe6203 | 2018-09-29 07:34:55 -0700 | [diff] [blame] | 1357 | * @} |
| 1358 | */ |
| 1359 | |
| 1360 | /** |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1361 | * @addtogroup clock_apis |
| 1362 | * @{ |
| 1363 | */ |
| 1364 | |
| 1365 | /** |
| 1366 | * @brief Generate null timeout delay. |
| 1367 | * |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1368 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1369 | * not to wait if the requested operation cannot be performed immediately. |
| 1370 | * |
| 1371 | * @return Timeout delay value. |
| 1372 | */ |
| 1373 | #define K_NO_WAIT 0 |
| 1374 | |
| 1375 | /** |
| 1376 | * @brief Generate timeout delay from milliseconds. |
| 1377 | * |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1378 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1379 | * to wait up to @a ms milliseconds to perform the requested operation. |
| 1380 | * |
| 1381 | * @param ms Duration in milliseconds. |
| 1382 | * |
| 1383 | * @return Timeout delay value. |
| 1384 | */ |
Johan Hedberg | 1447169 | 2016-11-13 10:52:15 +0200 | [diff] [blame] | 1385 | #define K_MSEC(ms) (ms) |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1386 | |
| 1387 | /** |
| 1388 | * @brief Generate timeout delay from seconds. |
| 1389 | * |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1390 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1391 | * to wait up to @a s seconds to perform the requested operation. |
| 1392 | * |
| 1393 | * @param s Duration in seconds. |
| 1394 | * |
| 1395 | * @return Timeout delay value. |
| 1396 | */ |
Johan Hedberg | 1447169 | 2016-11-13 10:52:15 +0200 | [diff] [blame] | 1397 | #define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC) |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1398 | |
| 1399 | /** |
| 1400 | * @brief Generate timeout delay from minutes. |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1401 | |
| 1402 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1403 | * to wait up to @a m minutes to perform the requested operation. |
| 1404 | * |
| 1405 | * @param m Duration in minutes. |
| 1406 | * |
| 1407 | * @return Timeout delay value. |
| 1408 | */ |
Johan Hedberg | 1447169 | 2016-11-13 10:52:15 +0200 | [diff] [blame] | 1409 | #define K_MINUTES(m) K_SECONDS((m) * 60) |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1410 | |
| 1411 | /** |
| 1412 | * @brief Generate timeout delay from hours. |
| 1413 | * |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1414 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1415 | * to wait up to @a h hours to perform the requested operation. |
| 1416 | * |
| 1417 | * @param h Duration in hours. |
| 1418 | * |
| 1419 | * @return Timeout delay value. |
| 1420 | */ |
Johan Hedberg | 1447169 | 2016-11-13 10:52:15 +0200 | [diff] [blame] | 1421 | #define K_HOURS(h) K_MINUTES((h) * 60) |
| 1422 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1423 | /** |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1424 | * @brief Generate infinite timeout delay. |
| 1425 | * |
Maksim Masalski | fe1ff2f | 2019-10-29 16:50:44 +0800 | [diff] [blame] | 1426 | * This macro generates a timeout delay that instructs a kernel API |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1427 | * to wait as long as necessary to perform the requested operation. |
| 1428 | * |
| 1429 | * @return Timeout delay value. |
| 1430 | */ |
| 1431 | #define K_FOREVER (-1) |
| 1432 | |
| 1433 | /** |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 1434 | * @} |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1435 | */ |
| 1436 | |
| 1437 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1438 | * @cond INTERNAL_HIDDEN |
| 1439 | */ |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 1440 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1441 | struct k_timer { |
| 1442 | /* |
| 1443 | * _timeout structure must be first here if we want to use |
| 1444 | * dynamic timer allocation. timeout.node is used in the double-linked |
| 1445 | * list of free timers |
| 1446 | */ |
| 1447 | struct _timeout timeout; |
| 1448 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1449 | /* wait queue for the (single) thread waiting on this timer */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1450 | _wait_q_t wait_q; |
| 1451 | |
| 1452 | /* runs in ISR context */ |
Flavio Ceolin | 4b35dd2 | 2018-11-16 19:06:59 -0800 | [diff] [blame] | 1453 | void (*expiry_fn)(struct k_timer *timer); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1454 | |
| 1455 | /* runs in the context of the thread that calls k_timer_stop() */ |
Flavio Ceolin | 4b35dd2 | 2018-11-16 19:06:59 -0800 | [diff] [blame] | 1456 | void (*stop_fn)(struct k_timer *timer); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1457 | |
| 1458 | /* timer period */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 1459 | s32_t period; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1460 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1461 | /* timer status */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 1462 | u32_t status; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1463 | |
Benjamin Walsh | e4e98f9 | 2017-01-12 19:38:53 -0500 | [diff] [blame] | 1464 | /* user-specific data, also used to support legacy features */ |
| 1465 | void *user_data; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1466 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 1467 | _OBJECT_TRACING_NEXT_PTR(k_timer) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 1468 | _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1469 | }; |
| 1470 | |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 1471 | #define Z_TIMER_INITIALIZER(obj, expiry, stop) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1472 | { \ |
Krzysztof Chruscinski | be06327 | 2019-02-13 11:19:54 +0100 | [diff] [blame] | 1473 | .timeout = { \ |
| 1474 | .node = {},\ |
| 1475 | .dticks = 0, \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1476 | .fn = z_timer_expiration_handler \ |
Krzysztof Chruscinski | be06327 | 2019-02-13 11:19:54 +0100 | [diff] [blame] | 1477 | }, \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1478 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 1479 | .expiry_fn = expiry, \ |
| 1480 | .stop_fn = stop, \ |
Krzysztof Chruscinski | be06327 | 2019-02-13 11:19:54 +0100 | [diff] [blame] | 1481 | .period = 0, \ |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 1482 | .status = 0, \ |
Benjamin Walsh | e4e98f9 | 2017-01-12 19:38:53 -0500 | [diff] [blame] | 1483 | .user_data = 0, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 1484 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1485 | } |
| 1486 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 1487 | #define K_TIMER_INITIALIZER __DEPRECATED_MACRO Z_TIMER_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 1488 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1489 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1490 | * INTERNAL_HIDDEN @endcond |
| 1491 | */ |
| 1492 | |
| 1493 | /** |
| 1494 | * @defgroup timer_apis Timer APIs |
| 1495 | * @ingroup kernel_apis |
| 1496 | * @{ |
| 1497 | */ |
| 1498 | |
| 1499 | /** |
Allan Stephens | 5eceb85 | 2016-11-16 10:16:30 -0500 | [diff] [blame] | 1500 | * @typedef k_timer_expiry_t |
| 1501 | * @brief Timer expiry function type. |
| 1502 | * |
| 1503 | * A timer's expiry function is executed by the system clock interrupt handler |
| 1504 | * each time the timer expires. The expiry function is optional, and is only |
| 1505 | * invoked if the timer has been initialized with one. |
| 1506 | * |
| 1507 | * @param timer Address of timer. |
| 1508 | * |
| 1509 | * @return N/A |
| 1510 | */ |
| 1511 | typedef void (*k_timer_expiry_t)(struct k_timer *timer); |
| 1512 | |
| 1513 | /** |
| 1514 | * @typedef k_timer_stop_t |
| 1515 | * @brief Timer stop function type. |
| 1516 | * |
| 1517 | * A timer's stop function is executed if the timer is stopped prematurely. |
| 1518 | * The function runs in the context of the thread that stops the timer. |
| 1519 | * The stop function is optional, and is only invoked if the timer has been |
| 1520 | * initialized with one. |
| 1521 | * |
| 1522 | * @param timer Address of timer. |
| 1523 | * |
| 1524 | * @return N/A |
| 1525 | */ |
| 1526 | typedef void (*k_timer_stop_t)(struct k_timer *timer); |
| 1527 | |
| 1528 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1529 | * @brief Statically define and initialize a timer. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1530 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1531 | * 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] | 1532 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 1533 | * @code extern struct k_timer <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1534 | * |
| 1535 | * @param name Name of the timer variable. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1536 | * @param expiry_fn Function to invoke each time the timer expires. |
| 1537 | * @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] | 1538 | */ |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 1539 | #define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 1540 | Z_STRUCT_SECTION_ITERABLE(k_timer, name) = \ |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 1541 | Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1542 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1543 | /** |
| 1544 | * @brief Initialize a timer. |
| 1545 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1546 | * This routine initializes a timer, prior to its first use. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1547 | * |
| 1548 | * @param timer Address of timer. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1549 | * @param expiry_fn Function to invoke each time the timer expires. |
| 1550 | * @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] | 1551 | * |
| 1552 | * @return N/A |
| 1553 | */ |
| 1554 | extern void k_timer_init(struct k_timer *timer, |
Allan Stephens | 5eceb85 | 2016-11-16 10:16:30 -0500 | [diff] [blame] | 1555 | k_timer_expiry_t expiry_fn, |
| 1556 | k_timer_stop_t stop_fn); |
Andy Ross | 8d8b2ac | 2016-09-23 10:08:54 -0700 | [diff] [blame] | 1557 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1558 | /** |
| 1559 | * @brief Start a timer. |
| 1560 | * |
| 1561 | * This routine starts a timer, and resets its status to zero. The timer |
| 1562 | * begins counting down using the specified duration and period values. |
| 1563 | * |
| 1564 | * Attempting to start a timer that is already running is permitted. |
| 1565 | * The timer's status is reset to zero and the timer begins counting down |
| 1566 | * using the new duration and period values. |
| 1567 | * |
| 1568 | * @param timer Address of timer. |
| 1569 | * @param duration Initial timer duration (in milliseconds). |
| 1570 | * @param period Timer period (in milliseconds). |
| 1571 | * |
| 1572 | * @return N/A |
| 1573 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1574 | __syscall void k_timer_start(struct k_timer *timer, |
| 1575 | s32_t duration, s32_t period); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1576 | |
| 1577 | /** |
| 1578 | * @brief Stop a timer. |
| 1579 | * |
| 1580 | * This routine stops a running timer prematurely. The timer's stop function, |
| 1581 | * if one exists, is invoked by the caller. |
| 1582 | * |
| 1583 | * 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] | 1584 | * effect on the timer. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1585 | * |
Anas Nashif | 4fb12ae | 2017-02-01 20:06:55 -0500 | [diff] [blame] | 1586 | * @note Can be called by ISRs. The stop handler has to be callable from ISRs |
| 1587 | * if @a k_timer_stop is to be called from ISRs. |
| 1588 | * |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1589 | * @param timer Address of timer. |
| 1590 | * |
| 1591 | * @return N/A |
| 1592 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1593 | __syscall void k_timer_stop(struct k_timer *timer); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1594 | |
| 1595 | /** |
| 1596 | * @brief Read timer status. |
| 1597 | * |
| 1598 | * This routine reads the timer's status, which indicates the number of times |
| 1599 | * it has expired since its status was last read. |
| 1600 | * |
| 1601 | * Calling this routine resets the timer's status to zero. |
| 1602 | * |
| 1603 | * @param timer Address of timer. |
| 1604 | * |
| 1605 | * @return Timer status. |
| 1606 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1607 | __syscall u32_t k_timer_status_get(struct k_timer *timer); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1608 | |
| 1609 | /** |
| 1610 | * @brief Synchronize thread to timer expiration. |
| 1611 | * |
| 1612 | * This routine blocks the calling thread until the timer's status is non-zero |
| 1613 | * (indicating that it has expired at least once since it was last examined) |
| 1614 | * or the timer is stopped. If the timer status is already non-zero, |
| 1615 | * or the timer is already stopped, the caller continues without waiting. |
| 1616 | * |
| 1617 | * Calling this routine resets the timer's status to zero. |
| 1618 | * |
| 1619 | * This routine must not be used by interrupt handlers, since they are not |
| 1620 | * allowed to block. |
| 1621 | * |
| 1622 | * @param timer Address of timer. |
| 1623 | * |
| 1624 | * @return Timer status. |
| 1625 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1626 | __syscall u32_t k_timer_status_sync(struct k_timer *timer); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1627 | |
Andy Ross | 52e444b | 2018-09-28 09:06:37 -0700 | [diff] [blame] | 1628 | extern s32_t z_timeout_remaining(struct _timeout *timeout); |
| 1629 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1630 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1631 | * @brief Get time remaining before a timer next expires. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1632 | * |
| 1633 | * This routine computes the (approximate) time remaining before a running |
| 1634 | * timer next expires. If the timer is not running, it returns zero. |
| 1635 | * |
| 1636 | * @param timer Address of timer. |
| 1637 | * |
| 1638 | * @return Remaining time (in milliseconds). |
| 1639 | */ |
Flavio Ceolin | f1e5303 | 2018-12-04 16:03:13 -0800 | [diff] [blame] | 1640 | __syscall u32_t k_timer_remaining_get(struct k_timer *timer); |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1641 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1642 | 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] | 1643 | { |
Charles E. Youse | 0ad4022 | 2019-03-01 10:51:04 -0800 | [diff] [blame] | 1644 | const s32_t ticks = z_timeout_remaining(&timer->timeout); |
Andy Ross | 8892406 | 2019-10-03 11:43:10 -0700 | [diff] [blame] | 1645 | return (ticks > 0) ? (u32_t)k_ticks_to_ms_floor64(ticks) : 0U; |
Johan Hedberg | f99ad3f | 2016-12-09 10:39:49 +0200 | [diff] [blame] | 1646 | } |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1647 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1648 | /** |
Benjamin Walsh | e4e98f9 | 2017-01-12 19:38:53 -0500 | [diff] [blame] | 1649 | * @brief Associate user-specific data with a timer. |
| 1650 | * |
| 1651 | * This routine records the @a user_data with the @a timer, to be retrieved |
| 1652 | * later. |
| 1653 | * |
| 1654 | * It can be used e.g. in a timer handler shared across multiple subsystems to |
| 1655 | * retrieve data specific to the subsystem this timer is associated with. |
| 1656 | * |
| 1657 | * @param timer Address of timer. |
| 1658 | * @param user_data User data to associate with the timer. |
| 1659 | * |
| 1660 | * @return N/A |
| 1661 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1662 | __syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data); |
| 1663 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 1664 | /** |
| 1665 | * @internal |
| 1666 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1667 | 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] | 1668 | void *user_data) |
Benjamin Walsh | e4e98f9 | 2017-01-12 19:38:53 -0500 | [diff] [blame] | 1669 | { |
| 1670 | timer->user_data = user_data; |
| 1671 | } |
| 1672 | |
| 1673 | /** |
| 1674 | * @brief Retrieve the user-specific data from a timer. |
| 1675 | * |
| 1676 | * @param timer Address of timer. |
| 1677 | * |
| 1678 | * @return The user data. |
| 1679 | */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 1680 | __syscall void *k_timer_user_data_get(struct k_timer *timer); |
| 1681 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1682 | 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] | 1683 | { |
| 1684 | return timer->user_data; |
| 1685 | } |
| 1686 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 1687 | /** @} */ |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1688 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1689 | /** |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1690 | * @addtogroup clock_apis |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1691 | * @{ |
| 1692 | */ |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1693 | |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1694 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1695 | * @brief Get system uptime. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1696 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1697 | * This routine returns the elapsed time since the system booted, |
| 1698 | * in milliseconds. |
| 1699 | * |
David B. Kinder | 00c41ea | 2019-06-10 11:13:33 -0700 | [diff] [blame] | 1700 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1701 | * @rst |
David B. Kinder | 00c41ea | 2019-06-10 11:13:33 -0700 | [diff] [blame] | 1702 | * While this function returns time in milliseconds, it does |
| 1703 | * not mean it has millisecond resolution. The actual resolution depends on |
Andy Ross | 669730f | 2019-06-11 11:18:20 -0700 | [diff] [blame] | 1704 | * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option. |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1705 | * @endrst |
Paul Sokolovsky | 65d51fd | 2019-02-04 22:44:50 +0300 | [diff] [blame] | 1706 | * |
| 1707 | * @return Current uptime in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1708 | */ |
Andrew Boie | a73d373 | 2017-10-08 12:23:55 -0700 | [diff] [blame] | 1709 | __syscall s64_t k_uptime_get(void); |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1710 | |
Ramesh Thomas | 89ffd44 | 2017-02-05 19:37:19 -0800 | [diff] [blame] | 1711 | /** |
| 1712 | * @brief Enable clock always on in tickless kernel |
| 1713 | * |
Andy Ross | 1db9f18 | 2019-06-25 10:09:45 -0700 | [diff] [blame] | 1714 | * Deprecated. This does nothing (it was always just a hint). This |
| 1715 | * functionality has been migrated to the SYSTEM_CLOCK_SLOPPY_IDLE |
| 1716 | * kconfig. |
Ramesh Thomas | 89ffd44 | 2017-02-05 19:37:19 -0800 | [diff] [blame] | 1717 | * |
| 1718 | * @retval prev_status Previous status of always on flag |
| 1719 | */ |
Andy Ross | 1db9f18 | 2019-06-25 10:09:45 -0700 | [diff] [blame] | 1720 | /* LCOV_EXCL_START */ |
| 1721 | __deprecated static inline int k_enable_sys_clock_always_on(void) |
| 1722 | { |
| 1723 | __ASSERT(IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE), |
| 1724 | "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead"); |
| 1725 | |
| 1726 | return !IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE); |
| 1727 | } |
| 1728 | /* LCOV_EXCL_STOP */ |
Ramesh Thomas | 89ffd44 | 2017-02-05 19:37:19 -0800 | [diff] [blame] | 1729 | |
| 1730 | /** |
| 1731 | * @brief Disable clock always on in tickless kernel |
| 1732 | * |
Andy Ross | 1db9f18 | 2019-06-25 10:09:45 -0700 | [diff] [blame] | 1733 | * Deprecated. This does nothing (it was always just a hint). This |
| 1734 | * functionality has been migrated to the SYS_CLOCK_SLOPPY_IDLE |
| 1735 | * kconfig. |
Ramesh Thomas | 89ffd44 | 2017-02-05 19:37:19 -0800 | [diff] [blame] | 1736 | */ |
Andy Ross | 1db9f18 | 2019-06-25 10:09:45 -0700 | [diff] [blame] | 1737 | /* LCOV_EXCL_START */ |
| 1738 | __deprecated static inline void k_disable_sys_clock_always_on(void) |
| 1739 | { |
| 1740 | __ASSERT(!IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE), |
| 1741 | "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead"); |
| 1742 | } |
| 1743 | /* LCOV_EXCL_STOP */ |
Ramesh Thomas | 89ffd44 | 2017-02-05 19:37:19 -0800 | [diff] [blame] | 1744 | |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1745 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1746 | * @brief Get system uptime (32-bit version). |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1747 | * |
Peter Bigot | a6067a3 | 2019-08-28 08:19:26 -0500 | [diff] [blame] | 1748 | * This routine returns the lower 32 bits of the system uptime in |
| 1749 | * milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1750 | * |
Peter Bigot | a6067a3 | 2019-08-28 08:19:26 -0500 | [diff] [blame] | 1751 | * Because correct conversion requires full precision of the system |
| 1752 | * clock there is no benefit to using this over k_uptime_get() unless |
| 1753 | * you know the application will never run long enough for the system |
| 1754 | * clock to approach 2^32 ticks. Calls to this function may involve |
| 1755 | * interrupt blocking and 64-bit math. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1756 | * |
David B. Kinder | 00c41ea | 2019-06-10 11:13:33 -0700 | [diff] [blame] | 1757 | * @note |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1758 | * @rst |
David B. Kinder | 00c41ea | 2019-06-10 11:13:33 -0700 | [diff] [blame] | 1759 | * While this function returns time in milliseconds, it does |
| 1760 | * not mean it has millisecond resolution. The actual resolution depends on |
Andy Ross | 669730f | 2019-06-11 11:18:20 -0700 | [diff] [blame] | 1761 | * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option |
David B. Kinder | 8de9cc7 | 2019-06-25 10:44:55 -0700 | [diff] [blame] | 1762 | * @endrst |
Paul Sokolovsky | 65d51fd | 2019-02-04 22:44:50 +0300 | [diff] [blame] | 1763 | * |
Peter Bigot | a6067a3 | 2019-08-28 08:19:26 -0500 | [diff] [blame] | 1764 | * @return The low 32 bits of the current uptime, in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1765 | */ |
Peter Bigot | a6067a3 | 2019-08-28 08:19:26 -0500 | [diff] [blame] | 1766 | static inline u32_t k_uptime_get_32(void) |
| 1767 | { |
| 1768 | return (u32_t)k_uptime_get(); |
| 1769 | } |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1770 | |
| 1771 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1772 | * @brief Get elapsed time. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1773 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1774 | * This routine computes the elapsed time between the current system uptime |
| 1775 | * and an earlier reference time, in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1776 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1777 | * @param reftime Pointer to a reference time, which is updated to the current |
| 1778 | * uptime upon return. |
| 1779 | * |
| 1780 | * @return Elapsed time. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1781 | */ |
Andy Ross | 987c0e5 | 2018-09-27 16:50:00 -0700 | [diff] [blame] | 1782 | static inline s64_t k_uptime_delta(s64_t *reftime) |
| 1783 | { |
| 1784 | s64_t uptime, delta; |
| 1785 | |
| 1786 | uptime = k_uptime_get(); |
| 1787 | delta = uptime - *reftime; |
| 1788 | *reftime = uptime; |
| 1789 | |
| 1790 | return delta; |
| 1791 | } |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1792 | |
| 1793 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1794 | * @brief Get elapsed time (32-bit version). |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1795 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1796 | * This routine computes the elapsed time between the current system uptime |
| 1797 | * and an earlier reference time, in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1798 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1799 | * This routine can be more efficient than k_uptime_delta(), as it reduces the |
| 1800 | * need for interrupt locking and 64-bit math. However, the 32-bit result |
| 1801 | * cannot hold an elapsed time larger than approximately 50 days, so the |
| 1802 | * caller must handle possible rollovers. |
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 | * @param reftime Pointer to a reference time, which is updated to the current |
| 1805 | * uptime upon return. |
| 1806 | * |
| 1807 | * @return Elapsed time. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1808 | */ |
Andy Ross | 987c0e5 | 2018-09-27 16:50:00 -0700 | [diff] [blame] | 1809 | static inline u32_t k_uptime_delta_32(s64_t *reftime) |
| 1810 | { |
| 1811 | return (u32_t)k_uptime_delta(reftime); |
| 1812 | } |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1813 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1814 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1815 | * @brief Read the hardware clock. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1816 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1817 | * This routine returns the current time, as measured by the system's hardware |
| 1818 | * clock. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1819 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1820 | * @return Current hardware clock up-counter (in cycles). |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1821 | */ |
Andrew Boie | 979b17f | 2019-10-03 15:20:41 -0700 | [diff] [blame] | 1822 | static inline u32_t k_cycle_get_32(void) |
| 1823 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 1824 | return arch_k_cycle_get_32(); |
Andrew Boie | 979b17f | 2019-10-03 15:20:41 -0700 | [diff] [blame] | 1825 | } |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1826 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1827 | /** |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 1828 | * @} |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1829 | */ |
| 1830 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1831 | /** |
| 1832 | * @cond INTERNAL_HIDDEN |
| 1833 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1834 | |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1835 | struct k_queue { |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1836 | sys_sflist_t data_q; |
Andy Ross | 603ea42 | 2018-07-25 13:01:54 -0700 | [diff] [blame] | 1837 | struct k_spinlock lock; |
Luiz Augusto von Dentz | 84db641 | 2017-07-13 12:43:59 +0300 | [diff] [blame] | 1838 | union { |
| 1839 | _wait_q_t wait_q; |
| 1840 | |
| 1841 | _POLL_EVENT; |
| 1842 | }; |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1843 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 1844 | _OBJECT_TRACING_NEXT_PTR(k_queue) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 1845 | _OBJECT_TRACING_LINKED_FLAG |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1846 | }; |
| 1847 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 1848 | #define _K_QUEUE_INITIALIZER(obj) \ |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1849 | { \ |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1850 | .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \ |
Stephanos Ioannidis | f628dcd | 2019-09-11 18:09:49 +0900 | [diff] [blame] | 1851 | .lock = { }, \ |
| 1852 | { \ |
| 1853 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
| 1854 | _POLL_EVENT_OBJ_INIT(obj) \ |
| 1855 | }, \ |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1856 | _OBJECT_TRACING_INIT \ |
| 1857 | } |
| 1858 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 1859 | #define K_QUEUE_INITIALIZER __DEPRECATED_MACRO _K_QUEUE_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 1860 | |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1861 | extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free); |
| 1862 | |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1863 | /** |
| 1864 | * INTERNAL_HIDDEN @endcond |
| 1865 | */ |
| 1866 | |
| 1867 | /** |
| 1868 | * @defgroup queue_apis Queue APIs |
| 1869 | * @ingroup kernel_apis |
| 1870 | * @{ |
| 1871 | */ |
| 1872 | |
| 1873 | /** |
| 1874 | * @brief Initialize a queue. |
| 1875 | * |
| 1876 | * This routine initializes a queue object, prior to its first use. |
| 1877 | * |
| 1878 | * @param queue Address of the queue. |
| 1879 | * |
| 1880 | * @return N/A |
| 1881 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1882 | __syscall void k_queue_init(struct k_queue *queue); |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1883 | |
| 1884 | /** |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 1885 | * @brief Cancel waiting on a queue. |
| 1886 | * |
| 1887 | * This routine causes first thread pending on @a queue, if any, to |
| 1888 | * 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] | 1889 | * If the queue is being waited on by k_poll(), it will return with |
| 1890 | * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent |
| 1891 | * k_queue_get() will return NULL). |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 1892 | * |
| 1893 | * @note Can be called by ISRs. |
| 1894 | * |
| 1895 | * @param queue Address of the queue. |
| 1896 | * |
| 1897 | * @return N/A |
| 1898 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1899 | __syscall void k_queue_cancel_wait(struct k_queue *queue); |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 1900 | |
| 1901 | /** |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1902 | * @brief Append an element to the end of a queue. |
| 1903 | * |
| 1904 | * 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] | 1905 | * aligned on a word boundary, and the first word of the item is reserved |
| 1906 | * for the kernel's use. |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1907 | * |
| 1908 | * @note Can be called by ISRs. |
| 1909 | * |
| 1910 | * @param queue Address of the queue. |
| 1911 | * @param data Address of the data item. |
| 1912 | * |
| 1913 | * @return N/A |
| 1914 | */ |
| 1915 | extern void k_queue_append(struct k_queue *queue, void *data); |
| 1916 | |
| 1917 | /** |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1918 | * @brief Append an element to a queue. |
| 1919 | * |
Andrew Boie | ac3dcc1 | 2019-04-01 12:28:03 -0700 | [diff] [blame] | 1920 | * This routine appends a data item to @a queue. There is an implicit memory |
| 1921 | * allocation to create an additional temporary bookkeeping data structure from |
| 1922 | * the calling thread's resource pool, which is automatically freed when the |
| 1923 | * item is removed. The data itself is not copied. |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1924 | * |
| 1925 | * @note Can be called by ISRs. |
| 1926 | * |
| 1927 | * @param queue Address of the queue. |
| 1928 | * @param data Address of the data item. |
| 1929 | * |
| 1930 | * @retval 0 on success |
| 1931 | * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool |
| 1932 | */ |
Adithya Baglody | 2a78b8d | 2018-10-25 12:09:04 +0530 | [diff] [blame] | 1933 | __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] | 1934 | |
| 1935 | /** |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1936 | * @brief Prepend an element to a queue. |
| 1937 | * |
| 1938 | * 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] | 1939 | * aligned on a word boundary, and the first word of the item is reserved |
| 1940 | * for the kernel's use. |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1941 | * |
| 1942 | * @note Can be called by ISRs. |
| 1943 | * |
| 1944 | * @param queue Address of the queue. |
| 1945 | * @param data Address of the data item. |
| 1946 | * |
| 1947 | * @return N/A |
| 1948 | */ |
| 1949 | extern void k_queue_prepend(struct k_queue *queue, void *data); |
| 1950 | |
| 1951 | /** |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1952 | * @brief Prepend an element to a queue. |
| 1953 | * |
Andrew Boie | ac3dcc1 | 2019-04-01 12:28:03 -0700 | [diff] [blame] | 1954 | * This routine prepends a data item to @a queue. There is an implicit memory |
| 1955 | * allocation to create an additional temporary bookkeeping data structure from |
| 1956 | * the calling thread's resource pool, which is automatically freed when the |
| 1957 | * item is removed. The data itself is not copied. |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 1958 | * |
| 1959 | * @note Can be called by ISRs. |
| 1960 | * |
| 1961 | * @param queue Address of the queue. |
| 1962 | * @param data Address of the data item. |
| 1963 | * |
| 1964 | * @retval 0 on success |
| 1965 | * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool |
| 1966 | */ |
Adithya Baglody | 2a78b8d | 2018-10-25 12:09:04 +0530 | [diff] [blame] | 1967 | __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] | 1968 | |
| 1969 | /** |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1970 | * @brief Inserts an element to a queue. |
| 1971 | * |
| 1972 | * 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] | 1973 | * data item must be aligned on a word boundary, and the first word of |
| 1974 | * the item is reserved for the kernel's use. |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 1975 | * |
| 1976 | * @note Can be called by ISRs. |
| 1977 | * |
| 1978 | * @param queue Address of the queue. |
| 1979 | * @param prev Address of the previous data item. |
| 1980 | * @param data Address of the data item. |
| 1981 | * |
| 1982 | * @return N/A |
| 1983 | */ |
| 1984 | extern void k_queue_insert(struct k_queue *queue, void *prev, void *data); |
| 1985 | |
| 1986 | /** |
| 1987 | * @brief Atomically append a list of elements to a queue. |
| 1988 | * |
| 1989 | * 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] | 1990 | * 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] | 1991 | * in each data item pointing to the next data item; the list must be |
| 1992 | * NULL-terminated. |
| 1993 | * |
| 1994 | * @note Can be called by ISRs. |
| 1995 | * |
| 1996 | * @param queue Address of the queue. |
| 1997 | * @param head Pointer to first node in singly-linked list. |
| 1998 | * @param tail Pointer to last node in singly-linked list. |
| 1999 | * |
| 2000 | * @return N/A |
| 2001 | */ |
| 2002 | extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail); |
| 2003 | |
| 2004 | /** |
| 2005 | * @brief Atomically add a list of elements to a queue. |
| 2006 | * |
| 2007 | * This routine adds a list of data items to @a queue in one operation. |
| 2008 | * The data items must be in a singly-linked list implemented using a |
| 2009 | * sys_slist_t object. Upon completion, the original list is empty. |
| 2010 | * |
| 2011 | * @note Can be called by ISRs. |
| 2012 | * |
| 2013 | * @param queue Address of the queue. |
| 2014 | * @param list Pointer to sys_slist_t object. |
| 2015 | * |
| 2016 | * @return N/A |
| 2017 | */ |
| 2018 | extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list); |
| 2019 | |
| 2020 | /** |
| 2021 | * @brief Get an element from a queue. |
| 2022 | * |
Nicolas Pitre | 659fa0d | 2019-05-21 22:13:01 -0400 | [diff] [blame] | 2023 | * This routine removes first data item from @a queue. The first word of the |
| 2024 | * data item is reserved for the kernel's use. |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2025 | * |
| 2026 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 2027 | * |
| 2028 | * @param queue Address of the queue. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 2029 | * @param timeout Non-negative waiting period to obtain a data item (in |
| 2030 | * milliseconds), or one of the special values K_NO_WAIT and |
| 2031 | * K_FOREVER. |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2032 | * |
| 2033 | * @return Address of the data item if successful; NULL if returned |
| 2034 | * without waiting, or waiting period timed out. |
| 2035 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2036 | __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] | 2037 | |
| 2038 | /** |
Luiz Augusto von Dentz | 50b9377 | 2017-07-03 16:52:45 +0300 | [diff] [blame] | 2039 | * @brief Remove an element from a queue. |
| 2040 | * |
Nicolas Pitre | 659fa0d | 2019-05-21 22:13:01 -0400 | [diff] [blame] | 2041 | * This routine removes data item from @a queue. The first word of the |
| 2042 | * 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] | 2043 | * rely on sys_slist_find_and_remove which is not a constant time operation. |
| 2044 | * |
| 2045 | * @note Can be called by ISRs |
| 2046 | * |
| 2047 | * @param queue Address of the queue. |
| 2048 | * @param data Address of the data item. |
| 2049 | * |
| 2050 | * @return true if data item was removed |
| 2051 | */ |
| 2052 | static inline bool k_queue_remove(struct k_queue *queue, void *data) |
| 2053 | { |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2054 | 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] | 2055 | } |
| 2056 | |
| 2057 | /** |
Dhananjay Gundapu Jayakrishnan | 24bfa40 | 2018-08-22 12:33:00 +0200 | [diff] [blame] | 2058 | * @brief Append an element to a queue only if it's not present already. |
| 2059 | * |
Nicolas Pitre | 659fa0d | 2019-05-21 22:13:01 -0400 | [diff] [blame] | 2060 | * This routine appends data item to @a queue. The first word of the data |
| 2061 | * 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] | 2062 | * relies on sys_slist_is_node_in_list which is not a constant time operation. |
| 2063 | * |
| 2064 | * @note Can be called by ISRs |
| 2065 | * |
| 2066 | * @param queue Address of the queue. |
| 2067 | * @param data Address of the data item. |
| 2068 | * |
| 2069 | * @return true if data item was added, false if not |
| 2070 | */ |
| 2071 | static inline bool k_queue_unique_append(struct k_queue *queue, void *data) |
| 2072 | { |
| 2073 | sys_sfnode_t *test; |
| 2074 | |
| 2075 | SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) { |
| 2076 | if (test == (sys_sfnode_t *) data) { |
| 2077 | return false; |
| 2078 | } |
| 2079 | } |
| 2080 | |
| 2081 | k_queue_append(queue, data); |
| 2082 | return true; |
| 2083 | } |
| 2084 | |
| 2085 | /** |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2086 | * @brief Query a queue to see if it has data available. |
| 2087 | * |
| 2088 | * Note that the data might be already gone by the time this function returns |
| 2089 | * if other threads are also trying to read from the queue. |
| 2090 | * |
| 2091 | * @note Can be called by ISRs. |
| 2092 | * |
| 2093 | * @param queue Address of the queue. |
| 2094 | * |
| 2095 | * @return Non-zero if the queue is empty. |
| 2096 | * @return 0 if data is available. |
| 2097 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2098 | __syscall int k_queue_is_empty(struct k_queue *queue); |
| 2099 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2100 | 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] | 2101 | { |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2102 | return (int)sys_sflist_is_empty(&queue->data_q); |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2103 | } |
| 2104 | |
| 2105 | /** |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2106 | * @brief Peek element at the head of queue. |
| 2107 | * |
| 2108 | * Return element from the head of queue without removing it. |
| 2109 | * |
| 2110 | * @param queue Address of the queue. |
| 2111 | * |
| 2112 | * @return Head element, or NULL if queue is empty. |
| 2113 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2114 | __syscall void *k_queue_peek_head(struct k_queue *queue); |
| 2115 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2116 | 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] | 2117 | { |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2118 | 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] | 2119 | } |
| 2120 | |
| 2121 | /** |
| 2122 | * @brief Peek element at the tail of queue. |
| 2123 | * |
| 2124 | * Return element from the tail of queue without removing it. |
| 2125 | * |
| 2126 | * @param queue Address of the queue. |
| 2127 | * |
| 2128 | * @return Tail element, or NULL if queue is empty. |
| 2129 | */ |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2130 | __syscall void *k_queue_peek_tail(struct k_queue *queue); |
| 2131 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2132 | 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] | 2133 | { |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2134 | 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] | 2135 | } |
| 2136 | |
| 2137 | /** |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2138 | * @brief Statically define and initialize a queue. |
| 2139 | * |
| 2140 | * The queue can be accessed outside the module where it is defined using: |
| 2141 | * |
| 2142 | * @code extern struct k_queue <name>; @endcode |
| 2143 | * |
| 2144 | * @param name Name of the queue. |
| 2145 | */ |
| 2146 | #define K_QUEUE_DEFINE(name) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 2147 | Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2148 | _K_QUEUE_INITIALIZER(name) |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2149 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 2150 | /** @} */ |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 2151 | |
Wentong Wu | 5611e92 | 2019-06-20 23:51:27 +0800 | [diff] [blame] | 2152 | #ifdef CONFIG_USERSPACE |
| 2153 | /** |
| 2154 | * @brief futex structure |
| 2155 | * |
| 2156 | * A k_futex is a lightweight mutual exclusion primitive designed |
| 2157 | * to minimize kernel involvement. Uncontended operation relies |
| 2158 | * only on atomic access to shared memory. k_futex are tracked as |
| 2159 | * kernel objects and can live in user memory so any access bypass |
| 2160 | * the kernel object permission management mechanism. |
| 2161 | */ |
| 2162 | struct k_futex { |
| 2163 | atomic_t val; |
| 2164 | }; |
| 2165 | |
| 2166 | /** |
| 2167 | * @brief futex kernel data structure |
| 2168 | * |
| 2169 | * z_futex_data are the helper data structure for k_futex to complete |
| 2170 | * futex contended operation on kernel side, structure z_futex_data |
| 2171 | * of every futex object is invisible in user mode. |
| 2172 | */ |
| 2173 | struct z_futex_data { |
| 2174 | _wait_q_t wait_q; |
| 2175 | struct k_spinlock lock; |
| 2176 | }; |
| 2177 | |
| 2178 | #define Z_FUTEX_DATA_INITIALIZER(obj) \ |
| 2179 | { \ |
| 2180 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q) \ |
| 2181 | } |
| 2182 | |
| 2183 | /** |
| 2184 | * @defgroup futex_apis FUTEX APIs |
| 2185 | * @ingroup kernel_apis |
| 2186 | * @{ |
| 2187 | */ |
| 2188 | |
| 2189 | /** |
Wentong Wu | 5611e92 | 2019-06-20 23:51:27 +0800 | [diff] [blame] | 2190 | * @brief Pend the current thread on a futex |
| 2191 | * |
| 2192 | * Tests that the supplied futex contains the expected value, and if so, |
| 2193 | * goes to sleep until some other thread calls k_futex_wake() on it. |
| 2194 | * |
| 2195 | * @param futex Address of the futex. |
| 2196 | * @param expected Expected value of the futex, if it is different the caller |
| 2197 | * will not wait on it. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 2198 | * @param timeout Non-negative waiting period on the futex, in milliseconds, or |
| 2199 | * one of the special values K_NO_WAIT or K_FOREVER. |
Wentong Wu | 5611e92 | 2019-06-20 23:51:27 +0800 | [diff] [blame] | 2200 | * @retval -EACCES Caller does not have read access to futex address. |
| 2201 | * @retval -EAGAIN If the futex value did not match the expected parameter. |
| 2202 | * @retval -EINVAL Futex parameter address not recognized by the kernel. |
| 2203 | * @retval -ETIMEDOUT Thread woke up due to timeout and not a futex wakeup. |
| 2204 | * @retval 0 if the caller went to sleep and was woken up. The caller |
| 2205 | * should check the futex's value on wakeup to determine if it needs |
| 2206 | * to block again. |
| 2207 | */ |
| 2208 | __syscall int k_futex_wait(struct k_futex *futex, int expected, s32_t timeout); |
| 2209 | |
| 2210 | /** |
| 2211 | * @brief Wake one/all threads pending on a futex |
| 2212 | * |
| 2213 | * Wake up the highest priority thread pending on the supplied futex, or |
| 2214 | * wakeup all the threads pending on the supplied futex, and the behavior |
| 2215 | * depends on wake_all. |
| 2216 | * |
| 2217 | * @param futex Futex to wake up pending threads. |
| 2218 | * @param wake_all If true, wake up all pending threads; If false, |
| 2219 | * wakeup the highest priority thread. |
| 2220 | * @retval -EACCES Caller does not have access to the futex address. |
| 2221 | * @retval -EINVAL Futex parameter address not recognized by the kernel. |
| 2222 | * @retval Number of threads that were woken up. |
| 2223 | */ |
| 2224 | __syscall int k_futex_wake(struct k_futex *futex, bool wake_all); |
| 2225 | |
| 2226 | /** @} */ |
| 2227 | #endif |
| 2228 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2229 | struct k_fifo { |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2230 | struct k_queue _queue; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2231 | }; |
| 2232 | |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2233 | /** |
| 2234 | * @cond INTERNAL_HIDDEN |
| 2235 | */ |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 2236 | #define Z_FIFO_INITIALIZER(obj) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2237 | { \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2238 | ._queue = _K_QUEUE_INITIALIZER(obj._queue) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2239 | } |
| 2240 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 2241 | #define K_FIFO_INITIALIZER __DEPRECATED_MACRO Z_FIFO_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2242 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2243 | /** |
| 2244 | * INTERNAL_HIDDEN @endcond |
| 2245 | */ |
| 2246 | |
| 2247 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2248 | * @defgroup fifo_apis FIFO APIs |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2249 | * @ingroup kernel_apis |
| 2250 | * @{ |
| 2251 | */ |
| 2252 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2253 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2254 | * @brief Initialize a FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2255 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2256 | * This routine initializes a FIFO queue, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2257 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2258 | * @param fifo Address of the FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2259 | * |
| 2260 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2261 | * @req K-FIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2262 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2263 | #define k_fifo_init(fifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2264 | k_queue_init(&(fifo)->_queue) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2265 | |
| 2266 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2267 | * @brief Cancel waiting on a FIFO queue. |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 2268 | * |
| 2269 | * This routine causes first thread pending on @a fifo, if any, to |
| 2270 | * return from k_fifo_get() call with NULL value (as if timeout |
| 2271 | * expired). |
| 2272 | * |
| 2273 | * @note Can be called by ISRs. |
| 2274 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2275 | * @param fifo Address of the FIFO queue. |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 2276 | * |
| 2277 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2278 | * @req K-FIFO-001 |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 2279 | */ |
| 2280 | #define k_fifo_cancel_wait(fifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2281 | k_queue_cancel_wait(&(fifo)->_queue) |
Paul Sokolovsky | 3f50707 | 2017-04-25 17:54:31 +0300 | [diff] [blame] | 2282 | |
| 2283 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2284 | * @brief Add an element to a FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2285 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2286 | * 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] | 2287 | * aligned on a word boundary, and the first word of the item is reserved |
| 2288 | * for the kernel's use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2289 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2290 | * @note Can be called by ISRs. |
| 2291 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2292 | * @param fifo Address of the FIFO. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2293 | * @param data Address of the data item. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2294 | * |
| 2295 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2296 | * @req K-FIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2297 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2298 | #define k_fifo_put(fifo, data) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2299 | k_queue_append(&(fifo)->_queue, data) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2300 | |
| 2301 | /** |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2302 | * @brief Add an element to a FIFO queue. |
| 2303 | * |
Andrew Boie | ac3dcc1 | 2019-04-01 12:28:03 -0700 | [diff] [blame] | 2304 | * This routine adds a data item to @a fifo. There is an implicit memory |
| 2305 | * allocation to create an additional temporary bookkeeping data structure from |
| 2306 | * the calling thread's resource pool, which is automatically freed when the |
| 2307 | * item is removed. The data itself is not copied. |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2308 | * |
| 2309 | * @note Can be called by ISRs. |
| 2310 | * |
| 2311 | * @param fifo Address of the FIFO. |
| 2312 | * @param data Address of the data item. |
| 2313 | * |
| 2314 | * @retval 0 on success |
| 2315 | * @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] | 2316 | * @req K-FIFO-001 |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2317 | */ |
| 2318 | #define k_fifo_alloc_put(fifo, data) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2319 | k_queue_alloc_append(&(fifo)->_queue, data) |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2320 | |
| 2321 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2322 | * @brief Atomically add a list of elements to a FIFO. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2323 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2324 | * 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] | 2325 | * 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] | 2326 | * each data item pointing to the next data item; the list must be |
| 2327 | * NULL-terminated. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2328 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2329 | * @note Can be called by ISRs. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2330 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2331 | * @param fifo Address of the FIFO queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2332 | * @param head Pointer to first node in singly-linked list. |
| 2333 | * @param tail Pointer to last node in singly-linked list. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2334 | * |
| 2335 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2336 | * @req K-FIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2337 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2338 | #define k_fifo_put_list(fifo, head, tail) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2339 | k_queue_append_list(&(fifo)->_queue, head, tail) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2340 | |
| 2341 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2342 | * @brief Atomically add a list of elements to a FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2343 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2344 | * This routine adds a list of data items to @a fifo in one operation. |
| 2345 | * The data items must be in a singly-linked list implemented using a |
| 2346 | * 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] | 2347 | * and must be re-initialized via sys_slist_init(). |
| 2348 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2349 | * @note Can be called by ISRs. |
| 2350 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2351 | * @param fifo Address of the FIFO queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2352 | * @param list Pointer to sys_slist_t object. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2353 | * |
| 2354 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2355 | * @req K-FIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2356 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2357 | #define k_fifo_put_slist(fifo, list) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2358 | k_queue_merge_slist(&(fifo)->_queue, list) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2359 | |
| 2360 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2361 | * @brief Get an element from a FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2362 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2363 | * 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] | 2364 | * 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] | 2365 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2366 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 2367 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2368 | * @param fifo Address of the FIFO queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2369 | * @param timeout Waiting period to obtain a data item (in milliseconds), |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2370 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 2371 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2372 | * @return Address of the data item if successful; NULL if returned |
| 2373 | * without waiting, or waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2374 | * @req K-FIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2375 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2376 | #define k_fifo_get(fifo, timeout) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2377 | k_queue_get(&(fifo)->_queue, timeout) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2378 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2379 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2380 | * @brief Query a FIFO queue to see if it has data available. |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2381 | * |
| 2382 | * 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] | 2383 | * if other threads is also trying to read from the FIFO. |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2384 | * |
| 2385 | * @note Can be called by ISRs. |
| 2386 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2387 | * @param fifo Address of the FIFO queue. |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2388 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2389 | * @return Non-zero if the FIFO queue is empty. |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2390 | * @return 0 if data is available. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2391 | * @req K-FIFO-001 |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2392 | */ |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 2393 | #define k_fifo_is_empty(fifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2394 | k_queue_is_empty(&(fifo)->_queue) |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 2395 | |
| 2396 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2397 | * @brief Peek element at the head of a FIFO queue. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2398 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2399 | * 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] | 2400 | * 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] | 2401 | * on each iteration of processing, a head container will be peeked, |
| 2402 | * 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] | 2403 | * it will be completely remove from the FIFO queue. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2404 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2405 | * @param fifo Address of the 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 Head element, or NULL if the FIFO queue is empty. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2408 | * @req K-FIFO-001 |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2409 | */ |
| 2410 | #define k_fifo_peek_head(fifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2411 | k_queue_peek_head(&(fifo)->_queue) |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2412 | |
| 2413 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2414 | * @brief Peek element at the tail of FIFO queue. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2415 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2416 | * Return element from the tail of FIFO queue (without removing it). A usecase |
| 2417 | * for this is if elements of the FIFO queue are themselves containers. Then |
| 2418 | * 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] | 2419 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2420 | * @param fifo Address of the FIFO queue. |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2421 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2422 | * @return Tail element, or NULL if a FIFO queue is empty. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2423 | * @req K-FIFO-001 |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2424 | */ |
| 2425 | #define k_fifo_peek_tail(fifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2426 | k_queue_peek_tail(&(fifo)->_queue) |
Paul Sokolovsky | 16bb3ec | 2017-06-08 17:13:03 +0300 | [diff] [blame] | 2427 | |
| 2428 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2429 | * @brief Statically define and initialize a FIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2430 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2431 | * 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] | 2432 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 2433 | * @code extern struct k_fifo <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2434 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2435 | * @param name Name of the FIFO queue. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2436 | * @req K-FIFO-002 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2437 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2438 | #define K_FIFO_DEFINE(name) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 2439 | Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \ |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 2440 | Z_FIFO_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2441 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 2442 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2443 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2444 | struct k_lifo { |
Luiz Augusto von Dentz | 0dc4dd4 | 2017-02-21 15:49:52 +0200 | [diff] [blame] | 2445 | struct k_queue _queue; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2446 | }; |
| 2447 | |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2448 | /** |
| 2449 | * @cond INTERNAL_HIDDEN |
| 2450 | */ |
| 2451 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2452 | #define _K_LIFO_INITIALIZER(obj) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2453 | { \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2454 | ._queue = _K_QUEUE_INITIALIZER(obj._queue) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2455 | } |
| 2456 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 2457 | #define K_LIFO_INITIALIZER __DEPRECATED_MACRO _K_LIFO_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2458 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2459 | /** |
| 2460 | * INTERNAL_HIDDEN @endcond |
| 2461 | */ |
| 2462 | |
| 2463 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2464 | * @defgroup lifo_apis LIFO APIs |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2465 | * @ingroup kernel_apis |
| 2466 | * @{ |
| 2467 | */ |
| 2468 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2469 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2470 | * @brief Initialize a LIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2471 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2472 | * This routine initializes a LIFO queue object, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2473 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2474 | * @param lifo Address of the LIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2475 | * |
| 2476 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2477 | * @req K-LIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2478 | */ |
Luiz Augusto von Dentz | 0dc4dd4 | 2017-02-21 15:49:52 +0200 | [diff] [blame] | 2479 | #define k_lifo_init(lifo) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2480 | k_queue_init(&(lifo)->_queue) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2481 | |
| 2482 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2483 | * @brief Add an element to a LIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2484 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2485 | * 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] | 2486 | * 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] | 2487 | * reserved for the kernel's use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2488 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2489 | * @note Can be called by ISRs. |
| 2490 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2491 | * @param lifo Address of the LIFO queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2492 | * @param data Address of the data item. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2493 | * |
| 2494 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2495 | * @req K-LIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2496 | */ |
Luiz Augusto von Dentz | 0dc4dd4 | 2017-02-21 15:49:52 +0200 | [diff] [blame] | 2497 | #define k_lifo_put(lifo, data) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2498 | k_queue_prepend(&(lifo)->_queue, data) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2499 | |
| 2500 | /** |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2501 | * @brief Add an element to a LIFO queue. |
| 2502 | * |
Andrew Boie | ac3dcc1 | 2019-04-01 12:28:03 -0700 | [diff] [blame] | 2503 | * This routine adds a data item to @a lifo. There is an implicit memory |
| 2504 | * allocation to create an additional temporary bookkeeping data structure from |
| 2505 | * the calling thread's resource pool, which is automatically freed when the |
| 2506 | * item is removed. The data itself is not copied. |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2507 | * |
| 2508 | * @note Can be called by ISRs. |
| 2509 | * |
| 2510 | * @param lifo Address of the LIFO. |
| 2511 | * @param data Address of the data item. |
| 2512 | * |
| 2513 | * @retval 0 on success |
| 2514 | * @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] | 2515 | * @req K-LIFO-001 |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2516 | */ |
| 2517 | #define k_lifo_alloc_put(lifo, data) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2518 | k_queue_alloc_prepend(&(lifo)->_queue, data) |
Andrew Boie | 2b9b4b2 | 2018-04-27 13:21:22 -0700 | [diff] [blame] | 2519 | |
| 2520 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2521 | * @brief Get an element from a LIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2522 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2523 | * 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] | 2524 | * 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] | 2525 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2526 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 2527 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2528 | * @param lifo Address of the LIFO queue. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2529 | * @param timeout Waiting period to obtain a data item (in milliseconds), |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2530 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 2531 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2532 | * @return Address of the data item if successful; NULL if returned |
| 2533 | * without waiting, or waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2534 | * @req K-LIFO-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2535 | */ |
Luiz Augusto von Dentz | 0dc4dd4 | 2017-02-21 15:49:52 +0200 | [diff] [blame] | 2536 | #define k_lifo_get(lifo, timeout) \ |
Nicolas Pitre | a04a2ca | 2019-05-20 23:02:39 -0400 | [diff] [blame] | 2537 | k_queue_get(&(lifo)->_queue, timeout) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2538 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2539 | /** |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2540 | * @brief Statically define and initialize a LIFO queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2541 | * |
Anas Nashif | 585fd1f | 2018-02-25 08:04:59 -0600 | [diff] [blame] | 2542 | * 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] | 2543 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 2544 | * @code extern struct k_lifo <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2545 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2546 | * @param name Name of the fifo. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2547 | * @req K-LIFO-002 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2548 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2549 | #define K_LIFO_DEFINE(name) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 2550 | Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2551 | _K_LIFO_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2552 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 2553 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2554 | |
| 2555 | /** |
| 2556 | * @cond INTERNAL_HIDDEN |
| 2557 | */ |
Adithya Baglody | 28080d3 | 2018-10-15 11:48:51 +0530 | [diff] [blame] | 2558 | #define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2559 | |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2560 | typedef uintptr_t stack_data_t; |
| 2561 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2562 | struct k_stack { |
| 2563 | _wait_q_t wait_q; |
Andy Ross | f0933d0 | 2018-07-26 10:23:02 -0700 | [diff] [blame] | 2564 | struct k_spinlock lock; |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2565 | stack_data_t *base, *next, *top; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2566 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 2567 | _OBJECT_TRACING_NEXT_PTR(k_stack) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 2568 | _OBJECT_TRACING_LINKED_FLAG |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2569 | u8_t flags; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2570 | }; |
| 2571 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2572 | #define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2573 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2574 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2575 | .base = stack_buffer, \ |
| 2576 | .next = stack_buffer, \ |
| 2577 | .top = stack_buffer + stack_num_entries, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 2578 | _OBJECT_TRACING_INIT \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2579 | } |
| 2580 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 2581 | #define K_STACK_INITIALIZER __DEPRECATED_MACRO _K_STACK_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2582 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2583 | /** |
| 2584 | * INTERNAL_HIDDEN @endcond |
| 2585 | */ |
| 2586 | |
| 2587 | /** |
| 2588 | * @defgroup stack_apis Stack APIs |
| 2589 | * @ingroup kernel_apis |
| 2590 | * @{ |
| 2591 | */ |
| 2592 | |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2593 | /** |
| 2594 | * @brief Initialize a stack. |
| 2595 | * |
| 2596 | * This routine initializes a stack object, prior to its first use. |
| 2597 | * |
| 2598 | * @param stack Address of the stack. |
| 2599 | * @param buffer Address of array used to hold stacked values. |
| 2600 | * @param num_entries Maximum number of values that can be stacked. |
| 2601 | * |
| 2602 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2603 | * @req K-STACK-001 |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2604 | */ |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2605 | void k_stack_init(struct k_stack *stack, |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2606 | stack_data_t *buffer, u32_t num_entries); |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2607 | |
| 2608 | |
| 2609 | /** |
| 2610 | * @brief Initialize a stack. |
| 2611 | * |
| 2612 | * This routine initializes a stack object, prior to its first use. Internal |
| 2613 | * buffers will be allocated from the calling thread's resource pool. |
| 2614 | * This memory will be released if k_stack_cleanup() is called, or |
| 2615 | * userspace is enabled and the stack object loses all references to it. |
| 2616 | * |
| 2617 | * @param stack Address of the stack. |
| 2618 | * @param num_entries Maximum number of values that can be stacked. |
| 2619 | * |
| 2620 | * @return -ENOMEM if memory couldn't be allocated |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2621 | * @req K-STACK-001 |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2622 | */ |
| 2623 | |
Adithya Baglody | 28080d3 | 2018-10-15 11:48:51 +0530 | [diff] [blame] | 2624 | __syscall s32_t k_stack_alloc_init(struct k_stack *stack, |
| 2625 | u32_t num_entries); |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2626 | |
| 2627 | /** |
| 2628 | * @brief Release a stack's allocated buffer |
| 2629 | * |
| 2630 | * If a stack object was given a dynamically allocated buffer via |
| 2631 | * k_stack_alloc_init(), this will free it. This function does nothing |
| 2632 | * if the buffer wasn't dynamically allocated. |
| 2633 | * |
| 2634 | * @param stack Address of the stack. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2635 | * @req K-STACK-001 |
Andrew Boie | f3bee95 | 2018-05-02 17:44:39 -0700 | [diff] [blame] | 2636 | */ |
| 2637 | void k_stack_cleanup(struct k_stack *stack); |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2638 | |
| 2639 | /** |
| 2640 | * @brief Push an element onto a stack. |
| 2641 | * |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2642 | * 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] | 2643 | * |
| 2644 | * @note Can be called by ISRs. |
| 2645 | * |
| 2646 | * @param stack Address of the stack. |
| 2647 | * @param data Value to push onto the stack. |
| 2648 | * |
| 2649 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2650 | * @req K-STACK-001 |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2651 | */ |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2652 | __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] | 2653 | |
| 2654 | /** |
| 2655 | * @brief Pop an element from a stack. |
| 2656 | * |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2657 | * This routine removes a stack_data_t value from @a stack in a "last in, |
| 2658 | * first out" manner and stores the value in @a data. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2659 | * |
| 2660 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 2661 | * |
| 2662 | * @param stack Address of the stack. |
| 2663 | * @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] | 2664 | * @param timeout Non-negative waiting period to obtain a value (in |
| 2665 | * milliseconds), or one of the special values K_NO_WAIT and |
| 2666 | * K_FOREVER. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2667 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2668 | * @retval 0 Element popped from stack. |
| 2669 | * @retval -EBUSY Returned without waiting. |
| 2670 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2671 | * @req K-STACK-001 |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2672 | */ |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 2673 | __syscall int k_stack_pop(struct k_stack *stack, stack_data_t *data, |
| 2674 | s32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2675 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2676 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2677 | * @brief Statically define and initialize a stack |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2678 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2679 | * 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] | 2680 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 2681 | * @code extern struct k_stack <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2682 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2683 | * @param name Name of the stack. |
| 2684 | * @param stack_num_entries Maximum number of values that can be stacked. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2685 | * @req K-STACK-002 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2686 | */ |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 2687 | #define K_STACK_DEFINE(name, stack_num_entries) \ |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 2688 | stack_data_t __noinit \ |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 2689 | _k_stack_buf_##name[stack_num_entries]; \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 2690 | Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2691 | _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \ |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 2692 | stack_num_entries) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2693 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 2694 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2695 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2696 | struct k_work; |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 2697 | struct k_work_poll; |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2698 | |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 2699 | /* private, used by k_poll and k_work_poll */ |
Piotr Zięcik | 1c4177d | 2019-08-27 12:19:26 +0200 | [diff] [blame] | 2700 | typedef int (*_poller_cb_t)(struct k_poll_event *event, u32_t state); |
| 2701 | struct _poller { |
| 2702 | volatile bool is_polling; |
| 2703 | struct k_thread *thread; |
| 2704 | _poller_cb_t cb; |
| 2705 | }; |
| 2706 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2707 | /** |
Anas Nashif | 29f37f0 | 2019-01-21 14:30:35 -0500 | [diff] [blame] | 2708 | * @addtogroup thread_apis |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2709 | * @{ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2710 | */ |
| 2711 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2712 | /** |
| 2713 | * @typedef k_work_handler_t |
| 2714 | * @brief Work item handler function type. |
| 2715 | * |
| 2716 | * A work item's handler function is executed by a workqueue's thread |
| 2717 | * when the work item is processed by the workqueue. |
| 2718 | * |
| 2719 | * @param work Address of the work item. |
| 2720 | * |
| 2721 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2722 | * @req K-WORK-001 |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2723 | */ |
| 2724 | typedef void (*k_work_handler_t)(struct k_work *work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2725 | |
| 2726 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2727 | * @cond INTERNAL_HIDDEN |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2728 | */ |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2729 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2730 | struct k_work_q { |
Luiz Augusto von Dentz | adb581b | 2017-07-03 19:09:44 +0300 | [diff] [blame] | 2731 | struct k_queue queue; |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 2732 | struct k_thread thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2733 | }; |
| 2734 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2735 | enum { |
Iván Briano | 9c7b5ea | 2016-10-04 18:11:05 -0300 | [diff] [blame] | 2736 | K_WORK_STATE_PENDING, /* Work item pending state */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2737 | }; |
| 2738 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2739 | struct k_work { |
Luiz Augusto von Dentz | adb581b | 2017-07-03 19:09:44 +0300 | [diff] [blame] | 2740 | void *_reserved; /* Used by k_queue implementation. */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2741 | k_work_handler_t handler; |
| 2742 | atomic_t flags[1]; |
| 2743 | }; |
| 2744 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2745 | struct k_delayed_work { |
| 2746 | struct k_work work; |
| 2747 | struct _timeout timeout; |
| 2748 | struct k_work_q *work_q; |
| 2749 | }; |
| 2750 | |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 2751 | struct k_work_poll { |
| 2752 | struct k_work work; |
| 2753 | struct _poller poller; |
| 2754 | struct k_poll_event *events; |
| 2755 | int num_events; |
| 2756 | k_work_handler_t real_handler; |
| 2757 | struct _timeout timeout; |
| 2758 | int poll_result; |
| 2759 | }; |
| 2760 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2761 | extern struct k_work_q k_sys_work_q; |
| 2762 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2763 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2764 | * INTERNAL_HIDDEN @endcond |
| 2765 | */ |
| 2766 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2767 | #define Z_WORK_INITIALIZER(work_handler) \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2768 | { \ |
| 2769 | ._reserved = NULL, \ |
| 2770 | .handler = work_handler, \ |
| 2771 | .flags = { 0 } \ |
| 2772 | } |
| 2773 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 2774 | #define K_WORK_INITIALIZER __DEPRECATED_MACRO Z_WORK_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2775 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2776 | /** |
| 2777 | * @brief Initialize a statically-defined work item. |
| 2778 | * |
| 2779 | * This macro can be used to initialize a statically-defined workqueue work |
| 2780 | * item, prior to its first use. For example, |
| 2781 | * |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2782 | * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2783 | * |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2784 | * @param work Symbol name for work item object |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2785 | * @param work_handler Function to invoke each time work item is processed. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2786 | * @req K-WORK-002 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2787 | */ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 2788 | #define K_WORK_DEFINE(work, work_handler) \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2789 | struct k_work work = Z_WORK_INITIALIZER(work_handler) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2790 | |
| 2791 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2792 | * @brief Initialize a work item. |
| 2793 | * |
| 2794 | * This routine initializes a workqueue work item, prior to its first use. |
| 2795 | * |
| 2796 | * @param work Address of work item. |
| 2797 | * @param handler Function to invoke each time work item is processed. |
| 2798 | * |
| 2799 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2800 | * @req K-WORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2801 | */ |
| 2802 | static inline void k_work_init(struct k_work *work, k_work_handler_t handler) |
| 2803 | { |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 2804 | *work = (struct k_work)Z_WORK_INITIALIZER(handler); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2805 | } |
| 2806 | |
| 2807 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2808 | * @brief Submit a work item. |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 2809 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2810 | * This routine submits work item @a work to be processed by workqueue |
| 2811 | * @a work_q. If the work item is already pending in the workqueue's queue |
| 2812 | * as a result of an earlier submission, this routine has no effect on the |
| 2813 | * work item. If the work item has already been processed, or is currently |
| 2814 | * being processed, its work is considered complete and the work item can be |
| 2815 | * resubmitted. |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 2816 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2817 | * @warning |
| 2818 | * A submitted work item must not be modified until it has been processed |
| 2819 | * by the workqueue. |
| 2820 | * |
| 2821 | * @note Can be called by ISRs. |
| 2822 | * |
| 2823 | * @param work_q Address of workqueue. |
| 2824 | * @param work Address of work item. |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 2825 | * |
| 2826 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2827 | * @req K-WORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2828 | */ |
| 2829 | static inline void k_work_submit_to_queue(struct k_work_q *work_q, |
| 2830 | struct k_work *work) |
| 2831 | { |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 2832 | 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] | 2833 | k_queue_append(&work_q->queue, work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2834 | } |
| 2835 | } |
| 2836 | |
| 2837 | /** |
Andrew Boie | 2b1d54e | 2018-11-12 14:25:19 -0800 | [diff] [blame] | 2838 | * @brief Submit a work item to a user mode workqueue |
| 2839 | * |
David B. Kinder | 06d7835 | 2018-12-17 14:32:40 -0800 | [diff] [blame] | 2840 | * 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] | 2841 | * memory allocation is made from the caller's resource pool which is freed |
| 2842 | * once the worker thread consumes the k_work item. The workqueue |
| 2843 | * thread must have memory access to the k_work item being submitted. The caller |
| 2844 | * must have permission granted on the work_q parameter's queue object. |
| 2845 | * |
| 2846 | * Otherwise this works the same as k_work_submit_to_queue(). |
| 2847 | * |
| 2848 | * @note Can be called by ISRs. |
| 2849 | * |
| 2850 | * @param work_q Address of workqueue. |
| 2851 | * @param work Address of work item. |
| 2852 | * |
| 2853 | * @retval -EBUSY if the work item was already in some workqueue |
| 2854 | * @retval -ENOMEM if no memory for thread resource pool allocation |
| 2855 | * @retval 0 Success |
| 2856 | * @req K-WORK-001 |
| 2857 | */ |
| 2858 | static inline int k_work_submit_to_user_queue(struct k_work_q *work_q, |
| 2859 | struct k_work *work) |
| 2860 | { |
| 2861 | int ret = -EBUSY; |
| 2862 | |
| 2863 | if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) { |
| 2864 | ret = k_queue_alloc_append(&work_q->queue, work); |
| 2865 | |
| 2866 | /* Couldn't insert into the queue. Clear the pending bit |
| 2867 | * so the work item can be submitted again |
| 2868 | */ |
Flavio Ceolin | 76b3518 | 2018-12-16 12:48:29 -0800 | [diff] [blame] | 2869 | if (ret != 0) { |
Andrew Boie | 2b1d54e | 2018-11-12 14:25:19 -0800 | [diff] [blame] | 2870 | atomic_clear_bit(work->flags, K_WORK_STATE_PENDING); |
| 2871 | } |
| 2872 | } |
| 2873 | |
| 2874 | return ret; |
| 2875 | } |
| 2876 | |
| 2877 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2878 | * @brief Check if a work item is pending. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2879 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2880 | * This routine indicates if work item @a work is pending in a workqueue's |
| 2881 | * queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2882 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2883 | * @note Can be called by ISRs. |
| 2884 | * |
| 2885 | * @param work Address of work item. |
| 2886 | * |
Flavio Ceolin | 82ef4f8 | 2018-11-21 18:12:34 -0800 | [diff] [blame] | 2887 | * @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] | 2888 | * @req K-WORK-001 |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 2889 | */ |
Flavio Ceolin | 82ef4f8 | 2018-11-21 18:12:34 -0800 | [diff] [blame] | 2890 | static inline bool k_work_pending(struct k_work *work) |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 2891 | { |
Iván Briano | 9c7b5ea | 2016-10-04 18:11:05 -0300 | [diff] [blame] | 2892 | return atomic_test_bit(work->flags, K_WORK_STATE_PENDING); |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 2893 | } |
| 2894 | |
| 2895 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2896 | * @brief Start a workqueue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2897 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2898 | * This routine starts workqueue @a work_q. The workqueue spawns its work |
| 2899 | * processing thread, which runs forever. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2900 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2901 | * @param work_q Address of workqueue. |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 2902 | * @param stack Pointer to work queue thread's stack space, as defined by |
| 2903 | * K_THREAD_STACK_DEFINE() |
| 2904 | * @param stack_size Size of the work queue thread's stack (in bytes), which |
| 2905 | * should either be the same constant passed to |
| 2906 | * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF(). |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2907 | * @param prio Priority of the work queue's thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2908 | * |
| 2909 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2910 | * @req K-WORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2911 | */ |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 2912 | extern void k_work_q_start(struct k_work_q *work_q, |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 2913 | k_thread_stack_t *stack, |
Benjamin Walsh | 669360d | 2016-11-14 16:46:14 -0500 | [diff] [blame] | 2914 | size_t stack_size, int prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2915 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2916 | /** |
Andrew Boie | 2b1d54e | 2018-11-12 14:25:19 -0800 | [diff] [blame] | 2917 | * @brief Start a workqueue in user mode |
| 2918 | * |
| 2919 | * This works identically to k_work_q_start() except it is callable from user |
| 2920 | * mode, and the worker thread created will run in user mode. |
| 2921 | * The caller must have permissions granted on both the work_q parameter's |
| 2922 | * thread and queue objects, and the same restrictions on priority apply as |
| 2923 | * k_thread_create(). |
| 2924 | * |
| 2925 | * @param work_q Address of workqueue. |
| 2926 | * @param stack Pointer to work queue thread's stack space, as defined by |
| 2927 | * K_THREAD_STACK_DEFINE() |
| 2928 | * @param stack_size Size of the work queue thread's stack (in bytes), which |
| 2929 | * should either be the same constant passed to |
| 2930 | * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF(). |
| 2931 | * @param prio Priority of the work queue's thread. |
| 2932 | * |
| 2933 | * @return N/A |
| 2934 | * @req K-WORK-001 |
| 2935 | */ |
| 2936 | extern void k_work_q_user_start(struct k_work_q *work_q, |
| 2937 | k_thread_stack_t *stack, |
| 2938 | size_t stack_size, int prio); |
| 2939 | |
| 2940 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2941 | * @brief Initialize a delayed work item. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2942 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2943 | * This routine initializes a workqueue delayed work item, prior to |
| 2944 | * its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2945 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2946 | * @param work Address of delayed work item. |
| 2947 | * @param handler Function to invoke each time work item is processed. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2948 | * |
| 2949 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2950 | * @req K-DWORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2951 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 2952 | extern void k_delayed_work_init(struct k_delayed_work *work, |
| 2953 | k_work_handler_t handler); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2954 | |
| 2955 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2956 | * @brief Submit a delayed work item. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2957 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2958 | * This routine schedules work item @a work to be processed by workqueue |
| 2959 | * @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] | 2960 | * 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] | 2961 | * Only when the countdown completes is the work item actually submitted to |
| 2962 | * the workqueue and becomes pending. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2963 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2964 | * Submitting a previously submitted delayed work item that is still |
Andy Ross | 03c1d28 | 2018-02-13 12:13:25 -0800 | [diff] [blame] | 2965 | * counting down cancels the existing submission and restarts the |
| 2966 | * countdown using the new delay. Note that this behavior is |
| 2967 | * inherently subject to race conditions with the pre-existing |
| 2968 | * timeouts and work queue, so care must be taken to synchronize such |
| 2969 | * resubmissions externally. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2970 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2971 | * @warning |
| 2972 | * A delayed work item must not be modified until it has been processed |
| 2973 | * by the workqueue. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2974 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2975 | * @note Can be called by ISRs. |
| 2976 | * |
| 2977 | * @param work_q Address of workqueue. |
| 2978 | * @param work Address of delayed work item. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 2979 | * @param delay Non-negative delay before submitting the work item (in |
| 2980 | * milliseconds). |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2981 | * |
| 2982 | * @retval 0 Work item countdown started. |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2983 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 2984 | * @retval -EADDRINUSE Work item is pending on a different workqueue. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 2985 | * @req K-DWORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2986 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 2987 | extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q, |
| 2988 | struct k_delayed_work *work, |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 2989 | s32_t delay); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2990 | |
| 2991 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2992 | * @brief Cancel a delayed work item. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2993 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2994 | * This routine cancels the submission of delayed work item @a work. |
David B. Kinder | 8b986d7 | 2017-04-18 15:56:26 -0700 | [diff] [blame] | 2995 | * 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] | 2996 | * underway. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2997 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 2998 | * @note Can be called by ISRs. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2999 | * |
Andy Ross | d7ae2a8 | 2019-03-08 08:51:13 -0800 | [diff] [blame] | 3000 | * @note The result of calling this on a k_delayed_work item that has |
| 3001 | * not been submitted (i.e. before the return of the |
| 3002 | * k_delayed_work_submit_to_queue() call) is undefined. |
| 3003 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3004 | * @param work Address of delayed work item. |
| 3005 | * |
David B. Kinder | 8b986d7 | 2017-04-18 15:56:26 -0700 | [diff] [blame] | 3006 | * @retval 0 Work item countdown canceled. |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3007 | * @retval -EINVAL Work item is being processed or has completed its work. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3008 | * @req K-DWORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3009 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 3010 | extern int k_delayed_work_cancel(struct k_delayed_work *work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3011 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3012 | /** |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3013 | * @brief Submit a work item to the system workqueue. |
| 3014 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3015 | * This routine submits work item @a work to be processed by the system |
| 3016 | * workqueue. If the work item is already pending in the workqueue's queue |
| 3017 | * as a result of an earlier submission, this routine has no effect on the |
| 3018 | * work item. If the work item has already been processed, or is currently |
| 3019 | * being processed, its work is considered complete and the work item can be |
| 3020 | * resubmitted. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3021 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3022 | * @warning |
| 3023 | * Work items submitted to the system workqueue should avoid using handlers |
| 3024 | * that block or yield since this may prevent the system workqueue from |
| 3025 | * processing other work items in a timely manner. |
| 3026 | * |
| 3027 | * @note Can be called by ISRs. |
| 3028 | * |
| 3029 | * @param work Address of work item. |
| 3030 | * |
| 3031 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3032 | * @req K-WORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3033 | */ |
| 3034 | static inline void k_work_submit(struct k_work *work) |
| 3035 | { |
| 3036 | k_work_submit_to_queue(&k_sys_work_q, work); |
| 3037 | } |
| 3038 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3039 | /** |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3040 | * @brief Submit a delayed work item to the system workqueue. |
| 3041 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3042 | * This routine schedules work item @a work to be processed by the system |
| 3043 | * workqueue after a delay of @a delay milliseconds. The routine initiates |
David B. Kinder | 8b986d7 | 2017-04-18 15:56:26 -0700 | [diff] [blame] | 3044 | * 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] | 3045 | * Only when the countdown completes is the work item actually submitted to |
| 3046 | * the workqueue and becomes pending. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3047 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3048 | * Submitting a previously submitted delayed work item that is still |
| 3049 | * counting down cancels the existing submission and restarts the countdown |
| 3050 | * using the new delay. If the work item is currently pending on the |
| 3051 | * workqueue's queue because the countdown has completed it is too late to |
| 3052 | * resubmit the item, and resubmission fails without impacting the work item. |
| 3053 | * If the work item has already been processed, or is currently being processed, |
| 3054 | * its work is considered complete and the work item can be resubmitted. |
| 3055 | * |
| 3056 | * @warning |
| 3057 | * Work items submitted to the system workqueue should avoid using handlers |
| 3058 | * that block or yield since this may prevent the system workqueue from |
| 3059 | * processing other work items in a timely manner. |
| 3060 | * |
| 3061 | * @note Can be called by ISRs. |
| 3062 | * |
| 3063 | * @param work Address of delayed work item. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3064 | * @param delay Non-negative delay before submitting the work item (in |
| 3065 | * milliseconds). |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3066 | * |
| 3067 | * @retval 0 Work item countdown started. |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 3068 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 3069 | * @retval -EADDRINUSE Work item is pending on a different workqueue. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3070 | * @req K-DWORK-001 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3071 | */ |
| 3072 | static inline int k_delayed_work_submit(struct k_delayed_work *work, |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3073 | s32_t delay) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3074 | { |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 3075 | 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] | 3076 | } |
| 3077 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3078 | /** |
Johan Hedberg | c8201b2 | 2016-12-09 10:42:22 +0200 | [diff] [blame] | 3079 | * @brief Get time remaining before a delayed work gets scheduled. |
| 3080 | * |
| 3081 | * This routine computes the (approximate) time remaining before a |
| 3082 | * 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] | 3083 | * scheduled, it returns zero. |
Johan Hedberg | c8201b2 | 2016-12-09 10:42:22 +0200 | [diff] [blame] | 3084 | * |
| 3085 | * @param work Delayed work item. |
| 3086 | * |
| 3087 | * @return Remaining time (in milliseconds). |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3088 | * @req K-DWORK-001 |
Johan Hedberg | c8201b2 | 2016-12-09 10:42:22 +0200 | [diff] [blame] | 3089 | */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3090 | 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] | 3091 | { |
Andy Ross | 8892406 | 2019-10-03 11:43:10 -0700 | [diff] [blame] | 3092 | return k_ticks_to_ms_floor64(z_timeout_remaining(&work->timeout)); |
Johan Hedberg | c8201b2 | 2016-12-09 10:42:22 +0200 | [diff] [blame] | 3093 | } |
| 3094 | |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 3095 | /** |
| 3096 | * @brief Initialize a triggered work item. |
| 3097 | * |
| 3098 | * This routine initializes a workqueue triggered work item, prior to |
| 3099 | * its first use. |
| 3100 | * |
| 3101 | * @param work Address of triggered work item. |
| 3102 | * @param handler Function to invoke each time work item is processed. |
| 3103 | * |
| 3104 | * @return N/A |
| 3105 | */ |
| 3106 | extern void k_work_poll_init(struct k_work_poll *work, |
| 3107 | k_work_handler_t handler); |
| 3108 | |
| 3109 | /** |
| 3110 | * @brief Submit a triggered work item. |
| 3111 | * |
| 3112 | * This routine schedules work item @a work to be processed by workqueue |
| 3113 | * @a work_q when one of the given @a events is signaled. The routine |
| 3114 | * initiates internal poller for the work item and then returns to the caller. |
| 3115 | * Only when one of the watched events happen the work item is actually |
| 3116 | * submitted to the workqueue and becomes pending. |
| 3117 | * |
| 3118 | * Submitting a previously submitted triggered work item that is still |
| 3119 | * waiting for the event cancels the existing submission and reschedules it |
| 3120 | * 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] | 3121 | * 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] | 3122 | * so care must be taken to synchronize such resubmissions externally. |
| 3123 | * |
| 3124 | * @note Can be called by ISRs. |
| 3125 | * |
| 3126 | * @warning |
| 3127 | * Provided array of events as well as a triggered work item must be placed |
| 3128 | * in persistent memory (valid until work handler execution or work |
| 3129 | * cancellation) and cannot be modified after submission. |
| 3130 | * |
| 3131 | * @param work_q Address of workqueue. |
| 3132 | * @param work Address of delayed work item. |
| 3133 | * @param events An array of pointers to events which trigger the work. |
| 3134 | * @param num_events The number of events in the array. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3135 | * @param timeout Non-negative timeout after which the work will be scheduled |
| 3136 | * for execution even if not triggered. |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 3137 | * |
| 3138 | * |
| 3139 | * @retval 0 Work item started watching for events. |
| 3140 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 3141 | * @retval -EADDRINUSE Work item is pending on a different workqueue. |
| 3142 | */ |
| 3143 | extern int k_work_poll_submit_to_queue(struct k_work_q *work_q, |
| 3144 | struct k_work_poll *work, |
| 3145 | struct k_poll_event *events, |
| 3146 | int num_events, |
| 3147 | s32_t timeout); |
| 3148 | |
| 3149 | /** |
| 3150 | * @brief Submit a triggered work item to the system workqueue. |
| 3151 | * |
| 3152 | * This routine schedules work item @a work to be processed by system |
| 3153 | * workqueue when one of the given @a events is signaled. The routine |
| 3154 | * initiates internal poller for the work item and then returns to the caller. |
| 3155 | * Only when one of the watched events happen the work item is actually |
| 3156 | * submitted to the workqueue and becomes pending. |
| 3157 | * |
| 3158 | * Submitting a previously submitted triggered work item that is still |
| 3159 | * waiting for the event cancels the existing submission and reschedules it |
| 3160 | * 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] | 3161 | * 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] | 3162 | * so care must be taken to synchronize such resubmissions externally. |
| 3163 | * |
| 3164 | * @note Can be called by ISRs. |
| 3165 | * |
| 3166 | * @warning |
| 3167 | * Provided array of events as well as a triggered work item must not be |
| 3168 | * modified until the item has been processed by the workqueue. |
| 3169 | * |
| 3170 | * @param work Address of delayed work item. |
| 3171 | * @param events An array of pointers to events which trigger the work. |
| 3172 | * @param num_events The number of events in the array. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3173 | * @param timeout Non-negative timeout after which the work will be scheduled |
| 3174 | * for execution even if not triggered. |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 3175 | * |
| 3176 | * @retval 0 Work item started watching for events. |
| 3177 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 3178 | * @retval -EADDRINUSE Work item is pending on a different workqueue. |
| 3179 | */ |
| 3180 | static inline int k_work_poll_submit(struct k_work_poll *work, |
| 3181 | struct k_poll_event *events, |
| 3182 | int num_events, |
| 3183 | s32_t timeout) |
| 3184 | { |
| 3185 | return k_work_poll_submit_to_queue(&k_sys_work_q, work, |
| 3186 | events, num_events, timeout); |
| 3187 | } |
| 3188 | |
| 3189 | /** |
| 3190 | * @brief Cancel a triggered work item. |
| 3191 | * |
| 3192 | * This routine cancels the submission of triggered work item @a work. |
| 3193 | * A triggered work item can only be canceled if no event triggered work |
| 3194 | * submission. |
| 3195 | * |
| 3196 | * @note Can be called by ISRs. |
| 3197 | * |
| 3198 | * @param work Address of delayed work item. |
| 3199 | * |
David B. Kinder | 73896c0 | 2019-10-28 16:27:57 -0700 | [diff] [blame] | 3200 | * @retval 0 Work item canceled. |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 3201 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 3202 | */ |
| 3203 | extern int k_work_poll_cancel(struct k_work_poll *work); |
| 3204 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 3205 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3206 | /** |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3207 | * @defgroup mutex_apis Mutex APIs |
| 3208 | * @ingroup kernel_apis |
| 3209 | * @{ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3210 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3211 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3212 | /** |
| 3213 | * Mutex Structure |
| 3214 | * @ingroup mutex_apis |
| 3215 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3216 | struct k_mutex { |
| 3217 | _wait_q_t wait_q; |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3218 | /** Mutex owner */ |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 3219 | struct k_thread *owner; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3220 | u32_t lock_count; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3221 | int owner_orig_prio; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3222 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 3223 | _OBJECT_TRACING_NEXT_PTR(k_mutex) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 3224 | _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3225 | }; |
| 3226 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3227 | /** |
| 3228 | * @cond INTERNAL_HIDDEN |
| 3229 | */ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3230 | #define _K_MUTEX_INITIALIZER(obj) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3231 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3232 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3233 | .owner = NULL, \ |
| 3234 | .lock_count = 0, \ |
| 3235 | .owner_orig_prio = K_LOWEST_THREAD_PRIO, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 3236 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3237 | } |
| 3238 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 3239 | #define K_MUTEX_INITIALIZER __DEPRECATED_MACRO _K_MUTEX_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3240 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3241 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3242 | * INTERNAL_HIDDEN @endcond |
| 3243 | */ |
| 3244 | |
| 3245 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3246 | * @brief Statically define and initialize a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3247 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3248 | * 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] | 3249 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 3250 | * @code extern struct k_mutex <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3251 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3252 | * @param name Name of the mutex. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3253 | * @req K-MUTEX-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3254 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3255 | #define K_MUTEX_DEFINE(name) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 3256 | Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3257 | _K_MUTEX_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3258 | |
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 | * @brief Initialize a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3261 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3262 | * This routine initializes a mutex object, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3263 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3264 | * Upon completion, the mutex is available and does not have an owner. |
| 3265 | * |
| 3266 | * @param mutex Address of the mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3267 | * |
| 3268 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3269 | * @req K-MUTEX-002 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3270 | */ |
Andrew Boie | 2f7519b | 2017-09-29 03:33:06 -0700 | [diff] [blame] | 3271 | __syscall void k_mutex_init(struct k_mutex *mutex); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3272 | |
| 3273 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3274 | * @brief Lock a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3275 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3276 | * This routine locks @a mutex. If the mutex is locked by another thread, |
| 3277 | * the calling thread waits until the mutex becomes available or until |
| 3278 | * a timeout occurs. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3279 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3280 | * A thread is permitted to lock a mutex it has already locked. The operation |
| 3281 | * completes immediately and the lock count is increased by 1. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3282 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3283 | * @param mutex Address of the mutex. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3284 | * @param timeout Non-negative waiting period to lock the mutex (in |
| 3285 | * milliseconds), or one of the special values K_NO_WAIT and |
| 3286 | * K_FOREVER. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3287 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3288 | * @retval 0 Mutex locked. |
| 3289 | * @retval -EBUSY Returned without waiting. |
| 3290 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3291 | * @req K-MUTEX-002 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3292 | */ |
Andrew Boie | 2f7519b | 2017-09-29 03:33:06 -0700 | [diff] [blame] | 3293 | __syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3294 | |
| 3295 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3296 | * @brief Unlock a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3297 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3298 | * This routine unlocks @a mutex. The mutex must already be locked by the |
| 3299 | * calling thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3300 | * |
| 3301 | * 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] | 3302 | * 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] | 3303 | * thread. |
| 3304 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3305 | * @param mutex Address of the mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3306 | * |
| 3307 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3308 | * @req K-MUTEX-002 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3309 | */ |
Andrew Boie | 2f7519b | 2017-09-29 03:33:06 -0700 | [diff] [blame] | 3310 | __syscall void k_mutex_unlock(struct k_mutex *mutex); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3311 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3312 | /** |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 3313 | * @} |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3314 | */ |
| 3315 | |
| 3316 | /** |
| 3317 | * @cond INTERNAL_HIDDEN |
| 3318 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3319 | |
| 3320 | struct k_sem { |
| 3321 | _wait_q_t wait_q; |
Adithya Baglody | 4b06621 | 2018-10-16 11:59:12 +0530 | [diff] [blame] | 3322 | u32_t count; |
| 3323 | u32_t limit; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 3324 | _POLL_EVENT; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3325 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 3326 | _OBJECT_TRACING_NEXT_PTR(k_sem) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 3327 | _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3328 | }; |
| 3329 | |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 3330 | #define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3331 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3332 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3333 | .count = initial_count, \ |
| 3334 | .limit = count_limit, \ |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 3335 | _POLL_EVENT_OBJ_INIT(obj) \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 3336 | _OBJECT_TRACING_INIT \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3337 | } |
| 3338 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 3339 | #define K_SEM_INITIALIZER __DEPRECATED_MACRO Z_SEM_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3340 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3341 | /** |
| 3342 | * INTERNAL_HIDDEN @endcond |
| 3343 | */ |
| 3344 | |
| 3345 | /** |
| 3346 | * @defgroup semaphore_apis Semaphore APIs |
| 3347 | * @ingroup kernel_apis |
| 3348 | * @{ |
| 3349 | */ |
| 3350 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3351 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3352 | * @brief Initialize a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3353 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3354 | * This routine initializes a semaphore object, prior to its first use. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3355 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3356 | * @param sem Address of the semaphore. |
| 3357 | * @param initial_count Initial semaphore count. |
| 3358 | * @param limit Maximum permitted semaphore count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3359 | * |
| 3360 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3361 | * @req K-SEM-001 |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3362 | */ |
Andrew Boie | 9928023 | 2017-09-29 14:17:47 -0700 | [diff] [blame] | 3363 | __syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count, |
| 3364 | unsigned int limit); |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3365 | |
| 3366 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3367 | * @brief Take a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3368 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3369 | * This routine takes @a sem. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3370 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3371 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 3372 | * |
| 3373 | * @param sem Address of the semaphore. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3374 | * @param timeout Non-negative waiting period to take the semaphore (in |
| 3375 | * milliseconds), or one of the special values K_NO_WAIT and |
| 3376 | * K_FOREVER. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3377 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3378 | * @retval 0 Semaphore taken. |
| 3379 | * @retval -EBUSY Returned without waiting. |
| 3380 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3381 | * @req K-SEM-001 |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3382 | */ |
Andrew Boie | 9928023 | 2017-09-29 14:17:47 -0700 | [diff] [blame] | 3383 | __syscall int k_sem_take(struct k_sem *sem, s32_t timeout); |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3384 | |
| 3385 | /** |
| 3386 | * @brief Give a semaphore. |
| 3387 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3388 | * This routine gives @a sem, unless the semaphore is already at its maximum |
| 3389 | * permitted count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3390 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3391 | * @note Can be called by ISRs. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3392 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3393 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3394 | * |
| 3395 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3396 | * @req K-SEM-001 |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3397 | */ |
Andrew Boie | 9928023 | 2017-09-29 14:17:47 -0700 | [diff] [blame] | 3398 | __syscall void k_sem_give(struct k_sem *sem); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3399 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3400 | /** |
| 3401 | * @brief Reset a semaphore's count to zero. |
| 3402 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3403 | * This routine sets the count of @a sem to zero. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3404 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3405 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3406 | * |
| 3407 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3408 | * @req K-SEM-001 |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3409 | */ |
Andrew Boie | 990bf16 | 2017-10-03 12:36:49 -0700 | [diff] [blame] | 3410 | __syscall void k_sem_reset(struct k_sem *sem); |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 3411 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 3412 | /** |
| 3413 | * @internal |
| 3414 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3415 | static inline void z_impl_k_sem_reset(struct k_sem *sem) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3416 | { |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 3417 | sem->count = 0U; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3418 | } |
| 3419 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3420 | /** |
| 3421 | * @brief Get a semaphore's count. |
| 3422 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3423 | * This routine returns the current count of @a sem. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3424 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3425 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3426 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3427 | * @return Current semaphore count. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3428 | * @req K-SEM-001 |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3429 | */ |
Andrew Boie | 990bf16 | 2017-10-03 12:36:49 -0700 | [diff] [blame] | 3430 | __syscall unsigned int k_sem_count_get(struct k_sem *sem); |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 3431 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 3432 | /** |
| 3433 | * @internal |
| 3434 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3435 | 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] | 3436 | { |
| 3437 | return sem->count; |
| 3438 | } |
| 3439 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3440 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3441 | * @brief Statically define and initialize a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3442 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3443 | * 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] | 3444 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 3445 | * @code extern struct k_sem <name>; @endcode |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3446 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3447 | * @param name Name of the semaphore. |
| 3448 | * @param initial_count Initial semaphore count. |
| 3449 | * @param count_limit Maximum permitted semaphore count. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3450 | * @req K-SEM-002 |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 3451 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3452 | #define K_SEM_DEFINE(name, initial_count, count_limit) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 3453 | Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \ |
Patrik Flykt | 97b3bd1 | 2019-03-12 15:15:42 -0600 | [diff] [blame] | 3454 | Z_SEM_INITIALIZER(name, initial_count, count_limit); \ |
Rajavardhan Gundi | 68040c8 | 2018-04-27 10:15:15 +0530 | [diff] [blame] | 3455 | BUILD_ASSERT(((count_limit) != 0) && \ |
| 3456 | ((initial_count) <= (count_limit))); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3457 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 3458 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3459 | |
| 3460 | /** |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3461 | * @defgroup msgq_apis Message Queue APIs |
| 3462 | * @ingroup kernel_apis |
| 3463 | * @{ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3464 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3465 | |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3466 | /** |
| 3467 | * @brief Message Queue Structure |
| 3468 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3469 | struct k_msgq { |
| 3470 | _wait_q_t wait_q; |
Andy Ross | be03dbd | 2018-07-26 10:23:02 -0700 | [diff] [blame] | 3471 | struct k_spinlock lock; |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 3472 | size_t msg_size; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3473 | u32_t max_msgs; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3474 | char *buffer_start; |
| 3475 | char *buffer_end; |
| 3476 | char *read_ptr; |
| 3477 | char *write_ptr; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3478 | u32_t used_msgs; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3479 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 3480 | _OBJECT_TRACING_NEXT_PTR(k_msgq) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 3481 | _OBJECT_TRACING_LINKED_FLAG |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 3482 | u8_t flags; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3483 | }; |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3484 | /** |
| 3485 | * @cond INTERNAL_HIDDEN |
| 3486 | */ |
| 3487 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3488 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3489 | #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] | 3490 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3491 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3492 | .msg_size = q_msg_size, \ |
Charles E. Youse | 6d01f67 | 2019-03-18 10:27:34 -0700 | [diff] [blame] | 3493 | .max_msgs = q_max_msgs, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3494 | .buffer_start = q_buffer, \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3495 | .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3496 | .read_ptr = q_buffer, \ |
| 3497 | .write_ptr = q_buffer, \ |
| 3498 | .used_msgs = 0, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 3499 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3500 | } |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 3501 | #define K_MSGQ_INITIALIZER __DEPRECATED_MACRO _K_MSGQ_INITIALIZER |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3502 | /** |
| 3503 | * INTERNAL_HIDDEN @endcond |
| 3504 | */ |
| 3505 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3506 | |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 3507 | #define K_MSGQ_FLAG_ALLOC BIT(0) |
| 3508 | |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3509 | /** |
| 3510 | * @brief Message Queue Attributes |
| 3511 | */ |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 3512 | struct k_msgq_attrs { |
| 3513 | size_t msg_size; |
| 3514 | u32_t max_msgs; |
| 3515 | u32_t used_msgs; |
| 3516 | }; |
| 3517 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3518 | |
| 3519 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3520 | * @brief Statically define and initialize a message queue. |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3521 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3522 | * The message queue's ring buffer contains space for @a q_max_msgs messages, |
| 3523 | * 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] | 3524 | * @a q_align -byte boundary, which must be a power of 2. To ensure that each |
| 3525 | * message is similarly aligned to this boundary, @a q_msg_size must also be |
| 3526 | * a multiple of @a q_align. |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3527 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3528 | * The message queue can be accessed outside the module where it is defined |
| 3529 | * using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3530 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 3531 | * @code extern struct k_msgq <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3532 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3533 | * @param q_name Name of the message queue. |
| 3534 | * @param q_msg_size Message size (in bytes). |
| 3535 | * @param q_max_msgs Maximum number of messages that can be queued. |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 3536 | * @param q_align Alignment of the message queue's ring buffer. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3537 | * |
| 3538 | * @req K-MSGQ-001 |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3539 | */ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 3540 | #define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \ |
| 3541 | static char __noinit __aligned(q_align) \ |
| 3542 | _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \ |
| 3543 | Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \ |
| 3544 | _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 3545 | q_msg_size, q_max_msgs) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3546 | |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3547 | /** |
| 3548 | * @brief Initialize a message queue. |
| 3549 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3550 | * This routine initializes a message queue object, prior to its first use. |
| 3551 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 3552 | * The message queue's ring buffer must contain space for @a max_msgs messages, |
| 3553 | * each of which is @a msg_size bytes long. The buffer must be aligned to an |
| 3554 | * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure |
| 3555 | * that each message is similarly aligned to this boundary, @a q_msg_size |
| 3556 | * must also be a multiple of N. |
| 3557 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3558 | * @param q Address of the message queue. |
| 3559 | * @param buffer Pointer to ring buffer that holds queued messages. |
| 3560 | * @param msg_size Message size (in bytes). |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3561 | * @param max_msgs Maximum number of messages that can be queued. |
| 3562 | * |
| 3563 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3564 | * @req K-MSGQ-002 |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3565 | */ |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 3566 | void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size, |
| 3567 | u32_t max_msgs); |
| 3568 | |
| 3569 | /** |
| 3570 | * @brief Initialize a message queue. |
| 3571 | * |
| 3572 | * This routine initializes a message queue object, prior to its first use, |
| 3573 | * allocating its internal ring buffer from the calling thread's resource |
| 3574 | * pool. |
| 3575 | * |
| 3576 | * Memory allocated for the ring buffer can be released by calling |
| 3577 | * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses |
| 3578 | * all of its references. |
| 3579 | * |
| 3580 | * @param q Address of the message queue. |
| 3581 | * @param msg_size Message size (in bytes). |
| 3582 | * @param max_msgs Maximum number of messages that can be queued. |
| 3583 | * |
| 3584 | * @return 0 on success, -ENOMEM if there was insufficient memory in the |
| 3585 | * thread's resource pool, or -EINVAL if the size parameters cause |
| 3586 | * an integer overflow. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3587 | * @req K-MSGQ-002 |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 3588 | */ |
| 3589 | __syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size, |
| 3590 | u32_t max_msgs); |
| 3591 | |
| 3592 | |
| 3593 | void k_msgq_cleanup(struct k_msgq *q); |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3594 | |
| 3595 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3596 | * @brief Send a message to a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3597 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3598 | * This routine sends a message to message queue @a q. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3599 | * |
Benjamin Walsh | 8215ce1 | 2016-11-09 19:45:19 -0500 | [diff] [blame] | 3600 | * @note Can be called by ISRs. |
| 3601 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3602 | * @param q Address of the message queue. |
| 3603 | * @param data Pointer to the message. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3604 | * @param timeout Non-negative waiting period to add the message (in |
| 3605 | * milliseconds), or one of the special values K_NO_WAIT and |
| 3606 | * K_FOREVER. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3607 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3608 | * @retval 0 Message sent. |
| 3609 | * @retval -ENOMSG Returned without waiting or queue purged. |
| 3610 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3611 | * @req K-MSGQ-002 |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3612 | */ |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 3613 | __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] | 3614 | |
| 3615 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3616 | * @brief Receive a message from a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3617 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3618 | * This routine receives a message from message queue @a q in a "first in, |
| 3619 | * first out" manner. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3620 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3621 | * @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] | 3622 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3623 | * @param q Address of the message queue. |
| 3624 | * @param data Address of area to hold the received message. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3625 | * @param timeout Non-negative waiting period to receive the message (in |
| 3626 | * milliseconds), or one of the special values K_NO_WAIT and |
| 3627 | * K_FOREVER. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3628 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3629 | * @retval 0 Message received. |
| 3630 | * @retval -ENOMSG Returned without waiting. |
| 3631 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3632 | * @req K-MSGQ-002 |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3633 | */ |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 3634 | __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] | 3635 | |
| 3636 | /** |
Sathish Kuttan | 3efd8e1 | 2018-11-09 21:03:10 -0800 | [diff] [blame] | 3637 | * @brief Peek/read a message from a message queue. |
| 3638 | * |
| 3639 | * This routine reads a message from message queue @a q in a "first in, |
| 3640 | * first out" manner and leaves the message in the queue. |
| 3641 | * |
| 3642 | * @note Can be called by ISRs. |
| 3643 | * |
| 3644 | * @param q Address of the message queue. |
| 3645 | * @param data Address of area to hold the message read from the queue. |
| 3646 | * |
| 3647 | * @retval 0 Message read. |
| 3648 | * @retval -ENOMSG Returned when the queue has no message. |
| 3649 | * @req K-MSGQ-002 |
| 3650 | */ |
| 3651 | __syscall int k_msgq_peek(struct k_msgq *q, void *data); |
| 3652 | |
| 3653 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3654 | * @brief Purge a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3655 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3656 | * This routine discards all unreceived messages in a message queue's ring |
| 3657 | * buffer. Any threads that are blocked waiting to send a message to the |
| 3658 | * message queue are unblocked and see an -ENOMSG error code. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3659 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3660 | * @param q Address of the message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3661 | * |
| 3662 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3663 | * @req K-MSGQ-002 |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3664 | */ |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 3665 | __syscall void k_msgq_purge(struct k_msgq *q); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3666 | |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 3667 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3668 | * @brief Get the amount of free space in a message queue. |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 3669 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3670 | * This routine returns the number of unused entries in a message queue's |
| 3671 | * ring buffer. |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 3672 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3673 | * @param q Address of the message queue. |
| 3674 | * |
| 3675 | * @return Number of unused ring buffer entries. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3676 | * @req K-MSGQ-002 |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 3677 | */ |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 3678 | __syscall u32_t k_msgq_num_free_get(struct k_msgq *q); |
| 3679 | |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 3680 | /** |
| 3681 | * @brief Get basic attributes of a message queue. |
| 3682 | * |
| 3683 | * This routine fetches basic attributes of message queue into attr argument. |
| 3684 | * |
| 3685 | * @param q Address of the message queue. |
| 3686 | * @param attrs pointer to message queue attribute structure. |
| 3687 | * |
| 3688 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3689 | * @req K-MSGQ-003 |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 3690 | */ |
| 3691 | __syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs); |
| 3692 | |
| 3693 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3694 | 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] | 3695 | { |
| 3696 | return q->max_msgs - q->used_msgs; |
| 3697 | } |
| 3698 | |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3699 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3700 | * @brief Get the number of messages in a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3701 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3702 | * 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] | 3703 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3704 | * @param q Address of the message queue. |
| 3705 | * |
| 3706 | * @return Number of messages. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3707 | * @req K-MSGQ-002 |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 3708 | */ |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 3709 | __syscall u32_t k_msgq_num_used_get(struct k_msgq *q); |
| 3710 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3711 | 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] | 3712 | { |
| 3713 | return q->used_msgs; |
| 3714 | } |
| 3715 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 3716 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3717 | |
| 3718 | /** |
| 3719 | * @defgroup mem_pool_apis Memory Pool APIs |
| 3720 | * @ingroup kernel_apis |
| 3721 | * @{ |
| 3722 | */ |
| 3723 | |
Andy Ross | 73cb958 | 2017-05-09 10:42:39 -0700 | [diff] [blame] | 3724 | /* Note on sizing: the use of a 20 bit field for block means that, |
| 3725 | * assuming a reasonable minimum block size of 16 bytes, we're limited |
| 3726 | * to 16M of memory managed by a single pool. Long term it would be |
| 3727 | * good to move to a variable bit size based on configuration. |
| 3728 | */ |
| 3729 | struct k_mem_block_id { |
| 3730 | u32_t pool : 8; |
| 3731 | u32_t level : 4; |
| 3732 | u32_t block : 20; |
| 3733 | }; |
| 3734 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3735 | struct k_mem_block { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3736 | void *data; |
Andy Ross | 73cb958 | 2017-05-09 10:42:39 -0700 | [diff] [blame] | 3737 | struct k_mem_block_id id; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3738 | }; |
| 3739 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 3740 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3741 | |
| 3742 | /** |
| 3743 | * @defgroup mailbox_apis Mailbox APIs |
| 3744 | * @ingroup kernel_apis |
| 3745 | * @{ |
| 3746 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3747 | |
| 3748 | struct k_mbox_msg { |
| 3749 | /** internal use only - needed for legacy API support */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3750 | u32_t _mailbox; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3751 | /** size of message (in bytes) */ |
Peter Mitsis | d93078c | 2016-10-14 12:59:37 -0400 | [diff] [blame] | 3752 | size_t size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3753 | /** application-defined information value */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3754 | u32_t info; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3755 | /** sender's message data buffer */ |
| 3756 | void *tx_data; |
| 3757 | /** internal use only - needed for legacy API support */ |
| 3758 | void *_rx_data; |
| 3759 | /** message data block descriptor */ |
| 3760 | struct k_mem_block tx_block; |
| 3761 | /** source thread id */ |
| 3762 | k_tid_t rx_source_thread; |
| 3763 | /** target thread id */ |
| 3764 | k_tid_t tx_target_thread; |
| 3765 | /** internal use only - thread waiting on send (may be a dummy) */ |
| 3766 | k_tid_t _syncing_thread; |
| 3767 | #if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0) |
| 3768 | /** internal use only - semaphore used during asynchronous send */ |
| 3769 | struct k_sem *_async_sem; |
| 3770 | #endif |
| 3771 | }; |
| 3772 | |
| 3773 | struct k_mbox { |
| 3774 | _wait_q_t tx_msg_queue; |
| 3775 | _wait_q_t rx_msg_queue; |
Andy Ross | 9eeb6b8 | 2018-07-25 15:06:24 -0700 | [diff] [blame] | 3776 | struct k_spinlock lock; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3777 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 3778 | _OBJECT_TRACING_NEXT_PTR(k_mbox) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 3779 | _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3780 | }; |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3781 | /** |
| 3782 | * @cond INTERNAL_HIDDEN |
| 3783 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3784 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3785 | #define _K_MBOX_INITIALIZER(obj) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3786 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3787 | .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \ |
| 3788 | .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 3789 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3790 | } |
| 3791 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 3792 | #define K_MBOX_INITIALIZER __DEPRECATED_MACRO _K_MBOX_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3793 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3794 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3795 | * INTERNAL_HIDDEN @endcond |
| 3796 | */ |
| 3797 | |
| 3798 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3799 | * @brief Statically define and initialize a mailbox. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3800 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3801 | * 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] | 3802 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 3803 | * @code extern struct k_mbox <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3804 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3805 | * @param name Name of the mailbox. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3806 | * @req K-MBOX-001 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3807 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3808 | #define K_MBOX_DEFINE(name) \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 3809 | Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3810 | _K_MBOX_INITIALIZER(name) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3811 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3812 | /** |
| 3813 | * @brief Initialize a mailbox. |
| 3814 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3815 | * This routine initializes a mailbox object, prior to its first use. |
| 3816 | * |
| 3817 | * @param mbox Address of the mailbox. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3818 | * |
| 3819 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3820 | * @req K-MBOX-002 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3821 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3822 | extern void k_mbox_init(struct k_mbox *mbox); |
| 3823 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3824 | /** |
| 3825 | * @brief Send a mailbox message in a synchronous manner. |
| 3826 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3827 | * This routine sends a message to @a mbox and waits for a receiver to both |
| 3828 | * receive and process it. The message data may be in a buffer, in a memory |
| 3829 | * pool block, or non-existent (i.e. an empty message). |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3830 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3831 | * @param mbox Address of the mailbox. |
| 3832 | * @param tx_msg Address of the transmit message descriptor. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3833 | * @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] | 3834 | * milliseconds), or one of the special values K_NO_WAIT |
| 3835 | * and K_FOREVER. Once the message has been received, |
| 3836 | * this routine waits as long as necessary for the message |
| 3837 | * to be completely processed. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3838 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3839 | * @retval 0 Message sent. |
| 3840 | * @retval -ENOMSG Returned without waiting. |
| 3841 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3842 | * @req K-MBOX-002 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3843 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 3844 | 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] | 3845 | s32_t timeout); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3846 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3847 | /** |
| 3848 | * @brief Send a mailbox message in an asynchronous manner. |
| 3849 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3850 | * This routine sends a message to @a mbox without waiting for a receiver |
| 3851 | * to process it. The message data may be in a buffer, in a memory pool block, |
| 3852 | * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem |
| 3853 | * will be given when the message has been both received and completely |
| 3854 | * processed by the receiver. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3855 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3856 | * @param mbox Address of the mailbox. |
| 3857 | * @param tx_msg Address of the transmit message descriptor. |
| 3858 | * @param sem Address of a semaphore, or NULL if none is needed. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3859 | * |
| 3860 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3861 | * @req K-MBOX-002 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3862 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 3863 | 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] | 3864 | struct k_sem *sem); |
| 3865 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3866 | /** |
| 3867 | * @brief Receive a mailbox message. |
| 3868 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3869 | * This routine receives a message from @a mbox, then optionally retrieves |
| 3870 | * its data and disposes of the message. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3871 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3872 | * @param mbox Address of the mailbox. |
| 3873 | * @param rx_msg Address of the receive message descriptor. |
| 3874 | * @param buffer Address of the buffer to receive data, or NULL to defer data |
| 3875 | * retrieval and message disposal until later. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3876 | * @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] | 3877 | * milliseconds), or one of the special values K_NO_WAIT |
| 3878 | * and K_FOREVER. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3879 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3880 | * @retval 0 Message received. |
| 3881 | * @retval -ENOMSG Returned without waiting. |
| 3882 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3883 | * @req K-MBOX-002 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3884 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 3885 | 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] | 3886 | void *buffer, s32_t timeout); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3887 | |
| 3888 | /** |
| 3889 | * @brief Retrieve mailbox message data into a buffer. |
| 3890 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3891 | * This routine completes the processing of a received message by retrieving |
| 3892 | * its data into a buffer, then disposing of the message. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3893 | * |
| 3894 | * Alternatively, this routine can be used to dispose of a received message |
| 3895 | * without retrieving its data. |
| 3896 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3897 | * @param rx_msg Address of the receive message descriptor. |
| 3898 | * @param buffer Address of the buffer to receive data, or NULL to discard |
| 3899 | * the data. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3900 | * |
| 3901 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3902 | * @req K-MBOX-002 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3903 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 3904 | 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] | 3905 | |
| 3906 | /** |
| 3907 | * @brief Retrieve mailbox message data into a memory pool block. |
| 3908 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3909 | * This routine completes the processing of a received message by retrieving |
| 3910 | * its data into a memory pool block, then disposing of the message. |
| 3911 | * The memory pool block that results from successful retrieval must be |
| 3912 | * returned to the pool once the data has been processed, even in cases |
| 3913 | * where zero bytes of data are retrieved. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3914 | * |
| 3915 | * Alternatively, this routine can be used to dispose of a received message |
| 3916 | * without retrieving its data. In this case there is no need to return a |
| 3917 | * memory pool block to the pool. |
| 3918 | * |
| 3919 | * This routine allocates a new memory pool block for the data only if the |
| 3920 | * data is not already in one. If a new block cannot be allocated, the routine |
| 3921 | * returns a failure code and the received message is left unchanged. This |
| 3922 | * permits the caller to reattempt data retrieval at a later time or to dispose |
| 3923 | * of the received message without retrieving its data. |
| 3924 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3925 | * @param rx_msg Address of a receive message descriptor. |
| 3926 | * @param pool Address of memory pool, or NULL to discard data. |
| 3927 | * @param block Address of the area to hold memory pool block info. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 3928 | * @param timeout Non-negative waiting period to wait for a memory pool block |
| 3929 | * (in milliseconds), or one of the special values K_NO_WAIT |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3930 | * and K_FOREVER. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3931 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3932 | * @retval 0 Data retrieved. |
| 3933 | * @retval -ENOMEM Returned without waiting. |
| 3934 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 3935 | * @req K-MBOX-002 |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 3936 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 3937 | 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] | 3938 | struct k_mem_pool *pool, |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 3939 | struct k_mem_block *block, s32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3940 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 3941 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3942 | |
| 3943 | /** |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3944 | * @defgroup pipe_apis Pipe APIs |
| 3945 | * @ingroup kernel_apis |
| 3946 | * @{ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3947 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3948 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3949 | /** Pipe Structure */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3950 | struct k_pipe { |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3951 | unsigned char *buffer; /**< Pipe buffer: may be NULL */ |
| 3952 | size_t size; /**< Buffer size */ |
| 3953 | size_t bytes_used; /**< # bytes used in buffer */ |
| 3954 | size_t read_index; /**< Where in buffer to read from */ |
| 3955 | size_t write_index; /**< Where in buffer to write */ |
Andy Ross | f582b55 | 2019-02-05 16:10:18 -0800 | [diff] [blame] | 3956 | struct k_spinlock lock; /**< Synchronization lock */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3957 | |
| 3958 | struct { |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3959 | _wait_q_t readers; /**< Reader wait queue */ |
| 3960 | _wait_q_t writers; /**< Writer wait queue */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3961 | } wait_q; |
| 3962 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 3963 | _OBJECT_TRACING_NEXT_PTR(k_pipe) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 3964 | _OBJECT_TRACING_LINKED_FLAG |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3965 | u8_t flags; /**< Flags */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3966 | }; |
| 3967 | |
Anas Nashif | ce78d16 | 2018-05-24 12:43:11 -0500 | [diff] [blame] | 3968 | /** |
| 3969 | * @cond INTERNAL_HIDDEN |
| 3970 | */ |
| 3971 | #define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */ |
| 3972 | |
Krzysztof Chruscinski | be06327 | 2019-02-13 11:19:54 +0100 | [diff] [blame] | 3973 | #define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \ |
| 3974 | { \ |
| 3975 | .buffer = pipe_buffer, \ |
| 3976 | .size = pipe_buffer_size, \ |
| 3977 | .bytes_used = 0, \ |
| 3978 | .read_index = 0, \ |
| 3979 | .write_index = 0, \ |
| 3980 | .lock = {}, \ |
| 3981 | .wait_q = { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 3982 | .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \ |
| 3983 | .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \ |
Krzysztof Chruscinski | be06327 | 2019-02-13 11:19:54 +0100 | [diff] [blame] | 3984 | }, \ |
| 3985 | _OBJECT_TRACING_INIT \ |
| 3986 | .flags = 0 \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3987 | } |
| 3988 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 3989 | #define K_PIPE_INITIALIZER __DEPRECATED_MACRO _K_PIPE_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 3990 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3991 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3992 | * INTERNAL_HIDDEN @endcond |
| 3993 | */ |
| 3994 | |
| 3995 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3996 | * @brief Statically define and initialize a pipe. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3997 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3998 | * 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] | 3999 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 4000 | * @code extern struct k_pipe <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4001 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4002 | * @param name Name of the pipe. |
| 4003 | * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes), |
| 4004 | * or zero if no ring buffer is used. |
| 4005 | * @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] | 4006 | * |
| 4007 | * @req K-PIPE-001 |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4008 | */ |
Andrew Boie | 44fe812 | 2018-04-12 17:38:12 -0700 | [diff] [blame] | 4009 | #define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \ |
Andrew Boie | 41f6011 | 2019-01-31 15:53:24 -0800 | [diff] [blame] | 4010 | static unsigned char __noinit __aligned(pipe_align) \ |
Andrew Boie | 44fe812 | 2018-04-12 17:38:12 -0700 | [diff] [blame] | 4011 | _k_pipe_buf_##name[pipe_buffer_size]; \ |
Nicolas Pitre | b1d3742 | 2019-06-03 10:51:32 -0400 | [diff] [blame] | 4012 | Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 4013 | _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4014 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4015 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4016 | * @brief Initialize a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4017 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4018 | * This routine initializes a pipe object, prior to its first use. |
| 4019 | * |
| 4020 | * @param pipe Address of the pipe. |
| 4021 | * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer |
| 4022 | * is used. |
| 4023 | * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring |
| 4024 | * buffer is used. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4025 | * |
| 4026 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4027 | * @req K-PIPE-002 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4028 | */ |
Andrew Boie | 44fe812 | 2018-04-12 17:38:12 -0700 | [diff] [blame] | 4029 | void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size); |
| 4030 | |
| 4031 | /** |
| 4032 | * @brief Release a pipe's allocated buffer |
| 4033 | * |
| 4034 | * If a pipe object was given a dynamically allocated buffer via |
| 4035 | * k_pipe_alloc_init(), this will free it. This function does nothing |
| 4036 | * if the buffer wasn't dynamically allocated. |
| 4037 | * |
| 4038 | * @param pipe Address of the pipe. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4039 | * @req K-PIPE-002 |
Andrew Boie | 44fe812 | 2018-04-12 17:38:12 -0700 | [diff] [blame] | 4040 | */ |
| 4041 | void k_pipe_cleanup(struct k_pipe *pipe); |
| 4042 | |
| 4043 | /** |
| 4044 | * @brief Initialize a pipe and allocate a buffer for it |
| 4045 | * |
| 4046 | * Storage for the buffer region will be allocated from the calling thread's |
| 4047 | * resource pool. This memory will be released if k_pipe_cleanup() is called, |
| 4048 | * or userspace is enabled and the pipe object loses all references to it. |
| 4049 | * |
| 4050 | * This function should only be called on uninitialized pipe objects. |
| 4051 | * |
| 4052 | * @param pipe Address of the pipe. |
| 4053 | * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring |
| 4054 | * buffer is used. |
| 4055 | * @retval 0 on success |
David B. Kinder | fcbd8fb | 2018-05-23 12:06:24 -0700 | [diff] [blame] | 4056 | * @retval -ENOMEM if memory couldn't be allocated |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4057 | * @req K-PIPE-002 |
Andrew Boie | 44fe812 | 2018-04-12 17:38:12 -0700 | [diff] [blame] | 4058 | */ |
| 4059 | __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] | 4060 | |
| 4061 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4062 | * @brief Write data to a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4063 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4064 | * 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] | 4065 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4066 | * @param pipe Address of the pipe. |
| 4067 | * @param data Address of data to write. |
| 4068 | * @param bytes_to_write Size of data (in bytes). |
| 4069 | * @param bytes_written Address of area to hold the number of bytes written. |
| 4070 | * @param min_xfer Minimum number of bytes to write. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 4071 | * @param timeout Non-negative waiting period to wait for the data to be written |
| 4072 | * (in milliseconds), or one of the special values K_NO_WAIT |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4073 | * and K_FOREVER. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4074 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4075 | * @retval 0 At least @a min_xfer bytes of data were written. |
| 4076 | * @retval -EIO Returned without waiting; zero data bytes were written. |
| 4077 | * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4078 | * minus one data bytes were written. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4079 | * @req K-PIPE-002 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4080 | */ |
Andrew Boie | b9a0578 | 2017-09-29 16:05:32 -0700 | [diff] [blame] | 4081 | __syscall int k_pipe_put(struct k_pipe *pipe, void *data, |
| 4082 | size_t bytes_to_write, size_t *bytes_written, |
| 4083 | size_t min_xfer, s32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4084 | |
| 4085 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4086 | * @brief Read data from a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4087 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4088 | * 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] | 4089 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4090 | * @param pipe Address of the pipe. |
| 4091 | * @param data Address to place the data read from pipe. |
| 4092 | * @param bytes_to_read Maximum number of data bytes to read. |
| 4093 | * @param bytes_read Address of area to hold the number of bytes read. |
| 4094 | * @param min_xfer Minimum number of data bytes to read. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 4095 | * @param timeout Non-negative waiting period to wait for the data to be read |
| 4096 | * (in milliseconds), or one of the special values K_NO_WAIT |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4097 | * and K_FOREVER. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4098 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4099 | * @retval 0 At least @a min_xfer bytes of data were read. |
| 4100 | * @retval -EIO Returned without waiting; zero data bytes were read. |
| 4101 | * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4102 | * minus one data bytes were read. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4103 | * @req K-PIPE-002 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4104 | */ |
Andrew Boie | b9a0578 | 2017-09-29 16:05:32 -0700 | [diff] [blame] | 4105 | __syscall int k_pipe_get(struct k_pipe *pipe, void *data, |
| 4106 | size_t bytes_to_read, size_t *bytes_read, |
| 4107 | size_t min_xfer, s32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4108 | |
| 4109 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4110 | * @brief Write memory block to a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4111 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4112 | * This routine writes the data contained in a memory block to @a pipe. |
| 4113 | * 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] | 4114 | * 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] | 4115 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4116 | * @param pipe Address of the pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4117 | * @param block Memory block containing data to send |
| 4118 | * @param size Number of data bytes in memory block to send |
| 4119 | * @param sem Semaphore to signal upon completion (else NULL) |
| 4120 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4121 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4122 | * @req K-PIPE-002 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4123 | */ |
| 4124 | extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block, |
| 4125 | size_t size, struct k_sem *sem); |
| 4126 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4127 | /** @} */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4128 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4129 | /** |
| 4130 | * @cond INTERNAL_HIDDEN |
| 4131 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4132 | |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4133 | struct k_mem_slab { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4134 | _wait_q_t wait_q; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4135 | u32_t num_blocks; |
Peter Mitsis | fb02d57 | 2016-10-13 16:55:45 -0400 | [diff] [blame] | 4136 | size_t block_size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4137 | char *buffer; |
| 4138 | char *free_list; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4139 | u32_t num_used; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4140 | |
Flavio Ceolin | d1ed336 | 2018-12-07 11:39:13 -0800 | [diff] [blame] | 4141 | _OBJECT_TRACING_NEXT_PTR(k_mem_slab) |
Shih-Wei Teng | 5ebceeb | 2019-10-08 14:37:47 +0800 | [diff] [blame] | 4142 | _OBJECT_TRACING_LINKED_FLAG |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4143 | }; |
| 4144 | |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 4145 | #define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4146 | slab_num_blocks) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4147 | { \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4148 | .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4149 | .num_blocks = slab_num_blocks, \ |
| 4150 | .block_size = slab_block_size, \ |
| 4151 | .buffer = slab_buffer, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4152 | .free_list = NULL, \ |
| 4153 | .num_used = 0, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 4154 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4155 | } |
| 4156 | |
Peter A. Bigot | b4d5e67 | 2019-11-02 10:35:08 -0500 | [diff] [blame] | 4157 | #define K_MEM_SLAB_INITIALIZER __DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 4158 | |
| 4159 | |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 4160 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4161 | * INTERNAL_HIDDEN @endcond |
| 4162 | */ |
| 4163 | |
| 4164 | /** |
| 4165 | * @defgroup mem_slab_apis Memory Slab APIs |
| 4166 | * @ingroup kernel_apis |
| 4167 | * @{ |
| 4168 | */ |
| 4169 | |
| 4170 | /** |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 4171 | * @brief Statically define and initialize a memory slab. |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 4172 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 4173 | * The memory slab's buffer contains @a slab_num_blocks memory blocks |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4174 | * 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] | 4175 | * @a slab_align -byte boundary. To ensure that each memory block is similarly |
| 4176 | * 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] | 4177 | * @a slab_align. |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 4178 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 4179 | * 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] | 4180 | * using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4181 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 4182 | * @code extern struct k_mem_slab <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4183 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4184 | * @param name Name of the memory slab. |
| 4185 | * @param slab_block_size Size of each memory block (in bytes). |
| 4186 | * @param slab_num_blocks Number memory blocks. |
| 4187 | * @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] | 4188 | * @req K-MSLAB-001 |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 4189 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4190 | #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] | 4191 | char __noinit __aligned(WB_UP(slab_align)) \ |
| 4192 | _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] | 4193 | Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \ |
Andrew Boie | 65a9d2a | 2017-06-27 10:51:23 -0700 | [diff] [blame] | 4194 | _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \ |
Nicolas Pitre | 46cd5a0 | 2019-05-21 21:40:38 -0400 | [diff] [blame] | 4195 | WB_UP(slab_block_size), slab_num_blocks) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4196 | |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4197 | /** |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4198 | * @brief Initialize a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4199 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4200 | * Initializes a memory slab, prior to its first use. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4201 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 4202 | * The memory slab's buffer contains @a slab_num_blocks memory blocks |
| 4203 | * 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] | 4204 | * N-byte boundary matching a word boundary, where N is a power of 2 |
| 4205 | * (i.e. 4 on 32-bit systems, 8, 16, ...). |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 4206 | * To ensure that each memory block is similarly aligned to this boundary, |
| 4207 | * @a slab_block_size must also be a multiple of N. |
| 4208 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4209 | * @param slab Address of the memory slab. |
| 4210 | * @param buffer Pointer to buffer used for the memory blocks. |
| 4211 | * @param block_size Size of each memory block (in bytes). |
| 4212 | * @param num_blocks Number of memory blocks. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4213 | * |
| 4214 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4215 | * @req K-MSLAB-002 |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4216 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4217 | 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] | 4218 | size_t block_size, u32_t num_blocks); |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4219 | |
| 4220 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4221 | * @brief Allocate memory from a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4222 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4223 | * This routine allocates a memory block from a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4224 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4225 | * @param slab Address of the memory slab. |
| 4226 | * @param mem Pointer to block address area. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 4227 | * @param timeout Non-negative waiting period to wait for operation to complete |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4228 | * (in milliseconds). Use K_NO_WAIT to return without waiting, |
| 4229 | * or K_FOREVER to wait as long as necessary. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4230 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4231 | * @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] | 4232 | * is set to the starting address of the memory block. |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4233 | * @retval -ENOMEM Returned without waiting. |
| 4234 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4235 | * @req K-MSLAB-002 |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4236 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4237 | 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] | 4238 | s32_t timeout); |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4239 | |
| 4240 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4241 | * @brief Free memory allocated from a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4242 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4243 | * This routine releases a previously allocated memory block back to its |
| 4244 | * associated memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4245 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4246 | * @param slab Address of the memory slab. |
| 4247 | * @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] | 4248 | * |
| 4249 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4250 | * @req K-MSLAB-002 |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4251 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4252 | 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] | 4253 | |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4254 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4255 | * @brief Get the number of used blocks in a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4256 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4257 | * This routine gets the number of memory blocks that are currently |
| 4258 | * allocated in @a slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4259 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4260 | * @param slab Address of the memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4261 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4262 | * @return Number of allocated memory blocks. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4263 | * @req K-MSLAB-002 |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 4264 | */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4265 | 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] | 4266 | { |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4267 | return slab->num_used; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4268 | } |
| 4269 | |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4270 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4271 | * @brief Get the number of unused blocks in a memory slab. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4272 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4273 | * This routine gets the number of memory blocks that are currently |
| 4274 | * unallocated in @a slab. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4275 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4276 | * @param slab Address of the memory slab. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4277 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4278 | * @return Number of unallocated memory blocks. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4279 | * @req K-MSLAB-002 |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4280 | */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4281 | 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] | 4282 | { |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 4283 | return slab->num_blocks - slab->num_used; |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 4284 | } |
| 4285 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4286 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4287 | |
| 4288 | /** |
| 4289 | * @cond INTERNAL_HIDDEN |
| 4290 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4291 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 4292 | struct k_mem_pool { |
Andrew Boie | aa6de29 | 2018-03-06 17:12:37 -0800 | [diff] [blame] | 4293 | struct sys_mem_pool_base base; |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 4294 | _wait_q_t wait_q; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4295 | }; |
| 4296 | |
Dmitriy Korovkin | 0741467 | 2016-11-03 13:35:42 -0400 | [diff] [blame] | 4297 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4298 | * INTERNAL_HIDDEN @endcond |
Dmitriy Korovkin | 0741467 | 2016-11-03 13:35:42 -0400 | [diff] [blame] | 4299 | */ |
| 4300 | |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 4301 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4302 | * @addtogroup mem_pool_apis |
| 4303 | * @{ |
| 4304 | */ |
| 4305 | |
| 4306 | /** |
| 4307 | * @brief Statically define and initialize a memory pool. |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 4308 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4309 | * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes |
| 4310 | * long. The memory pool allows blocks to be repeatedly partitioned into |
| 4311 | * 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] | 4312 | * to a @a align -byte boundary. |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 4313 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4314 | * If the pool is to be accessed outside the module where it is defined, it |
| 4315 | * can be declared via |
| 4316 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 4317 | * @code extern struct k_mem_pool <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 4318 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4319 | * @param name Name of the memory pool. |
Andy Ross | 73cb958 | 2017-05-09 10:42:39 -0700 | [diff] [blame] | 4320 | * @param minsz Size of the smallest blocks in the pool (in bytes). |
| 4321 | * @param maxsz Size of the largest blocks in the pool (in bytes). |
| 4322 | * @param nmax Number of maximum sized blocks in the pool. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4323 | * @param align Alignment of the pool's buffer (power of 2). |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4324 | * @req K-MPOOL-001 |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 4325 | */ |
Andy Ross | 73cb958 | 2017-05-09 10:42:39 -0700 | [diff] [blame] | 4326 | #define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \ |
Nicolas Pitre | cf97437 | 2019-06-26 11:32:58 -0400 | [diff] [blame] | 4327 | char __aligned(WB_UP(align)) _mpool_buf_##name[WB_UP(maxsz) * nmax \ |
Andy Ross | 73cb958 | 2017-05-09 10:42:39 -0700 | [diff] [blame] | 4328 | + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4329 | 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] | 4330 | Z_STRUCT_SECTION_ITERABLE(k_mem_pool, name) = { \ |
Andrew Boie | aa6de29 | 2018-03-06 17:12:37 -0800 | [diff] [blame] | 4331 | .base = { \ |
| 4332 | .buf = _mpool_buf_##name, \ |
Nicolas Pitre | cf97437 | 2019-06-26 11:32:58 -0400 | [diff] [blame] | 4333 | .max_sz = WB_UP(maxsz), \ |
Andrew Boie | aa6de29 | 2018-03-06 17:12:37 -0800 | [diff] [blame] | 4334 | .n_max = nmax, \ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4335 | .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \ |
Andrew Boie | aa6de29 | 2018-03-06 17:12:37 -0800 | [diff] [blame] | 4336 | .levels = _mpool_lvls_##name, \ |
| 4337 | .flags = SYS_MEM_POOL_KERNEL \ |
| 4338 | } \ |
Johann Fischer | 223a2b9 | 2019-07-04 15:55:20 +0200 | [diff] [blame] | 4339 | }; \ |
Nicolas Pitre | b2a022b | 2019-09-26 16:36:40 -0400 | [diff] [blame] | 4340 | BUILD_ASSERT(WB_UP(maxsz) >= _MPOOL_MINBLK) |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 4341 | |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4342 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4343 | * @brief Allocate memory from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4344 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4345 | * This routine allocates a memory block from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4346 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4347 | * @param pool Address of the memory pool. |
| 4348 | * @param block Pointer to block descriptor for the allocated memory. |
| 4349 | * @param size Amount of memory to allocate (in bytes). |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 4350 | * @param timeout Non-negative waiting period to wait for operation to complete |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4351 | * (in milliseconds). Use K_NO_WAIT to return without waiting, |
| 4352 | * or K_FOREVER to wait as long as necessary. |
| 4353 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4354 | * @retval 0 Memory allocated. The @a data field of the block descriptor |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4355 | * is set to the starting address of the memory block. |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 4356 | * @retval -ENOMEM Returned without waiting. |
| 4357 | * @retval -EAGAIN Waiting period timed out. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4358 | * @req K-MPOOL-002 |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4359 | */ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 4360 | 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] | 4361 | size_t size, s32_t timeout); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4362 | |
| 4363 | /** |
Andrew Boie | a2480bd | 2018-04-12 16:59:02 -0700 | [diff] [blame] | 4364 | * @brief Allocate memory from a memory pool with malloc() semantics |
| 4365 | * |
| 4366 | * Such memory must be released using k_free(). |
| 4367 | * |
| 4368 | * @param pool Address of the memory pool. |
| 4369 | * @param size Amount of memory to allocate (in bytes). |
| 4370 | * @return Address of the allocated memory if successful, otherwise NULL |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4371 | * @req K-MPOOL-002 |
Andrew Boie | a2480bd | 2018-04-12 16:59:02 -0700 | [diff] [blame] | 4372 | */ |
| 4373 | extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size); |
| 4374 | |
| 4375 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4376 | * @brief Free memory allocated from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4377 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4378 | * This routine releases a previously allocated memory block back to its |
| 4379 | * memory pool. |
| 4380 | * |
| 4381 | * @param block Pointer to block descriptor for the allocated memory. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4382 | * |
| 4383 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4384 | * @req K-MPOOL-002 |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4385 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4386 | extern void k_mem_pool_free(struct k_mem_block *block); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4387 | |
| 4388 | /** |
Johan Hedberg | 7d887cb | 2018-01-11 20:45:27 +0200 | [diff] [blame] | 4389 | * @brief Free memory allocated from a memory pool. |
| 4390 | * |
| 4391 | * This routine releases a previously allocated memory block back to its |
| 4392 | * memory pool |
| 4393 | * |
| 4394 | * @param id Memory block identifier. |
| 4395 | * |
| 4396 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4397 | * @req K-MPOOL-002 |
Johan Hedberg | 7d887cb | 2018-01-11 20:45:27 +0200 | [diff] [blame] | 4398 | */ |
| 4399 | extern void k_mem_pool_free_id(struct k_mem_block_id *id); |
| 4400 | |
| 4401 | /** |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4402 | * @} |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4403 | */ |
| 4404 | |
| 4405 | /** |
| 4406 | * @defgroup heap_apis Heap Memory Pool APIs |
| 4407 | * @ingroup kernel_apis |
| 4408 | * @{ |
| 4409 | */ |
| 4410 | |
| 4411 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4412 | * @brief Allocate memory from heap. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4413 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4414 | * This routine provides traditional malloc() semantics. Memory is |
Allan Stephens | 480a131 | 2016-10-13 15:44:48 -0500 | [diff] [blame] | 4415 | * allocated from the heap memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4416 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4417 | * @param size Amount of memory requested (in bytes). |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4418 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4419 | * @return Address of the allocated memory if successful; otherwise NULL. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4420 | * @req K-HEAP-001 |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4421 | */ |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 4422 | extern void *k_malloc(size_t size); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4423 | |
| 4424 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4425 | * @brief Free memory allocated from heap. |
Allan Stephens | 480a131 | 2016-10-13 15:44:48 -0500 | [diff] [blame] | 4426 | * |
| 4427 | * This routine provides traditional free() semantics. The memory being |
Andrew Boie | a2480bd | 2018-04-12 16:59:02 -0700 | [diff] [blame] | 4428 | * returned must have been allocated from the heap memory pool or |
| 4429 | * k_mem_pool_malloc(). |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4430 | * |
Anas Nashif | 345fdd5 | 2016-12-20 08:36:04 -0500 | [diff] [blame] | 4431 | * If @a ptr is NULL, no operation is performed. |
| 4432 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 4433 | * @param ptr Pointer to previously allocated memory. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4434 | * |
| 4435 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4436 | * @req K-HEAP-001 |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 4437 | */ |
| 4438 | extern void k_free(void *ptr); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4439 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4440 | /** |
Andrew Boie | 7f95e83 | 2017-11-08 14:40:01 -0800 | [diff] [blame] | 4441 | * @brief Allocate memory from heap, array style |
| 4442 | * |
| 4443 | * This routine provides traditional calloc() semantics. Memory is |
| 4444 | * allocated from the heap memory pool and zeroed. |
| 4445 | * |
| 4446 | * @param nmemb Number of elements in the requested array |
| 4447 | * @param size Size of each array element (in bytes). |
| 4448 | * |
| 4449 | * @return Address of the allocated memory if successful; otherwise NULL. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4450 | * @req K-HEAP-001 |
Andrew Boie | 7f95e83 | 2017-11-08 14:40:01 -0800 | [diff] [blame] | 4451 | */ |
| 4452 | extern void *k_calloc(size_t nmemb, size_t size); |
| 4453 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4454 | /** @} */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 4455 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4456 | /* polling API - PRIVATE */ |
| 4457 | |
Benjamin Walsh | b017986 | 2017-02-02 16:39:57 -0500 | [diff] [blame] | 4458 | #ifdef CONFIG_POLL |
Flavio Ceolin | 6fdc56d | 2018-09-18 12:32:27 -0700 | [diff] [blame] | 4459 | #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] | 4460 | #else |
Flavio Ceolin | 6fdc56d | 2018-09-18 12:32:27 -0700 | [diff] [blame] | 4461 | #define _INIT_OBJ_POLL_EVENT(obj) do { } while (false) |
Benjamin Walsh | b017986 | 2017-02-02 16:39:57 -0500 | [diff] [blame] | 4462 | #endif |
| 4463 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4464 | /* private - types bit positions */ |
| 4465 | enum _poll_types_bits { |
| 4466 | /* can be used to ignore an event */ |
| 4467 | _POLL_TYPE_IGNORE, |
| 4468 | |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 4469 | /* to be signaled by k_poll_signal_raise() */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4470 | _POLL_TYPE_SIGNAL, |
| 4471 | |
| 4472 | /* semaphore availability */ |
| 4473 | _POLL_TYPE_SEM_AVAILABLE, |
| 4474 | |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 4475 | /* queue/fifo/lifo data availability */ |
| 4476 | _POLL_TYPE_DATA_AVAILABLE, |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4477 | |
| 4478 | _POLL_NUM_TYPES |
| 4479 | }; |
| 4480 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4481 | #define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1)) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4482 | |
| 4483 | /* private - states bit positions */ |
| 4484 | enum _poll_states_bits { |
| 4485 | /* default state when creating event */ |
| 4486 | _POLL_STATE_NOT_READY, |
| 4487 | |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 4488 | /* signaled by k_poll_signal_raise() */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4489 | _POLL_STATE_SIGNALED, |
| 4490 | |
| 4491 | /* semaphore is available */ |
| 4492 | _POLL_STATE_SEM_AVAILABLE, |
| 4493 | |
Luiz Augusto von Dentz | a7ddb87 | 2017-02-21 14:50:42 +0200 | [diff] [blame] | 4494 | /* data is available to read on queue/fifo/lifo */ |
| 4495 | _POLL_STATE_DATA_AVAILABLE, |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4496 | |
Paul Sokolovsky | 45c0b20 | 2018-08-21 23:29:11 +0300 | [diff] [blame] | 4497 | /* queue/fifo/lifo wait was cancelled */ |
| 4498 | _POLL_STATE_CANCELLED, |
| 4499 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4500 | _POLL_NUM_STATES |
| 4501 | }; |
| 4502 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4503 | #define Z_POLL_STATE_BIT(state) (1 << ((state) - 1)) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4504 | |
| 4505 | #define _POLL_EVENT_NUM_UNUSED_BITS \ |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4506 | (32 - (0 \ |
| 4507 | + 8 /* tag */ \ |
| 4508 | + _POLL_NUM_TYPES \ |
| 4509 | + _POLL_NUM_STATES \ |
| 4510 | + 1 /* modes */ \ |
| 4511 | )) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4512 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4513 | /* end of polling API - PRIVATE */ |
| 4514 | |
| 4515 | |
| 4516 | /** |
| 4517 | * @defgroup poll_apis Async polling APIs |
| 4518 | * @ingroup kernel_apis |
| 4519 | * @{ |
| 4520 | */ |
| 4521 | |
| 4522 | /* Public polling API */ |
| 4523 | |
| 4524 | /* public - values for k_poll_event.type bitfield */ |
| 4525 | #define K_POLL_TYPE_IGNORE 0 |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4526 | #define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL) |
| 4527 | #define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE) |
| 4528 | #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] | 4529 | #define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4530 | |
| 4531 | /* public - polling modes */ |
| 4532 | enum k_poll_modes { |
| 4533 | /* polling thread does not take ownership of objects when available */ |
| 4534 | K_POLL_MODE_NOTIFY_ONLY = 0, |
| 4535 | |
| 4536 | K_POLL_NUM_MODES |
| 4537 | }; |
| 4538 | |
| 4539 | /* public - values for k_poll_event.state bitfield */ |
| 4540 | #define K_POLL_STATE_NOT_READY 0 |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4541 | #define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED) |
| 4542 | #define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE) |
| 4543 | #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] | 4544 | #define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4545 | #define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4546 | |
| 4547 | /* public - poll signal object */ |
| 4548 | struct k_poll_signal { |
| 4549 | /* PRIVATE - DO NOT TOUCH */ |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 4550 | sys_dlist_t poll_events; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4551 | |
| 4552 | /* |
| 4553 | * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until |
| 4554 | * user resets it to 0. |
| 4555 | */ |
| 4556 | unsigned int signaled; |
| 4557 | |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 4558 | /* custom result value passed to k_poll_signal_raise() if needed */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4559 | int result; |
| 4560 | }; |
| 4561 | |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 4562 | #define K_POLL_SIGNAL_INITIALIZER(obj) \ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4563 | { \ |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 4564 | .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4565 | .signaled = 0, \ |
| 4566 | .result = 0, \ |
| 4567 | } |
| 4568 | |
| 4569 | struct k_poll_event { |
| 4570 | /* PRIVATE - DO NOT TOUCH */ |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 4571 | sys_dnode_t _node; |
| 4572 | |
| 4573 | /* PRIVATE - DO NOT TOUCH */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4574 | struct _poller *poller; |
| 4575 | |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4576 | /* optional user-specified tag, opaque, untouched by the API */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4577 | u32_t tag:8; |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4578 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4579 | /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4580 | u32_t type:_POLL_NUM_TYPES; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4581 | |
| 4582 | /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4583 | u32_t state:_POLL_NUM_STATES; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4584 | |
| 4585 | /* mode of operation, from enum k_poll_modes */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4586 | u32_t mode:1; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4587 | |
| 4588 | /* unused bits in 32-bit word */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4589 | u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4590 | |
| 4591 | /* per-type data */ |
| 4592 | union { |
| 4593 | void *obj; |
| 4594 | struct k_poll_signal *signal; |
| 4595 | struct k_sem *sem; |
| 4596 | struct k_fifo *fifo; |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 4597 | struct k_queue *queue; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4598 | }; |
| 4599 | }; |
| 4600 | |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4601 | #define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4602 | { \ |
| 4603 | .poller = NULL, \ |
| 4604 | .type = event_type, \ |
| 4605 | .state = K_POLL_STATE_NOT_READY, \ |
| 4606 | .mode = event_mode, \ |
| 4607 | .unused = 0, \ |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4608 | { .obj = event_obj }, \ |
| 4609 | } |
| 4610 | |
| 4611 | #define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \ |
| 4612 | event_tag) \ |
| 4613 | { \ |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4614 | .tag = event_tag, \ |
Markus Fuchs | be21d3f | 2019-10-09 21:31:25 +0200 | [diff] [blame] | 4615 | .type = event_type, \ |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 4616 | .state = K_POLL_STATE_NOT_READY, \ |
| 4617 | .mode = event_mode, \ |
| 4618 | .unused = 0, \ |
| 4619 | { .obj = event_obj }, \ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4620 | } |
| 4621 | |
| 4622 | /** |
| 4623 | * @brief Initialize one struct k_poll_event instance |
| 4624 | * |
| 4625 | * After this routine is called on a poll event, the event it ready to be |
| 4626 | * placed in an event array to be passed to k_poll(). |
| 4627 | * |
| 4628 | * @param event The event to initialize. |
| 4629 | * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx |
| 4630 | * values. Only values that apply to the same object being polled |
| 4631 | * can be used together. Choosing K_POLL_TYPE_IGNORE disables the |
| 4632 | * event. |
Paul Sokolovsky | cfef979 | 2017-07-18 11:53:06 +0300 | [diff] [blame] | 4633 | * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY. |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4634 | * @param obj Kernel object or poll signal. |
| 4635 | * |
| 4636 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4637 | * @req K-POLL-001 |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4638 | */ |
| 4639 | |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 4640 | 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] | 4641 | int mode, void *obj); |
| 4642 | |
| 4643 | /** |
| 4644 | * @brief Wait for one or many of multiple poll events to occur |
| 4645 | * |
| 4646 | * This routine allows a thread to wait concurrently for one or many of |
| 4647 | * multiple poll events to have occurred. Such events can be a kernel object |
| 4648 | * being available, like a semaphore, or a poll signal event. |
| 4649 | * |
| 4650 | * When an event notifies that a kernel object is available, the kernel object |
| 4651 | * is not "given" to the thread calling k_poll(): it merely signals the fact |
| 4652 | * that the object was available when the k_poll() call was in effect. Also, |
| 4653 | * all threads trying to acquire an object the regular way, i.e. by pending on |
| 4654 | * the object, have precedence over the thread polling on the object. This |
| 4655 | * means that the polling thread will never get the poll event on an object |
| 4656 | * until the object becomes available and its pend queue is empty. For this |
| 4657 | * reason, the k_poll() call is more effective when the objects being polled |
| 4658 | * only have one thread, the polling thread, trying to acquire them. |
| 4659 | * |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 4660 | * When k_poll() returns 0, the caller should loop on all the events that were |
| 4661 | * passed to k_poll() and check the state field for the values that were |
| 4662 | * expected and take the associated actions. |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4663 | * |
| 4664 | * Before being reused for another call to k_poll(), the user has to reset the |
| 4665 | * state field to K_POLL_STATE_NOT_READY. |
| 4666 | * |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4667 | * When called from user mode, a temporary memory allocation is required from |
| 4668 | * the caller's resource pool. |
| 4669 | * |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4670 | * @param events An array of pointers to events to be polled for. |
| 4671 | * @param num_events The number of events in the array. |
Krzysztof Chruscinski | 94f742e | 2019-11-07 19:28:00 +0100 | [diff] [blame] | 4672 | * @param timeout Non-negative waiting period for an event to be ready (in |
| 4673 | * milliseconds), or one of the special values K_NO_WAIT and |
| 4674 | * K_FOREVER. |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4675 | * |
| 4676 | * @retval 0 One or more events are ready. |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4677 | * @retval -EAGAIN Waiting period timed out. |
Paul Sokolovsky | 45c0b20 | 2018-08-21 23:29:11 +0300 | [diff] [blame] | 4678 | * @retval -EINTR Polling has been interrupted, e.g. with |
| 4679 | * k_queue_cancel_wait(). All output events are still set and valid, |
| 4680 | * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other |
| 4681 | * words, -EINTR status means that at least one of output events is |
| 4682 | * K_POLL_STATE_CANCELLED. |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4683 | * @retval -ENOMEM Thread resource pool insufficient memory (user mode only) |
| 4684 | * @retval -EINVAL Bad parameters (user mode only) |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4685 | * @req K-POLL-001 |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4686 | */ |
| 4687 | |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4688 | __syscall int k_poll(struct k_poll_event *events, int num_events, |
| 4689 | s32_t timeout); |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4690 | |
| 4691 | /** |
Benjamin Walsh | a304f16 | 2017-02-02 16:46:09 -0500 | [diff] [blame] | 4692 | * @brief Initialize a poll signal object. |
| 4693 | * |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 4694 | * 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] | 4695 | * |
| 4696 | * @param signal A poll signal. |
| 4697 | * |
| 4698 | * @return N/A |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4699 | * @req K-POLL-001 |
Benjamin Walsh | a304f16 | 2017-02-02 16:46:09 -0500 | [diff] [blame] | 4700 | */ |
| 4701 | |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4702 | __syscall void k_poll_signal_init(struct k_poll_signal *signal); |
| 4703 | |
| 4704 | /* |
| 4705 | * @brief Reset a poll signal object's state to unsignaled. |
| 4706 | * |
| 4707 | * @param signal A poll signal object |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4708 | * @req K-POLL-001 |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4709 | */ |
| 4710 | __syscall void k_poll_signal_reset(struct k_poll_signal *signal); |
| 4711 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4712 | 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] | 4713 | { |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 4714 | signal->signaled = 0U; |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4715 | } |
| 4716 | |
| 4717 | /** |
David B. Kinder | fcbd8fb | 2018-05-23 12:06:24 -0700 | [diff] [blame] | 4718 | * @brief Fetch the signaled state and result value of a poll signal |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4719 | * |
| 4720 | * @param signal A poll signal object |
| 4721 | * @param signaled An integer buffer which will be written nonzero if the |
| 4722 | * object was signaled |
| 4723 | * @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] | 4724 | * result value if the object was signaled, or an undefined |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4725 | * value if it was not. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4726 | * @req K-POLL-001 |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 4727 | */ |
| 4728 | __syscall void k_poll_signal_check(struct k_poll_signal *signal, |
| 4729 | unsigned int *signaled, int *result); |
Benjamin Walsh | a304f16 | 2017-02-02 16:46:09 -0500 | [diff] [blame] | 4730 | |
| 4731 | /** |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4732 | * @brief Signal a poll signal object. |
| 4733 | * |
| 4734 | * This routine makes ready a poll signal, which is basically a poll event of |
| 4735 | * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be |
| 4736 | * made ready to run. A @a result value can be specified. |
| 4737 | * |
| 4738 | * The poll signal contains a 'signaled' field that, when set by |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 4739 | * 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] | 4740 | * k_poll_signal_reset(). It thus has to be reset by the user before being |
| 4741 | * passed again to k_poll() or k_poll() will consider it being signaled, and |
| 4742 | * will return immediately. |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4743 | * |
Peter A. Bigot | 773bd98 | 2019-04-30 07:06:39 -0500 | [diff] [blame] | 4744 | * @note The result is stored and the 'signaled' field is set even if |
| 4745 | * this function returns an error indicating that an expiring poll was |
| 4746 | * not notified. The next k_poll() will detect the missed raise. |
| 4747 | * |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4748 | * @param signal A poll signal. |
| 4749 | * @param result The value to store in the result field of the signal. |
| 4750 | * |
| 4751 | * @retval 0 The signal was delivered successfully. |
| 4752 | * @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] | 4753 | * @req K-POLL-001 |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4754 | */ |
| 4755 | |
Flavio Ceolin | aecd4ec | 2018-11-02 12:35:30 -0700 | [diff] [blame] | 4756 | __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] | 4757 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 4758 | /** |
| 4759 | * @internal |
| 4760 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4761 | 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] | 4762 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 4763 | /** @} */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 4764 | |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 4765 | /** |
Anas Nashif | 30c3cff | 2019-01-22 08:18:13 -0500 | [diff] [blame] | 4766 | * @defgroup cpu_idle_apis CPU Idling APIs |
| 4767 | * @ingroup kernel_apis |
| 4768 | * @{ |
| 4769 | */ |
Anas Nashif | 30c3cff | 2019-01-22 08:18:13 -0500 | [diff] [blame] | 4770 | /** |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 4771 | * @brief Make the CPU idle. |
| 4772 | * |
| 4773 | * This function makes the CPU idle until an event wakes it up. |
| 4774 | * |
| 4775 | * In a regular system, the idle thread should be the only thread responsible |
| 4776 | * for making the CPU idle and triggering any type of power management. |
| 4777 | * However, in some more constrained systems, such as a single-threaded system, |
| 4778 | * the only thread would be responsible for this if needed. |
| 4779 | * |
| 4780 | * @return N/A |
Anas Nashif | 30c3cff | 2019-01-22 08:18:13 -0500 | [diff] [blame] | 4781 | * @req K-CPU-IDLE-001 |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 4782 | */ |
Andrew Boie | 07525a3 | 2019-09-21 16:17:23 -0700 | [diff] [blame] | 4783 | static inline void k_cpu_idle(void) |
| 4784 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4785 | arch_cpu_idle(); |
Andrew Boie | 07525a3 | 2019-09-21 16:17:23 -0700 | [diff] [blame] | 4786 | } |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 4787 | |
| 4788 | /** |
| 4789 | * @brief Make the CPU idle in an atomic fashion. |
| 4790 | * |
| 4791 | * Similar to k_cpu_idle(), but called with interrupts locked if operations |
| 4792 | * must be done atomically before making the CPU idle. |
| 4793 | * |
| 4794 | * @param key Interrupt locking key obtained from irq_lock(). |
| 4795 | * |
| 4796 | * @return N/A |
Anas Nashif | 30c3cff | 2019-01-22 08:18:13 -0500 | [diff] [blame] | 4797 | * @req K-CPU-IDLE-002 |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 4798 | */ |
Andrew Boie | 07525a3 | 2019-09-21 16:17:23 -0700 | [diff] [blame] | 4799 | static inline void k_cpu_atomic_idle(unsigned int key) |
| 4800 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4801 | arch_cpu_atomic_idle(key); |
Andrew Boie | 07525a3 | 2019-09-21 16:17:23 -0700 | [diff] [blame] | 4802 | } |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 4803 | |
Anas Nashif | 30c3cff | 2019-01-22 08:18:13 -0500 | [diff] [blame] | 4804 | /** |
| 4805 | * @} |
| 4806 | */ |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 4807 | |
| 4808 | /** |
| 4809 | * @internal |
| 4810 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4811 | extern void z_sys_power_save_idle_exit(s32_t ticks); |
Andrew Boie | 350f88d | 2017-01-18 13:13:45 -0800 | [diff] [blame] | 4812 | |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4813 | #ifdef ARCH_EXCEPT |
Ioannis Glaropoulos | df02923 | 2019-10-07 11:24:36 +0200 | [diff] [blame] | 4814 | /* This architecture has direct support for triggering a CPU exception */ |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4815 | #define z_except_reason(reason) ARCH_EXCEPT(reason) |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4816 | #else |
| 4817 | |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4818 | /* NOTE: This is the implementation for arches that do not implement |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4819 | * ARCH_EXCEPT() to generate a real CPU exception. |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4820 | * |
| 4821 | * We won't have a real exception frame to determine the PC value when |
| 4822 | * the oops occurred, so print file and line number before we jump into |
| 4823 | * the fatal error handler. |
| 4824 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4825 | #define z_except_reason(reason) do { \ |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4826 | printk("@ %s:%d:\n", __FILE__, __LINE__); \ |
Andrew Boie | 5623637 | 2019-07-15 15:22:29 -0700 | [diff] [blame] | 4827 | z_fatal_error(reason, NULL); \ |
Flavio Ceolin | 6fdc56d | 2018-09-18 12:32:27 -0700 | [diff] [blame] | 4828 | } while (false) |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4829 | |
| 4830 | #endif /* _ARCH__EXCEPT */ |
| 4831 | |
| 4832 | /** |
| 4833 | * @brief Fatally terminate a thread |
| 4834 | * |
| 4835 | * This should be called when a thread has encountered an unrecoverable |
| 4836 | * runtime condition and needs to terminate. What this ultimately |
| 4837 | * means is determined by the _fatal_error_handler() implementation, which |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 4838 | * will be called will reason code K_ERR_KERNEL_OOPS. |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4839 | * |
| 4840 | * If this is called from ISR context, the default system fatal error handler |
| 4841 | * will treat it as an unrecoverable system error, just like k_panic(). |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4842 | * @req K-MISC-003 |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4843 | */ |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 4844 | #define k_oops() z_except_reason(K_ERR_KERNEL_OOPS) |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4845 | |
| 4846 | /** |
| 4847 | * @brief Fatally terminate the system |
| 4848 | * |
| 4849 | * This should be called when the Zephyr kernel has encountered an |
| 4850 | * unrecoverable runtime condition and needs to terminate. What this ultimately |
| 4851 | * means is determined by the _fatal_error_handler() implementation, which |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 4852 | * will be called will reason code K_ERR_KERNEL_PANIC. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4853 | * @req K-MISC-004 |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4854 | */ |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 4855 | #define k_panic() z_except_reason(K_ERR_KERNEL_PANIC) |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 4856 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4857 | /* |
| 4858 | * private APIs that are utilized by one or more public APIs |
| 4859 | */ |
| 4860 | |
Stephanos Ioannidis | 2d74604 | 2019-10-25 00:08:21 +0900 | [diff] [blame] | 4861 | /** |
| 4862 | * @internal |
| 4863 | */ |
| 4864 | extern void z_init_thread_base(struct _thread_base *thread_base, |
| 4865 | int priority, u32_t initial_state, |
| 4866 | unsigned int options); |
| 4867 | |
Benjamin Walsh | b12a8e0 | 2016-12-14 15:24:12 -0500 | [diff] [blame] | 4868 | #ifdef CONFIG_MULTITHREADING |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 4869 | /** |
| 4870 | * @internal |
| 4871 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4872 | extern void z_init_static_threads(void); |
Benjamin Walsh | b12a8e0 | 2016-12-14 15:24:12 -0500 | [diff] [blame] | 4873 | #else |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 4874 | /** |
| 4875 | * @internal |
| 4876 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4877 | #define z_init_static_threads() do { } while (false) |
Benjamin Walsh | b12a8e0 | 2016-12-14 15:24:12 -0500 | [diff] [blame] | 4878 | #endif |
| 4879 | |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 4880 | /** |
| 4881 | * @internal |
| 4882 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4883 | extern bool z_is_thread_essential(void); |
Anas Nashif | 954d550 | 2018-02-25 08:37:28 -0600 | [diff] [blame] | 4884 | /** |
| 4885 | * @internal |
| 4886 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 4887 | extern void z_timer_expiration_handler(struct _timeout *t); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 4888 | |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4889 | /* arch/cpu.h may declare an architecture or platform-specific macro |
| 4890 | * for properly declaring stacks, compatible with MMU/MPU constraints if |
| 4891 | * enabled |
| 4892 | */ |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 4893 | |
| 4894 | /** |
| 4895 | * @brief Obtain an extern reference to a stack |
| 4896 | * |
| 4897 | * This macro properly brings the symbol of a thread stack declared |
| 4898 | * elsewhere into scope. |
| 4899 | * |
| 4900 | * @param sym Thread stack symbol name |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4901 | * @req K-MISC-005 |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 4902 | */ |
| 4903 | #define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[] |
| 4904 | |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4905 | #ifdef ARCH_THREAD_STACK_DEFINE |
| 4906 | #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] | 4907 | #define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \ |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4908 | ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) |
| 4909 | #define K_THREAD_STACK_LEN(size) ARCH_THREAD_STACK_LEN(size) |
| 4910 | #define K_THREAD_STACK_MEMBER(sym, size) ARCH_THREAD_STACK_MEMBER(sym, size) |
| 4911 | #define K_THREAD_STACK_SIZEOF(sym) ARCH_THREAD_STACK_SIZEOF(sym) |
| 4912 | #define K_THREAD_STACK_RESERVED ARCH_THREAD_STACK_RESERVED |
Andrew Boie | 4e5c093 | 2019-04-04 12:05:28 -0700 | [diff] [blame] | 4913 | static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym) |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 4914 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 4915 | return ARCH_THREAD_STACK_BUFFER(sym); |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 4916 | } |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4917 | #else |
| 4918 | /** |
| 4919 | * @brief Declare a toplevel thread stack memory region |
| 4920 | * |
| 4921 | * This declares a region of memory suitable for use as a thread's stack. |
| 4922 | * |
| 4923 | * This is the generic, historical definition. Align to STACK_ALIGN and put in |
| 4924 | * 'noinit' section so that it isn't zeroed at boot |
| 4925 | * |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 4926 | * 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] | 4927 | * k_thread_create(), but should otherwise not be manipulated. If the buffer |
Andrew Boie | 4e5c093 | 2019-04-04 12:05:28 -0700 | [diff] [blame] | 4928 | * inside needs to be examined, examine thread->stack_info for the associated |
| 4929 | * thread object to obtain the boundaries. |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4930 | * |
| 4931 | * It is legal to precede this definition with the 'static' keyword. |
| 4932 | * |
| 4933 | * It is NOT legal to take the sizeof(sym) and pass that to the stackSize |
| 4934 | * parameter of k_thread_create(), it may not be the same as the |
| 4935 | * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead. |
| 4936 | * |
Andrew Boie | e2d7791 | 2018-05-30 09:45:35 -0700 | [diff] [blame] | 4937 | * Some arches may round the size of the usable stack region up to satisfy |
| 4938 | * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned |
| 4939 | * size. |
| 4940 | * |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4941 | * @param sym Thread stack symbol name |
| 4942 | * @param size Size of the stack memory region |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4943 | * @req K-TSTACK-001 |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4944 | */ |
| 4945 | #define K_THREAD_STACK_DEFINE(sym, size) \ |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 4946 | struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size] |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4947 | |
| 4948 | /** |
Rajavardhan Gundi | d4dd928 | 2018-06-27 13:26:20 +0530 | [diff] [blame] | 4949 | * @brief Calculate size of stacks to be allocated in a stack array |
| 4950 | * |
| 4951 | * This macro calculates the size to be allocated for the stacks |
| 4952 | * inside a stack array. It accepts the indicated "size" as a parameter |
| 4953 | * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer |
| 4954 | * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used. |
| 4955 | * |
| 4956 | * @param size Size of the stack memory region |
| 4957 | * @req K-TSTACK-001 |
| 4958 | */ |
| 4959 | #define K_THREAD_STACK_LEN(size) (size) |
| 4960 | |
| 4961 | /** |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4962 | * @brief Declare a toplevel array of thread stack memory regions |
| 4963 | * |
| 4964 | * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE |
| 4965 | * definition for additional details and constraints. |
| 4966 | * |
| 4967 | * This is the generic, historical definition. Align to STACK_ALIGN and put in |
| 4968 | * 'noinit' section so that it isn't zeroed at boot |
| 4969 | * |
| 4970 | * @param sym Thread stack symbol name |
| 4971 | * @param nmemb Number of stacks to declare |
| 4972 | * @param size Size of the stack memory region |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4973 | * @req K-TSTACK-001 |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4974 | */ |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4975 | #define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \ |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 4976 | struct _k_thread_stack_element __noinit \ |
Rajavardhan Gundi | d4dd928 | 2018-06-27 13:26:20 +0530 | [diff] [blame] | 4977 | __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)] |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4978 | |
| 4979 | /** |
| 4980 | * @brief Declare an embedded stack memory region |
| 4981 | * |
| 4982 | * Used for stacks embedded within other data structures. Use is highly |
| 4983 | * discouraged but in some cases necessary. For memory protection scenarios, |
| 4984 | * it is very important that any RAM preceding this member not be writable |
| 4985 | * by threads else a stack overflow will lead to silent corruption. In other |
| 4986 | * words, the containing data structure should live in RAM owned by the kernel. |
| 4987 | * |
| 4988 | * @param sym Thread stack symbol name |
| 4989 | * @param size Size of the stack memory region |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 4990 | * @req K-TSTACK-001 |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4991 | */ |
| 4992 | #define K_THREAD_STACK_MEMBER(sym, size) \ |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 4993 | struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size] |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 4994 | |
| 4995 | /** |
| 4996 | * @brief Return the size in bytes of a stack memory region |
| 4997 | * |
| 4998 | * Convenience macro for passing the desired stack size to k_thread_create() |
| 4999 | * since the underlying implementation may actually create something larger |
| 5000 | * (for instance a guard area). |
| 5001 | * |
Andrew Boie | e2d7791 | 2018-05-30 09:45:35 -0700 | [diff] [blame] | 5002 | * The value returned here is not guaranteed to match the 'size' parameter |
| 5003 | * passed to K_THREAD_STACK_DEFINE and may be larger. |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5004 | * |
| 5005 | * @param sym Stack memory symbol |
| 5006 | * @return Size of the stack |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5007 | * @req K-TSTACK-001 |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5008 | */ |
| 5009 | #define K_THREAD_STACK_SIZEOF(sym) sizeof(sym) |
| 5010 | |
Andrew Boie | 575abc0 | 2019-03-19 10:42:24 -0700 | [diff] [blame] | 5011 | |
| 5012 | /** |
| 5013 | * @brief Indicate how much additional memory is reserved for stack objects |
| 5014 | * |
| 5015 | * Any given stack declaration may have additional memory in it for guard |
| 5016 | * areas or supervisor mode stacks. This macro indicates how much space |
| 5017 | * is reserved for this. The memory reserved may not be contiguous within |
| 5018 | * the stack object, and does not account for additional space used due to |
| 5019 | * enforce alignment. |
| 5020 | */ |
| 5021 | #define K_THREAD_STACK_RESERVED 0 |
| 5022 | |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5023 | /** |
| 5024 | * @brief Get a pointer to the physical stack buffer |
| 5025 | * |
Andrew Boie | 4e5c093 | 2019-04-04 12:05:28 -0700 | [diff] [blame] | 5026 | * This macro is deprecated. If a stack buffer needs to be examined, the |
| 5027 | * bounds should be obtained from the associated thread's stack_info struct. |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5028 | * |
| 5029 | * @param sym Declared stack symbol name |
| 5030 | * @return The buffer itself, a char * |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5031 | * @req K-TSTACK-001 |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5032 | */ |
Andrew Boie | 4e5c093 | 2019-04-04 12:05:28 -0700 | [diff] [blame] | 5033 | static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym) |
Andrew Boie | 507852a | 2017-07-25 18:47:07 -0700 | [diff] [blame] | 5034 | { |
| 5035 | return (char *)sym; |
| 5036 | } |
Andrew Boie | dc5d935 | 2017-06-02 12:56:47 -0700 | [diff] [blame] | 5037 | |
| 5038 | #endif /* _ARCH_DECLARE_STACK */ |
| 5039 | |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5040 | /** |
| 5041 | * @defgroup mem_domain_apis Memory domain APIs |
| 5042 | * @ingroup kernel_apis |
| 5043 | * @{ |
| 5044 | */ |
| 5045 | |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5046 | /** |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5047 | * @def K_MEM_PARTITION_DEFINE |
| 5048 | * @brief Used to declare a memory partition |
| 5049 | * @req K-MP-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5050 | */ |
| 5051 | #ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK |
| 5052 | #define K_MEM_PARTITION_DEFINE(name, start, size, attr) \ |
| 5053 | _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \ |
Andrew Boie | 41f6011 | 2019-01-31 15:53:24 -0800 | [diff] [blame] | 5054 | struct k_mem_partition name =\ |
Nicolas Pitre | 58d839b | 2019-05-21 11:32:21 -0400 | [diff] [blame] | 5055 | { (uintptr_t)start, size, attr} |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5056 | #else |
| 5057 | #define K_MEM_PARTITION_DEFINE(name, start, size, attr) \ |
Andrew Boie | 41f6011 | 2019-01-31 15:53:24 -0800 | [diff] [blame] | 5058 | struct k_mem_partition name =\ |
Nicolas Pitre | 58d839b | 2019-05-21 11:32:21 -0400 | [diff] [blame] | 5059 | { (uintptr_t)start, size, attr} |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5060 | #endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */ |
| 5061 | |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5062 | /* memory partition */ |
| 5063 | struct k_mem_partition { |
| 5064 | /* start address of memory partition */ |
Nicolas Pitre | 58d839b | 2019-05-21 11:32:21 -0400 | [diff] [blame] | 5065 | uintptr_t start; |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5066 | /* size of memory partition */ |
| 5067 | u32_t size; |
Ioannis Glaropoulos | 39bf24a | 2018-11-27 15:45:36 +0100 | [diff] [blame] | 5068 | #if defined(CONFIG_MEMORY_PROTECTION) |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5069 | /* attribute of memory partition */ |
Adithya Baglody | 83bedcc | 2017-10-06 15:43:11 +0530 | [diff] [blame] | 5070 | k_mem_partition_attr_t attr; |
Ioannis Glaropoulos | 39bf24a | 2018-11-27 15:45:36 +0100 | [diff] [blame] | 5071 | #endif /* CONFIG_MEMORY_PROTECTION */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5072 | }; |
| 5073 | |
Ioannis Glaropoulos | 12c0244 | 2018-09-25 16:05:24 +0200 | [diff] [blame] | 5074 | /* memory domain |
Adithya Baglody | 3a6d72e | 2018-02-13 16:44:44 +0530 | [diff] [blame] | 5075 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5076 | struct k_mem_domain { |
Adithya Baglody | 83bedcc | 2017-10-06 15:43:11 +0530 | [diff] [blame] | 5077 | #ifdef CONFIG_USERSPACE |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5078 | /* partitions in the domain */ |
| 5079 | struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS]; |
Adithya Baglody | 83bedcc | 2017-10-06 15:43:11 +0530 | [diff] [blame] | 5080 | #endif /* CONFIG_USERSPACE */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5081 | /* domain q */ |
| 5082 | sys_dlist_t mem_domain_q; |
Leandro Pereira | 08de658 | 2018-02-28 14:22:57 -0800 | [diff] [blame] | 5083 | /* number of partitions in the domain */ |
| 5084 | u8_t num_partitions; |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5085 | }; |
Adithya Baglody | 83bedcc | 2017-10-06 15:43:11 +0530 | [diff] [blame] | 5086 | |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5087 | |
| 5088 | /** |
| 5089 | * @brief Initialize a memory domain. |
| 5090 | * |
| 5091 | * Initialize a memory domain with given name and memory partitions. |
| 5092 | * |
Andrew Boie | fddd550 | 2019-07-30 18:07:32 -0700 | [diff] [blame] | 5093 | * See documentation for k_mem_domain_add_partition() for details about |
| 5094 | * partition constraints. |
| 5095 | * |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5096 | * @param domain The memory domain to be initialized. |
| 5097 | * @param num_parts The number of array items of "parts" parameter. |
| 5098 | * @param parts An array of pointers to the memory partitions. Can be NULL |
| 5099 | * if num_parts is zero. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5100 | * @req K-MD-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5101 | */ |
Leandro Pereira | 08de658 | 2018-02-28 14:22:57 -0800 | [diff] [blame] | 5102 | 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] | 5103 | struct k_mem_partition *parts[]); |
| 5104 | /** |
| 5105 | * @brief Destroy a memory domain. |
| 5106 | * |
| 5107 | * Destroy a memory domain. |
| 5108 | * |
| 5109 | * @param domain The memory domain to be destroyed. |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5110 | * @req K-MD-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5111 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5112 | extern void k_mem_domain_destroy(struct k_mem_domain *domain); |
| 5113 | |
| 5114 | /** |
| 5115 | * @brief Add a memory partition into a memory domain. |
| 5116 | * |
Andrew Boie | fddd550 | 2019-07-30 18:07:32 -0700 | [diff] [blame] | 5117 | * Add a memory partition into a memory domain. Partitions must conform to |
| 5118 | * the following constraints: |
| 5119 | * |
| 5120 | * - Partition bounds must be within system RAM boundaries on MMU-based |
| 5121 | * systems. |
| 5122 | * - Partitions in the same memory domain may not overlap each other. |
| 5123 | * - Partitions must not be defined which expose private kernel |
| 5124 | * data structures or kernel objects. |
| 5125 | * - The starting address alignment, and the partition size must conform to |
| 5126 | * the constraints of the underlying memory management hardware, which |
| 5127 | * varies per architecture. |
| 5128 | * - Memory domain partitions are only intended to control access to memory |
| 5129 | * from user mode threads. |
| 5130 | * |
| 5131 | * Violating these constraints may lead to CPU exceptions or undefined |
| 5132 | * behavior. |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5133 | * |
| 5134 | * @param domain The memory domain to be added a memory partition. |
| 5135 | * @param part The memory partition to be added |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5136 | * @req K-MD-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5137 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5138 | extern void k_mem_domain_add_partition(struct k_mem_domain *domain, |
| 5139 | struct k_mem_partition *part); |
| 5140 | |
| 5141 | /** |
| 5142 | * @brief Remove a memory partition from a memory domain. |
| 5143 | * |
| 5144 | * Remove a memory partition from a memory domain. |
| 5145 | * |
| 5146 | * @param domain The memory domain to be removed a memory partition. |
| 5147 | * @param part The memory partition to be removed |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5148 | * @req K-MD-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5149 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5150 | extern void k_mem_domain_remove_partition(struct k_mem_domain *domain, |
| 5151 | struct k_mem_partition *part); |
| 5152 | |
| 5153 | /** |
| 5154 | * @brief Add a thread into a memory domain. |
| 5155 | * |
| 5156 | * Add a thread into a memory domain. |
| 5157 | * |
| 5158 | * @param domain The memory domain that the thread is going to be added into. |
| 5159 | * @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] | 5160 | * |
| 5161 | * @req K-MD-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5162 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5163 | extern void k_mem_domain_add_thread(struct k_mem_domain *domain, |
| 5164 | k_tid_t thread); |
| 5165 | |
| 5166 | /** |
| 5167 | * @brief Remove a thread from its memory domain. |
| 5168 | * |
| 5169 | * Remove a thread from its memory domain. |
| 5170 | * |
| 5171 | * @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] | 5172 | * @req K-MD-001 |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5173 | */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5174 | extern void k_mem_domain_remove_thread(k_tid_t thread); |
| 5175 | |
Anas Nashif | 166f519 | 2018-02-25 08:02:36 -0600 | [diff] [blame] | 5176 | /** @} */ |
Chunlin Han | e9c9702 | 2017-07-07 20:29:30 +0800 | [diff] [blame] | 5177 | |
Andrew Boie | 756f907 | 2017-10-10 16:01:49 -0700 | [diff] [blame] | 5178 | /** |
| 5179 | * @brief Emit a character buffer to the console device |
| 5180 | * |
| 5181 | * @param c String of characters to print |
| 5182 | * @param n The length of the string |
Anas Nashif | c8e0d0c | 2018-05-21 11:09:59 -0400 | [diff] [blame] | 5183 | * |
| 5184 | * @req K-MISC-006 |
Andrew Boie | 756f907 | 2017-10-10 16:01:49 -0700 | [diff] [blame] | 5185 | */ |
| 5186 | __syscall void k_str_out(char *c, size_t n); |
| 5187 | |
Ioannis Glaropoulos | a6cb8b0 | 2019-05-09 21:55:10 +0200 | [diff] [blame] | 5188 | /** |
| 5189 | * @brief Disable preservation of floating point context information. |
| 5190 | * |
| 5191 | * This routine informs the kernel that the specified thread |
| 5192 | * will no longer be using the floating point registers. |
| 5193 | * |
| 5194 | * @warning |
| 5195 | * Some architectures apply restrictions on how the disabling of floating |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 5196 | * point preservation may be requested, see arch_float_disable. |
Ioannis Glaropoulos | a6cb8b0 | 2019-05-09 21:55:10 +0200 | [diff] [blame] | 5197 | * |
| 5198 | * @warning |
| 5199 | * This routine should only be used to disable floating point support for |
| 5200 | * a thread that currently has such support enabled. |
| 5201 | * |
| 5202 | * @param thread ID of thread. |
| 5203 | * |
| 5204 | * @retval 0 On success. |
| 5205 | * @retval -ENOSYS If the floating point disabling is not implemented. |
| 5206 | * -EINVAL If the floating point disabling could not be performed. |
| 5207 | */ |
| 5208 | __syscall int k_float_disable(struct k_thread *thread); |
| 5209 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 5210 | #ifdef __cplusplus |
| 5211 | } |
| 5212 | #endif |
| 5213 | |
Anas Nashif | 10291a0 | 2019-06-25 12:25:12 -0400 | [diff] [blame] | 5214 | #include <debug/tracing.h> |
Andrew Boie | fa94ee7 | 2017-09-28 16:54:35 -0700 | [diff] [blame] | 5215 | #include <syscalls/kernel.h> |
| 5216 | |
Benjamin Walsh | dfa7ce5 | 2017-01-22 17:06:05 -0500 | [diff] [blame] | 5217 | #endif /* !_ASMLANGUAGE */ |
| 5218 | |
Flavio Ceolin | 67ca176 | 2018-09-14 10:43:44 -0700 | [diff] [blame] | 5219 | #endif /* ZEPHYR_INCLUDE_KERNEL_H_ */ |