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