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