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