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