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