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