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