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