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