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