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