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