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