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