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 | { \ |
Mazen NEIFER | 967cb2e | 2017-02-02 10:42:18 +0100 | [diff] [blame] | 378 | {.init_stack = (stack)}, \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 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 | * |
Anas Nashif | 4fb12ae | 2017-02-01 20:06:55 -0500 | [diff] [blame] | 935 | * @note Can be called by ISRs. The stop handler has to be callable from ISRs |
| 936 | * if @a k_timer_stop is to be called from ISRs. |
| 937 | * |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 938 | * @param timer Address of timer. |
| 939 | * |
| 940 | * @return N/A |
| 941 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 942 | extern void k_timer_stop(struct k_timer *timer); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 943 | |
| 944 | /** |
| 945 | * @brief Read timer status. |
| 946 | * |
| 947 | * This routine reads the timer's status, which indicates the number of times |
| 948 | * it has expired since its status was last read. |
| 949 | * |
| 950 | * Calling this routine resets the timer's status to zero. |
| 951 | * |
| 952 | * @param timer Address of timer. |
| 953 | * |
| 954 | * @return Timer status. |
| 955 | */ |
| 956 | extern uint32_t k_timer_status_get(struct k_timer *timer); |
| 957 | |
| 958 | /** |
| 959 | * @brief Synchronize thread to timer expiration. |
| 960 | * |
| 961 | * This routine blocks the calling thread until the timer's status is non-zero |
| 962 | * (indicating that it has expired at least once since it was last examined) |
| 963 | * or the timer is stopped. If the timer status is already non-zero, |
| 964 | * or the timer is already stopped, the caller continues without waiting. |
| 965 | * |
| 966 | * Calling this routine resets the timer's status to zero. |
| 967 | * |
| 968 | * This routine must not be used by interrupt handlers, since they are not |
| 969 | * allowed to block. |
| 970 | * |
| 971 | * @param timer Address of timer. |
| 972 | * |
| 973 | * @return Timer status. |
| 974 | */ |
| 975 | extern uint32_t k_timer_status_sync(struct k_timer *timer); |
| 976 | |
| 977 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 978 | * @brief Get time remaining before a timer next expires. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 979 | * |
| 980 | * This routine computes the (approximate) time remaining before a running |
| 981 | * timer next expires. If the timer is not running, it returns zero. |
| 982 | * |
| 983 | * @param timer Address of timer. |
| 984 | * |
| 985 | * @return Remaining time (in milliseconds). |
| 986 | */ |
Johan Hedberg | f99ad3f | 2016-12-09 10:39:49 +0200 | [diff] [blame] | 987 | static inline int32_t k_timer_remaining_get(struct k_timer *timer) |
| 988 | { |
| 989 | return _timeout_remaining_get(&timer->timeout); |
| 990 | } |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 991 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 992 | /** |
Benjamin Walsh | e4e98f9 | 2017-01-12 19:38:53 -0500 | [diff] [blame] | 993 | * @brief Associate user-specific data with a timer. |
| 994 | * |
| 995 | * This routine records the @a user_data with the @a timer, to be retrieved |
| 996 | * later. |
| 997 | * |
| 998 | * It can be used e.g. in a timer handler shared across multiple subsystems to |
| 999 | * retrieve data specific to the subsystem this timer is associated with. |
| 1000 | * |
| 1001 | * @param timer Address of timer. |
| 1002 | * @param user_data User data to associate with the timer. |
| 1003 | * |
| 1004 | * @return N/A |
| 1005 | */ |
| 1006 | static inline void k_timer_user_data_set(struct k_timer *timer, |
| 1007 | void *user_data) |
| 1008 | { |
| 1009 | timer->user_data = user_data; |
| 1010 | } |
| 1011 | |
| 1012 | /** |
| 1013 | * @brief Retrieve the user-specific data from a timer. |
| 1014 | * |
| 1015 | * @param timer Address of timer. |
| 1016 | * |
| 1017 | * @return The user data. |
| 1018 | */ |
| 1019 | static inline void *k_timer_user_data_get(struct k_timer *timer) |
| 1020 | { |
| 1021 | return timer->user_data; |
| 1022 | } |
| 1023 | |
| 1024 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1025 | * @} end defgroup timer_apis |
| 1026 | */ |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1027 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1028 | /** |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1029 | * @addtogroup clock_apis |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1030 | * @{ |
| 1031 | */ |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 1032 | |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1033 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1034 | * @brief Get system uptime. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1035 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1036 | * This routine returns the elapsed time since the system booted, |
| 1037 | * in milliseconds. |
| 1038 | * |
| 1039 | * @return Current uptime. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1040 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1041 | extern int64_t k_uptime_get(void); |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1042 | |
| 1043 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1044 | * @brief Get system uptime (32-bit version). |
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 returns the lower 32-bits of the elapsed time since the system |
| 1047 | * booted, in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1048 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1049 | * This routine can be more efficient than k_uptime_get(), as it reduces the |
| 1050 | * need for interrupt locking and 64-bit math. However, the 32-bit result |
| 1051 | * cannot hold a system uptime time larger than approximately 50 days, so the |
| 1052 | * caller must handle possible rollovers. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1053 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1054 | * @return Current uptime. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1055 | */ |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1056 | extern uint32_t k_uptime_get_32(void); |
| 1057 | |
| 1058 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1059 | * @brief Get elapsed time. |
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 | * This routine computes the elapsed time between the current system uptime |
| 1062 | * and an earlier reference time, in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1063 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1064 | * @param reftime Pointer to a reference time, which is updated to the current |
| 1065 | * uptime upon return. |
| 1066 | * |
| 1067 | * @return Elapsed time. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1068 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1069 | extern int64_t k_uptime_delta(int64_t *reftime); |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1070 | |
| 1071 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1072 | * @brief Get elapsed time (32-bit version). |
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 computes the elapsed time between the current system uptime |
| 1075 | * and an earlier reference time, in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1076 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1077 | * This routine can be more efficient than k_uptime_delta(), as it reduces the |
| 1078 | * need for interrupt locking and 64-bit math. However, the 32-bit result |
| 1079 | * cannot hold an elapsed time larger than approximately 50 days, so the |
| 1080 | * caller must handle possible rollovers. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1081 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1082 | * @param reftime Pointer to a reference time, which is updated to the current |
| 1083 | * uptime upon return. |
| 1084 | * |
| 1085 | * @return Elapsed time. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1086 | */ |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 1087 | extern uint32_t k_uptime_delta_32(int64_t *reftime); |
| 1088 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1089 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1090 | * @brief Read the hardware 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 | * This routine returns the current time, as measured by the system's hardware |
| 1093 | * clock. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1094 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1095 | * @return Current hardware clock up-counter (in cycles). |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1096 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1097 | extern uint32_t k_cycle_get_32(void); |
| 1098 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1099 | /** |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 1100 | * @} end addtogroup clock_apis |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1101 | */ |
| 1102 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1103 | /** |
| 1104 | * @cond INTERNAL_HIDDEN |
| 1105 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1106 | |
| 1107 | struct k_fifo { |
| 1108 | _wait_q_t wait_q; |
| 1109 | sys_slist_t data_q; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 1110 | _POLL_EVENT; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1111 | |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 1112 | _OBJECT_TRACING_NEXT_PTR(k_fifo); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1113 | }; |
| 1114 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1115 | #define K_FIFO_INITIALIZER(obj) \ |
| 1116 | { \ |
| 1117 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 1118 | .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 1119 | _POLL_EVENT_OBJ_INIT \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 1120 | _OBJECT_TRACING_INIT \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | /** |
| 1124 | * INTERNAL_HIDDEN @endcond |
| 1125 | */ |
| 1126 | |
| 1127 | /** |
| 1128 | * @defgroup fifo_apis Fifo APIs |
| 1129 | * @ingroup kernel_apis |
| 1130 | * @{ |
| 1131 | */ |
| 1132 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1133 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1134 | * @brief Initialize a fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1135 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1136 | * This routine initializes a fifo object, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1137 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1138 | * @param fifo Address of the fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1139 | * |
| 1140 | * @return N/A |
| 1141 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1142 | extern void k_fifo_init(struct k_fifo *fifo); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1143 | |
| 1144 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1145 | * @brief Add an element to a fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1146 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1147 | * This routine adds a data item to @a fifo. A fifo data item must be |
| 1148 | * aligned on a 4-byte boundary, and the first 32 bits of the item are |
| 1149 | * reserved for the kernel's use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1150 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1151 | * @note Can be called by ISRs. |
| 1152 | * |
| 1153 | * @param fifo Address of the fifo. |
| 1154 | * @param data Address of the data item. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1155 | * |
| 1156 | * @return N/A |
| 1157 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1158 | extern void k_fifo_put(struct k_fifo *fifo, void *data); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1159 | |
| 1160 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1161 | * @brief Atomically add a list of elements to a fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1162 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1163 | * This routine adds a list of data items to @a fifo in one operation. |
| 1164 | * The data items must be in a singly-linked list, with the first 32 bits |
| 1165 | * each data item pointing to the next data item; the list must be |
| 1166 | * NULL-terminated. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1167 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1168 | * @note Can be called by ISRs. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1169 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1170 | * @param fifo Address of the fifo. |
| 1171 | * @param head Pointer to first node in singly-linked list. |
| 1172 | * @param tail Pointer to last node in singly-linked list. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1173 | * |
| 1174 | * @return N/A |
| 1175 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1176 | 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] | 1177 | |
| 1178 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1179 | * @brief Atomically add a list of elements to a fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1180 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1181 | * This routine adds a list of data items to @a fifo in one operation. |
| 1182 | * The data items must be in a singly-linked list implemented using a |
| 1183 | * 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] | 1184 | * and must be re-initialized via sys_slist_init(). |
| 1185 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1186 | * @note Can be called by ISRs. |
| 1187 | * |
| 1188 | * @param fifo Address of the fifo. |
| 1189 | * @param list Pointer to sys_slist_t object. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1190 | * |
| 1191 | * @return N/A |
| 1192 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1193 | 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] | 1194 | |
| 1195 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1196 | * @brief Get an element from a fifo. |
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 | * This routine removes a data item from @a fifo in a "first in, first out" |
| 1199 | * 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] | 1200 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1201 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 1202 | * |
| 1203 | * @param fifo Address of the fifo. |
| 1204 | * @param timeout Waiting period to obtain a data item (in milliseconds), |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1205 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 1206 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 1207 | * @return Address of the data item if successful; NULL if returned |
| 1208 | * without waiting, or waiting period timed out. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1209 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1210 | extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout); |
| 1211 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1212 | /** |
Benjamin Walsh | 39b80d8 | 2017-01-28 10:06:07 -0500 | [diff] [blame] | 1213 | * @brief Query a fifo to see if it has data available. |
| 1214 | * |
| 1215 | * Note that the data might be already gone by the time this function returns |
| 1216 | * if other threads is also trying to read from the fifo. |
| 1217 | * |
| 1218 | * @note Can be called by ISRs. |
| 1219 | * |
| 1220 | * @param fifo Address of the fifo. |
| 1221 | * |
| 1222 | * @return Non-zero if the fifo is empty. |
| 1223 | * @return 0 if data is available. |
| 1224 | */ |
| 1225 | static inline int k_fifo_is_empty(struct k_fifo *fifo) |
| 1226 | { |
| 1227 | return (int)sys_slist_is_empty(&fifo->data_q); |
| 1228 | } |
| 1229 | |
| 1230 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1231 | * @brief Statically define and initialize a fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1232 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1233 | * 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] | 1234 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 1235 | * @code extern struct k_fifo <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1236 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1237 | * @param name Name of the fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1238 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1239 | #define K_FIFO_DEFINE(name) \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 1240 | struct k_fifo name \ |
| 1241 | __in_section(_k_fifo, static, name) = \ |
| 1242 | K_FIFO_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1243 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1244 | /** |
| 1245 | * @} end defgroup fifo_apis |
| 1246 | */ |
| 1247 | |
| 1248 | /** |
| 1249 | * @cond INTERNAL_HIDDEN |
| 1250 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1251 | |
| 1252 | struct k_lifo { |
| 1253 | _wait_q_t wait_q; |
| 1254 | void *list; |
| 1255 | |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 1256 | _OBJECT_TRACING_NEXT_PTR(k_lifo); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1257 | }; |
| 1258 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1259 | #define K_LIFO_INITIALIZER(obj) \ |
| 1260 | { \ |
| 1261 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 1262 | .list = NULL, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 1263 | _OBJECT_TRACING_INIT \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | /** |
| 1267 | * INTERNAL_HIDDEN @endcond |
| 1268 | */ |
| 1269 | |
| 1270 | /** |
| 1271 | * @defgroup lifo_apis Lifo APIs |
| 1272 | * @ingroup kernel_apis |
| 1273 | * @{ |
| 1274 | */ |
| 1275 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1276 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1277 | * @brief Initialize a lifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1278 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1279 | * This routine initializes a lifo object, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1280 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1281 | * @param lifo Address of the lifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1282 | * |
| 1283 | * @return N/A |
| 1284 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1285 | extern void k_lifo_init(struct k_lifo *lifo); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1286 | |
| 1287 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1288 | * @brief Add an element to a lifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1289 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1290 | * This routine adds a data item to @a lifo. A lifo data item must be |
| 1291 | * aligned on a 4-byte boundary, and the first 32 bits of the item are |
| 1292 | * reserved for the kernel's use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1293 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1294 | * @note Can be called by ISRs. |
| 1295 | * |
| 1296 | * @param lifo Address of the lifo. |
| 1297 | * @param data Address of the data item. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1298 | * |
| 1299 | * @return N/A |
| 1300 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1301 | extern void k_lifo_put(struct k_lifo *lifo, void *data); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1302 | |
| 1303 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1304 | * @brief Get an element from a lifo. |
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 | * This routine removes a data item from @a lifo in a "last in, first out" |
| 1307 | * 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] | 1308 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1309 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 1310 | * |
| 1311 | * @param lifo Address of the lifo. |
| 1312 | * @param timeout Waiting period to obtain a data item (in milliseconds), |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1313 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 1314 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 1315 | * @return Address of the data item if successful; NULL if returned |
| 1316 | * without waiting, or waiting period timed out. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1317 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1318 | extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout); |
| 1319 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1320 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1321 | * @brief Statically define and initialize a lifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1322 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1323 | * 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] | 1324 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 1325 | * @code extern struct k_lifo <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1326 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1327 | * @param name Name of the fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1328 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1329 | #define K_LIFO_DEFINE(name) \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 1330 | struct k_lifo name \ |
| 1331 | __in_section(_k_lifo, static, name) = \ |
| 1332 | K_LIFO_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1333 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1334 | /** |
| 1335 | * @} end defgroup lifo_apis |
| 1336 | */ |
| 1337 | |
| 1338 | /** |
| 1339 | * @cond INTERNAL_HIDDEN |
| 1340 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1341 | |
| 1342 | struct k_stack { |
| 1343 | _wait_q_t wait_q; |
| 1344 | uint32_t *base, *next, *top; |
| 1345 | |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 1346 | _OBJECT_TRACING_NEXT_PTR(k_stack); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1347 | }; |
| 1348 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1349 | #define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \ |
| 1350 | { \ |
| 1351 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 1352 | .base = stack_buffer, \ |
| 1353 | .next = stack_buffer, \ |
| 1354 | .top = stack_buffer + stack_num_entries, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 1355 | _OBJECT_TRACING_INIT \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | /** |
| 1359 | * INTERNAL_HIDDEN @endcond |
| 1360 | */ |
| 1361 | |
| 1362 | /** |
| 1363 | * @defgroup stack_apis Stack APIs |
| 1364 | * @ingroup kernel_apis |
| 1365 | * @{ |
| 1366 | */ |
| 1367 | |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1368 | /** |
| 1369 | * @brief Initialize a stack. |
| 1370 | * |
| 1371 | * This routine initializes a stack object, prior to its first use. |
| 1372 | * |
| 1373 | * @param stack Address of the stack. |
| 1374 | * @param buffer Address of array used to hold stacked values. |
| 1375 | * @param num_entries Maximum number of values that can be stacked. |
| 1376 | * |
| 1377 | * @return N/A |
| 1378 | */ |
Allan Stephens | 018cd9a | 2016-10-07 15:13:24 -0500 | [diff] [blame] | 1379 | extern void k_stack_init(struct k_stack *stack, |
| 1380 | uint32_t *buffer, int num_entries); |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1381 | |
| 1382 | /** |
| 1383 | * @brief Push an element onto a stack. |
| 1384 | * |
| 1385 | * This routine adds a 32-bit value @a data to @a stack. |
| 1386 | * |
| 1387 | * @note Can be called by ISRs. |
| 1388 | * |
| 1389 | * @param stack Address of the stack. |
| 1390 | * @param data Value to push onto the stack. |
| 1391 | * |
| 1392 | * @return N/A |
| 1393 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1394 | extern void k_stack_push(struct k_stack *stack, uint32_t data); |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1395 | |
| 1396 | /** |
| 1397 | * @brief Pop an element from a stack. |
| 1398 | * |
| 1399 | * This routine removes a 32-bit value from @a stack in a "last in, first out" |
| 1400 | * manner and stores the value in @a data. |
| 1401 | * |
| 1402 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 1403 | * |
| 1404 | * @param stack Address of the stack. |
| 1405 | * @param data Address of area to hold the value popped from the stack. |
| 1406 | * @param timeout Waiting period to obtain a value (in milliseconds), |
| 1407 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 1408 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 1409 | * @retval 0 Element popped from stack. |
| 1410 | * @retval -EBUSY Returned without waiting. |
| 1411 | * @retval -EAGAIN Waiting period timed out. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1412 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1413 | extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout); |
| 1414 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1415 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1416 | * @brief Statically define and initialize a stack |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1417 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1418 | * 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] | 1419 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 1420 | * @code extern struct k_stack <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1421 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1422 | * @param name Name of the stack. |
| 1423 | * @param stack_num_entries Maximum number of values that can be stacked. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1424 | */ |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 1425 | #define K_STACK_DEFINE(name, stack_num_entries) \ |
| 1426 | uint32_t __noinit \ |
| 1427 | _k_stack_buf_##name[stack_num_entries]; \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 1428 | struct k_stack name \ |
| 1429 | __in_section(_k_stack, static, name) = \ |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 1430 | K_STACK_INITIALIZER(name, _k_stack_buf_##name, \ |
| 1431 | stack_num_entries) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1432 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1433 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1434 | * @} end defgroup stack_apis |
| 1435 | */ |
| 1436 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1437 | struct k_work; |
| 1438 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1439 | /** |
| 1440 | * @defgroup workqueue_apis Workqueue Thread APIs |
| 1441 | * @ingroup kernel_apis |
| 1442 | * @{ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1443 | */ |
| 1444 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1445 | /** |
| 1446 | * @typedef k_work_handler_t |
| 1447 | * @brief Work item handler function type. |
| 1448 | * |
| 1449 | * A work item's handler function is executed by a workqueue's thread |
| 1450 | * when the work item is processed by the workqueue. |
| 1451 | * |
| 1452 | * @param work Address of the work item. |
| 1453 | * |
| 1454 | * @return N/A |
| 1455 | */ |
| 1456 | typedef void (*k_work_handler_t)(struct k_work *work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1457 | |
| 1458 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1459 | * @cond INTERNAL_HIDDEN |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1460 | */ |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1461 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1462 | struct k_work_q { |
| 1463 | struct k_fifo fifo; |
| 1464 | }; |
| 1465 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1466 | enum { |
Iván Briano | 9c7b5ea | 2016-10-04 18:11:05 -0300 | [diff] [blame] | 1467 | K_WORK_STATE_PENDING, /* Work item pending state */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1468 | }; |
| 1469 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1470 | struct k_work { |
| 1471 | void *_reserved; /* Used by k_fifo implementation. */ |
| 1472 | k_work_handler_t handler; |
| 1473 | atomic_t flags[1]; |
| 1474 | }; |
| 1475 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1476 | struct k_delayed_work { |
| 1477 | struct k_work work; |
| 1478 | struct _timeout timeout; |
| 1479 | struct k_work_q *work_q; |
| 1480 | }; |
| 1481 | |
| 1482 | extern struct k_work_q k_sys_work_q; |
| 1483 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1484 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1485 | * INTERNAL_HIDDEN @endcond |
| 1486 | */ |
| 1487 | |
| 1488 | /** |
| 1489 | * @brief Initialize a statically-defined work item. |
| 1490 | * |
| 1491 | * This macro can be used to initialize a statically-defined workqueue work |
| 1492 | * item, prior to its first use. For example, |
| 1493 | * |
| 1494 | * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode |
| 1495 | * |
| 1496 | * @param work_handler Function to invoke each time work item is processed. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1497 | */ |
| 1498 | #define K_WORK_INITIALIZER(work_handler) \ |
| 1499 | { \ |
| 1500 | ._reserved = NULL, \ |
| 1501 | .handler = work_handler, \ |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 1502 | .flags = { 0 } \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1506 | * @brief Initialize a work item. |
| 1507 | * |
| 1508 | * This routine initializes a workqueue work item, prior to its first use. |
| 1509 | * |
| 1510 | * @param work Address of work item. |
| 1511 | * @param handler Function to invoke each time work item is processed. |
| 1512 | * |
| 1513 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1514 | */ |
| 1515 | static inline void k_work_init(struct k_work *work, k_work_handler_t handler) |
| 1516 | { |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 1517 | atomic_clear_bit(work->flags, K_WORK_STATE_PENDING); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1518 | work->handler = handler; |
| 1519 | } |
| 1520 | |
| 1521 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1522 | * @brief Submit a work item. |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 1523 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1524 | * This routine submits work item @a work to be processed by workqueue |
| 1525 | * @a work_q. If the work item is already pending in the workqueue's queue |
| 1526 | * as a result of an earlier submission, this routine has no effect on the |
| 1527 | * work item. If the work item has already been processed, or is currently |
| 1528 | * being processed, its work is considered complete and the work item can be |
| 1529 | * resubmitted. |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 1530 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1531 | * @warning |
| 1532 | * A submitted work item must not be modified until it has been processed |
| 1533 | * by the workqueue. |
| 1534 | * |
| 1535 | * @note Can be called by ISRs. |
| 1536 | * |
| 1537 | * @param work_q Address of workqueue. |
| 1538 | * @param work Address of work item. |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 1539 | * |
| 1540 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1541 | */ |
| 1542 | static inline void k_work_submit_to_queue(struct k_work_q *work_q, |
| 1543 | struct k_work *work) |
| 1544 | { |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 1545 | if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1546 | k_fifo_put(&work_q->fifo, work); |
| 1547 | } |
| 1548 | } |
| 1549 | |
| 1550 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1551 | * @brief Check if a work item is pending. |
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 | * This routine indicates if work item @a work is pending in a workqueue's |
| 1554 | * queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1555 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1556 | * @note Can be called by ISRs. |
| 1557 | * |
| 1558 | * @param work Address of work item. |
| 1559 | * |
| 1560 | * @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] | 1561 | */ |
| 1562 | static inline int k_work_pending(struct k_work *work) |
| 1563 | { |
Iván Briano | 9c7b5ea | 2016-10-04 18:11:05 -0300 | [diff] [blame] | 1564 | return atomic_test_bit(work->flags, K_WORK_STATE_PENDING); |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1568 | * @brief Start a workqueue. |
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 | * This routine starts workqueue @a work_q. The workqueue spawns its work |
| 1571 | * processing thread, which runs forever. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1572 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1573 | * @param work_q Address of workqueue. |
| 1574 | * @param stack Pointer to work queue thread's stack space. |
| 1575 | * @param stack_size Size of the work queue thread's stack (in bytes). |
| 1576 | * @param prio Priority of the work queue's thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1577 | * |
| 1578 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1579 | */ |
Allan Stephens | 904cf97 | 2016-10-07 13:59:23 -0500 | [diff] [blame] | 1580 | 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] | 1581 | size_t stack_size, int prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1582 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1583 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1584 | * @brief Initialize a delayed work item. |
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 | * This routine initializes a workqueue delayed work item, prior to |
| 1587 | * its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1588 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1589 | * @param work Address of delayed work item. |
| 1590 | * @param handler Function to invoke each time work item is processed. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1591 | * |
| 1592 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1593 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 1594 | extern void k_delayed_work_init(struct k_delayed_work *work, |
| 1595 | k_work_handler_t handler); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1596 | |
| 1597 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1598 | * @brief Submit a delayed work item. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1599 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1600 | * This routine schedules work item @a work to be processed by workqueue |
| 1601 | * @a work_q after a delay of @a delay milliseconds. The routine initiates |
| 1602 | * an asychronous countdown for the work item and then returns to the caller. |
| 1603 | * Only when the countdown completes is the work item actually submitted to |
| 1604 | * the workqueue and becomes pending. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1605 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1606 | * Submitting a previously submitted delayed work item that is still |
| 1607 | * counting down cancels the existing submission and restarts the countdown |
| 1608 | * using the new delay. If the work item is currently pending on the |
| 1609 | * workqueue's queue because the countdown has completed it is too late to |
| 1610 | * resubmit the item, and resubmission fails without impacting the work item. |
| 1611 | * If the work item has already been processed, or is currently being processed, |
| 1612 | * its work is considered complete and the work item can be resubmitted. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1613 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1614 | * @warning |
| 1615 | * A delayed work item must not be modified until it has been processed |
| 1616 | * by the workqueue. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1617 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1618 | * @note Can be called by ISRs. |
| 1619 | * |
| 1620 | * @param work_q Address of workqueue. |
| 1621 | * @param work Address of delayed work item. |
| 1622 | * @param delay Delay before submitting the work item (in milliseconds). |
| 1623 | * |
| 1624 | * @retval 0 Work item countdown started. |
| 1625 | * @retval -EINPROGRESS Work item is already pending. |
| 1626 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 1627 | * @retval -EADDRINUSE Work item is pending on a different workqueue. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1628 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 1629 | extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q, |
| 1630 | struct k_delayed_work *work, |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 1631 | int32_t delay); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1632 | |
| 1633 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1634 | * @brief Cancel a delayed work item. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1635 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1636 | * This routine cancels the submission of delayed work item @a work. |
| 1637 | * A delayed work item can only be cancelled while its countdown is still |
| 1638 | * underway. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1639 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1640 | * @note Can be called by ISRs. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1641 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1642 | * @param work Address of delayed work item. |
| 1643 | * |
| 1644 | * @retval 0 Work item countdown cancelled. |
| 1645 | * @retval -EINPROGRESS Work item is already pending. |
| 1646 | * @retval -EINVAL Work item is being processed or has completed its work. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1647 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 1648 | extern int k_delayed_work_cancel(struct k_delayed_work *work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1649 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1650 | /** |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1651 | * @brief Submit a work item to the system workqueue. |
| 1652 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1653 | * This routine submits work item @a work to be processed by the system |
| 1654 | * workqueue. If the work item is already pending in the workqueue's queue |
| 1655 | * as a result of an earlier submission, this routine has no effect on the |
| 1656 | * work item. If the work item has already been processed, or is currently |
| 1657 | * being processed, its work is considered complete and the work item can be |
| 1658 | * resubmitted. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1659 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1660 | * @warning |
| 1661 | * Work items submitted to the system workqueue should avoid using handlers |
| 1662 | * that block or yield since this may prevent the system workqueue from |
| 1663 | * processing other work items in a timely manner. |
| 1664 | * |
| 1665 | * @note Can be called by ISRs. |
| 1666 | * |
| 1667 | * @param work Address of work item. |
| 1668 | * |
| 1669 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1670 | */ |
| 1671 | static inline void k_work_submit(struct k_work *work) |
| 1672 | { |
| 1673 | k_work_submit_to_queue(&k_sys_work_q, work); |
| 1674 | } |
| 1675 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1676 | /** |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1677 | * @brief Submit a delayed work item to the system workqueue. |
| 1678 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1679 | * This routine schedules work item @a work to be processed by the system |
| 1680 | * workqueue after a delay of @a delay milliseconds. The routine initiates |
| 1681 | * an asychronous countdown for the work item and then returns to the caller. |
| 1682 | * Only when the countdown completes is the work item actually submitted to |
| 1683 | * the workqueue and becomes pending. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1684 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1685 | * Submitting a previously submitted delayed work item that is still |
| 1686 | * counting down cancels the existing submission and restarts the countdown |
| 1687 | * using the new delay. If the work item is currently pending on the |
| 1688 | * workqueue's queue because the countdown has completed it is too late to |
| 1689 | * resubmit the item, and resubmission fails without impacting the work item. |
| 1690 | * If the work item has already been processed, or is currently being processed, |
| 1691 | * its work is considered complete and the work item can be resubmitted. |
| 1692 | * |
| 1693 | * @warning |
| 1694 | * Work items submitted to the system workqueue should avoid using handlers |
| 1695 | * that block or yield since this may prevent the system workqueue from |
| 1696 | * processing other work items in a timely manner. |
| 1697 | * |
| 1698 | * @note Can be called by ISRs. |
| 1699 | * |
| 1700 | * @param work Address of delayed work item. |
| 1701 | * @param delay Delay before submitting the work item (in milliseconds). |
| 1702 | * |
| 1703 | * @retval 0 Work item countdown started. |
| 1704 | * @retval -EINPROGRESS Work item is already pending. |
| 1705 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 1706 | * @retval -EADDRINUSE Work item is pending on a different workqueue. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1707 | */ |
| 1708 | static inline int k_delayed_work_submit(struct k_delayed_work *work, |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1709 | int32_t delay) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1710 | { |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 1711 | 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] | 1712 | } |
| 1713 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1714 | /** |
Johan Hedberg | c8201b2 | 2016-12-09 10:42:22 +0200 | [diff] [blame] | 1715 | * @brief Get time remaining before a delayed work gets scheduled. |
| 1716 | * |
| 1717 | * This routine computes the (approximate) time remaining before a |
| 1718 | * delayed work gets executed. If the delayed work is not waiting to be |
| 1719 | * schedules, it returns zero. |
| 1720 | * |
| 1721 | * @param work Delayed work item. |
| 1722 | * |
| 1723 | * @return Remaining time (in milliseconds). |
| 1724 | */ |
| 1725 | static inline int32_t k_delayed_work_remaining_get(struct k_delayed_work *work) |
| 1726 | { |
| 1727 | return _timeout_remaining_get(&work->timeout); |
| 1728 | } |
| 1729 | |
| 1730 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1731 | * @} end defgroup workqueue_apis |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1732 | */ |
| 1733 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1734 | /** |
| 1735 | * @cond INTERNAL_HIDDEN |
| 1736 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1737 | |
| 1738 | struct k_mutex { |
| 1739 | _wait_q_t wait_q; |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 1740 | struct k_thread *owner; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1741 | uint32_t lock_count; |
| 1742 | int owner_orig_prio; |
| 1743 | #ifdef CONFIG_OBJECT_MONITOR |
| 1744 | int num_lock_state_changes; |
| 1745 | int num_conflicts; |
| 1746 | #endif |
| 1747 | |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 1748 | _OBJECT_TRACING_NEXT_PTR(k_mutex); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1749 | }; |
| 1750 | |
| 1751 | #ifdef CONFIG_OBJECT_MONITOR |
| 1752 | #define _MUTEX_INIT_OBJECT_MONITOR \ |
| 1753 | .num_lock_state_changes = 0, .num_conflicts = 0, |
| 1754 | #else |
| 1755 | #define _MUTEX_INIT_OBJECT_MONITOR |
| 1756 | #endif |
| 1757 | |
| 1758 | #define K_MUTEX_INITIALIZER(obj) \ |
| 1759 | { \ |
| 1760 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 1761 | .owner = NULL, \ |
| 1762 | .lock_count = 0, \ |
| 1763 | .owner_orig_prio = K_LOWEST_THREAD_PRIO, \ |
| 1764 | _MUTEX_INIT_OBJECT_MONITOR \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 1765 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1766 | } |
| 1767 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1768 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1769 | * INTERNAL_HIDDEN @endcond |
| 1770 | */ |
| 1771 | |
| 1772 | /** |
| 1773 | * @defgroup mutex_apis Mutex APIs |
| 1774 | * @ingroup kernel_apis |
| 1775 | * @{ |
| 1776 | */ |
| 1777 | |
| 1778 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1779 | * @brief Statically define and initialize a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1780 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1781 | * 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] | 1782 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 1783 | * @code extern struct k_mutex <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1784 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1785 | * @param name Name of the mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1786 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1787 | #define K_MUTEX_DEFINE(name) \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 1788 | struct k_mutex name \ |
| 1789 | __in_section(_k_mutex, static, name) = \ |
| 1790 | K_MUTEX_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1791 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1792 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1793 | * @brief Initialize a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1794 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1795 | * This routine initializes a mutex object, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1796 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1797 | * Upon completion, the mutex is available and does not have an owner. |
| 1798 | * |
| 1799 | * @param mutex Address of the mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1800 | * |
| 1801 | * @return N/A |
| 1802 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1803 | extern void k_mutex_init(struct k_mutex *mutex); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1804 | |
| 1805 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1806 | * @brief Lock a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1807 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1808 | * This routine locks @a mutex. If the mutex is locked by another thread, |
| 1809 | * the calling thread waits until the mutex becomes available or until |
| 1810 | * a timeout occurs. |
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 | * A thread is permitted to lock a mutex it has already locked. The operation |
| 1813 | * completes immediately and the lock count is increased by 1. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1814 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1815 | * @param mutex Address of the mutex. |
| 1816 | * @param timeout Waiting period to lock the mutex (in milliseconds), |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1817 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 1818 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 1819 | * @retval 0 Mutex locked. |
| 1820 | * @retval -EBUSY Returned without waiting. |
| 1821 | * @retval -EAGAIN Waiting period timed out. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1822 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1823 | extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1824 | |
| 1825 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1826 | * @brief Unlock a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1827 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1828 | * This routine unlocks @a mutex. The mutex must already be locked by the |
| 1829 | * calling thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1830 | * |
| 1831 | * 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] | 1832 | * 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] | 1833 | * thread. |
| 1834 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1835 | * @param mutex Address of the mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1836 | * |
| 1837 | * @return N/A |
| 1838 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1839 | extern void k_mutex_unlock(struct k_mutex *mutex); |
| 1840 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1841 | /** |
| 1842 | * @} end defgroup mutex_apis |
| 1843 | */ |
| 1844 | |
| 1845 | /** |
| 1846 | * @cond INTERNAL_HIDDEN |
| 1847 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1848 | |
| 1849 | struct k_sem { |
| 1850 | _wait_q_t wait_q; |
| 1851 | unsigned int count; |
| 1852 | unsigned int limit; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 1853 | _POLL_EVENT; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1854 | |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 1855 | _OBJECT_TRACING_NEXT_PTR(k_sem); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1856 | }; |
| 1857 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1858 | #define K_SEM_INITIALIZER(obj, initial_count, count_limit) \ |
| 1859 | { \ |
| 1860 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 1861 | .count = initial_count, \ |
| 1862 | .limit = count_limit, \ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 1863 | _POLL_EVENT_OBJ_INIT \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 1864 | _OBJECT_TRACING_INIT \ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | /** |
| 1868 | * INTERNAL_HIDDEN @endcond |
| 1869 | */ |
| 1870 | |
| 1871 | /** |
| 1872 | * @defgroup semaphore_apis Semaphore APIs |
| 1873 | * @ingroup kernel_apis |
| 1874 | * @{ |
| 1875 | */ |
| 1876 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1877 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1878 | * @brief Initialize a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1879 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1880 | * This routine initializes a semaphore object, prior to its first use. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1881 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1882 | * @param sem Address of the semaphore. |
| 1883 | * @param initial_count Initial semaphore count. |
| 1884 | * @param limit Maximum permitted semaphore count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1885 | * |
| 1886 | * @return N/A |
| 1887 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1888 | extern void k_sem_init(struct k_sem *sem, unsigned int initial_count, |
| 1889 | unsigned int limit); |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1890 | |
| 1891 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1892 | * @brief Take a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1893 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1894 | * This routine takes @a sem. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1895 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1896 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 1897 | * |
| 1898 | * @param sem Address of the semaphore. |
| 1899 | * @param timeout Waiting period to take the semaphore (in milliseconds), |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1900 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 1901 | * |
Benjamin Walsh | 91f834c | 2016-12-01 11:39:49 -0500 | [diff] [blame] | 1902 | * @note When porting code from the nanokernel legacy API to the new API, be |
| 1903 | * careful with the return value of this function. The return value is the |
| 1904 | * reverse of the one of nano_sem_take family of APIs: 0 means success, and |
| 1905 | * non-zero means failure, while the nano_sem_take family returns 1 for success |
| 1906 | * and 0 for failure. |
| 1907 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 1908 | * @retval 0 Semaphore taken. |
| 1909 | * @retval -EBUSY Returned without waiting. |
| 1910 | * @retval -EAGAIN Waiting period timed out. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1911 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1912 | extern int k_sem_take(struct k_sem *sem, int32_t timeout); |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1913 | |
| 1914 | /** |
| 1915 | * @brief Give a semaphore. |
| 1916 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1917 | * This routine gives @a sem, unless the semaphore is already at its maximum |
| 1918 | * permitted count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1919 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1920 | * @note Can be called by ISRs. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1921 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1922 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1923 | * |
| 1924 | * @return N/A |
| 1925 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1926 | extern void k_sem_give(struct k_sem *sem); |
| 1927 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1928 | /** |
| 1929 | * @brief Reset a semaphore's count to zero. |
| 1930 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1931 | * This routine sets the count of @a sem to zero. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1932 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1933 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1934 | * |
| 1935 | * @return N/A |
| 1936 | */ |
Benjamin Walsh | 70c68b9 | 2016-09-21 10:37:34 -0400 | [diff] [blame] | 1937 | static inline void k_sem_reset(struct k_sem *sem) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1938 | { |
| 1939 | sem->count = 0; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1940 | } |
| 1941 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1942 | /** |
| 1943 | * @brief Get a semaphore's count. |
| 1944 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1945 | * This routine returns the current count of @a sem. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1946 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1947 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1948 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1949 | * @return Current semaphore count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1950 | */ |
Tomasz Bursztyka | 276086d | 2016-09-21 16:03:21 +0200 | [diff] [blame] | 1951 | static inline unsigned int k_sem_count_get(struct k_sem *sem) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1952 | { |
| 1953 | return sem->count; |
| 1954 | } |
| 1955 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1956 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1957 | * @brief Statically define and initialize a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1958 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1959 | * 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] | 1960 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 1961 | * @code extern struct k_sem <name>; @endcode |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1962 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1963 | * @param name Name of the semaphore. |
| 1964 | * @param initial_count Initial semaphore count. |
| 1965 | * @param count_limit Maximum permitted semaphore count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1966 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1967 | #define K_SEM_DEFINE(name, initial_count, count_limit) \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 1968 | struct k_sem name \ |
| 1969 | __in_section(_k_sem, static, name) = \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1970 | K_SEM_INITIALIZER(name, initial_count, count_limit) |
| 1971 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1972 | /** |
| 1973 | * @} end defgroup semaphore_apis |
| 1974 | */ |
| 1975 | |
| 1976 | /** |
| 1977 | * @defgroup alert_apis Alert APIs |
| 1978 | * @ingroup kernel_apis |
| 1979 | * @{ |
| 1980 | */ |
| 1981 | |
Allan Stephens | 5eceb85 | 2016-11-16 10:16:30 -0500 | [diff] [blame] | 1982 | /** |
| 1983 | * @typedef k_alert_handler_t |
| 1984 | * @brief Alert handler function type. |
| 1985 | * |
| 1986 | * An alert's alert handler function is invoked by the system workqueue |
| 1987 | * when the alert is signalled. The alert handler function is optional, |
| 1988 | * and is only invoked if the alert has been initialized with one. |
| 1989 | * |
| 1990 | * @param alert Address of the alert. |
| 1991 | * |
| 1992 | * @return 0 if alert has been consumed; non-zero if alert should pend. |
| 1993 | */ |
| 1994 | typedef int (*k_alert_handler_t)(struct k_alert *alert); |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1995 | |
| 1996 | /** |
| 1997 | * @} end defgroup alert_apis |
| 1998 | */ |
| 1999 | |
| 2000 | /** |
| 2001 | * @cond INTERNAL_HIDDEN |
| 2002 | */ |
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 | #define K_ALERT_DEFAULT NULL |
| 2005 | #define K_ALERT_IGNORE ((void *)(-1)) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2006 | |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 2007 | struct k_alert { |
| 2008 | k_alert_handler_t handler; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2009 | atomic_t send_count; |
| 2010 | struct k_work work_item; |
| 2011 | struct k_sem sem; |
| 2012 | |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 2013 | _OBJECT_TRACING_NEXT_PTR(k_alert); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2014 | }; |
| 2015 | |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 2016 | extern void _alert_deliver(struct k_work *work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2017 | |
Peter Mitsis | 058fa4e | 2016-10-25 14:42:30 -0400 | [diff] [blame] | 2018 | #define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2019 | { \ |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 2020 | .handler = (k_alert_handler_t)alert_handler, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2021 | .send_count = ATOMIC_INIT(0), \ |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 2022 | .work_item = K_WORK_INITIALIZER(_alert_deliver), \ |
Peter Mitsis | 058fa4e | 2016-10-25 14:42:30 -0400 | [diff] [blame] | 2023 | .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 2024 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2025 | } |
| 2026 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2027 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2028 | * INTERNAL_HIDDEN @endcond |
| 2029 | */ |
| 2030 | |
| 2031 | /** |
| 2032 | * @addtogroup alert_apis |
| 2033 | * @{ |
| 2034 | */ |
| 2035 | |
| 2036 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2037 | * @brief Statically define and initialize an alert. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2038 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 2039 | * 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] | 2040 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 2041 | * @code extern struct k_alert <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2042 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2043 | * @param name Name of the alert. |
| 2044 | * @param alert_handler Action to take when alert is sent. Specify either |
| 2045 | * the address of a function to be invoked by the system workqueue |
| 2046 | * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or |
| 2047 | * K_ALERT_DEFAULT (which causes the alert to pend). |
| 2048 | * @param max_num_pending_alerts Maximum number of pending alerts. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2049 | */ |
Peter Mitsis | 058fa4e | 2016-10-25 14:42:30 -0400 | [diff] [blame] | 2050 | #define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \ |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 2051 | struct k_alert name \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 2052 | __in_section(_k_alert, static, name) = \ |
Peter Mitsis | 058fa4e | 2016-10-25 14:42:30 -0400 | [diff] [blame] | 2053 | K_ALERT_INITIALIZER(name, alert_handler, \ |
| 2054 | max_num_pending_alerts) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2055 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2056 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2057 | * @brief Initialize an alert. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2058 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2059 | * This routine initializes an alert object, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2060 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2061 | * @param alert Address of the alert. |
| 2062 | * @param handler Action to take when alert is sent. Specify either the address |
| 2063 | * of a function to be invoked by the system workqueue thread, |
| 2064 | * K_ALERT_IGNORE (which causes the alert to be ignored), or |
| 2065 | * K_ALERT_DEFAULT (which causes the alert to pend). |
| 2066 | * @param max_num_pending_alerts Maximum number of pending alerts. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2067 | * |
| 2068 | * @return N/A |
| 2069 | */ |
Peter Mitsis | 058fa4e | 2016-10-25 14:42:30 -0400 | [diff] [blame] | 2070 | extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler, |
| 2071 | unsigned int max_num_pending_alerts); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2072 | |
| 2073 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2074 | * @brief Receive an alert. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2075 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2076 | * This routine receives a pending alert for @a alert. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2077 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2078 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 2079 | * |
| 2080 | * @param alert Address of the alert. |
| 2081 | * @param timeout Waiting period to receive the alert (in milliseconds), |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2082 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 2083 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2084 | * @retval 0 Alert received. |
| 2085 | * @retval -EBUSY Returned without waiting. |
| 2086 | * @retval -EAGAIN Waiting period timed out. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2087 | */ |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 2088 | extern int k_alert_recv(struct k_alert *alert, int32_t timeout); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2089 | |
| 2090 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2091 | * @brief Signal an alert. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2092 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2093 | * This routine signals @a alert. The action specified for @a alert will |
| 2094 | * be taken, which may trigger the execution of an alert handler function |
| 2095 | * and/or cause the alert to pend (assuming the alert has not reached its |
| 2096 | * maximum number of pending alerts). |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2097 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2098 | * @note Can be called by ISRs. |
| 2099 | * |
| 2100 | * @param alert Address of the alert. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2101 | * |
| 2102 | * @return N/A |
| 2103 | */ |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 2104 | extern void k_alert_send(struct k_alert *alert); |
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 | * @} end addtogroup alert_apis |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2108 | */ |
| 2109 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2110 | /** |
| 2111 | * @cond INTERNAL_HIDDEN |
| 2112 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2113 | |
| 2114 | struct k_msgq { |
| 2115 | _wait_q_t wait_q; |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 2116 | size_t msg_size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2117 | uint32_t max_msgs; |
| 2118 | char *buffer_start; |
| 2119 | char *buffer_end; |
| 2120 | char *read_ptr; |
| 2121 | char *write_ptr; |
| 2122 | uint32_t used_msgs; |
| 2123 | |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 2124 | _OBJECT_TRACING_NEXT_PTR(k_msgq); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2125 | }; |
| 2126 | |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 2127 | #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] | 2128 | { \ |
| 2129 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 2130 | .max_msgs = q_max_msgs, \ |
| 2131 | .msg_size = q_msg_size, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2132 | .buffer_start = q_buffer, \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 2133 | .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2134 | .read_ptr = q_buffer, \ |
| 2135 | .write_ptr = q_buffer, \ |
| 2136 | .used_msgs = 0, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 2137 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2138 | } |
| 2139 | |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 2140 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2141 | * INTERNAL_HIDDEN @endcond |
| 2142 | */ |
| 2143 | |
| 2144 | /** |
| 2145 | * @defgroup msgq_apis Message Queue APIs |
| 2146 | * @ingroup kernel_apis |
| 2147 | * @{ |
| 2148 | */ |
| 2149 | |
| 2150 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2151 | * @brief Statically define and initialize a message queue. |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 2152 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2153 | * The message queue's ring buffer contains space for @a q_max_msgs messages, |
| 2154 | * 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] | 2155 | * @a q_align -byte boundary, which must be a power of 2. To ensure that each |
| 2156 | * message is similarly aligned to this boundary, @a q_msg_size must also be |
| 2157 | * a multiple of @a q_align. |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 2158 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2159 | * The message queue can be accessed outside the module where it is defined |
| 2160 | * using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2161 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 2162 | * @code extern struct k_msgq <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2163 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2164 | * @param q_name Name of the message queue. |
| 2165 | * @param q_msg_size Message size (in bytes). |
| 2166 | * @param q_max_msgs Maximum number of messages that can be queued. |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 2167 | * @param q_align Alignment of the message queue's ring buffer. |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 2168 | */ |
| 2169 | #define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \ |
| 2170 | static char __noinit __aligned(q_align) \ |
| 2171 | _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 2172 | struct k_msgq q_name \ |
| 2173 | __in_section(_k_msgq, static, q_name) = \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 2174 | K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \ |
| 2175 | q_msg_size, q_max_msgs) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2176 | |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2177 | /** |
| 2178 | * @brief Initialize a message queue. |
| 2179 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2180 | * This routine initializes a message queue object, prior to its first use. |
| 2181 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 2182 | * The message queue's ring buffer must contain space for @a max_msgs messages, |
| 2183 | * each of which is @a msg_size bytes long. The buffer must be aligned to an |
| 2184 | * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure |
| 2185 | * that each message is similarly aligned to this boundary, @a q_msg_size |
| 2186 | * must also be a multiple of N. |
| 2187 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2188 | * @param q Address of the message queue. |
| 2189 | * @param buffer Pointer to ring buffer that holds queued messages. |
| 2190 | * @param msg_size Message size (in bytes). |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2191 | * @param max_msgs Maximum number of messages that can be queued. |
| 2192 | * |
| 2193 | * @return N/A |
| 2194 | */ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 2195 | extern void k_msgq_init(struct k_msgq *q, char *buffer, |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 2196 | size_t msg_size, uint32_t max_msgs); |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2197 | |
| 2198 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2199 | * @brief Send a message to a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2200 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2201 | * This routine sends a message to message queue @a q. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2202 | * |
Benjamin Walsh | 8215ce1 | 2016-11-09 19:45:19 -0500 | [diff] [blame] | 2203 | * @note Can be called by ISRs. |
| 2204 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2205 | * @param q Address of the message queue. |
| 2206 | * @param data Pointer to the message. |
| 2207 | * @param timeout Waiting period to add the message (in milliseconds), |
| 2208 | * or one of the special values K_NO_WAIT and K_FOREVER. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2209 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2210 | * @retval 0 Message sent. |
| 2211 | * @retval -ENOMSG Returned without waiting or queue purged. |
| 2212 | * @retval -EAGAIN Waiting period timed out. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2213 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2214 | 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] | 2215 | |
| 2216 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2217 | * @brief Receive a message from a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2218 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2219 | * This routine receives a message from message queue @a q in a "first in, |
| 2220 | * first out" manner. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2221 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2222 | * @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] | 2223 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2224 | * @param q Address of the message queue. |
| 2225 | * @param data Address of area to hold the received message. |
| 2226 | * @param timeout Waiting period to receive the message (in milliseconds), |
| 2227 | * or one of the special values K_NO_WAIT and K_FOREVER. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2228 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2229 | * @retval 0 Message received. |
| 2230 | * @retval -ENOMSG Returned without waiting. |
| 2231 | * @retval -EAGAIN Waiting period timed out. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2232 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2233 | 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] | 2234 | |
| 2235 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2236 | * @brief Purge a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2237 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2238 | * This routine discards all unreceived messages in a message queue's ring |
| 2239 | * buffer. Any threads that are blocked waiting to send a message to the |
| 2240 | * message queue are unblocked and see an -ENOMSG error code. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2241 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2242 | * @param q Address of the message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2243 | * |
| 2244 | * @return N/A |
| 2245 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2246 | extern void k_msgq_purge(struct k_msgq *q); |
| 2247 | |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 2248 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2249 | * @brief Get the amount of free space in a message queue. |
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 | * This routine returns the number of unused entries in a message queue's |
| 2252 | * ring buffer. |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 2253 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2254 | * @param q Address of the message queue. |
| 2255 | * |
| 2256 | * @return Number of unused ring buffer entries. |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 2257 | */ |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 2258 | 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] | 2259 | { |
| 2260 | return q->max_msgs - q->used_msgs; |
| 2261 | } |
| 2262 | |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2263 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2264 | * @brief Get the number of messages in a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2265 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2266 | * 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] | 2267 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2268 | * @param q Address of the message queue. |
| 2269 | * |
| 2270 | * @return Number of messages. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2271 | */ |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 2272 | 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] | 2273 | { |
| 2274 | return q->used_msgs; |
| 2275 | } |
| 2276 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2277 | /** |
| 2278 | * @} end defgroup msgq_apis |
| 2279 | */ |
| 2280 | |
| 2281 | /** |
| 2282 | * @defgroup mem_pool_apis Memory Pool APIs |
| 2283 | * @ingroup kernel_apis |
| 2284 | * @{ |
| 2285 | */ |
| 2286 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2287 | struct k_mem_block { |
Peter Mitsis | 0cb65c3 | 2016-09-29 14:07:36 -0400 | [diff] [blame] | 2288 | struct k_mem_pool *pool_id; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2289 | void *addr_in_pool; |
| 2290 | void *data; |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 2291 | size_t req_size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2292 | }; |
| 2293 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2294 | /** |
| 2295 | * @} end defgroup mem_pool_apis |
| 2296 | */ |
| 2297 | |
| 2298 | /** |
| 2299 | * @defgroup mailbox_apis Mailbox APIs |
| 2300 | * @ingroup kernel_apis |
| 2301 | * @{ |
| 2302 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2303 | |
| 2304 | struct k_mbox_msg { |
| 2305 | /** internal use only - needed for legacy API support */ |
| 2306 | uint32_t _mailbox; |
| 2307 | /** size of message (in bytes) */ |
Peter Mitsis | d93078c | 2016-10-14 12:59:37 -0400 | [diff] [blame] | 2308 | size_t size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2309 | /** application-defined information value */ |
| 2310 | uint32_t info; |
| 2311 | /** sender's message data buffer */ |
| 2312 | void *tx_data; |
| 2313 | /** internal use only - needed for legacy API support */ |
| 2314 | void *_rx_data; |
| 2315 | /** message data block descriptor */ |
| 2316 | struct k_mem_block tx_block; |
| 2317 | /** source thread id */ |
| 2318 | k_tid_t rx_source_thread; |
| 2319 | /** target thread id */ |
| 2320 | k_tid_t tx_target_thread; |
| 2321 | /** internal use only - thread waiting on send (may be a dummy) */ |
| 2322 | k_tid_t _syncing_thread; |
| 2323 | #if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0) |
| 2324 | /** internal use only - semaphore used during asynchronous send */ |
| 2325 | struct k_sem *_async_sem; |
| 2326 | #endif |
| 2327 | }; |
| 2328 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2329 | /** |
| 2330 | * @cond INTERNAL_HIDDEN |
| 2331 | */ |
| 2332 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2333 | struct k_mbox { |
| 2334 | _wait_q_t tx_msg_queue; |
| 2335 | _wait_q_t rx_msg_queue; |
| 2336 | |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 2337 | _OBJECT_TRACING_NEXT_PTR(k_mbox); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2338 | }; |
| 2339 | |
| 2340 | #define K_MBOX_INITIALIZER(obj) \ |
| 2341 | { \ |
| 2342 | .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \ |
| 2343 | .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 2344 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2345 | } |
| 2346 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2347 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2348 | * INTERNAL_HIDDEN @endcond |
| 2349 | */ |
| 2350 | |
| 2351 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2352 | * @brief Statically define and initialize a mailbox. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2353 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2354 | * 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] | 2355 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 2356 | * @code extern struct k_mbox <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2357 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2358 | * @param name Name of the mailbox. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2359 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2360 | #define K_MBOX_DEFINE(name) \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 2361 | struct k_mbox name \ |
| 2362 | __in_section(_k_mbox, static, name) = \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2363 | K_MBOX_INITIALIZER(name) \ |
| 2364 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2365 | /** |
| 2366 | * @brief Initialize a mailbox. |
| 2367 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2368 | * This routine initializes a mailbox object, prior to its first use. |
| 2369 | * |
| 2370 | * @param mbox Address of the mailbox. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2371 | * |
| 2372 | * @return N/A |
| 2373 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2374 | extern void k_mbox_init(struct k_mbox *mbox); |
| 2375 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2376 | /** |
| 2377 | * @brief Send a mailbox message in a synchronous manner. |
| 2378 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2379 | * This routine sends a message to @a mbox and waits for a receiver to both |
| 2380 | * receive and process it. The message data may be in a buffer, in a memory |
| 2381 | * pool block, or non-existent (i.e. an empty message). |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2382 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2383 | * @param mbox Address of the mailbox. |
| 2384 | * @param tx_msg Address of the transmit message descriptor. |
| 2385 | * @param timeout Waiting period for the message to be received (in |
| 2386 | * milliseconds), or one of the special values K_NO_WAIT |
| 2387 | * and K_FOREVER. Once the message has been received, |
| 2388 | * this routine waits as long as necessary for the message |
| 2389 | * to be completely processed. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2390 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2391 | * @retval 0 Message sent. |
| 2392 | * @retval -ENOMSG Returned without waiting. |
| 2393 | * @retval -EAGAIN Waiting period timed out. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2394 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 2395 | 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] | 2396 | int32_t timeout); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2397 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2398 | /** |
| 2399 | * @brief Send a mailbox message in an asynchronous manner. |
| 2400 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2401 | * This routine sends a message to @a mbox without waiting for a receiver |
| 2402 | * to process it. The message data may be in a buffer, in a memory pool block, |
| 2403 | * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem |
| 2404 | * will be given when the message has been both received and completely |
| 2405 | * processed by the receiver. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2406 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2407 | * @param mbox Address of the mailbox. |
| 2408 | * @param tx_msg Address of the transmit message descriptor. |
| 2409 | * @param sem Address of a semaphore, or NULL if none is needed. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2410 | * |
| 2411 | * @return N/A |
| 2412 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 2413 | 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] | 2414 | struct k_sem *sem); |
| 2415 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2416 | /** |
| 2417 | * @brief Receive a mailbox message. |
| 2418 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2419 | * This routine receives a message from @a mbox, then optionally retrieves |
| 2420 | * its data and disposes of the message. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2421 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2422 | * @param mbox Address of the mailbox. |
| 2423 | * @param rx_msg Address of the receive message descriptor. |
| 2424 | * @param buffer Address of the buffer to receive data, or NULL to defer data |
| 2425 | * retrieval and message disposal until later. |
| 2426 | * @param timeout Waiting period for a message to be received (in |
| 2427 | * milliseconds), or one of the special values K_NO_WAIT |
| 2428 | * and K_FOREVER. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2429 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2430 | * @retval 0 Message received. |
| 2431 | * @retval -ENOMSG Returned without waiting. |
| 2432 | * @retval -EAGAIN Waiting period timed out. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2433 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 2434 | 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] | 2435 | void *buffer, int32_t timeout); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2436 | |
| 2437 | /** |
| 2438 | * @brief Retrieve mailbox message data into a buffer. |
| 2439 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2440 | * This routine completes the processing of a received message by retrieving |
| 2441 | * its data into a buffer, then disposing of the message. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2442 | * |
| 2443 | * Alternatively, this routine can be used to dispose of a received message |
| 2444 | * without retrieving its data. |
| 2445 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2446 | * @param rx_msg Address of the receive message descriptor. |
| 2447 | * @param buffer Address of the buffer to receive data, or NULL to discard |
| 2448 | * the data. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2449 | * |
| 2450 | * @return N/A |
| 2451 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 2452 | 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] | 2453 | |
| 2454 | /** |
| 2455 | * @brief Retrieve mailbox message data into a memory pool block. |
| 2456 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2457 | * This routine completes the processing of a received message by retrieving |
| 2458 | * its data into a memory pool block, then disposing of the message. |
| 2459 | * The memory pool block that results from successful retrieval must be |
| 2460 | * returned to the pool once the data has been processed, even in cases |
| 2461 | * where zero bytes of data are retrieved. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2462 | * |
| 2463 | * Alternatively, this routine can be used to dispose of a received message |
| 2464 | * without retrieving its data. In this case there is no need to return a |
| 2465 | * memory pool block to the pool. |
| 2466 | * |
| 2467 | * This routine allocates a new memory pool block for the data only if the |
| 2468 | * data is not already in one. If a new block cannot be allocated, the routine |
| 2469 | * returns a failure code and the received message is left unchanged. This |
| 2470 | * permits the caller to reattempt data retrieval at a later time or to dispose |
| 2471 | * of the received message without retrieving its data. |
| 2472 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2473 | * @param rx_msg Address of a receive message descriptor. |
| 2474 | * @param pool Address of memory pool, or NULL to discard data. |
| 2475 | * @param block Address of the area to hold memory pool block info. |
| 2476 | * @param timeout Waiting period to wait for a memory pool block (in |
| 2477 | * milliseconds), or one of the special values K_NO_WAIT |
| 2478 | * and K_FOREVER. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2479 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2480 | * @retval 0 Data retrieved. |
| 2481 | * @retval -ENOMEM Returned without waiting. |
| 2482 | * @retval -EAGAIN Waiting period timed out. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2483 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 2484 | 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] | 2485 | struct k_mem_pool *pool, |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2486 | struct k_mem_block *block, int32_t timeout); |
| 2487 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2488 | /** |
| 2489 | * @} end defgroup mailbox_apis |
| 2490 | */ |
| 2491 | |
| 2492 | /** |
| 2493 | * @cond INTERNAL_HIDDEN |
| 2494 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2495 | |
| 2496 | struct k_pipe { |
| 2497 | unsigned char *buffer; /* Pipe buffer: may be NULL */ |
| 2498 | size_t size; /* Buffer size */ |
| 2499 | size_t bytes_used; /* # bytes used in buffer */ |
| 2500 | size_t read_index; /* Where in buffer to read from */ |
| 2501 | size_t write_index; /* Where in buffer to write */ |
| 2502 | |
| 2503 | struct { |
| 2504 | _wait_q_t readers; /* Reader wait queue */ |
| 2505 | _wait_q_t writers; /* Writer wait queue */ |
| 2506 | } wait_q; |
| 2507 | |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 2508 | _OBJECT_TRACING_NEXT_PTR(k_pipe); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2509 | }; |
| 2510 | |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 2511 | #define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2512 | { \ |
| 2513 | .buffer = pipe_buffer, \ |
| 2514 | .size = pipe_buffer_size, \ |
| 2515 | .bytes_used = 0, \ |
| 2516 | .read_index = 0, \ |
| 2517 | .write_index = 0, \ |
| 2518 | .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \ |
| 2519 | .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 2520 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2521 | } |
| 2522 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2523 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2524 | * INTERNAL_HIDDEN @endcond |
| 2525 | */ |
| 2526 | |
| 2527 | /** |
| 2528 | * @defgroup pipe_apis Pipe APIs |
| 2529 | * @ingroup kernel_apis |
| 2530 | * @{ |
| 2531 | */ |
| 2532 | |
| 2533 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2534 | * @brief Statically define and initialize a pipe. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2535 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2536 | * 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] | 2537 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 2538 | * @code extern struct k_pipe <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2539 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2540 | * @param name Name of the pipe. |
| 2541 | * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes), |
| 2542 | * or zero if no ring buffer is used. |
| 2543 | * @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] | 2544 | */ |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 2545 | #define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \ |
| 2546 | static unsigned char __noinit __aligned(pipe_align) \ |
| 2547 | _k_pipe_buf_##name[pipe_buffer_size]; \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 2548 | struct k_pipe name \ |
| 2549 | __in_section(_k_pipe, static, name) = \ |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 2550 | K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2551 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2552 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2553 | * @brief Initialize a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2554 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2555 | * This routine initializes a pipe object, prior to its first use. |
| 2556 | * |
| 2557 | * @param pipe Address of the pipe. |
| 2558 | * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer |
| 2559 | * is used. |
| 2560 | * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring |
| 2561 | * buffer is used. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2562 | * |
| 2563 | * @return N/A |
| 2564 | */ |
| 2565 | extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, |
| 2566 | size_t size); |
| 2567 | |
| 2568 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2569 | * @brief Write data to a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2570 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2571 | * 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] | 2572 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2573 | * @param pipe Address of the pipe. |
| 2574 | * @param data Address of data to write. |
| 2575 | * @param bytes_to_write Size of data (in bytes). |
| 2576 | * @param bytes_written Address of area to hold the number of bytes written. |
| 2577 | * @param min_xfer Minimum number of bytes to write. |
| 2578 | * @param timeout Waiting period to wait for the data to be written (in |
| 2579 | * milliseconds), or one of the special values K_NO_WAIT |
| 2580 | * and K_FOREVER. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2581 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2582 | * @retval 0 At least @a min_xfer bytes of data were written. |
| 2583 | * @retval -EIO Returned without waiting; zero data bytes were written. |
| 2584 | * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2585 | * minus one data bytes were written. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2586 | */ |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 2587 | extern int k_pipe_put(struct k_pipe *pipe, void *data, |
| 2588 | size_t bytes_to_write, size_t *bytes_written, |
| 2589 | size_t min_xfer, int32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2590 | |
| 2591 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2592 | * @brief Read data from a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2593 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2594 | * 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] | 2595 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2596 | * @param pipe Address of the pipe. |
| 2597 | * @param data Address to place the data read from pipe. |
| 2598 | * @param bytes_to_read Maximum number of data bytes to read. |
| 2599 | * @param bytes_read Address of area to hold the number of bytes read. |
| 2600 | * @param min_xfer Minimum number of data bytes to read. |
| 2601 | * @param timeout Waiting period to wait for the data to be read (in |
| 2602 | * milliseconds), or one of the special values K_NO_WAIT |
| 2603 | * and K_FOREVER. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2604 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2605 | * @retval 0 At least @a min_xfer bytes of data were read. |
| 2606 | * @retval -EIO Returned without waiting; zero data bytes were read. |
| 2607 | * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2608 | * minus one data bytes were read. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2609 | */ |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 2610 | extern int k_pipe_get(struct k_pipe *pipe, void *data, |
| 2611 | size_t bytes_to_read, size_t *bytes_read, |
| 2612 | size_t min_xfer, int32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2613 | |
| 2614 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2615 | * @brief Write memory block to a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2616 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2617 | * This routine writes the data contained in a memory block to @a pipe. |
| 2618 | * 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] | 2619 | * 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] | 2620 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2621 | * @param pipe Address of the pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2622 | * @param block Memory block containing data to send |
| 2623 | * @param size Number of data bytes in memory block to send |
| 2624 | * @param sem Semaphore to signal upon completion (else NULL) |
| 2625 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2626 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2627 | */ |
| 2628 | extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block, |
| 2629 | size_t size, struct k_sem *sem); |
| 2630 | |
| 2631 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2632 | * @} end defgroup pipe_apis |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2633 | */ |
| 2634 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2635 | /** |
| 2636 | * @cond INTERNAL_HIDDEN |
| 2637 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2638 | |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2639 | struct k_mem_slab { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2640 | _wait_q_t wait_q; |
Peter Mitsis | fb02d57 | 2016-10-13 16:55:45 -0400 | [diff] [blame] | 2641 | uint32_t num_blocks; |
| 2642 | size_t block_size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2643 | char *buffer; |
| 2644 | char *free_list; |
Peter Mitsis | fb02d57 | 2016-10-13 16:55:45 -0400 | [diff] [blame] | 2645 | uint32_t num_used; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2646 | |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 2647 | _OBJECT_TRACING_NEXT_PTR(k_mem_slab); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2648 | }; |
| 2649 | |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2650 | #define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \ |
| 2651 | slab_num_blocks) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2652 | { \ |
| 2653 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2654 | .num_blocks = slab_num_blocks, \ |
| 2655 | .block_size = slab_block_size, \ |
| 2656 | .buffer = slab_buffer, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2657 | .free_list = NULL, \ |
| 2658 | .num_used = 0, \ |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 2659 | _OBJECT_TRACING_INIT \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2660 | } |
| 2661 | |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 2662 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2663 | * INTERNAL_HIDDEN @endcond |
| 2664 | */ |
| 2665 | |
| 2666 | /** |
| 2667 | * @defgroup mem_slab_apis Memory Slab APIs |
| 2668 | * @ingroup kernel_apis |
| 2669 | * @{ |
| 2670 | */ |
| 2671 | |
| 2672 | /** |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 2673 | * @brief Statically define and initialize a memory slab. |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 2674 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 2675 | * The memory slab's buffer contains @a slab_num_blocks memory blocks |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2676 | * 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] | 2677 | * @a slab_align -byte boundary. To ensure that each memory block is similarly |
| 2678 | * 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] | 2679 | * @a slab_align. |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 2680 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 2681 | * 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] | 2682 | * using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2683 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 2684 | * @code extern struct k_mem_slab <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2685 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2686 | * @param name Name of the memory slab. |
| 2687 | * @param slab_block_size Size of each memory block (in bytes). |
| 2688 | * @param slab_num_blocks Number memory blocks. |
| 2689 | * @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] | 2690 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2691 | #define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \ |
| 2692 | char __noinit __aligned(slab_align) \ |
| 2693 | _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \ |
| 2694 | struct k_mem_slab name \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 2695 | __in_section(_k_mem_slab, static, name) = \ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2696 | K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \ |
| 2697 | slab_block_size, slab_num_blocks) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2698 | |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2699 | /** |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2700 | * @brief Initialize a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2701 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2702 | * Initializes a memory slab, prior to its first use. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2703 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 2704 | * The memory slab's buffer contains @a slab_num_blocks memory blocks |
| 2705 | * that are @a slab_block_size bytes long. The buffer must be aligned to an |
| 2706 | * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...). |
| 2707 | * To ensure that each memory block is similarly aligned to this boundary, |
| 2708 | * @a slab_block_size must also be a multiple of N. |
| 2709 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2710 | * @param slab Address of the memory slab. |
| 2711 | * @param buffer Pointer to buffer used for the memory blocks. |
| 2712 | * @param block_size Size of each memory block (in bytes). |
| 2713 | * @param num_blocks Number of memory blocks. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2714 | * |
| 2715 | * @return N/A |
| 2716 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2717 | 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] | 2718 | size_t block_size, uint32_t num_blocks); |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2719 | |
| 2720 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2721 | * @brief Allocate memory from a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2722 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2723 | * This routine allocates a memory block from a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2724 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2725 | * @param slab Address of the memory slab. |
| 2726 | * @param mem Pointer to block address area. |
| 2727 | * @param timeout Maximum time to wait for operation to complete |
| 2728 | * (in milliseconds). Use K_NO_WAIT to return without waiting, |
| 2729 | * or K_FOREVER to wait as long as necessary. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2730 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2731 | * @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] | 2732 | * is set to the starting address of the memory block. |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2733 | * @retval -ENOMEM Returned without waiting. |
| 2734 | * @retval -EAGAIN Waiting period timed out. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2735 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2736 | extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem, |
| 2737 | int32_t timeout); |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2738 | |
| 2739 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2740 | * @brief Free memory allocated from a 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 | * This routine releases a previously allocated memory block back to its |
| 2743 | * associated memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2744 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2745 | * @param slab Address of the memory slab. |
| 2746 | * @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] | 2747 | * |
| 2748 | * @return N/A |
| 2749 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2750 | 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] | 2751 | |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2752 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2753 | * @brief Get the number of used blocks in a memory 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 | * This routine gets the number of memory blocks that are currently |
| 2756 | * allocated in @a slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2757 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2758 | * @param slab Address of the memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2759 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2760 | * @return Number of allocated memory blocks. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2761 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2762 | 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] | 2763 | { |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2764 | return slab->num_used; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2765 | } |
| 2766 | |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2767 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2768 | * @brief Get the number of unused blocks in a memory 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 | * This routine gets the number of memory blocks that are currently |
| 2771 | * unallocated in @a slab. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2772 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2773 | * @param slab Address of the memory slab. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2774 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2775 | * @return Number of unallocated memory blocks. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2776 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2777 | 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] | 2778 | { |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2779 | return slab->num_blocks - slab->num_used; |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2780 | } |
| 2781 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2782 | /** |
| 2783 | * @} end defgroup mem_slab_apis |
| 2784 | */ |
| 2785 | |
| 2786 | /** |
| 2787 | * @cond INTERNAL_HIDDEN |
| 2788 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2789 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2790 | /* |
| 2791 | * Memory pool requires a buffer and two arrays of structures for the |
| 2792 | * memory block accounting: |
| 2793 | * A set of arrays of k_mem_pool_quad_block structures where each keeps a |
| 2794 | * status of four blocks of memory. |
| 2795 | */ |
| 2796 | struct k_mem_pool_quad_block { |
| 2797 | char *mem_blocks; /* pointer to the first of four memory blocks */ |
| 2798 | uint32_t mem_status; /* four bits. If bit is set, memory block is |
| 2799 | allocated */ |
| 2800 | }; |
| 2801 | /* |
| 2802 | * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting |
| 2803 | * blocks of one size. Block sizes go from maximal to minimal. Next memory |
| 2804 | * block size is 4 times less than the previous one and thus requires 4 times |
| 2805 | * bigger array of k_mem_pool_quad_block structures to keep track of the |
| 2806 | * memory blocks. |
| 2807 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2808 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2809 | /* |
| 2810 | * The array of k_mem_pool_block_set keeps the information of each array of |
| 2811 | * k_mem_pool_quad_block structures |
| 2812 | */ |
| 2813 | struct k_mem_pool_block_set { |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 2814 | size_t block_size; /* memory block size */ |
| 2815 | 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] | 2816 | struct k_mem_pool_quad_block *quad_block; |
| 2817 | int count; |
| 2818 | }; |
| 2819 | |
| 2820 | /* Memory pool descriptor */ |
| 2821 | struct k_mem_pool { |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 2822 | size_t max_block_size; |
| 2823 | size_t min_block_size; |
| 2824 | uint32_t nr_of_maxblocks; |
| 2825 | uint32_t nr_of_block_sets; |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2826 | struct k_mem_pool_block_set *block_set; |
| 2827 | char *bufblock; |
| 2828 | _wait_q_t wait_q; |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 2829 | _OBJECT_TRACING_NEXT_PTR(k_mem_pool); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2830 | }; |
| 2831 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2832 | #ifdef CONFIG_ARM |
| 2833 | #define _SECTION_TYPE_SIGN "%" |
| 2834 | #else |
| 2835 | #define _SECTION_TYPE_SIGN "@" |
| 2836 | #endif |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2837 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2838 | /* |
| 2839 | * Static memory pool initialization |
| 2840 | */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2841 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2842 | /* |
| 2843 | * Use .altmacro to be able to recalculate values and pass them as string |
| 2844 | * arguments when calling assembler macros resursively |
| 2845 | */ |
| 2846 | __asm__(".altmacro\n\t"); |
| 2847 | |
| 2848 | /* |
| 2849 | * Recursively calls a macro |
| 2850 | * The followig global symbols need to be initialized: |
| 2851 | * __memory_pool_max_block_size - maximal size of the memory block |
| 2852 | * __memory_pool_min_block_size - minimal size of the memory block |
| 2853 | * Notes: |
| 2854 | * Global symbols are used due the fact that assembler macro allows only |
| 2855 | * one argument be passed with the % conversion |
| 2856 | * Some assemblers do not get division operation ("/"). To avoid it >> 2 |
| 2857 | * is used instead of / 4. |
| 2858 | * n_max argument needs to go first in the invoked macro, as some |
| 2859 | * assemblers concatenate \name and %(\n_max * 4) arguments |
| 2860 | * if \name goes first |
| 2861 | */ |
| 2862 | __asm__(".macro __do_recurse macro_name, name, n_max\n\t" |
| 2863 | ".ifge __memory_pool_max_block_size >> 2 -" |
| 2864 | " __memory_pool_min_block_size\n\t\t" |
| 2865 | "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t" |
| 2866 | "\\macro_name %(\\n_max * 4) \\name\n\t" |
| 2867 | ".endif\n\t" |
| 2868 | ".endm\n"); |
| 2869 | |
| 2870 | /* |
| 2871 | * Build quad blocks |
| 2872 | * Macro allocates space in memory for the array of k_mem_pool_quad_block |
| 2873 | * structures and recursively calls itself for the next array, 4 times |
| 2874 | * larger. |
| 2875 | * The followig global symbols need to be initialized: |
| 2876 | * __memory_pool_max_block_size - maximal size of the memory block |
| 2877 | * __memory_pool_min_block_size - minimal size of the memory block |
| 2878 | * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block) |
| 2879 | */ |
| 2880 | __asm__(".macro _build_quad_blocks n_max, name\n\t" |
Dmitriy Korovkin | 3c90651 | 2016-10-06 15:50:40 -0400 | [diff] [blame] | 2881 | ".balign 4\n\t" |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2882 | "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t" |
| 2883 | ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t" |
| 2884 | ".if \\n_max % 4\n\t\t" |
| 2885 | ".skip __memory_pool_quad_block_size\n\t" |
| 2886 | ".endif\n\t" |
| 2887 | "__do_recurse _build_quad_blocks \\name \\n_max\n\t" |
| 2888 | ".endm\n"); |
| 2889 | |
| 2890 | /* |
| 2891 | * Build block sets and initialize them |
| 2892 | * Macro initializes the k_mem_pool_block_set structure and |
| 2893 | * recursively calls itself for the next one. |
| 2894 | * The followig global symbols need to be initialized: |
| 2895 | * __memory_pool_max_block_size - maximal size of the memory block |
| 2896 | * __memory_pool_min_block_size - minimal size of the memory block |
| 2897 | * __memory_pool_block_set_count, the number of the elements in the |
| 2898 | * block set array must be set to 0. Macro calculates it's real |
| 2899 | * value. |
| 2900 | * Since the macro initializes pointers to an array of k_mem_pool_quad_block |
| 2901 | * structures, _build_quad_blocks must be called prior it. |
| 2902 | */ |
| 2903 | __asm__(".macro _build_block_set n_max, name\n\t" |
| 2904 | ".int __memory_pool_max_block_size\n\t" /* block_size */ |
| 2905 | ".if \\n_max % 4\n\t\t" |
| 2906 | ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */ |
| 2907 | ".else\n\t\t" |
| 2908 | ".int \\n_max >> 2\n\t" |
| 2909 | ".endif\n\t" |
| 2910 | ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */ |
| 2911 | ".int 0\n\t" /* count */ |
| 2912 | "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t" |
| 2913 | "__do_recurse _build_block_set \\name \\n_max\n\t" |
| 2914 | ".endm\n"); |
| 2915 | |
| 2916 | /* |
| 2917 | * Build a memory pool structure and initialize it |
| 2918 | * Macro uses __memory_pool_block_set_count global symbol, |
| 2919 | * block set addresses and buffer address, it may be called only after |
| 2920 | * _build_block_set |
| 2921 | */ |
| 2922 | __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] | 2923 | ".pushsection ._k_mem_pool.static.\\name,\"aw\"," |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2924 | _SECTION_TYPE_SIGN "progbits\n\t" |
| 2925 | ".globl \\name\n\t" |
| 2926 | "\\name:\n\t" |
| 2927 | ".int \\max_size\n\t" /* max_block_size */ |
| 2928 | ".int \\min_size\n\t" /* min_block_size */ |
| 2929 | ".int \\n_max\n\t" /* nr_of_maxblocks */ |
| 2930 | ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */ |
| 2931 | ".int _mem_pool_block_sets_\\name\n\t" /* block_set */ |
| 2932 | ".int _mem_pool_buffer_\\name\n\t" /* bufblock */ |
| 2933 | ".int 0\n\t" /* wait_q->head */ |
| 2934 | ".int 0\n\t" /* wait_q->next */ |
| 2935 | ".popsection\n\t" |
| 2936 | ".endm\n"); |
| 2937 | |
| 2938 | #define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \ |
| 2939 | __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \ |
| 2940 | _SECTION_TYPE_SIGN "progbits\n\t"); \ |
| 2941 | __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \ |
| 2942 | __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \ |
| 2943 | __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \ |
| 2944 | STRINGIFY(name) "\n\t"); \ |
| 2945 | __asm__(".popsection\n\t") |
| 2946 | |
| 2947 | #define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \ |
| 2948 | __asm__("__memory_pool_block_set_count = 0\n\t"); \ |
| 2949 | __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \ |
| 2950 | __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \ |
| 2951 | _SECTION_TYPE_SIGN "progbits\n\t"); \ |
Dmitriy Korovkin | 3c90651 | 2016-10-06 15:50:40 -0400 | [diff] [blame] | 2952 | __asm__(".balign 4\n\t"); \ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2953 | __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \ |
| 2954 | __asm__("_build_block_set " STRINGIFY(n_max) " " \ |
| 2955 | STRINGIFY(name) "\n\t"); \ |
| 2956 | __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \ |
| 2957 | __asm__(".int __memory_pool_block_set_count\n\t"); \ |
| 2958 | __asm__(".popsection\n\t"); \ |
| 2959 | extern uint32_t _mem_pool_block_set_count_##name; \ |
| 2960 | extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[] |
| 2961 | |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 2962 | #define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \ |
| 2963 | char __noinit __aligned(align) \ |
| 2964 | _mem_pool_buffer_##name[(max_size) * (n_max)] |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2965 | |
Dmitriy Korovkin | 0741467 | 2016-11-03 13:35:42 -0400 | [diff] [blame] | 2966 | /* |
| 2967 | * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block) |
| 2968 | * to __memory_pool_quad_block_size absolute symbol. |
| 2969 | * This function does not get called, but compiler calculates the value and |
| 2970 | * assigns it to the absolute symbol, that, in turn is used by assembler macros. |
| 2971 | */ |
| 2972 | static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void) |
| 2973 | { |
| 2974 | __asm__(".globl __memory_pool_quad_block_size\n\t" |
Mazen NEIFER | dc391f5 | 2017-01-22 17:20:22 +0100 | [diff] [blame] | 2975 | #if defined(CONFIG_NIOS2) || defined(CONFIG_XTENSA) |
Dmitriy Korovkin | 0741467 | 2016-11-03 13:35:42 -0400 | [diff] [blame] | 2976 | "__memory_pool_quad_block_size = %0\n\t" |
| 2977 | #else |
| 2978 | "__memory_pool_quad_block_size = %c0\n\t" |
| 2979 | #endif |
| 2980 | : |
| 2981 | : "n"(sizeof(struct k_mem_pool_quad_block))); |
| 2982 | } |
| 2983 | |
| 2984 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2985 | * INTERNAL_HIDDEN @endcond |
Dmitriy Korovkin | 0741467 | 2016-11-03 13:35:42 -0400 | [diff] [blame] | 2986 | */ |
| 2987 | |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 2988 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2989 | * @addtogroup mem_pool_apis |
| 2990 | * @{ |
| 2991 | */ |
| 2992 | |
| 2993 | /** |
| 2994 | * @brief Statically define and initialize a memory pool. |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 2995 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2996 | * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes |
| 2997 | * long. The memory pool allows blocks to be repeatedly partitioned into |
| 2998 | * quarters, down to blocks of @a min_size bytes long. The buffer is aligned |
| 2999 | * 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] | 3000 | * 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] | 3001 | * @a align. |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 3002 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3003 | * If the pool is to be accessed outside the module where it is defined, it |
| 3004 | * can be declared via |
| 3005 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 3006 | * @code extern struct k_mem_pool <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 3007 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3008 | * @param name Name of the memory pool. |
| 3009 | * @param min_size Size of the smallest blocks in the pool (in bytes). |
| 3010 | * @param max_size Size of the largest blocks in the pool (in bytes). |
| 3011 | * @param n_max Number of maximum sized blocks in the pool. |
| 3012 | * @param align Alignment of the pool's buffer (power of 2). |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 3013 | */ |
| 3014 | #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] | 3015 | _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \ |
| 3016 | _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \ |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 3017 | _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 3018 | __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \ |
| 3019 | STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \ |
| 3020 | extern struct k_mem_pool name |
| 3021 | |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 3022 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3023 | * @brief Allocate memory from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 3024 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3025 | * This routine allocates a memory block from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 3026 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3027 | * @param pool Address of the memory pool. |
| 3028 | * @param block Pointer to block descriptor for the allocated memory. |
| 3029 | * @param size Amount of memory to allocate (in bytes). |
| 3030 | * @param timeout Maximum time to wait for operation to complete |
| 3031 | * (in milliseconds). Use K_NO_WAIT to return without waiting, |
| 3032 | * or K_FOREVER to wait as long as necessary. |
| 3033 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3034 | * @retval 0 Memory allocated. The @a data field of the block descriptor |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3035 | * is set to the starting address of the memory block. |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 3036 | * @retval -ENOMEM Returned without waiting. |
| 3037 | * @retval -EAGAIN Waiting period timed out. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 3038 | */ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 3039 | 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] | 3040 | size_t size, int32_t timeout); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 3041 | |
| 3042 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3043 | * @brief Free memory allocated from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 3044 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3045 | * This routine releases a previously allocated memory block back to its |
| 3046 | * memory pool. |
| 3047 | * |
| 3048 | * @param block Pointer to block descriptor for the allocated memory. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 3049 | * |
| 3050 | * @return N/A |
| 3051 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3052 | extern void k_mem_pool_free(struct k_mem_block *block); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 3053 | |
| 3054 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3055 | * @brief Defragment a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 3056 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3057 | * This routine instructs a memory pool to concatenate unused memory blocks |
| 3058 | * into larger blocks wherever possible. Manually defragmenting the memory |
| 3059 | * pool may speed up future allocations of memory blocks by eliminating the |
| 3060 | * need for the memory pool to perform an automatic partial defragmentation. |
| 3061 | * |
| 3062 | * @param pool Address of the memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 3063 | * |
| 3064 | * @return N/A |
| 3065 | */ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 3066 | extern void k_mem_pool_defrag(struct k_mem_pool *pool); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 3067 | |
| 3068 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3069 | * @} end addtogroup mem_pool_apis |
| 3070 | */ |
| 3071 | |
| 3072 | /** |
| 3073 | * @defgroup heap_apis Heap Memory Pool APIs |
| 3074 | * @ingroup kernel_apis |
| 3075 | * @{ |
| 3076 | */ |
| 3077 | |
| 3078 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3079 | * @brief Allocate memory from heap. |
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 | * This routine provides traditional malloc() semantics. Memory is |
Allan Stephens | 480a131 | 2016-10-13 15:44:48 -0500 | [diff] [blame] | 3082 | * allocated from the heap memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 3083 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3084 | * @param size Amount of memory requested (in bytes). |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 3085 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3086 | * @return Address of the allocated memory if successful; otherwise NULL. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 3087 | */ |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 3088 | extern void *k_malloc(size_t size); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 3089 | |
| 3090 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3091 | * @brief Free memory allocated from heap. |
Allan Stephens | 480a131 | 2016-10-13 15:44:48 -0500 | [diff] [blame] | 3092 | * |
| 3093 | * This routine provides traditional free() semantics. The memory being |
| 3094 | * returned must have been allocated from the heap memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 3095 | * |
Anas Nashif | 345fdd5 | 2016-12-20 08:36:04 -0500 | [diff] [blame] | 3096 | * If @a ptr is NULL, no operation is performed. |
| 3097 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 3098 | * @param ptr Pointer to previously allocated memory. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 3099 | * |
| 3100 | * @return N/A |
| 3101 | */ |
| 3102 | extern void k_free(void *ptr); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3103 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 3104 | /** |
| 3105 | * @} end defgroup heap_apis |
| 3106 | */ |
| 3107 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 3108 | /* polling API - PRIVATE */ |
| 3109 | |
Benjamin Walsh | b017986 | 2017-02-02 16:39:57 -0500 | [diff] [blame] | 3110 | #ifdef CONFIG_POLL |
| 3111 | #define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0)) |
| 3112 | #else |
| 3113 | #define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0)) |
| 3114 | #endif |
| 3115 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 3116 | /* private - implementation data created as needed, per-type */ |
| 3117 | struct _poller { |
| 3118 | struct k_thread *thread; |
| 3119 | }; |
| 3120 | |
| 3121 | /* private - types bit positions */ |
| 3122 | enum _poll_types_bits { |
| 3123 | /* can be used to ignore an event */ |
| 3124 | _POLL_TYPE_IGNORE, |
| 3125 | |
| 3126 | /* to be signaled by k_poll_signal() */ |
| 3127 | _POLL_TYPE_SIGNAL, |
| 3128 | |
| 3129 | /* semaphore availability */ |
| 3130 | _POLL_TYPE_SEM_AVAILABLE, |
| 3131 | |
| 3132 | /* fifo data availability */ |
| 3133 | _POLL_TYPE_FIFO_DATA_AVAILABLE, |
| 3134 | |
| 3135 | _POLL_NUM_TYPES |
| 3136 | }; |
| 3137 | |
| 3138 | #define _POLL_TYPE_BIT(type) (1 << ((type) - 1)) |
| 3139 | |
| 3140 | /* private - states bit positions */ |
| 3141 | enum _poll_states_bits { |
| 3142 | /* default state when creating event */ |
| 3143 | _POLL_STATE_NOT_READY, |
| 3144 | |
| 3145 | /* there was another poller already on the object */ |
| 3146 | _POLL_STATE_EADDRINUSE, |
| 3147 | |
| 3148 | /* signaled by k_poll_signal() */ |
| 3149 | _POLL_STATE_SIGNALED, |
| 3150 | |
| 3151 | /* semaphore is available */ |
| 3152 | _POLL_STATE_SEM_AVAILABLE, |
| 3153 | |
| 3154 | /* data is available to read on fifo */ |
| 3155 | _POLL_STATE_FIFO_DATA_AVAILABLE, |
| 3156 | |
| 3157 | _POLL_NUM_STATES |
| 3158 | }; |
| 3159 | |
| 3160 | #define _POLL_STATE_BIT(state) (1 << ((state) - 1)) |
| 3161 | |
| 3162 | #define _POLL_EVENT_NUM_UNUSED_BITS \ |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 3163 | (32 - (0 \ |
| 3164 | + 8 /* tag */ \ |
| 3165 | + _POLL_NUM_TYPES \ |
| 3166 | + _POLL_NUM_STATES \ |
| 3167 | + 1 /* modes */ \ |
| 3168 | )) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 3169 | |
| 3170 | #if _POLL_EVENT_NUM_UNUSED_BITS < 0 |
| 3171 | #error overflow of 32-bit word in struct k_poll_event |
| 3172 | #endif |
| 3173 | |
| 3174 | /* end of polling API - PRIVATE */ |
| 3175 | |
| 3176 | |
| 3177 | /** |
| 3178 | * @defgroup poll_apis Async polling APIs |
| 3179 | * @ingroup kernel_apis |
| 3180 | * @{ |
| 3181 | */ |
| 3182 | |
| 3183 | /* Public polling API */ |
| 3184 | |
| 3185 | /* public - values for k_poll_event.type bitfield */ |
| 3186 | #define K_POLL_TYPE_IGNORE 0 |
| 3187 | #define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL) |
| 3188 | #define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE) |
| 3189 | #define K_POLL_TYPE_FIFO_DATA_AVAILABLE \ |
| 3190 | _POLL_TYPE_BIT(_POLL_TYPE_FIFO_DATA_AVAILABLE) |
| 3191 | |
| 3192 | /* public - polling modes */ |
| 3193 | enum k_poll_modes { |
| 3194 | /* polling thread does not take ownership of objects when available */ |
| 3195 | K_POLL_MODE_NOTIFY_ONLY = 0, |
| 3196 | |
| 3197 | K_POLL_NUM_MODES |
| 3198 | }; |
| 3199 | |
| 3200 | /* public - values for k_poll_event.state bitfield */ |
| 3201 | #define K_POLL_STATE_NOT_READY 0 |
| 3202 | #define K_POLL_STATE_EADDRINUSE _POLL_STATE_BIT(_POLL_STATE_EADDRINUSE) |
| 3203 | #define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED) |
| 3204 | #define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE) |
| 3205 | #define K_POLL_STATE_FIFO_DATA_AVAILABLE \ |
| 3206 | _POLL_STATE_BIT(_POLL_STATE_FIFO_DATA_AVAILABLE) |
| 3207 | |
| 3208 | /* public - poll signal object */ |
| 3209 | struct k_poll_signal { |
| 3210 | /* PRIVATE - DO NOT TOUCH */ |
| 3211 | struct k_poll_event *poll_event; |
| 3212 | |
| 3213 | /* |
| 3214 | * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until |
| 3215 | * user resets it to 0. |
| 3216 | */ |
| 3217 | unsigned int signaled; |
| 3218 | |
| 3219 | /* custom result value passed to k_poll_signal() if needed */ |
| 3220 | int result; |
| 3221 | }; |
| 3222 | |
| 3223 | #define K_POLL_SIGNAL_INITIALIZER() \ |
| 3224 | { \ |
| 3225 | .poll_event = NULL, \ |
| 3226 | .signaled = 0, \ |
| 3227 | .result = 0, \ |
| 3228 | } |
| 3229 | |
| 3230 | struct k_poll_event { |
| 3231 | /* PRIVATE - DO NOT TOUCH */ |
| 3232 | struct _poller *poller; |
| 3233 | |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 3234 | /* optional user-specified tag, opaque, untouched by the API */ |
| 3235 | uint32_t tag:8; |
| 3236 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 3237 | /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */ |
| 3238 | uint32_t type:_POLL_NUM_TYPES; |
| 3239 | |
| 3240 | /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */ |
| 3241 | uint32_t state:_POLL_NUM_STATES; |
| 3242 | |
| 3243 | /* mode of operation, from enum k_poll_modes */ |
| 3244 | uint32_t mode:1; |
| 3245 | |
| 3246 | /* unused bits in 32-bit word */ |
| 3247 | uint32_t unused:_POLL_EVENT_NUM_UNUSED_BITS; |
| 3248 | |
| 3249 | /* per-type data */ |
| 3250 | union { |
| 3251 | void *obj; |
| 3252 | struct k_poll_signal *signal; |
| 3253 | struct k_sem *sem; |
| 3254 | struct k_fifo *fifo; |
| 3255 | }; |
| 3256 | }; |
| 3257 | |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 3258 | #define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 3259 | { \ |
| 3260 | .poller = NULL, \ |
| 3261 | .type = event_type, \ |
| 3262 | .state = K_POLL_STATE_NOT_READY, \ |
| 3263 | .mode = event_mode, \ |
| 3264 | .unused = 0, \ |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 3265 | { .obj = event_obj }, \ |
| 3266 | } |
| 3267 | |
| 3268 | #define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \ |
| 3269 | event_tag) \ |
| 3270 | { \ |
| 3271 | .type = event_type, \ |
| 3272 | .tag = event_tag, \ |
| 3273 | .state = K_POLL_STATE_NOT_READY, \ |
| 3274 | .mode = event_mode, \ |
| 3275 | .unused = 0, \ |
| 3276 | { .obj = event_obj }, \ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 3277 | } |
| 3278 | |
| 3279 | /** |
| 3280 | * @brief Initialize one struct k_poll_event instance |
| 3281 | * |
| 3282 | * After this routine is called on a poll event, the event it ready to be |
| 3283 | * placed in an event array to be passed to k_poll(). |
| 3284 | * |
| 3285 | * @param event The event to initialize. |
| 3286 | * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx |
| 3287 | * values. Only values that apply to the same object being polled |
| 3288 | * can be used together. Choosing K_POLL_TYPE_IGNORE disables the |
| 3289 | * event. |
| 3290 | * @param mode Future. Use K_POLL_MODE_INFORM_ONLY. |
| 3291 | * @param obj Kernel object or poll signal. |
| 3292 | * |
| 3293 | * @return N/A |
| 3294 | */ |
| 3295 | |
| 3296 | extern void k_poll_event_init(struct k_poll_event *event, uint32_t type, |
| 3297 | int mode, void *obj); |
| 3298 | |
| 3299 | /** |
| 3300 | * @brief Wait for one or many of multiple poll events to occur |
| 3301 | * |
| 3302 | * This routine allows a thread to wait concurrently for one or many of |
| 3303 | * multiple poll events to have occurred. Such events can be a kernel object |
| 3304 | * being available, like a semaphore, or a poll signal event. |
| 3305 | * |
| 3306 | * When an event notifies that a kernel object is available, the kernel object |
| 3307 | * is not "given" to the thread calling k_poll(): it merely signals the fact |
| 3308 | * that the object was available when the k_poll() call was in effect. Also, |
| 3309 | * all threads trying to acquire an object the regular way, i.e. by pending on |
| 3310 | * the object, have precedence over the thread polling on the object. This |
| 3311 | * means that the polling thread will never get the poll event on an object |
| 3312 | * until the object becomes available and its pend queue is empty. For this |
| 3313 | * reason, the k_poll() call is more effective when the objects being polled |
| 3314 | * only have one thread, the polling thread, trying to acquire them. |
| 3315 | * |
| 3316 | * Only one thread can be polling for a particular object at a given time. If |
| 3317 | * another thread tries to poll on it, the k_poll() call returns -EADDRINUSE |
| 3318 | * and returns as soon as it has finished handling the other events. This means |
| 3319 | * that k_poll() can return -EADDRINUSE and have the state value of some events |
| 3320 | * be non-K_POLL_STATE_NOT_READY. When this condition occurs, the @a timeout |
| 3321 | * parameter is ignored. |
| 3322 | * |
| 3323 | * When k_poll() returns 0 or -EADDRINUSE, the caller should loop on all the |
| 3324 | * events that were passed to k_poll() and check the state field for the values |
| 3325 | * that were expected and take the associated actions. |
| 3326 | * |
| 3327 | * Before being reused for another call to k_poll(), the user has to reset the |
| 3328 | * state field to K_POLL_STATE_NOT_READY. |
| 3329 | * |
| 3330 | * @param events An array of pointers to events to be polled for. |
| 3331 | * @param num_events The number of events in the array. |
| 3332 | * @param timeout Waiting period for an event to be ready (in milliseconds), |
| 3333 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 3334 | * |
| 3335 | * @retval 0 One or more events are ready. |
| 3336 | * @retval -EADDRINUSE One or more objects already had a poller. |
| 3337 | * @retval -EAGAIN Waiting period timed out. |
| 3338 | */ |
| 3339 | |
| 3340 | extern int k_poll(struct k_poll_event *events, int num_events, |
| 3341 | int32_t timeout); |
| 3342 | |
| 3343 | /** |
Benjamin Walsh | a304f16 | 2017-02-02 16:46:09 -0500 | [diff] [blame] | 3344 | * @brief Initialize a poll signal object. |
| 3345 | * |
| 3346 | * Ready a poll signal object to be signaled via k_poll_signal(). |
| 3347 | * |
| 3348 | * @param signal A poll signal. |
| 3349 | * |
| 3350 | * @return N/A |
| 3351 | */ |
| 3352 | |
| 3353 | extern void k_poll_signal_init(struct k_poll_signal *signal); |
| 3354 | |
| 3355 | /** |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 3356 | * @brief Signal a poll signal object. |
| 3357 | * |
| 3358 | * This routine makes ready a poll signal, which is basically a poll event of |
| 3359 | * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be |
| 3360 | * made ready to run. A @a result value can be specified. |
| 3361 | * |
| 3362 | * The poll signal contains a 'signaled' field that, when set by |
| 3363 | * k_poll_signal(), stays set until the user sets it back to 0. It thus has to |
| 3364 | * be reset by the user before being passed again to k_poll() or k_poll() will |
| 3365 | * consider it being signaled, and will return immediately. |
| 3366 | * |
| 3367 | * @param signal A poll signal. |
| 3368 | * @param result The value to store in the result field of the signal. |
| 3369 | * |
| 3370 | * @retval 0 The signal was delivered successfully. |
| 3371 | * @retval -EAGAIN The polling thread's timeout is in the process of expiring. |
| 3372 | */ |
| 3373 | |
| 3374 | extern int k_poll_signal(struct k_poll_signal *signal, int result); |
| 3375 | |
| 3376 | /* private internal function */ |
| 3377 | extern int _handle_obj_poll_event(struct k_poll_event **obj_poll_event, |
| 3378 | uint32_t state); |
| 3379 | |
| 3380 | /** |
| 3381 | * @} end defgroup poll_apis |
| 3382 | */ |
| 3383 | |
Benjamin Walsh | c3a2bbb | 2016-12-14 13:04:36 -0500 | [diff] [blame] | 3384 | /** |
| 3385 | * @brief Make the CPU idle. |
| 3386 | * |
| 3387 | * This function makes the CPU idle until an event wakes it up. |
| 3388 | * |
| 3389 | * In a regular system, the idle thread should be the only thread responsible |
| 3390 | * for making the CPU idle and triggering any type of power management. |
| 3391 | * However, in some more constrained systems, such as a single-threaded system, |
| 3392 | * the only thread would be responsible for this if needed. |
| 3393 | * |
| 3394 | * @return N/A |
| 3395 | */ |
| 3396 | extern void k_cpu_idle(void); |
| 3397 | |
| 3398 | /** |
| 3399 | * @brief Make the CPU idle in an atomic fashion. |
| 3400 | * |
| 3401 | * Similar to k_cpu_idle(), but called with interrupts locked if operations |
| 3402 | * must be done atomically before making the CPU idle. |
| 3403 | * |
| 3404 | * @param key Interrupt locking key obtained from irq_lock(). |
| 3405 | * |
| 3406 | * @return N/A |
| 3407 | */ |
| 3408 | extern void k_cpu_atomic_idle(unsigned int key); |
| 3409 | |
Andrew Boie | 350f88d | 2017-01-18 13:13:45 -0800 | [diff] [blame] | 3410 | extern void _sys_power_save_idle_exit(int32_t ticks); |
| 3411 | |
Anas Nashif | a614950 | 2017-01-17 07:47:31 -0500 | [diff] [blame] | 3412 | /* Include legacy APIs */ |
| 3413 | #if defined(CONFIG_LEGACY_KERNEL) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3414 | #include <legacy.h> |
Anas Nashif | a614950 | 2017-01-17 07:47:31 -0500 | [diff] [blame] | 3415 | #endif |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3416 | #include <arch/cpu.h> |
| 3417 | |
| 3418 | /* |
| 3419 | * private APIs that are utilized by one or more public APIs |
| 3420 | */ |
| 3421 | |
Benjamin Walsh | b12a8e0 | 2016-12-14 15:24:12 -0500 | [diff] [blame] | 3422 | #ifdef CONFIG_MULTITHREADING |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3423 | extern void _init_static_threads(void); |
Benjamin Walsh | b12a8e0 | 2016-12-14 15:24:12 -0500 | [diff] [blame] | 3424 | #else |
| 3425 | #define _init_static_threads() do { } while ((0)) |
| 3426 | #endif |
| 3427 | |
| 3428 | extern int _is_thread_essential(void); |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 3429 | extern void _timer_expiration_handler(struct _timeout *t); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3430 | |
| 3431 | #ifdef __cplusplus |
| 3432 | } |
| 3433 | #endif |
| 3434 | |
Andrew Boie | e004dec | 2016-11-07 09:01:19 -0800 | [diff] [blame] | 3435 | #if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) |
| 3436 | /* |
| 3437 | * Define new and delete operators. |
| 3438 | * At this moment, the operators do nothing since objects are supposed |
| 3439 | * to be statically allocated. |
| 3440 | */ |
| 3441 | inline void operator delete(void *ptr) |
| 3442 | { |
| 3443 | (void)ptr; |
| 3444 | } |
| 3445 | |
| 3446 | inline void operator delete[](void *ptr) |
| 3447 | { |
| 3448 | (void)ptr; |
| 3449 | } |
| 3450 | |
| 3451 | inline void *operator new(size_t size) |
| 3452 | { |
| 3453 | (void)size; |
| 3454 | return NULL; |
| 3455 | } |
| 3456 | |
| 3457 | inline void *operator new[](size_t size) |
| 3458 | { |
| 3459 | (void)size; |
| 3460 | return NULL; |
| 3461 | } |
| 3462 | |
| 3463 | /* Placement versions of operator new and delete */ |
| 3464 | inline void operator delete(void *ptr1, void *ptr2) |
| 3465 | { |
| 3466 | (void)ptr1; |
| 3467 | (void)ptr2; |
| 3468 | } |
| 3469 | |
| 3470 | inline void operator delete[](void *ptr1, void *ptr2) |
| 3471 | { |
| 3472 | (void)ptr1; |
| 3473 | (void)ptr2; |
| 3474 | } |
| 3475 | |
| 3476 | inline void *operator new(size_t size, void *ptr) |
| 3477 | { |
| 3478 | (void)size; |
| 3479 | return ptr; |
| 3480 | } |
| 3481 | |
| 3482 | inline void *operator new[](size_t size, void *ptr) |
| 3483 | { |
| 3484 | (void)size; |
| 3485 | return ptr; |
| 3486 | } |
| 3487 | |
| 3488 | #endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */ |
| 3489 | |
Benjamin Walsh | dfa7ce5 | 2017-01-22 17:06:05 -0500 | [diff] [blame] | 3490 | #endif /* !_ASMLANGUAGE */ |
| 3491 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3492 | #endif /* _kernel__h_ */ |