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