blob: c9a28b82cc5e76dafc344170ed76a4e025bf29ab [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @file
9 *
10 * @brief Public kernel APIs.
11 */
12
13#ifndef _kernel__h_
14#define _kernel__h_
15
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050016#if !defined(_ASMLANGUAGE)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040017#include <stddef.h>
Kumar Gala78908162017-04-19 10:32:08 -050018#include <zephyr/types.h>
Anas Nashif173902f2017-01-17 07:08:56 -050019#include <limits.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020#include <toolchain.h>
Anas Nashif397d29d2017-06-17 11:30:47 -040021#include <linker/sections.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040022#include <atomic.h>
23#include <errno.h>
24#include <misc/__assert.h>
25#include <misc/dlist.h>
26#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050027#include <misc/util.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050028#include <kernel_version.h>
Anas Nashifa6149502017-01-17 07:47:31 -050029#include <drivers/rand32.h>
Andrew Boie73abd322017-04-04 13:19:13 -070030#include <kernel_arch_thread.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040031
32#ifdef __cplusplus
33extern "C" {
34#endif
35
Anas Nashifbbb157d2017-01-15 08:46:31 -050036/**
37 * @brief Kernel APIs
38 * @defgroup kernel_apis Kernel APIs
39 * @{
40 * @}
41 */
42
Anas Nashif61f4b242016-11-18 10:53:59 -050043#ifdef CONFIG_KERNEL_DEBUG
44#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040045#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
46#else
47#define K_DEBUG(fmt, ...)
48#endif
49
Benjamin Walsh2f280412017-01-14 19:23:46 -050050#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
51#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
52#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
53#elif defined(CONFIG_COOP_ENABLED)
54#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
55#define _NUM_PREEMPT_PRIO (0)
56#elif defined(CONFIG_PREEMPT_ENABLED)
57#define _NUM_COOP_PRIO (0)
58#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
59#else
60#error "invalid configuration"
61#endif
62
63#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040064#define K_PRIO_PREEMPT(x) (x)
65
Benjamin Walsh456c6da2016-09-02 18:55:39 -040066#define K_ANY NULL
67#define K_END NULL
68
Benjamin Walshedb35702017-01-14 18:47:22 -050069#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040070#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050071#elif defined(CONFIG_COOP_ENABLED)
72#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
73#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040074#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050075#else
76#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040077#endif
78
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050079#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040080#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
81#else
82#define K_LOWEST_THREAD_PRIO -1
83#endif
84
Benjamin Walshfab8d922016-11-08 15:36:36 -050085#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
86
Benjamin Walsh456c6da2016-09-02 18:55:39 -040087#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
88#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
89
90typedef sys_dlist_t _wait_q_t;
91
Anas Nashif2f203c22016-12-18 06:57:45 -050092#ifdef CONFIG_OBJECT_TRACING
93#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
94#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040095#else
Anas Nashif2f203c22016-12-18 06:57:45 -050096#define _OBJECT_TRACING_INIT
97#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040098#endif
99
Benjamin Walshacc68c12017-01-29 18:57:45 -0500100#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300101#define _POLL_EVENT_OBJ_INIT(obj) \
102 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
103#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500104#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300105#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500106#define _POLL_EVENT
107#endif
108
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500109struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400110struct k_mutex;
111struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400112struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400113struct k_msgq;
114struct k_mbox;
115struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200116struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400117struct k_fifo;
118struct k_lifo;
119struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400120struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400121struct k_mem_pool;
122struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500123struct k_poll_event;
124struct k_poll_signal;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400125
Andrew Boie945af952017-08-22 13:15:23 -0700126enum k_objects {
127 K_OBJ_ALERT,
128 K_OBJ_DELAYED_WORK,
129 K_OBJ_MEM_SLAB,
130 K_OBJ_MSGQ,
131 K_OBJ_MUTEX,
132 K_OBJ_PIPE,
133 K_OBJ_SEM,
134 K_OBJ_STACK,
135 K_OBJ_THREAD,
136 K_OBJ_TIMER,
137 K_OBJ_WORK,
138 K_OBJ_WORK_Q,
139
140 K_OBJ_LAST
141};
142
143#ifdef CONFIG_USERSPACE
144/* Table generated by gperf, these objects are retrieved via
145 * _k_object_find() */
146struct _k_object {
147 char *name;
148 char perms[CONFIG_MAX_THREAD_BYTES];
149 char type;
150 char flags;
151} __packed;
152
153#define K_OBJ_FLAG_INITIALIZED BIT(0)
154/**
155 * Ensure a system object is a valid object of the expected type
156 *
157 * Searches for the object and ensures that it is indeed an object
158 * of the expected type, that the caller has the right permissions on it,
159 * and that the object has been initialized.
160 *
161 * This function is intended to be called on the kernel-side system
162 * call handlers to validate kernel object pointers passed in from
163 * userspace.
164 *
165 * @param obj Address of the kernel object
166 * @param otype Expected type of the kernel object
167 * @param init If true, this is for an init function and we will not error
168 * out if the object is not initialized
169 * @return 0 If the object is valid
170 * -EBADF if not a valid object of the specified type
171 * -EPERM If the caller does not have permissions
172 * -EINVAL Object is not intitialized
173 */
174int _k_object_validate(void *obj, enum k_objects otype, int init);
175
176
177/**
178 * Lookup a kernel object and init its metadata if it exists
179 *
180 * Calling this on an object will make it usable from userspace.
181 * Intended to be called as the last statement in kernel object init
182 * functions.
183 *
184 * @param object Address of the kernel object
185 */
186void _k_object_init(void *obj);
187
188
189/**
190 * grant a thread access to a kernel object
191 *
192 * The thread will be granted access to the object if the caller is from
193 * supervisor mode, or the caller is from user mode AND has permissions
194 * on the object already.
195 *
196 * @param object Address of kernel object
197 * @param thread Thread to grant access to the object
198 */
199void k_object_grant_access(void *object, struct k_thread *thread);
200
201#else
202static inline int _k_object_validate(void *obj, enum k_objects otype, int init)
203{
204 ARG_UNUSED(obj);
205 ARG_UNUSED(otype);
206 ARG_UNUSED(init);
207
208 return 0;
209}
210
211static inline void _k_object_init(void *obj)
212{
213 ARG_UNUSED(obj);
214}
215
216static inline void k_object_grant_access(void *object, struct k_thread *thread)
217{
218 ARG_UNUSED(object);
219 ARG_UNUSED(thread);
220}
221#endif /* CONFIG_USERSPACE */
222
Andrew Boie73abd322017-04-04 13:19:13 -0700223/* timeouts */
224
225struct _timeout;
226typedef void (*_timeout_func_t)(struct _timeout *t);
227
228struct _timeout {
229 sys_dnode_t node;
230 struct k_thread *thread;
231 sys_dlist_t *wait_q;
232 s32_t delta_ticks_from_prev;
233 _timeout_func_t func;
234};
235
236extern s32_t _timeout_remaining_get(struct _timeout *timeout);
237
238/* Threads */
239typedef void (*_thread_entry_t)(void *, void *, void *);
240
241#ifdef CONFIG_THREAD_MONITOR
242struct __thread_entry {
243 _thread_entry_t pEntry;
244 void *parameter1;
245 void *parameter2;
246 void *parameter3;
247};
248#endif
249
250/* can be used for creating 'dummy' threads, e.g. for pending on objects */
251struct _thread_base {
252
253 /* this thread's entry in a ready/wait queue */
254 sys_dnode_t k_q_node;
255
256 /* user facing 'thread options'; values defined in include/kernel.h */
257 u8_t user_options;
258
259 /* thread state */
260 u8_t thread_state;
261
262 /*
263 * scheduler lock count and thread priority
264 *
265 * These two fields control the preemptibility of a thread.
266 *
267 * When the scheduler is locked, sched_locked is decremented, which
268 * means that the scheduler is locked for values from 0xff to 0x01. A
269 * thread is coop if its prio is negative, thus 0x80 to 0xff when
270 * looked at the value as unsigned.
271 *
272 * By putting them end-to-end, this means that a thread is
273 * non-preemptible if the bundled value is greater than or equal to
274 * 0x0080.
275 */
276 union {
277 struct {
278#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
279 u8_t sched_locked;
280 s8_t prio;
281#else /* LITTLE and PDP */
282 s8_t prio;
283 u8_t sched_locked;
284#endif
285 };
286 u16_t preempt;
287 };
288
289 /* data returned by APIs */
290 void *swap_data;
291
292#ifdef CONFIG_SYS_CLOCK_EXISTS
293 /* this thread's entry in a timeout queue */
294 struct _timeout timeout;
295#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700296};
297
298typedef struct _thread_base _thread_base_t;
299
300#if defined(CONFIG_THREAD_STACK_INFO)
301/* Contains the stack information of a thread */
302struct _thread_stack_info {
303 /* Stack Start */
304 u32_t start;
305 /* Stack Size */
306 u32_t size;
307};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700308
309typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700310#endif /* CONFIG_THREAD_STACK_INFO */
311
312struct k_thread {
313
314 struct _thread_base base;
315
316 /* defined by the architecture, but all archs need these */
317 struct _caller_saved caller_saved;
318 struct _callee_saved callee_saved;
319
320 /* static thread init data */
321 void *init_data;
322
323 /* abort function */
324 void (*fn_abort)(void);
325
326#if defined(CONFIG_THREAD_MONITOR)
327 /* thread entry and parameters description */
328 struct __thread_entry *entry;
329
330 /* next item in list of all threads */
331 struct k_thread *next_thread;
332#endif
333
334#ifdef CONFIG_THREAD_CUSTOM_DATA
335 /* crude thread-local storage */
336 void *custom_data;
337#endif
338
339#ifdef CONFIG_ERRNO
340 /* per-thread errno variable */
341 int errno_var;
342#endif
343
344#if defined(CONFIG_THREAD_STACK_INFO)
345 /* Stack Info */
346 struct _thread_stack_info stack_info;
347#endif /* CONFIG_THREAD_STACK_INFO */
348
349 /* arch-specifics: must always be at the end */
350 struct _thread_arch arch;
351};
352
353typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400354typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700355#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400356
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400357enum execution_context_types {
358 K_ISR = 0,
359 K_COOP_THREAD,
360 K_PREEMPT_THREAD,
361};
362
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400363/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100364 * @defgroup profiling_apis Profiling APIs
365 * @ingroup kernel_apis
366 * @{
367 */
368
369/**
370 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
371 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700372 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100373 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
374 *
375 * CONFIG_MAIN_STACK_SIZE
376 * CONFIG_IDLE_STACK_SIZE
377 * CONFIG_ISR_STACK_SIZE
378 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
379 *
380 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
381 * produce output.
382 *
383 * @return N/A
384 */
385extern void k_call_stacks_analyze(void);
386
387/**
388 * @} end defgroup profiling_apis
389 */
390
391/**
Allan Stephensc98da842016-11-11 15:45:03 -0500392 * @defgroup thread_apis Thread APIs
393 * @ingroup kernel_apis
394 * @{
395 */
396
397/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500398 * @typedef k_thread_entry_t
399 * @brief Thread entry point function type.
400 *
401 * A thread's entry point function is invoked when the thread starts executing.
402 * Up to 3 argument values can be passed to the function.
403 *
404 * The thread terminates execution permanently if the entry point function
405 * returns. The thread is responsible for releasing any shared resources
406 * it may own (such as mutexes and dynamically allocated memory), prior to
407 * returning.
408 *
409 * @param p1 First argument.
410 * @param p2 Second argument.
411 * @param p3 Third argument.
412 *
413 * @return N/A
414 */
415typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
416
Benjamin Walshed240f22017-01-22 13:05:08 -0500417#endif /* !_ASMLANGUAGE */
418
419
420/*
421 * Thread user options. May be needed by assembly code. Common part uses low
422 * bits, arch-specific use high bits.
423 */
424
425/* system thread that must not abort */
426#define K_ESSENTIAL (1 << 0)
427
428#if defined(CONFIG_FP_SHARING)
429/* thread uses floating point registers */
430#define K_FP_REGS (1 << 1)
431#endif
432
433#ifdef CONFIG_X86
434/* x86 Bitmask definitions for threads user options */
435
436#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
437/* thread uses SSEx (and also FP) registers */
438#define K_SSE_REGS (1 << 7)
439#endif
440#endif
441
442/* end - thread options */
443
444#if !defined(_ASMLANGUAGE)
445
Andrew Boie507852a2017-07-25 18:47:07 -0700446/* Using typedef deliberately here, this is quite intended to be an opaque
447 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
448 *
449 * The purpose of this data type is to clearly distinguish between the
450 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
451 * buffer which composes the stack data actually used by the underlying
452 * thread; they cannot be used interchangably as some arches precede the
453 * stack buffer region with guard areas that trigger a MPU or MMU fault
454 * if written to.
455 *
456 * APIs that want to work with the buffer inside should continue to use
457 * char *.
458 *
459 * Stacks should always be created with K_THREAD_STACK_DEFINE().
460 */
461struct __packed _k_thread_stack_element {
462 char data;
463};
464typedef struct _k_thread_stack_element *k_thread_stack_t;
465
Allan Stephens5eceb852016-11-16 10:16:30 -0500466/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500467 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400468 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500469 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400470 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500471 * The new thread may be scheduled for immediate execution or a delayed start.
472 * If the newly spawned thread does not have a delayed start the kernel
473 * scheduler may preempt the current thread to allow the new thread to
474 * execute.
475 *
Andrew Boied26cf2d2017-03-30 13:07:02 -0700476 * Kernel data structures for bookkeeping and context storage for this thread
477 * will be placed at the beginning of the thread's stack memory region and may
478 * become corrupted if too much of the stack is used. This function has been
479 * deprecated in favor of k_thread_create() to give the user more control on
480 * where these data structures reside.
481 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500482 * Thread options are architecture-specific, and can include K_ESSENTIAL,
483 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
484 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400485 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700486 * The stack itself should be declared with K_THREAD_STACK_DEFINE or variant
487 * macros. The stack size parameter should either be a defined constant
488 * also passed to K_THREAD_STACK_DEFINE, or the value of K_THREAD_STACK_SIZEOF.
489 * Do not use regular C sizeof().
490 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400491 * @param stack Pointer to the stack space.
492 * @param stack_size Stack size in bytes.
493 * @param entry Thread entry function.
494 * @param p1 1st entry point parameter.
495 * @param p2 2nd entry point parameter.
496 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500497 * @param prio Thread priority.
498 * @param options Thread options.
Andrew Boie7d627c52017-08-30 11:01:56 -0700499 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay),
500 * or K_FOREVER (to not run until k_thread_start() is called)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400501 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500502 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400503 */
Andrew Boie507852a2017-07-25 18:47:07 -0700504extern __deprecated k_tid_t k_thread_spawn(k_thread_stack_t stack,
505 size_t stack_size, k_thread_entry_t entry,
506 void *p1, void *p2, void *p3,
507 int prio, u32_t options, s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400508
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400509/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700510 * @brief Create a thread.
511 *
512 * This routine initializes a thread, then schedules it for execution.
513 *
514 * The new thread may be scheduled for immediate execution or a delayed start.
515 * If the newly spawned thread does not have a delayed start the kernel
516 * scheduler may preempt the current thread to allow the new thread to
517 * execute.
518 *
519 * Thread options are architecture-specific, and can include K_ESSENTIAL,
520 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
521 * them using "|" (the logical OR operator).
522 *
523 * Historically, users often would use the beginning of the stack memory region
524 * to store the struct k_thread data, although corruption will occur if the
525 * stack overflows this region and stack protection features may not detect this
526 * situation.
527 *
528 * @param new_thread Pointer to uninitialized struct k_thread
529 * @param stack Pointer to the stack space.
530 * @param stack_size Stack size in bytes.
531 * @param entry Thread entry function.
532 * @param p1 1st entry point parameter.
533 * @param p2 2nd entry point parameter.
534 * @param p3 3rd entry point parameter.
535 * @param prio Thread priority.
536 * @param options Thread options.
537 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
538 *
539 * @return ID of new thread.
540 */
Andrew Boie507852a2017-07-25 18:47:07 -0700541extern k_tid_t k_thread_create(struct k_thread *new_thread,
542 k_thread_stack_t stack,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700543 size_t stack_size,
544 void (*entry)(void *, void *, void*),
545 void *p1, void *p2, void *p3,
546 int prio, u32_t options, s32_t delay);
547
548/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500549 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400550 *
Allan Stephensc98da842016-11-11 15:45:03 -0500551 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500552 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400553 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500554 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400555 *
556 * @return N/A
557 */
Kumar Galacc334c72017-04-21 10:55:34 -0500558extern void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400559
560/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500561 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400562 *
563 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500564 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400565 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400566 * @return N/A
567 */
Kumar Galacc334c72017-04-21 10:55:34 -0500568extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400569
570/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500571 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400572 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500573 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400574 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500575 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400576 *
577 * @return N/A
578 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400579extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400580
581/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500582 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400583 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500584 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400585 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500586 * If @a thread is not currently sleeping, the routine has no effect.
587 *
588 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400589 *
590 * @return N/A
591 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400592extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400593
594/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500595 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400596 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500597 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400598 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400599extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400600
601/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500602 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400603 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500604 * This routine prevents @a thread from executing if it has not yet started
605 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400606 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500607 * @param thread ID of thread to cancel.
608 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700609 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500610 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400611 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400612extern int k_thread_cancel(k_tid_t thread);
613
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400614/**
Allan Stephensc98da842016-11-11 15:45:03 -0500615 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400616 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500617 * This routine permanently stops execution of @a thread. The thread is taken
618 * off all kernel queues it is part of (i.e. the ready queue, the timeout
619 * queue, or a kernel object wait queue). However, any kernel resources the
620 * thread might currently own (such as mutexes or memory blocks) are not
621 * released. It is the responsibility of the caller of this routine to ensure
622 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400623 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500624 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400625 *
626 * @return N/A
627 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400628extern void k_thread_abort(k_tid_t thread);
629
Andrew Boie7d627c52017-08-30 11:01:56 -0700630
631/**
632 * @brief Start an inactive thread
633 *
634 * If a thread was created with K_FOREVER in the delay parameter, it will
635 * not be added to the scheduling queue until this function is called
636 * on it.
637 *
638 * @param thread thread to start
639 */
640extern void k_thread_start(k_tid_t thread);
641
Allan Stephensc98da842016-11-11 15:45:03 -0500642/**
643 * @cond INTERNAL_HIDDEN
644 */
645
Benjamin Walshd211a522016-12-06 11:44:01 -0500646/* timeout has timed out and is not on _timeout_q anymore */
647#define _EXPIRED (-2)
648
649/* timeout is not in use */
650#define _INACTIVE (-1)
651
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400652struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700653 struct k_thread *init_thread;
Andrew Boie507852a2017-07-25 18:47:07 -0700654 k_thread_stack_t init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400655 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500656 void (*init_entry)(void *, void *, void *);
657 void *init_p1;
658 void *init_p2;
659 void *init_p3;
660 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500661 u32_t init_options;
662 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500663 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500664 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400665};
666
Andrew Boied26cf2d2017-03-30 13:07:02 -0700667#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400668 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500669 prio, options, delay, abort, groups) \
670 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700671 .init_thread = (thread), \
672 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500673 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400674 .init_entry = (void (*)(void *, void *, void *))entry, \
675 .init_p1 = (void *)p1, \
676 .init_p2 = (void *)p2, \
677 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500678 .init_prio = (prio), \
679 .init_options = (options), \
680 .init_delay = (delay), \
681 .init_abort = (abort), \
682 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400683 }
684
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400685/**
Allan Stephensc98da842016-11-11 15:45:03 -0500686 * INTERNAL_HIDDEN @endcond
687 */
688
689/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500690 * @brief Statically define and initialize a thread.
691 *
692 * The thread may be scheduled for immediate execution or a delayed start.
693 *
694 * Thread options are architecture-specific, and can include K_ESSENTIAL,
695 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
696 * them using "|" (the logical OR operator).
697 *
698 * The ID of the thread can be accessed using:
699 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500700 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500701 *
702 * @param name Name of the thread.
703 * @param stack_size Stack size in bytes.
704 * @param entry Thread entry function.
705 * @param p1 1st entry point parameter.
706 * @param p2 2nd entry point parameter.
707 * @param p3 3rd entry point parameter.
708 * @param prio Thread priority.
709 * @param options Thread options.
710 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400711 *
712 * @internal It has been observed that the x86 compiler by default aligns
713 * these _static_thread_data structures to 32-byte boundaries, thereby
714 * wasting space. To work around this, force a 4-byte alignment.
715 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500716#define K_THREAD_DEFINE(name, stack_size, \
717 entry, p1, p2, p3, \
718 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700719 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700720 struct k_thread _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500721 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500722 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700723 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
724 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500725 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700726 NULL, 0); \
727 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400728
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400729/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500730 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400731 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500732 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400733 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500734 * @param thread ID of thread whose priority is needed.
735 *
736 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400737 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500738extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400739
740/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500741 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400742 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500743 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400744 *
745 * Rescheduling can occur immediately depending on the priority @a thread is
746 * set to:
747 *
748 * - If its priority is raised above the priority of the caller of this
749 * function, and the caller is preemptible, @a thread will be scheduled in.
750 *
751 * - If the caller operates on itself, it lowers its priority below that of
752 * other threads in the system, and the caller is preemptible, the thread of
753 * highest priority will be scheduled in.
754 *
755 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
756 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
757 * highest priority.
758 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500759 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400760 * @param prio New priority.
761 *
762 * @warning Changing the priority of a thread currently involved in mutex
763 * priority inheritance may result in undefined behavior.
764 *
765 * @return N/A
766 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400767extern void k_thread_priority_set(k_tid_t thread, int prio);
768
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400769/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500770 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400771 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500772 * This routine prevents the kernel scheduler from making @a thread the
773 * current thread. All other internal operations on @a thread are still
774 * performed; for example, any timeout it is waiting on keeps ticking,
775 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400776 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500777 * If @a thread is already suspended, the routine has no effect.
778 *
779 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400780 *
781 * @return N/A
782 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400783extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400784
785/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500786 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400787 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500788 * This routine allows the kernel scheduler to make @a thread the current
789 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400790 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500791 * If @a thread is not currently suspended, the routine has no effect.
792 *
793 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400794 *
795 * @return N/A
796 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400797extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400798
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400799/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500800 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400801 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500802 * This routine specifies how the scheduler will perform time slicing of
803 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400804 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500805 * To enable time slicing, @a slice must be non-zero. The scheduler
806 * ensures that no thread runs for more than the specified time limit
807 * before other threads of that priority are given a chance to execute.
808 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700809 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400810 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500811 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400812 * execute. Once the scheduler selects a thread for execution, there is no
813 * minimum guaranteed time the thread will execute before threads of greater or
814 * equal priority are scheduled.
815 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500816 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400817 * for execution, this routine has no effect; the thread is immediately
818 * rescheduled after the slice period expires.
819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500820 * To disable timeslicing, set both @a slice and @a prio to zero.
821 *
822 * @param slice Maximum time slice length (in milliseconds).
823 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400824 *
825 * @return N/A
826 */
Kumar Galacc334c72017-04-21 10:55:34 -0500827extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400828
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400829/**
Allan Stephensc98da842016-11-11 15:45:03 -0500830 * @} end defgroup thread_apis
831 */
832
833/**
834 * @addtogroup isr_apis
835 * @{
836 */
837
838/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500839 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400840 *
Allan Stephensc98da842016-11-11 15:45:03 -0500841 * This routine allows the caller to customize its actions, depending on
842 * whether it is a thread or an ISR.
843 *
844 * @note Can be called by ISRs.
845 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500846 * @return 0 if invoked by a thread.
847 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400848 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500849extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400850
Benjamin Walsh445830d2016-11-10 15:54:27 -0500851/**
852 * @brief Determine if code is running in a preemptible thread.
853 *
Allan Stephensc98da842016-11-11 15:45:03 -0500854 * This routine allows the caller to customize its actions, depending on
855 * whether it can be preempted by another thread. The routine returns a 'true'
856 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500857 *
Allan Stephensc98da842016-11-11 15:45:03 -0500858 * - The code is running in a thread, not at ISR.
859 * - The thread's priority is in the preemptible range.
860 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500861 *
Allan Stephensc98da842016-11-11 15:45:03 -0500862 * @note Can be called by ISRs.
863 *
864 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500865 * @return Non-zero if invoked by a preemptible thread.
866 */
867extern int k_is_preempt_thread(void);
868
Allan Stephensc98da842016-11-11 15:45:03 -0500869/**
870 * @} end addtogroup isr_apis
871 */
872
873/**
874 * @addtogroup thread_apis
875 * @{
876 */
877
878/**
879 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500880 *
Allan Stephensc98da842016-11-11 15:45:03 -0500881 * This routine prevents the current thread from being preempted by another
882 * thread by instructing the scheduler to treat it as a cooperative thread.
883 * If the thread subsequently performs an operation that makes it unready,
884 * it will be context switched out in the normal manner. When the thread
885 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500886 *
Allan Stephensc98da842016-11-11 15:45:03 -0500887 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500888 *
Allan Stephensc98da842016-11-11 15:45:03 -0500889 * @note k_sched_lock() and k_sched_unlock() should normally be used
890 * when the operation being performed can be safely interrupted by ISRs.
891 * However, if the amount of processing involved is very small, better
892 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500893 *
894 * @return N/A
895 */
896extern void k_sched_lock(void);
897
Allan Stephensc98da842016-11-11 15:45:03 -0500898/**
899 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500900 *
Allan Stephensc98da842016-11-11 15:45:03 -0500901 * This routine reverses the effect of a previous call to k_sched_lock().
902 * A thread must call the routine once for each time it called k_sched_lock()
903 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500904 *
905 * @return N/A
906 */
907extern void k_sched_unlock(void);
908
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400909/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500910 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400911 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500912 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400913 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500914 * Custom data is not used by the kernel itself, and is freely available
915 * for a thread to use as it sees fit. It can be used as a framework
916 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400917 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500918 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400919 *
920 * @return N/A
921 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400922extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400923
924/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500925 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400926 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500927 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400928 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500929 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400930 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400931extern void *k_thread_custom_data_get(void);
932
933/**
Allan Stephensc98da842016-11-11 15:45:03 -0500934 * @} end addtogroup thread_apis
935 */
936
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400937#include <sys_clock.h>
938
Allan Stephensc2f15a42016-11-17 12:24:22 -0500939/**
940 * @addtogroup clock_apis
941 * @{
942 */
943
944/**
945 * @brief Generate null timeout delay.
946 *
947 * This macro generates a timeout delay that that instructs a kernel API
948 * not to wait if the requested operation cannot be performed immediately.
949 *
950 * @return Timeout delay value.
951 */
952#define K_NO_WAIT 0
953
954/**
955 * @brief Generate timeout delay from milliseconds.
956 *
957 * This macro generates a timeout delay that that instructs a kernel API
958 * to wait up to @a ms milliseconds to perform the requested operation.
959 *
960 * @param ms Duration in milliseconds.
961 *
962 * @return Timeout delay value.
963 */
Johan Hedberg14471692016-11-13 10:52:15 +0200964#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500965
966/**
967 * @brief Generate timeout delay from seconds.
968 *
969 * This macro generates a timeout delay that that instructs a kernel API
970 * to wait up to @a s seconds to perform the requested operation.
971 *
972 * @param s Duration in seconds.
973 *
974 * @return Timeout delay value.
975 */
Johan Hedberg14471692016-11-13 10:52:15 +0200976#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500977
978/**
979 * @brief Generate timeout delay from minutes.
980 *
981 * This macro generates a timeout delay that that instructs a kernel API
982 * to wait up to @a m minutes to perform the requested operation.
983 *
984 * @param m Duration in minutes.
985 *
986 * @return Timeout delay value.
987 */
Johan Hedberg14471692016-11-13 10:52:15 +0200988#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500989
990/**
991 * @brief Generate timeout delay from hours.
992 *
993 * This macro generates a timeout delay that that instructs a kernel API
994 * to wait up to @a h hours to perform the requested operation.
995 *
996 * @param h Duration in hours.
997 *
998 * @return Timeout delay value.
999 */
Johan Hedberg14471692016-11-13 10:52:15 +02001000#define K_HOURS(h) K_MINUTES((h) * 60)
1001
Allan Stephensc98da842016-11-11 15:45:03 -05001002/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001003 * @brief Generate infinite timeout delay.
1004 *
1005 * This macro generates a timeout delay that that instructs a kernel API
1006 * to wait as long as necessary to perform the requested operation.
1007 *
1008 * @return Timeout delay value.
1009 */
1010#define K_FOREVER (-1)
1011
1012/**
1013 * @} end addtogroup clock_apis
1014 */
1015
1016/**
Allan Stephensc98da842016-11-11 15:45:03 -05001017 * @cond INTERNAL_HIDDEN
1018 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001019
Benjamin Walsh62092182016-12-20 14:39:08 -05001020/* kernel clocks */
1021
1022#if (sys_clock_ticks_per_sec == 1000) || \
1023 (sys_clock_ticks_per_sec == 500) || \
1024 (sys_clock_ticks_per_sec == 250) || \
1025 (sys_clock_ticks_per_sec == 125) || \
1026 (sys_clock_ticks_per_sec == 100) || \
1027 (sys_clock_ticks_per_sec == 50) || \
1028 (sys_clock_ticks_per_sec == 25) || \
1029 (sys_clock_ticks_per_sec == 20) || \
1030 (sys_clock_ticks_per_sec == 10) || \
1031 (sys_clock_ticks_per_sec == 1)
1032
1033 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1034#else
1035 /* yields horrible 64-bit math on many architectures: try to avoid */
1036 #define _NON_OPTIMIZED_TICKS_PER_SEC
1037#endif
1038
1039#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001040extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001041#else
Kumar Galacc334c72017-04-21 10:55:34 -05001042static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001043{
Kumar Galacc334c72017-04-21 10:55:34 -05001044 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001045}
1046#endif
1047
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001048/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001049#ifdef CONFIG_TICKLESS_KERNEL
1050#define _TICK_ALIGN 0
1051#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001052#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001053#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001054
Kumar Galacc334c72017-04-21 10:55:34 -05001055static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001056{
Benjamin Walsh62092182016-12-20 14:39:08 -05001057#ifdef CONFIG_SYS_CLOCK_EXISTS
1058
1059#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001060 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001061#else
Kumar Galacc334c72017-04-21 10:55:34 -05001062 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001063#endif
1064
1065#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001066 __ASSERT(ticks == 0, "");
1067 return 0;
1068#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001069}
1070
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001071struct k_timer {
1072 /*
1073 * _timeout structure must be first here if we want to use
1074 * dynamic timer allocation. timeout.node is used in the double-linked
1075 * list of free timers
1076 */
1077 struct _timeout timeout;
1078
Allan Stephens45bfa372016-10-12 12:39:42 -05001079 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001080 _wait_q_t wait_q;
1081
1082 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001083 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001084
1085 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001086 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001087
1088 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001089 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001090
Allan Stephens45bfa372016-10-12 12:39:42 -05001091 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001092 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001093
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001094 /* user-specific data, also used to support legacy features */
1095 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001096
Anas Nashif2f203c22016-12-18 06:57:45 -05001097 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001098};
1099
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001100#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001101 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001102 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001103 .timeout.wait_q = NULL, \
1104 .timeout.thread = NULL, \
1105 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001106 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001107 .expiry_fn = expiry, \
1108 .stop_fn = stop, \
1109 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001110 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001111 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001112 }
1113
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001114#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1115
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001116/**
Allan Stephensc98da842016-11-11 15:45:03 -05001117 * INTERNAL_HIDDEN @endcond
1118 */
1119
1120/**
1121 * @defgroup timer_apis Timer APIs
1122 * @ingroup kernel_apis
1123 * @{
1124 */
1125
1126/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001127 * @typedef k_timer_expiry_t
1128 * @brief Timer expiry function type.
1129 *
1130 * A timer's expiry function is executed by the system clock interrupt handler
1131 * each time the timer expires. The expiry function is optional, and is only
1132 * invoked if the timer has been initialized with one.
1133 *
1134 * @param timer Address of timer.
1135 *
1136 * @return N/A
1137 */
1138typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1139
1140/**
1141 * @typedef k_timer_stop_t
1142 * @brief Timer stop function type.
1143 *
1144 * A timer's stop function is executed if the timer is stopped prematurely.
1145 * The function runs in the context of the thread that stops the timer.
1146 * The stop function is optional, and is only invoked if the timer has been
1147 * initialized with one.
1148 *
1149 * @param timer Address of timer.
1150 *
1151 * @return N/A
1152 */
1153typedef void (*k_timer_stop_t)(struct k_timer *timer);
1154
1155/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001156 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001157 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001158 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001159 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001160 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001161 *
1162 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001163 * @param expiry_fn Function to invoke each time the timer expires.
1164 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001165 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001166#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001167 struct k_timer name \
1168 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001169 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001170
Allan Stephens45bfa372016-10-12 12:39:42 -05001171/**
1172 * @brief Initialize a timer.
1173 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001174 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001175 *
1176 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001177 * @param expiry_fn Function to invoke each time the timer expires.
1178 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001179 *
1180 * @return N/A
1181 */
1182extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001183 k_timer_expiry_t expiry_fn,
1184 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001185
Allan Stephens45bfa372016-10-12 12:39:42 -05001186/**
1187 * @brief Start a timer.
1188 *
1189 * This routine starts a timer, and resets its status to zero. The timer
1190 * begins counting down using the specified duration and period values.
1191 *
1192 * Attempting to start a timer that is already running is permitted.
1193 * The timer's status is reset to zero and the timer begins counting down
1194 * using the new duration and period values.
1195 *
1196 * @param timer Address of timer.
1197 * @param duration Initial timer duration (in milliseconds).
1198 * @param period Timer period (in milliseconds).
1199 *
1200 * @return N/A
1201 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001202extern void k_timer_start(struct k_timer *timer,
Kumar Galacc334c72017-04-21 10:55:34 -05001203 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001204
1205/**
1206 * @brief Stop a timer.
1207 *
1208 * This routine stops a running timer prematurely. The timer's stop function,
1209 * if one exists, is invoked by the caller.
1210 *
1211 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001212 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001213 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001214 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1215 * if @a k_timer_stop is to be called from ISRs.
1216 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001217 * @param timer Address of timer.
1218 *
1219 * @return N/A
1220 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001221extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001222
1223/**
1224 * @brief Read timer status.
1225 *
1226 * This routine reads the timer's status, which indicates the number of times
1227 * it has expired since its status was last read.
1228 *
1229 * Calling this routine resets the timer's status to zero.
1230 *
1231 * @param timer Address of timer.
1232 *
1233 * @return Timer status.
1234 */
Kumar Galacc334c72017-04-21 10:55:34 -05001235extern u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001236
1237/**
1238 * @brief Synchronize thread to timer expiration.
1239 *
1240 * This routine blocks the calling thread until the timer's status is non-zero
1241 * (indicating that it has expired at least once since it was last examined)
1242 * or the timer is stopped. If the timer status is already non-zero,
1243 * or the timer is already stopped, the caller continues without waiting.
1244 *
1245 * Calling this routine resets the timer's status to zero.
1246 *
1247 * This routine must not be used by interrupt handlers, since they are not
1248 * allowed to block.
1249 *
1250 * @param timer Address of timer.
1251 *
1252 * @return Timer status.
1253 */
Kumar Galacc334c72017-04-21 10:55:34 -05001254extern u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001255
1256/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001257 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001258 *
1259 * This routine computes the (approximate) time remaining before a running
1260 * timer next expires. If the timer is not running, it returns zero.
1261 *
1262 * @param timer Address of timer.
1263 *
1264 * @return Remaining time (in milliseconds).
1265 */
Kumar Galacc334c72017-04-21 10:55:34 -05001266static inline s32_t k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001267{
1268 return _timeout_remaining_get(&timer->timeout);
1269}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001270
Allan Stephensc98da842016-11-11 15:45:03 -05001271/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001272 * @brief Associate user-specific data with a timer.
1273 *
1274 * This routine records the @a user_data with the @a timer, to be retrieved
1275 * later.
1276 *
1277 * It can be used e.g. in a timer handler shared across multiple subsystems to
1278 * retrieve data specific to the subsystem this timer is associated with.
1279 *
1280 * @param timer Address of timer.
1281 * @param user_data User data to associate with the timer.
1282 *
1283 * @return N/A
1284 */
1285static inline void k_timer_user_data_set(struct k_timer *timer,
1286 void *user_data)
1287{
1288 timer->user_data = user_data;
1289}
1290
1291/**
1292 * @brief Retrieve the user-specific data from a timer.
1293 *
1294 * @param timer Address of timer.
1295 *
1296 * @return The user data.
1297 */
1298static inline void *k_timer_user_data_get(struct k_timer *timer)
1299{
1300 return timer->user_data;
1301}
1302
1303/**
Allan Stephensc98da842016-11-11 15:45:03 -05001304 * @} end defgroup timer_apis
1305 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001306
Allan Stephensc98da842016-11-11 15:45:03 -05001307/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001308 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001309 * @{
1310 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001311
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001312/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001313 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001314 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001315 * This routine returns the elapsed time since the system booted,
1316 * in milliseconds.
1317 *
1318 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001319 */
Kumar Galacc334c72017-04-21 10:55:34 -05001320extern s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001321
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001322#ifdef CONFIG_TICKLESS_KERNEL
1323/**
1324 * @brief Enable clock always on in tickless kernel
1325 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001326 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001327 * there are no timer events programmed in tickless kernel
1328 * scheduling. This is necessary if the clock is used to track
1329 * passage of time.
1330 *
1331 * @retval prev_status Previous status of always on flag
1332 */
1333static inline int k_enable_sys_clock_always_on(void)
1334{
1335 int prev_status = _sys_clock_always_on;
1336
1337 _sys_clock_always_on = 1;
1338 _enable_sys_clock();
1339
1340 return prev_status;
1341}
1342
1343/**
1344 * @brief Disable clock always on in tickless kernel
1345 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001346 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001347 * there are no timer events programmed in tickless kernel
1348 * scheduling. To save power, this routine should be called
1349 * immediately when clock is not used to track time.
1350 */
1351static inline void k_disable_sys_clock_always_on(void)
1352{
1353 _sys_clock_always_on = 0;
1354}
1355#else
1356#define k_enable_sys_clock_always_on() do { } while ((0))
1357#define k_disable_sys_clock_always_on() do { } while ((0))
1358#endif
1359
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001360/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001361 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001362 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001363 * This routine returns the lower 32-bits of the elapsed time since the system
1364 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001365 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001366 * This routine can be more efficient than k_uptime_get(), as it reduces the
1367 * need for interrupt locking and 64-bit math. However, the 32-bit result
1368 * cannot hold a system uptime time larger than approximately 50 days, so the
1369 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001370 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001371 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001372 */
Kumar Galacc334c72017-04-21 10:55:34 -05001373extern u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001374
1375/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001376 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001377 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001378 * This routine computes the elapsed time between the current system uptime
1379 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001380 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001381 * @param reftime Pointer to a reference time, which is updated to the current
1382 * uptime upon return.
1383 *
1384 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001385 */
Kumar Galacc334c72017-04-21 10:55:34 -05001386extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001387
1388/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001389 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001390 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001391 * This routine computes the elapsed time between the current system uptime
1392 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001393 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001394 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1395 * need for interrupt locking and 64-bit math. However, the 32-bit result
1396 * cannot hold an elapsed time larger than approximately 50 days, so the
1397 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001398 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001399 * @param reftime Pointer to a reference time, which is updated to the current
1400 * uptime upon return.
1401 *
1402 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001403 */
Kumar Galacc334c72017-04-21 10:55:34 -05001404extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001405
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001406/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001407 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001408 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001409 * This routine returns the current time, as measured by the system's hardware
1410 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001411 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001412 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001413 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001414#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001415
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001416/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001417 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001418 */
1419
Allan Stephensc98da842016-11-11 15:45:03 -05001420/**
1421 * @cond INTERNAL_HIDDEN
1422 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001423
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001424struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001425 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001426 union {
1427 _wait_q_t wait_q;
1428
1429 _POLL_EVENT;
1430 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001431
1432 _OBJECT_TRACING_NEXT_PTR(k_queue);
1433};
1434
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001435#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001436 { \
1437 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1438 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001439 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001440 _OBJECT_TRACING_INIT \
1441 }
1442
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001443#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1444
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001445/**
1446 * INTERNAL_HIDDEN @endcond
1447 */
1448
1449/**
1450 * @defgroup queue_apis Queue APIs
1451 * @ingroup kernel_apis
1452 * @{
1453 */
1454
1455/**
1456 * @brief Initialize a queue.
1457 *
1458 * This routine initializes a queue object, prior to its first use.
1459 *
1460 * @param queue Address of the queue.
1461 *
1462 * @return N/A
1463 */
1464extern void k_queue_init(struct k_queue *queue);
1465
1466/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001467 * @brief Cancel waiting on a queue.
1468 *
1469 * This routine causes first thread pending on @a queue, if any, to
1470 * return from k_queue_get() call with NULL value (as if timeout expired).
1471 *
1472 * @note Can be called by ISRs.
1473 *
1474 * @param queue Address of the queue.
1475 *
1476 * @return N/A
1477 */
1478extern void k_queue_cancel_wait(struct k_queue *queue);
1479
1480/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001481 * @brief Append an element to the end of a queue.
1482 *
1483 * This routine appends a data item to @a queue. A queue data item must be
1484 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1485 * reserved for the kernel's use.
1486 *
1487 * @note Can be called by ISRs.
1488 *
1489 * @param queue Address of the queue.
1490 * @param data Address of the data item.
1491 *
1492 * @return N/A
1493 */
1494extern void k_queue_append(struct k_queue *queue, void *data);
1495
1496/**
1497 * @brief Prepend an element to a queue.
1498 *
1499 * This routine prepends a data item to @a queue. A queue data item must be
1500 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1501 * reserved for the kernel's use.
1502 *
1503 * @note Can be called by ISRs.
1504 *
1505 * @param queue Address of the queue.
1506 * @param data Address of the data item.
1507 *
1508 * @return N/A
1509 */
1510extern void k_queue_prepend(struct k_queue *queue, void *data);
1511
1512/**
1513 * @brief Inserts an element to a queue.
1514 *
1515 * This routine inserts a data item to @a queue after previous item. A queue
1516 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1517 * item are reserved for the kernel's use.
1518 *
1519 * @note Can be called by ISRs.
1520 *
1521 * @param queue Address of the queue.
1522 * @param prev Address of the previous data item.
1523 * @param data Address of the data item.
1524 *
1525 * @return N/A
1526 */
1527extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1528
1529/**
1530 * @brief Atomically append a list of elements to a queue.
1531 *
1532 * This routine adds a list of data items to @a queue in one operation.
1533 * The data items must be in a singly-linked list, with the first 32 bits
1534 * in each data item pointing to the next data item; the list must be
1535 * NULL-terminated.
1536 *
1537 * @note Can be called by ISRs.
1538 *
1539 * @param queue Address of the queue.
1540 * @param head Pointer to first node in singly-linked list.
1541 * @param tail Pointer to last node in singly-linked list.
1542 *
1543 * @return N/A
1544 */
1545extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1546
1547/**
1548 * @brief Atomically add a list of elements to a queue.
1549 *
1550 * This routine adds a list of data items to @a queue in one operation.
1551 * The data items must be in a singly-linked list implemented using a
1552 * sys_slist_t object. Upon completion, the original list is empty.
1553 *
1554 * @note Can be called by ISRs.
1555 *
1556 * @param queue Address of the queue.
1557 * @param list Pointer to sys_slist_t object.
1558 *
1559 * @return N/A
1560 */
1561extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1562
1563/**
1564 * @brief Get an element from a queue.
1565 *
1566 * This routine removes first data item from @a queue. The first 32 bits of the
1567 * data item are reserved for the kernel's use.
1568 *
1569 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1570 *
1571 * @param queue Address of the queue.
1572 * @param timeout Waiting period to obtain a data item (in milliseconds),
1573 * or one of the special values K_NO_WAIT and K_FOREVER.
1574 *
1575 * @return Address of the data item if successful; NULL if returned
1576 * without waiting, or waiting period timed out.
1577 */
Kumar Galacc334c72017-04-21 10:55:34 -05001578extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001579
1580/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001581 * @brief Remove an element from a queue.
1582 *
1583 * This routine removes data item from @a queue. The first 32 bits of the
1584 * data item are reserved for the kernel's use. Removing elements from k_queue
1585 * rely on sys_slist_find_and_remove which is not a constant time operation.
1586 *
1587 * @note Can be called by ISRs
1588 *
1589 * @param queue Address of the queue.
1590 * @param data Address of the data item.
1591 *
1592 * @return true if data item was removed
1593 */
1594static inline bool k_queue_remove(struct k_queue *queue, void *data)
1595{
1596 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1597}
1598
1599/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001600 * @brief Query a queue to see if it has data available.
1601 *
1602 * Note that the data might be already gone by the time this function returns
1603 * if other threads are also trying to read from the queue.
1604 *
1605 * @note Can be called by ISRs.
1606 *
1607 * @param queue Address of the queue.
1608 *
1609 * @return Non-zero if the queue is empty.
1610 * @return 0 if data is available.
1611 */
1612static inline int k_queue_is_empty(struct k_queue *queue)
1613{
1614 return (int)sys_slist_is_empty(&queue->data_q);
1615}
1616
1617/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001618 * @brief Peek element at the head of queue.
1619 *
1620 * Return element from the head of queue without removing it.
1621 *
1622 * @param queue Address of the queue.
1623 *
1624 * @return Head element, or NULL if queue is empty.
1625 */
1626static inline void *k_queue_peek_head(struct k_queue *queue)
1627{
1628 return sys_slist_peek_head(&queue->data_q);
1629}
1630
1631/**
1632 * @brief Peek element at the tail of queue.
1633 *
1634 * Return element from the tail of queue without removing it.
1635 *
1636 * @param queue Address of the queue.
1637 *
1638 * @return Tail element, or NULL if queue is empty.
1639 */
1640static inline void *k_queue_peek_tail(struct k_queue *queue)
1641{
1642 return sys_slist_peek_tail(&queue->data_q);
1643}
1644
1645/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001646 * @brief Statically define and initialize a queue.
1647 *
1648 * The queue can be accessed outside the module where it is defined using:
1649 *
1650 * @code extern struct k_queue <name>; @endcode
1651 *
1652 * @param name Name of the queue.
1653 */
1654#define K_QUEUE_DEFINE(name) \
1655 struct k_queue name \
1656 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001657 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001658
1659/**
1660 * @} end defgroup queue_apis
1661 */
1662
1663/**
1664 * @cond INTERNAL_HIDDEN
1665 */
1666
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001667struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001668 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001669};
1670
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001671#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001672 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001673 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001674 }
1675
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001676#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1677
Allan Stephensc98da842016-11-11 15:45:03 -05001678/**
1679 * INTERNAL_HIDDEN @endcond
1680 */
1681
1682/**
1683 * @defgroup fifo_apis Fifo APIs
1684 * @ingroup kernel_apis
1685 * @{
1686 */
1687
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001688/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001689 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001690 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001691 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001692 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001693 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001694 *
1695 * @return N/A
1696 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001697#define k_fifo_init(fifo) \
1698 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001699
1700/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001701 * @brief Cancel waiting on a fifo.
1702 *
1703 * This routine causes first thread pending on @a fifo, if any, to
1704 * return from k_fifo_get() call with NULL value (as if timeout
1705 * expired).
1706 *
1707 * @note Can be called by ISRs.
1708 *
1709 * @param fifo Address of the fifo.
1710 *
1711 * @return N/A
1712 */
1713#define k_fifo_cancel_wait(fifo) \
1714 k_queue_cancel_wait((struct k_queue *) fifo)
1715
1716/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001717 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001718 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001719 * This routine adds a data item to @a fifo. A fifo data item must be
1720 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1721 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001722 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001723 * @note Can be called by ISRs.
1724 *
1725 * @param fifo Address of the fifo.
1726 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001727 *
1728 * @return N/A
1729 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001730#define k_fifo_put(fifo, data) \
1731 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001732
1733/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001734 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001735 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001736 * This routine adds a list of data items to @a fifo in one operation.
1737 * The data items must be in a singly-linked list, with the first 32 bits
1738 * each data item pointing to the next data item; the list must be
1739 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001740 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001741 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001742 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001743 * @param fifo Address of the fifo.
1744 * @param head Pointer to first node in singly-linked list.
1745 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001746 *
1747 * @return N/A
1748 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001749#define k_fifo_put_list(fifo, head, tail) \
1750 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001751
1752/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001753 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001754 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001755 * This routine adds a list of data items to @a fifo in one operation.
1756 * The data items must be in a singly-linked list implemented using a
1757 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001758 * and must be re-initialized via sys_slist_init().
1759 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001760 * @note Can be called by ISRs.
1761 *
1762 * @param fifo Address of the fifo.
1763 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001764 *
1765 * @return N/A
1766 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001767#define k_fifo_put_slist(fifo, list) \
1768 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001769
1770/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001771 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001772 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001773 * This routine removes a data item from @a fifo in a "first in, first out"
1774 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001775 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001776 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1777 *
1778 * @param fifo Address of the fifo.
1779 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001780 * or one of the special values K_NO_WAIT and K_FOREVER.
1781 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001782 * @return Address of the data item if successful; NULL if returned
1783 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001784 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001785#define k_fifo_get(fifo, timeout) \
1786 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001787
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001788/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001789 * @brief Query a fifo to see if it has data available.
1790 *
1791 * Note that the data might be already gone by the time this function returns
1792 * if other threads is also trying to read from the fifo.
1793 *
1794 * @note Can be called by ISRs.
1795 *
1796 * @param fifo Address of the fifo.
1797 *
1798 * @return Non-zero if the fifo is empty.
1799 * @return 0 if data is available.
1800 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001801#define k_fifo_is_empty(fifo) \
1802 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001803
1804/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001805 * @brief Peek element at the head of fifo.
1806 *
1807 * Return element from the head of fifo without removing it. A usecase
1808 * for this is if elements of the fifo are themselves containers. Then
1809 * on each iteration of processing, a head container will be peeked,
1810 * and some data processed out of it, and only if the container is empty,
1811 * it will be completely remove from the fifo.
1812 *
1813 * @param fifo Address of the fifo.
1814 *
1815 * @return Head element, or NULL if the fifo is empty.
1816 */
1817#define k_fifo_peek_head(fifo) \
1818 k_queue_peek_head((struct k_queue *) fifo)
1819
1820/**
1821 * @brief Peek element at the tail of fifo.
1822 *
1823 * Return element from the tail of fifo (without removing it). A usecase
1824 * for this is if elements of the fifo are themselves containers. Then
1825 * it may be useful to add more data to the last container in fifo.
1826 *
1827 * @param fifo Address of the fifo.
1828 *
1829 * @return Tail element, or NULL if fifo is empty.
1830 */
1831#define k_fifo_peek_tail(fifo) \
1832 k_queue_peek_tail((struct k_queue *) fifo)
1833
1834/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001835 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001836 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001837 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001838 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001839 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001840 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001841 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001842 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001843#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001844 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001845 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001846 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001847
Allan Stephensc98da842016-11-11 15:45:03 -05001848/**
1849 * @} end defgroup fifo_apis
1850 */
1851
1852/**
1853 * @cond INTERNAL_HIDDEN
1854 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001855
1856struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001857 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001858};
1859
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001860#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001861 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001862 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001863 }
1864
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001865#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1866
Allan Stephensc98da842016-11-11 15:45:03 -05001867/**
1868 * INTERNAL_HIDDEN @endcond
1869 */
1870
1871/**
1872 * @defgroup lifo_apis Lifo APIs
1873 * @ingroup kernel_apis
1874 * @{
1875 */
1876
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001877/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001878 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001879 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001880 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001882 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001883 *
1884 * @return N/A
1885 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001886#define k_lifo_init(lifo) \
1887 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001888
1889/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001890 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001891 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001892 * This routine adds a data item to @a lifo. A lifo data item must be
1893 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1894 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001895 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001896 * @note Can be called by ISRs.
1897 *
1898 * @param lifo Address of the lifo.
1899 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001900 *
1901 * @return N/A
1902 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001903#define k_lifo_put(lifo, data) \
1904 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001905
1906/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001907 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001908 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001909 * This routine removes a data item from @a lifo in a "last in, first out"
1910 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001911 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001912 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1913 *
1914 * @param lifo Address of the lifo.
1915 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001916 * or one of the special values K_NO_WAIT and K_FOREVER.
1917 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001918 * @return Address of the data item if successful; NULL if returned
1919 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001920 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001921#define k_lifo_get(lifo, timeout) \
1922 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001923
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001924/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001925 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001926 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001927 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001928 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001929 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001930 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001931 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001932 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001933#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001934 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001935 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001936 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001937
Allan Stephensc98da842016-11-11 15:45:03 -05001938/**
1939 * @} end defgroup lifo_apis
1940 */
1941
1942/**
1943 * @cond INTERNAL_HIDDEN
1944 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001945
1946struct k_stack {
1947 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001948 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001949
Anas Nashif2f203c22016-12-18 06:57:45 -05001950 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001951};
1952
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001953#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05001954 { \
1955 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1956 .base = stack_buffer, \
1957 .next = stack_buffer, \
1958 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001959 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001960 }
1961
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001962#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
1963
Allan Stephensc98da842016-11-11 15:45:03 -05001964/**
1965 * INTERNAL_HIDDEN @endcond
1966 */
1967
1968/**
1969 * @defgroup stack_apis Stack APIs
1970 * @ingroup kernel_apis
1971 * @{
1972 */
1973
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001974/**
1975 * @brief Initialize a stack.
1976 *
1977 * This routine initializes a stack object, prior to its first use.
1978 *
1979 * @param stack Address of the stack.
1980 * @param buffer Address of array used to hold stacked values.
1981 * @param num_entries Maximum number of values that can be stacked.
1982 *
1983 * @return N/A
1984 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001985extern void k_stack_init(struct k_stack *stack,
Kumar Galacc334c72017-04-21 10:55:34 -05001986 u32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001987
1988/**
1989 * @brief Push an element onto a stack.
1990 *
1991 * This routine adds a 32-bit value @a data to @a stack.
1992 *
1993 * @note Can be called by ISRs.
1994 *
1995 * @param stack Address of the stack.
1996 * @param data Value to push onto the stack.
1997 *
1998 * @return N/A
1999 */
Kumar Galacc334c72017-04-21 10:55:34 -05002000extern void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002001
2002/**
2003 * @brief Pop an element from a stack.
2004 *
2005 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2006 * manner and stores the value in @a data.
2007 *
2008 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2009 *
2010 * @param stack Address of the stack.
2011 * @param data Address of area to hold the value popped from the stack.
2012 * @param timeout Waiting period to obtain a value (in milliseconds),
2013 * or one of the special values K_NO_WAIT and K_FOREVER.
2014 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002015 * @retval 0 Element popped from stack.
2016 * @retval -EBUSY Returned without waiting.
2017 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002018 */
Kumar Galacc334c72017-04-21 10:55:34 -05002019extern int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002020
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002021/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002022 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002023 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002024 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002025 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002026 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002027 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002028 * @param name Name of the stack.
2029 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002030 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002031#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002032 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002033 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002034 struct k_stack name \
2035 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002036 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002037 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002038
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002039/**
Allan Stephensc98da842016-11-11 15:45:03 -05002040 * @} end defgroup stack_apis
2041 */
2042
Allan Stephens6bba9b02016-11-16 14:56:54 -05002043struct k_work;
2044
Allan Stephensc98da842016-11-11 15:45:03 -05002045/**
2046 * @defgroup workqueue_apis Workqueue Thread APIs
2047 * @ingroup kernel_apis
2048 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002049 */
2050
Allan Stephens6bba9b02016-11-16 14:56:54 -05002051/**
2052 * @typedef k_work_handler_t
2053 * @brief Work item handler function type.
2054 *
2055 * A work item's handler function is executed by a workqueue's thread
2056 * when the work item is processed by the workqueue.
2057 *
2058 * @param work Address of the work item.
2059 *
2060 * @return N/A
2061 */
2062typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002063
2064/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002065 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002066 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002067
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002068struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002069 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002070 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002071};
2072
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002073enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002074 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002075};
2076
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002077struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002078 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002079 k_work_handler_t handler;
2080 atomic_t flags[1];
2081};
2082
Allan Stephens6bba9b02016-11-16 14:56:54 -05002083struct k_delayed_work {
2084 struct k_work work;
2085 struct _timeout timeout;
2086 struct k_work_q *work_q;
2087};
2088
2089extern struct k_work_q k_sys_work_q;
2090
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002091/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002092 * INTERNAL_HIDDEN @endcond
2093 */
2094
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002095#define _K_WORK_INITIALIZER(work_handler) \
2096 { \
2097 ._reserved = NULL, \
2098 .handler = work_handler, \
2099 .flags = { 0 } \
2100 }
2101
2102#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2103
Allan Stephens6bba9b02016-11-16 14:56:54 -05002104/**
2105 * @brief Initialize a statically-defined work item.
2106 *
2107 * This macro can be used to initialize a statically-defined workqueue work
2108 * item, prior to its first use. For example,
2109 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002110 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002111 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002112 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002113 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002114 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002115#define K_WORK_DEFINE(work, work_handler) \
2116 struct k_work work \
2117 __in_section(_k_work, static, work) = \
2118 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002119
2120/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002121 * @brief Initialize a work item.
2122 *
2123 * This routine initializes a workqueue work item, prior to its first use.
2124 *
2125 * @param work Address of work item.
2126 * @param handler Function to invoke each time work item is processed.
2127 *
2128 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002129 */
2130static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2131{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002132 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002133 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002134 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002135}
2136
2137/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002138 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002139 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002140 * This routine submits work item @a work to be processed by workqueue
2141 * @a work_q. If the work item is already pending in the workqueue's queue
2142 * as a result of an earlier submission, this routine has no effect on the
2143 * work item. If the work item has already been processed, or is currently
2144 * being processed, its work is considered complete and the work item can be
2145 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002146 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002147 * @warning
2148 * A submitted work item must not be modified until it has been processed
2149 * by the workqueue.
2150 *
2151 * @note Can be called by ISRs.
2152 *
2153 * @param work_q Address of workqueue.
2154 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002155 *
2156 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002157 */
2158static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2159 struct k_work *work)
2160{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002161 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002162 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002163 }
2164}
2165
2166/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002167 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002168 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002169 * This routine indicates if work item @a work is pending in a workqueue's
2170 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002171 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002172 * @note Can be called by ISRs.
2173 *
2174 * @param work Address of work item.
2175 *
2176 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002177 */
2178static inline int k_work_pending(struct k_work *work)
2179{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002180 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002181}
2182
2183/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002184 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002185 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002186 * This routine starts workqueue @a work_q. The workqueue spawns its work
2187 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002188 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002189 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002190 * @param stack Pointer to work queue thread's stack space, as defined by
2191 * K_THREAD_STACK_DEFINE()
2192 * @param stack_size Size of the work queue thread's stack (in bytes), which
2193 * should either be the same constant passed to
2194 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002195 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002196 *
2197 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002198 */
Andrew Boie507852a2017-07-25 18:47:07 -07002199extern void k_work_q_start(struct k_work_q *work_q,
2200 k_thread_stack_t stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002201 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002202
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002203/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002204 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002205 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002206 * This routine initializes a workqueue delayed work item, prior to
2207 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002208 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002209 * @param work Address of delayed work item.
2210 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002211 *
2212 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002213 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002214extern void k_delayed_work_init(struct k_delayed_work *work,
2215 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002216
2217/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002218 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002219 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002220 * This routine schedules work item @a work to be processed by workqueue
2221 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002222 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002223 * Only when the countdown completes is the work item actually submitted to
2224 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002225 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002226 * Submitting a previously submitted delayed work item that is still
2227 * counting down cancels the existing submission and restarts the countdown
2228 * using the new delay. If the work item is currently pending on the
2229 * workqueue's queue because the countdown has completed it is too late to
2230 * resubmit the item, and resubmission fails without impacting the work item.
2231 * If the work item has already been processed, or is currently being processed,
2232 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002233 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002234 * @warning
2235 * A delayed work item must not be modified until it has been processed
2236 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002237 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002238 * @note Can be called by ISRs.
2239 *
2240 * @param work_q Address of workqueue.
2241 * @param work Address of delayed work item.
2242 * @param delay Delay before submitting the work item (in milliseconds).
2243 *
2244 * @retval 0 Work item countdown started.
2245 * @retval -EINPROGRESS Work item is already pending.
2246 * @retval -EINVAL Work item is being processed or has completed its work.
2247 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002248 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002249extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2250 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002251 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002252
2253/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002254 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002255 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002256 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002257 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002258 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002259 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002260 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002261 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002262 * @param work Address of delayed work item.
2263 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002264 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002265 * @retval -EINPROGRESS Work item is already pending.
2266 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002267 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002268extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002269
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002270/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002271 * @brief Submit a work item to the system workqueue.
2272 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002273 * This routine submits work item @a work to be processed by the system
2274 * workqueue. If the work item is already pending in the workqueue's queue
2275 * as a result of an earlier submission, this routine has no effect on the
2276 * work item. If the work item has already been processed, or is currently
2277 * being processed, its work is considered complete and the work item can be
2278 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002279 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002280 * @warning
2281 * Work items submitted to the system workqueue should avoid using handlers
2282 * that block or yield since this may prevent the system workqueue from
2283 * processing other work items in a timely manner.
2284 *
2285 * @note Can be called by ISRs.
2286 *
2287 * @param work Address of work item.
2288 *
2289 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002290 */
2291static inline void k_work_submit(struct k_work *work)
2292{
2293 k_work_submit_to_queue(&k_sys_work_q, work);
2294}
2295
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002296/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002297 * @brief Submit a delayed work item to the system workqueue.
2298 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002299 * This routine schedules work item @a work to be processed by the system
2300 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002301 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002302 * Only when the countdown completes is the work item actually submitted to
2303 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002304 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002305 * Submitting a previously submitted delayed work item that is still
2306 * counting down cancels the existing submission and restarts the countdown
2307 * using the new delay. If the work item is currently pending on the
2308 * workqueue's queue because the countdown has completed it is too late to
2309 * resubmit the item, and resubmission fails without impacting the work item.
2310 * If the work item has already been processed, or is currently being processed,
2311 * its work is considered complete and the work item can be resubmitted.
2312 *
2313 * @warning
2314 * Work items submitted to the system workqueue should avoid using handlers
2315 * that block or yield since this may prevent the system workqueue from
2316 * processing other work items in a timely manner.
2317 *
2318 * @note Can be called by ISRs.
2319 *
2320 * @param work Address of delayed work item.
2321 * @param delay Delay before submitting the work item (in milliseconds).
2322 *
2323 * @retval 0 Work item countdown started.
2324 * @retval -EINPROGRESS Work item is already pending.
2325 * @retval -EINVAL Work item is being processed or has completed its work.
2326 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002327 */
2328static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002329 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002330{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002331 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002332}
2333
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002334/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002335 * @brief Get time remaining before a delayed work gets scheduled.
2336 *
2337 * This routine computes the (approximate) time remaining before a
2338 * delayed work gets executed. If the delayed work is not waiting to be
2339 * schedules, it returns zero.
2340 *
2341 * @param work Delayed work item.
2342 *
2343 * @return Remaining time (in milliseconds).
2344 */
Kumar Galacc334c72017-04-21 10:55:34 -05002345static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002346{
2347 return _timeout_remaining_get(&work->timeout);
2348}
2349
2350/**
Allan Stephensc98da842016-11-11 15:45:03 -05002351 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002352 */
2353
Allan Stephensc98da842016-11-11 15:45:03 -05002354/**
2355 * @cond INTERNAL_HIDDEN
2356 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002357
2358struct k_mutex {
2359 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002360 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002361 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002362 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002363
Anas Nashif2f203c22016-12-18 06:57:45 -05002364 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002365};
2366
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002367#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002368 { \
2369 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2370 .owner = NULL, \
2371 .lock_count = 0, \
2372 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002373 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002374 }
2375
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002376#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2377
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002378/**
Allan Stephensc98da842016-11-11 15:45:03 -05002379 * INTERNAL_HIDDEN @endcond
2380 */
2381
2382/**
2383 * @defgroup mutex_apis Mutex APIs
2384 * @ingroup kernel_apis
2385 * @{
2386 */
2387
2388/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002389 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002390 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002391 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002392 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002393 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002394 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002395 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002396 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002397#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002398 struct k_mutex name \
2399 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002400 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002401
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002402/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002403 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002404 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002405 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002406 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002407 * Upon completion, the mutex is available and does not have an owner.
2408 *
2409 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002410 *
2411 * @return N/A
2412 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002413extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002414
2415/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002416 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002418 * This routine locks @a mutex. If the mutex is locked by another thread,
2419 * the calling thread waits until the mutex becomes available or until
2420 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002421 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002422 * A thread is permitted to lock a mutex it has already locked. The operation
2423 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002424 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002425 * @param mutex Address of the mutex.
2426 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002427 * or one of the special values K_NO_WAIT and K_FOREVER.
2428 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002429 * @retval 0 Mutex locked.
2430 * @retval -EBUSY Returned without waiting.
2431 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002432 */
Kumar Galacc334c72017-04-21 10:55:34 -05002433extern int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002434
2435/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002436 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002437 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002438 * This routine unlocks @a mutex. The mutex must already be locked by the
2439 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002440 *
2441 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002442 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002443 * thread.
2444 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002445 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002446 *
2447 * @return N/A
2448 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002449extern void k_mutex_unlock(struct k_mutex *mutex);
2450
Allan Stephensc98da842016-11-11 15:45:03 -05002451/**
2452 * @} end defgroup mutex_apis
2453 */
2454
2455/**
2456 * @cond INTERNAL_HIDDEN
2457 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002458
2459struct k_sem {
2460 _wait_q_t wait_q;
2461 unsigned int count;
2462 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002463 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002464
Anas Nashif2f203c22016-12-18 06:57:45 -05002465 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002466};
2467
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002468#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002469 { \
2470 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2471 .count = initial_count, \
2472 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002473 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002474 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002475 }
2476
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002477#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2478
Allan Stephensc98da842016-11-11 15:45:03 -05002479/**
2480 * INTERNAL_HIDDEN @endcond
2481 */
2482
2483/**
2484 * @defgroup semaphore_apis Semaphore APIs
2485 * @ingroup kernel_apis
2486 * @{
2487 */
2488
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002489/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002490 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002491 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002492 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002493 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002494 * @param sem Address of the semaphore.
2495 * @param initial_count Initial semaphore count.
2496 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002497 *
2498 * @return N/A
2499 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002500extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2501 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002502
2503/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002504 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002505 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002506 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002507 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002508 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2509 *
2510 * @param sem Address of the semaphore.
2511 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002512 * or one of the special values K_NO_WAIT and K_FOREVER.
2513 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002514 * @note When porting code from the nanokernel legacy API to the new API, be
2515 * careful with the return value of this function. The return value is the
2516 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2517 * non-zero means failure, while the nano_sem_take family returns 1 for success
2518 * and 0 for failure.
2519 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002520 * @retval 0 Semaphore taken.
2521 * @retval -EBUSY Returned without waiting.
2522 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002523 */
Kumar Galacc334c72017-04-21 10:55:34 -05002524extern int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002525
2526/**
2527 * @brief Give a semaphore.
2528 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002529 * This routine gives @a sem, unless the semaphore is already at its maximum
2530 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002531 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002532 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002533 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002534 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002535 *
2536 * @return N/A
2537 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002538extern void k_sem_give(struct k_sem *sem);
2539
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002540/**
2541 * @brief Reset a semaphore's count to zero.
2542 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002543 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002544 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002545 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002546 *
2547 * @return N/A
2548 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04002549static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002550{
2551 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002552}
2553
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002554/**
2555 * @brief Get a semaphore's count.
2556 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002557 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002558 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002559 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002560 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002561 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002562 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02002563static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002564{
2565 return sem->count;
2566}
2567
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002568/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002569 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002570 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002571 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002572 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002573 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002574 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002575 * @param name Name of the semaphore.
2576 * @param initial_count Initial semaphore count.
2577 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002578 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002579#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002580 struct k_sem name \
2581 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002582 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002583
Allan Stephensc98da842016-11-11 15:45:03 -05002584/**
2585 * @} end defgroup semaphore_apis
2586 */
2587
2588/**
2589 * @defgroup alert_apis Alert APIs
2590 * @ingroup kernel_apis
2591 * @{
2592 */
2593
Allan Stephens5eceb852016-11-16 10:16:30 -05002594/**
2595 * @typedef k_alert_handler_t
2596 * @brief Alert handler function type.
2597 *
2598 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002599 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002600 * and is only invoked if the alert has been initialized with one.
2601 *
2602 * @param alert Address of the alert.
2603 *
2604 * @return 0 if alert has been consumed; non-zero if alert should pend.
2605 */
2606typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002607
2608/**
2609 * @} end defgroup alert_apis
2610 */
2611
2612/**
2613 * @cond INTERNAL_HIDDEN
2614 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002615
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002616#define K_ALERT_DEFAULT NULL
2617#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002618
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002619struct k_alert {
2620 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002621 atomic_t send_count;
2622 struct k_work work_item;
2623 struct k_sem sem;
2624
Anas Nashif2f203c22016-12-18 06:57:45 -05002625 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002626};
2627
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002628extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002629
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002630#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002631 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002632 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002633 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002634 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2635 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002636 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002637 }
2638
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002639#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2640
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002641/**
Allan Stephensc98da842016-11-11 15:45:03 -05002642 * INTERNAL_HIDDEN @endcond
2643 */
2644
2645/**
2646 * @addtogroup alert_apis
2647 * @{
2648 */
2649
2650/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002651 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002652 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002653 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002654 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002655 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002656 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002657 * @param name Name of the alert.
2658 * @param alert_handler Action to take when alert is sent. Specify either
2659 * the address of a function to be invoked by the system workqueue
2660 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2661 * K_ALERT_DEFAULT (which causes the alert to pend).
2662 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002663 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002664#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002665 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002666 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002667 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002668 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002669
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002670/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002671 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002672 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002673 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002674 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002675 * @param alert Address of the alert.
2676 * @param handler Action to take when alert is sent. Specify either the address
2677 * of a function to be invoked by the system workqueue thread,
2678 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2679 * K_ALERT_DEFAULT (which causes the alert to pend).
2680 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002681 *
2682 * @return N/A
2683 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002684extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2685 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002686
2687/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002688 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002689 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002690 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002691 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002692 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2693 *
2694 * @param alert Address of the alert.
2695 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002696 * or one of the special values K_NO_WAIT and K_FOREVER.
2697 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002698 * @retval 0 Alert received.
2699 * @retval -EBUSY Returned without waiting.
2700 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002701 */
Kumar Galacc334c72017-04-21 10:55:34 -05002702extern int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002703
2704/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002705 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002706 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002707 * This routine signals @a alert. The action specified for @a alert will
2708 * be taken, which may trigger the execution of an alert handler function
2709 * and/or cause the alert to pend (assuming the alert has not reached its
2710 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002711 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002712 * @note Can be called by ISRs.
2713 *
2714 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002715 *
2716 * @return N/A
2717 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002718extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002719
2720/**
Allan Stephensc98da842016-11-11 15:45:03 -05002721 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002722 */
2723
Allan Stephensc98da842016-11-11 15:45:03 -05002724/**
2725 * @cond INTERNAL_HIDDEN
2726 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002727
2728struct k_msgq {
2729 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002730 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002731 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002732 char *buffer_start;
2733 char *buffer_end;
2734 char *read_ptr;
2735 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002736 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002737
Anas Nashif2f203c22016-12-18 06:57:45 -05002738 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002739};
2740
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002741#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002742 { \
2743 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002744 .max_msgs = q_max_msgs, \
2745 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002746 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002747 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002748 .read_ptr = q_buffer, \
2749 .write_ptr = q_buffer, \
2750 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002751 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002752 }
2753
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002754#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2755
Peter Mitsis1da807e2016-10-06 11:36:59 -04002756/**
Allan Stephensc98da842016-11-11 15:45:03 -05002757 * INTERNAL_HIDDEN @endcond
2758 */
2759
2760/**
2761 * @defgroup msgq_apis Message Queue APIs
2762 * @ingroup kernel_apis
2763 * @{
2764 */
2765
2766/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002767 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002768 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002769 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2770 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002771 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2772 * message is similarly aligned to this boundary, @a q_msg_size must also be
2773 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002774 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002775 * The message queue can be accessed outside the module where it is defined
2776 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002777 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002778 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002779 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002780 * @param q_name Name of the message queue.
2781 * @param q_msg_size Message size (in bytes).
2782 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002783 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002784 */
2785#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2786 static char __noinit __aligned(q_align) \
2787 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002788 struct k_msgq q_name \
2789 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002790 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002791 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002792
Peter Mitsisd7a37502016-10-13 11:37:40 -04002793/**
2794 * @brief Initialize a message queue.
2795 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002796 * This routine initializes a message queue object, prior to its first use.
2797 *
Allan Stephensda827222016-11-09 14:23:58 -06002798 * The message queue's ring buffer must contain space for @a max_msgs messages,
2799 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2800 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2801 * that each message is similarly aligned to this boundary, @a q_msg_size
2802 * must also be a multiple of N.
2803 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002804 * @param q Address of the message queue.
2805 * @param buffer Pointer to ring buffer that holds queued messages.
2806 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002807 * @param max_msgs Maximum number of messages that can be queued.
2808 *
2809 * @return N/A
2810 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002811extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002812 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002813
2814/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002815 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002817 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002818 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002819 * @note Can be called by ISRs.
2820 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002821 * @param q Address of the message queue.
2822 * @param data Pointer to the message.
2823 * @param timeout Waiting period to add the message (in milliseconds),
2824 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002825 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002826 * @retval 0 Message sent.
2827 * @retval -ENOMSG Returned without waiting or queue purged.
2828 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002829 */
Kumar Galacc334c72017-04-21 10:55:34 -05002830extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002831
2832/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002833 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002834 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002835 * This routine receives a message from message queue @a q in a "first in,
2836 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002837 *
Allan Stephensc98da842016-11-11 15:45:03 -05002838 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002839 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002840 * @param q Address of the message queue.
2841 * @param data Address of area to hold the received message.
2842 * @param timeout Waiting period to receive the message (in milliseconds),
2843 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002844 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002845 * @retval 0 Message received.
2846 * @retval -ENOMSG Returned without waiting.
2847 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002848 */
Kumar Galacc334c72017-04-21 10:55:34 -05002849extern int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002850
2851/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002852 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002853 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002854 * This routine discards all unreceived messages in a message queue's ring
2855 * buffer. Any threads that are blocked waiting to send a message to the
2856 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002857 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002858 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002859 *
2860 * @return N/A
2861 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002862extern void k_msgq_purge(struct k_msgq *q);
2863
Peter Mitsis67be2492016-10-07 11:44:34 -04002864/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002865 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002867 * This routine returns the number of unused entries in a message queue's
2868 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002869 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002870 * @param q Address of the message queue.
2871 *
2872 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002873 */
Kumar Galacc334c72017-04-21 10:55:34 -05002874static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002875{
2876 return q->max_msgs - q->used_msgs;
2877}
2878
Peter Mitsisd7a37502016-10-13 11:37:40 -04002879/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002880 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002882 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002884 * @param q Address of the message queue.
2885 *
2886 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002887 */
Kumar Galacc334c72017-04-21 10:55:34 -05002888static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002889{
2890 return q->used_msgs;
2891}
2892
Allan Stephensc98da842016-11-11 15:45:03 -05002893/**
2894 * @} end defgroup msgq_apis
2895 */
2896
2897/**
2898 * @defgroup mem_pool_apis Memory Pool APIs
2899 * @ingroup kernel_apis
2900 * @{
2901 */
2902
Andy Ross73cb9582017-05-09 10:42:39 -07002903/* Note on sizing: the use of a 20 bit field for block means that,
2904 * assuming a reasonable minimum block size of 16 bytes, we're limited
2905 * to 16M of memory managed by a single pool. Long term it would be
2906 * good to move to a variable bit size based on configuration.
2907 */
2908struct k_mem_block_id {
2909 u32_t pool : 8;
2910 u32_t level : 4;
2911 u32_t block : 20;
2912};
2913
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002914struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002915 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002916 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002917};
2918
Allan Stephensc98da842016-11-11 15:45:03 -05002919/**
2920 * @} end defgroup mem_pool_apis
2921 */
2922
2923/**
2924 * @defgroup mailbox_apis Mailbox APIs
2925 * @ingroup kernel_apis
2926 * @{
2927 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002928
2929struct k_mbox_msg {
2930 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002931 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002932 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002933 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002934 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002935 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002936 /** sender's message data buffer */
2937 void *tx_data;
2938 /** internal use only - needed for legacy API support */
2939 void *_rx_data;
2940 /** message data block descriptor */
2941 struct k_mem_block tx_block;
2942 /** source thread id */
2943 k_tid_t rx_source_thread;
2944 /** target thread id */
2945 k_tid_t tx_target_thread;
2946 /** internal use only - thread waiting on send (may be a dummy) */
2947 k_tid_t _syncing_thread;
2948#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2949 /** internal use only - semaphore used during asynchronous send */
2950 struct k_sem *_async_sem;
2951#endif
2952};
2953
Allan Stephensc98da842016-11-11 15:45:03 -05002954/**
2955 * @cond INTERNAL_HIDDEN
2956 */
2957
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002958struct k_mbox {
2959 _wait_q_t tx_msg_queue;
2960 _wait_q_t rx_msg_queue;
2961
Anas Nashif2f203c22016-12-18 06:57:45 -05002962 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002963};
2964
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002965#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002966 { \
2967 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2968 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002969 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002970 }
2971
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002972#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
2973
Peter Mitsis12092702016-10-14 12:57:23 -04002974/**
Allan Stephensc98da842016-11-11 15:45:03 -05002975 * INTERNAL_HIDDEN @endcond
2976 */
2977
2978/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002979 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002980 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002981 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002982 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002983 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002984 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002985 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002986 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002987#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002988 struct k_mbox name \
2989 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002990 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002991
Peter Mitsis12092702016-10-14 12:57:23 -04002992/**
2993 * @brief Initialize a mailbox.
2994 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002995 * This routine initializes a mailbox object, prior to its first use.
2996 *
2997 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002998 *
2999 * @return N/A
3000 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003001extern void k_mbox_init(struct k_mbox *mbox);
3002
Peter Mitsis12092702016-10-14 12:57:23 -04003003/**
3004 * @brief Send a mailbox message in a synchronous manner.
3005 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003006 * This routine sends a message to @a mbox and waits for a receiver to both
3007 * receive and process it. The message data may be in a buffer, in a memory
3008 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003009 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003010 * @param mbox Address of the mailbox.
3011 * @param tx_msg Address of the transmit message descriptor.
3012 * @param timeout Waiting period for the message to be received (in
3013 * milliseconds), or one of the special values K_NO_WAIT
3014 * and K_FOREVER. Once the message has been received,
3015 * this routine waits as long as necessary for the message
3016 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003017 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003018 * @retval 0 Message sent.
3019 * @retval -ENOMSG Returned without waiting.
3020 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003021 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003022extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003023 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003024
Peter Mitsis12092702016-10-14 12:57:23 -04003025/**
3026 * @brief Send a mailbox message in an asynchronous manner.
3027 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003028 * This routine sends a message to @a mbox without waiting for a receiver
3029 * to process it. The message data may be in a buffer, in a memory pool block,
3030 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3031 * will be given when the message has been both received and completely
3032 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003033 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003034 * @param mbox Address of the mailbox.
3035 * @param tx_msg Address of the transmit message descriptor.
3036 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003037 *
3038 * @return N/A
3039 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003040extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003041 struct k_sem *sem);
3042
Peter Mitsis12092702016-10-14 12:57:23 -04003043/**
3044 * @brief Receive a mailbox message.
3045 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003046 * This routine receives a message from @a mbox, then optionally retrieves
3047 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003048 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003049 * @param mbox Address of the mailbox.
3050 * @param rx_msg Address of the receive message descriptor.
3051 * @param buffer Address of the buffer to receive data, or NULL to defer data
3052 * retrieval and message disposal until later.
3053 * @param timeout Waiting period for a message to be received (in
3054 * milliseconds), or one of the special values K_NO_WAIT
3055 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003056 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003057 * @retval 0 Message received.
3058 * @retval -ENOMSG Returned without waiting.
3059 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003060 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003061extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003062 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003063
3064/**
3065 * @brief Retrieve mailbox message data into a buffer.
3066 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003067 * This routine completes the processing of a received message by retrieving
3068 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003069 *
3070 * Alternatively, this routine can be used to dispose of a received message
3071 * without retrieving its data.
3072 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003073 * @param rx_msg Address of the receive message descriptor.
3074 * @param buffer Address of the buffer to receive data, or NULL to discard
3075 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003076 *
3077 * @return N/A
3078 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003079extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003080
3081/**
3082 * @brief Retrieve mailbox message data into a memory pool block.
3083 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003084 * This routine completes the processing of a received message by retrieving
3085 * its data into a memory pool block, then disposing of the message.
3086 * The memory pool block that results from successful retrieval must be
3087 * returned to the pool once the data has been processed, even in cases
3088 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003089 *
3090 * Alternatively, this routine can be used to dispose of a received message
3091 * without retrieving its data. In this case there is no need to return a
3092 * memory pool block to the pool.
3093 *
3094 * This routine allocates a new memory pool block for the data only if the
3095 * data is not already in one. If a new block cannot be allocated, the routine
3096 * returns a failure code and the received message is left unchanged. This
3097 * permits the caller to reattempt data retrieval at a later time or to dispose
3098 * of the received message without retrieving its data.
3099 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003100 * @param rx_msg Address of a receive message descriptor.
3101 * @param pool Address of memory pool, or NULL to discard data.
3102 * @param block Address of the area to hold memory pool block info.
3103 * @param timeout Waiting period to wait for a memory pool block (in
3104 * milliseconds), or one of the special values K_NO_WAIT
3105 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003106 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003107 * @retval 0 Data retrieved.
3108 * @retval -ENOMEM Returned without waiting.
3109 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003110 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003111extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003112 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003113 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003114
Allan Stephensc98da842016-11-11 15:45:03 -05003115/**
3116 * @} end defgroup mailbox_apis
3117 */
3118
3119/**
3120 * @cond INTERNAL_HIDDEN
3121 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003122
3123struct k_pipe {
3124 unsigned char *buffer; /* Pipe buffer: may be NULL */
3125 size_t size; /* Buffer size */
3126 size_t bytes_used; /* # bytes used in buffer */
3127 size_t read_index; /* Where in buffer to read from */
3128 size_t write_index; /* Where in buffer to write */
3129
3130 struct {
3131 _wait_q_t readers; /* Reader wait queue */
3132 _wait_q_t writers; /* Writer wait queue */
3133 } wait_q;
3134
Anas Nashif2f203c22016-12-18 06:57:45 -05003135 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003136};
3137
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003138#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003139 { \
3140 .buffer = pipe_buffer, \
3141 .size = pipe_buffer_size, \
3142 .bytes_used = 0, \
3143 .read_index = 0, \
3144 .write_index = 0, \
3145 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3146 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003147 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003148 }
3149
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003150#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3151
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003152/**
Allan Stephensc98da842016-11-11 15:45:03 -05003153 * INTERNAL_HIDDEN @endcond
3154 */
3155
3156/**
3157 * @defgroup pipe_apis Pipe APIs
3158 * @ingroup kernel_apis
3159 * @{
3160 */
3161
3162/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003163 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003164 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003165 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003166 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003167 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003168 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003169 * @param name Name of the pipe.
3170 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3171 * or zero if no ring buffer is used.
3172 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003173 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003174#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3175 static unsigned char __noinit __aligned(pipe_align) \
3176 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003177 struct k_pipe name \
3178 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003179 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003180
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003181/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003182 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003183 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003184 * This routine initializes a pipe object, prior to its first use.
3185 *
3186 * @param pipe Address of the pipe.
3187 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3188 * is used.
3189 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3190 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003191 *
3192 * @return N/A
3193 */
3194extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3195 size_t size);
3196
3197/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003198 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003199 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003200 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003201 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003202 * @param pipe Address of the pipe.
3203 * @param data Address of data to write.
3204 * @param bytes_to_write Size of data (in bytes).
3205 * @param bytes_written Address of area to hold the number of bytes written.
3206 * @param min_xfer Minimum number of bytes to write.
3207 * @param timeout Waiting period to wait for the data to be written (in
3208 * milliseconds), or one of the special values K_NO_WAIT
3209 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003210 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003211 * @retval 0 At least @a min_xfer bytes of data were written.
3212 * @retval -EIO Returned without waiting; zero data bytes were written.
3213 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003214 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003215 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003216extern int k_pipe_put(struct k_pipe *pipe, void *data,
3217 size_t bytes_to_write, size_t *bytes_written,
Kumar Galacc334c72017-04-21 10:55:34 -05003218 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003219
3220/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003221 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003222 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003223 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003224 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003225 * @param pipe Address of the pipe.
3226 * @param data Address to place the data read from pipe.
3227 * @param bytes_to_read Maximum number of data bytes to read.
3228 * @param bytes_read Address of area to hold the number of bytes read.
3229 * @param min_xfer Minimum number of data bytes to read.
3230 * @param timeout Waiting period to wait for the data to be read (in
3231 * milliseconds), or one of the special values K_NO_WAIT
3232 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003233 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003234 * @retval 0 At least @a min_xfer bytes of data were read.
3235 * @retval -EIO Returned without waiting; zero data bytes were read.
3236 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003237 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003238 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003239extern int k_pipe_get(struct k_pipe *pipe, void *data,
3240 size_t bytes_to_read, size_t *bytes_read,
Kumar Galacc334c72017-04-21 10:55:34 -05003241 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003242
3243/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003244 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003245 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003246 * This routine writes the data contained in a memory block to @a pipe.
3247 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003248 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003249 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003250 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003251 * @param block Memory block containing data to send
3252 * @param size Number of data bytes in memory block to send
3253 * @param sem Semaphore to signal upon completion (else NULL)
3254 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003255 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003256 */
3257extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3258 size_t size, struct k_sem *sem);
3259
3260/**
Allan Stephensc98da842016-11-11 15:45:03 -05003261 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003262 */
3263
Allan Stephensc98da842016-11-11 15:45:03 -05003264/**
3265 * @cond INTERNAL_HIDDEN
3266 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003267
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003268struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003269 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003270 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003271 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003272 char *buffer;
3273 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003274 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003275
Anas Nashif2f203c22016-12-18 06:57:45 -05003276 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003277};
3278
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003279#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003280 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003281 { \
3282 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003283 .num_blocks = slab_num_blocks, \
3284 .block_size = slab_block_size, \
3285 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003286 .free_list = NULL, \
3287 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003288 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003289 }
3290
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003291#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3292
3293
Peter Mitsis578f9112016-10-07 13:50:31 -04003294/**
Allan Stephensc98da842016-11-11 15:45:03 -05003295 * INTERNAL_HIDDEN @endcond
3296 */
3297
3298/**
3299 * @defgroup mem_slab_apis Memory Slab APIs
3300 * @ingroup kernel_apis
3301 * @{
3302 */
3303
3304/**
Allan Stephensda827222016-11-09 14:23:58 -06003305 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003306 *
Allan Stephensda827222016-11-09 14:23:58 -06003307 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003308 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003309 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3310 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003311 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003312 *
Allan Stephensda827222016-11-09 14:23:58 -06003313 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003314 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003315 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003316 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003317 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003318 * @param name Name of the memory slab.
3319 * @param slab_block_size Size of each memory block (in bytes).
3320 * @param slab_num_blocks Number memory blocks.
3321 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003322 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003323#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3324 char __noinit __aligned(slab_align) \
3325 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3326 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003327 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003328 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003329 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003330
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003331/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003332 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003333 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003334 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003335 *
Allan Stephensda827222016-11-09 14:23:58 -06003336 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3337 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3338 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3339 * To ensure that each memory block is similarly aligned to this boundary,
3340 * @a slab_block_size must also be a multiple of N.
3341 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003342 * @param slab Address of the memory slab.
3343 * @param buffer Pointer to buffer used for the memory blocks.
3344 * @param block_size Size of each memory block (in bytes).
3345 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003346 *
3347 * @return N/A
3348 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003349extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003350 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003351
3352/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003353 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003355 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003356 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003357 * @param slab Address of the memory slab.
3358 * @param mem Pointer to block address area.
3359 * @param timeout Maximum time to wait for operation to complete
3360 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3361 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003362 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003363 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003364 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003365 * @retval -ENOMEM Returned without waiting.
3366 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003367 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003368extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003369 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003370
3371/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003372 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003373 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003374 * This routine releases a previously allocated memory block back to its
3375 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003376 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003377 * @param slab Address of the memory slab.
3378 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003379 *
3380 * @return N/A
3381 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003382extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003383
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003384/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003385 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003386 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003387 * This routine gets the number of memory blocks that are currently
3388 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003389 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003390 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003391 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003392 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003393 */
Kumar Galacc334c72017-04-21 10:55:34 -05003394static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003395{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003396 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003397}
3398
Peter Mitsisc001aa82016-10-13 13:53:37 -04003399/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003400 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003401 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003402 * This routine gets the number of memory blocks that are currently
3403 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003404 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003405 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003406 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003407 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003408 */
Kumar Galacc334c72017-04-21 10:55:34 -05003409static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003410{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003411 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003412}
3413
Allan Stephensc98da842016-11-11 15:45:03 -05003414/**
3415 * @} end defgroup mem_slab_apis
3416 */
3417
3418/**
3419 * @cond INTERNAL_HIDDEN
3420 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003421
Andy Ross73cb9582017-05-09 10:42:39 -07003422struct k_mem_pool_lvl {
3423 union {
3424 u32_t *bits_p;
3425 u32_t bits;
3426 };
3427 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003428};
3429
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003430struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003431 void *buf;
3432 size_t max_sz;
3433 u16_t n_max;
3434 u8_t n_levels;
3435 u8_t max_inline_level;
3436 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003437 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003438};
3439
Andy Ross73cb9582017-05-09 10:42:39 -07003440#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003441
Andy Ross73cb9582017-05-09 10:42:39 -07003442#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3443
3444#define _MPOOL_LVLS(maxsz, minsz) \
3445 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3446 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3447 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3448 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3449 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3450 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3451 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3452 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3453 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3454 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3455 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3456 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3457 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3458 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3459 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3460 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3461
3462/* Rounds the needed bits up to integer multiples of u32_t */
3463#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3464 ((((n_max) << (2*(l))) + 31) / 32)
3465
3466/* One word gets stored free unioned with the pointer, otherwise the
3467 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003468 */
Andy Ross73cb9582017-05-09 10:42:39 -07003469#define _MPOOL_LBIT_WORDS(n_max, l) \
3470 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3471 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003472
Andy Ross73cb9582017-05-09 10:42:39 -07003473/* How many bytes for the bitfields of a single level? */
3474#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3475 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3476 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003477
Andy Ross73cb9582017-05-09 10:42:39 -07003478/* Size of the bitmap array that follows the buffer in allocated memory */
3479#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3480 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3481 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3482 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3483 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3484 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3485 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3486 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3487 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3488 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3489 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3490 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3491 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3492 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3493 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3494 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3495 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003496
3497/**
Allan Stephensc98da842016-11-11 15:45:03 -05003498 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003499 */
3500
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003501/**
Allan Stephensc98da842016-11-11 15:45:03 -05003502 * @addtogroup mem_pool_apis
3503 * @{
3504 */
3505
3506/**
3507 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003508 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003509 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3510 * long. The memory pool allows blocks to be repeatedly partitioned into
3511 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003512 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003513 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003514 * If the pool is to be accessed outside the module where it is defined, it
3515 * can be declared via
3516 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003517 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003518 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003519 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003520 * @param minsz Size of the smallest blocks in the pool (in bytes).
3521 * @param maxsz Size of the largest blocks in the pool (in bytes).
3522 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003523 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003524 */
Andy Ross73cb9582017-05-09 10:42:39 -07003525#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3526 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3527 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3528 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3529 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3530 .buf = _mpool_buf_##name, \
3531 .max_sz = maxsz, \
3532 .n_max = nmax, \
3533 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3534 .levels = _mpool_lvls_##name, \
3535 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003536
Peter Mitsis937042c2016-10-13 13:18:26 -04003537/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003538 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003539 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003540 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003541 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003542 * @param pool Address of the memory pool.
3543 * @param block Pointer to block descriptor for the allocated memory.
3544 * @param size Amount of memory to allocate (in bytes).
3545 * @param timeout Maximum time to wait for operation to complete
3546 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3547 * or K_FOREVER to wait as long as necessary.
3548 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003549 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003550 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003551 * @retval -ENOMEM Returned without waiting.
3552 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003553 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003554extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003555 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003556
3557/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003558 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003559 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003560 * This routine releases a previously allocated memory block back to its
3561 * memory pool.
3562 *
3563 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003564 *
3565 * @return N/A
3566 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003567extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003568
3569/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003570 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003571 *
Andy Ross73cb9582017-05-09 10:42:39 -07003572 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003573 *
Andy Ross73cb9582017-05-09 10:42:39 -07003574 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003575 *
3576 * @return N/A
3577 */
Andy Ross73cb9582017-05-09 10:42:39 -07003578static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003579
3580/**
Allan Stephensc98da842016-11-11 15:45:03 -05003581 * @} end addtogroup mem_pool_apis
3582 */
3583
3584/**
3585 * @defgroup heap_apis Heap Memory Pool APIs
3586 * @ingroup kernel_apis
3587 * @{
3588 */
3589
3590/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003591 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003592 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003593 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003594 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003595 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003596 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003597 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003598 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003599 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003600extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003601
3602/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003603 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003604 *
3605 * This routine provides traditional free() semantics. The memory being
3606 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003607 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003608 * If @a ptr is NULL, no operation is performed.
3609 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003610 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003611 *
3612 * @return N/A
3613 */
3614extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003615
Allan Stephensc98da842016-11-11 15:45:03 -05003616/**
3617 * @} end defgroup heap_apis
3618 */
3619
Benjamin Walshacc68c12017-01-29 18:57:45 -05003620/* polling API - PRIVATE */
3621
Benjamin Walshb0179862017-02-02 16:39:57 -05003622#ifdef CONFIG_POLL
3623#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3624#else
3625#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3626#endif
3627
Benjamin Walshacc68c12017-01-29 18:57:45 -05003628/* private - implementation data created as needed, per-type */
3629struct _poller {
3630 struct k_thread *thread;
3631};
3632
3633/* private - types bit positions */
3634enum _poll_types_bits {
3635 /* can be used to ignore an event */
3636 _POLL_TYPE_IGNORE,
3637
3638 /* to be signaled by k_poll_signal() */
3639 _POLL_TYPE_SIGNAL,
3640
3641 /* semaphore availability */
3642 _POLL_TYPE_SEM_AVAILABLE,
3643
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003644 /* queue/fifo/lifo data availability */
3645 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003646
3647 _POLL_NUM_TYPES
3648};
3649
3650#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3651
3652/* private - states bit positions */
3653enum _poll_states_bits {
3654 /* default state when creating event */
3655 _POLL_STATE_NOT_READY,
3656
Benjamin Walshacc68c12017-01-29 18:57:45 -05003657 /* signaled by k_poll_signal() */
3658 _POLL_STATE_SIGNALED,
3659
3660 /* semaphore is available */
3661 _POLL_STATE_SEM_AVAILABLE,
3662
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003663 /* data is available to read on queue/fifo/lifo */
3664 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003665
3666 _POLL_NUM_STATES
3667};
3668
3669#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3670
3671#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003672 (32 - (0 \
3673 + 8 /* tag */ \
3674 + _POLL_NUM_TYPES \
3675 + _POLL_NUM_STATES \
3676 + 1 /* modes */ \
3677 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003678
3679#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3680#error overflow of 32-bit word in struct k_poll_event
3681#endif
3682
3683/* end of polling API - PRIVATE */
3684
3685
3686/**
3687 * @defgroup poll_apis Async polling APIs
3688 * @ingroup kernel_apis
3689 * @{
3690 */
3691
3692/* Public polling API */
3693
3694/* public - values for k_poll_event.type bitfield */
3695#define K_POLL_TYPE_IGNORE 0
3696#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3697#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003698#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3699#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003700
3701/* public - polling modes */
3702enum k_poll_modes {
3703 /* polling thread does not take ownership of objects when available */
3704 K_POLL_MODE_NOTIFY_ONLY = 0,
3705
3706 K_POLL_NUM_MODES
3707};
3708
3709/* public - values for k_poll_event.state bitfield */
3710#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003711#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3712#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003713#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3714#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003715
3716/* public - poll signal object */
3717struct k_poll_signal {
3718 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003719 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003720
3721 /*
3722 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3723 * user resets it to 0.
3724 */
3725 unsigned int signaled;
3726
3727 /* custom result value passed to k_poll_signal() if needed */
3728 int result;
3729};
3730
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003731#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003732 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003733 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003734 .signaled = 0, \
3735 .result = 0, \
3736 }
3737
3738struct k_poll_event {
3739 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003740 sys_dnode_t _node;
3741
3742 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003743 struct _poller *poller;
3744
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003745 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003746 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003747
Benjamin Walshacc68c12017-01-29 18:57:45 -05003748 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003749 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003750
3751 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003752 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003753
3754 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003755 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003756
3757 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003758 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003759
3760 /* per-type data */
3761 union {
3762 void *obj;
3763 struct k_poll_signal *signal;
3764 struct k_sem *sem;
3765 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003766 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003767 };
3768};
3769
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003770#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003771 { \
3772 .poller = NULL, \
3773 .type = event_type, \
3774 .state = K_POLL_STATE_NOT_READY, \
3775 .mode = event_mode, \
3776 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003777 { .obj = event_obj }, \
3778 }
3779
3780#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3781 event_tag) \
3782 { \
3783 .type = event_type, \
3784 .tag = event_tag, \
3785 .state = K_POLL_STATE_NOT_READY, \
3786 .mode = event_mode, \
3787 .unused = 0, \
3788 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003789 }
3790
3791/**
3792 * @brief Initialize one struct k_poll_event instance
3793 *
3794 * After this routine is called on a poll event, the event it ready to be
3795 * placed in an event array to be passed to k_poll().
3796 *
3797 * @param event The event to initialize.
3798 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3799 * values. Only values that apply to the same object being polled
3800 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3801 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003802 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003803 * @param obj Kernel object or poll signal.
3804 *
3805 * @return N/A
3806 */
3807
Kumar Galacc334c72017-04-21 10:55:34 -05003808extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003809 int mode, void *obj);
3810
3811/**
3812 * @brief Wait for one or many of multiple poll events to occur
3813 *
3814 * This routine allows a thread to wait concurrently for one or many of
3815 * multiple poll events to have occurred. Such events can be a kernel object
3816 * being available, like a semaphore, or a poll signal event.
3817 *
3818 * When an event notifies that a kernel object is available, the kernel object
3819 * is not "given" to the thread calling k_poll(): it merely signals the fact
3820 * that the object was available when the k_poll() call was in effect. Also,
3821 * all threads trying to acquire an object the regular way, i.e. by pending on
3822 * the object, have precedence over the thread polling on the object. This
3823 * means that the polling thread will never get the poll event on an object
3824 * until the object becomes available and its pend queue is empty. For this
3825 * reason, the k_poll() call is more effective when the objects being polled
3826 * only have one thread, the polling thread, trying to acquire them.
3827 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003828 * When k_poll() returns 0, the caller should loop on all the events that were
3829 * passed to k_poll() and check the state field for the values that were
3830 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003831 *
3832 * Before being reused for another call to k_poll(), the user has to reset the
3833 * state field to K_POLL_STATE_NOT_READY.
3834 *
3835 * @param events An array of pointers to events to be polled for.
3836 * @param num_events The number of events in the array.
3837 * @param timeout Waiting period for an event to be ready (in milliseconds),
3838 * or one of the special values K_NO_WAIT and K_FOREVER.
3839 *
3840 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003841 * @retval -EAGAIN Waiting period timed out.
3842 */
3843
3844extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003845 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003846
3847/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003848 * @brief Initialize a poll signal object.
3849 *
3850 * Ready a poll signal object to be signaled via k_poll_signal().
3851 *
3852 * @param signal A poll signal.
3853 *
3854 * @return N/A
3855 */
3856
3857extern void k_poll_signal_init(struct k_poll_signal *signal);
3858
3859/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003860 * @brief Signal a poll signal object.
3861 *
3862 * This routine makes ready a poll signal, which is basically a poll event of
3863 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3864 * made ready to run. A @a result value can be specified.
3865 *
3866 * The poll signal contains a 'signaled' field that, when set by
3867 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3868 * be reset by the user before being passed again to k_poll() or k_poll() will
3869 * consider it being signaled, and will return immediately.
3870 *
3871 * @param signal A poll signal.
3872 * @param result The value to store in the result field of the signal.
3873 *
3874 * @retval 0 The signal was delivered successfully.
3875 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3876 */
3877
3878extern int k_poll_signal(struct k_poll_signal *signal, int result);
3879
3880/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003881extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003882
3883/**
3884 * @} end defgroup poll_apis
3885 */
3886
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003887/**
3888 * @brief Make the CPU idle.
3889 *
3890 * This function makes the CPU idle until an event wakes it up.
3891 *
3892 * In a regular system, the idle thread should be the only thread responsible
3893 * for making the CPU idle and triggering any type of power management.
3894 * However, in some more constrained systems, such as a single-threaded system,
3895 * the only thread would be responsible for this if needed.
3896 *
3897 * @return N/A
3898 */
3899extern void k_cpu_idle(void);
3900
3901/**
3902 * @brief Make the CPU idle in an atomic fashion.
3903 *
3904 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3905 * must be done atomically before making the CPU idle.
3906 *
3907 * @param key Interrupt locking key obtained from irq_lock().
3908 *
3909 * @return N/A
3910 */
3911extern void k_cpu_atomic_idle(unsigned int key);
3912
Kumar Galacc334c72017-04-21 10:55:34 -05003913extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003914
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003915#include <arch/cpu.h>
3916
Andrew Boiecdb94d62017-04-18 15:22:05 -07003917#ifdef _ARCH_EXCEPT
3918/* This archtecture has direct support for triggering a CPU exception */
3919#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3920#else
3921
3922#include <misc/printk.h>
3923
3924/* NOTE: This is the implementation for arches that do not implement
3925 * _ARCH_EXCEPT() to generate a real CPU exception.
3926 *
3927 * We won't have a real exception frame to determine the PC value when
3928 * the oops occurred, so print file and line number before we jump into
3929 * the fatal error handler.
3930 */
3931#define _k_except_reason(reason) do { \
3932 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3933 _NanoFatalErrorHandler(reason, &_default_esf); \
3934 CODE_UNREACHABLE; \
3935 } while (0)
3936
3937#endif /* _ARCH__EXCEPT */
3938
3939/**
3940 * @brief Fatally terminate a thread
3941 *
3942 * This should be called when a thread has encountered an unrecoverable
3943 * runtime condition and needs to terminate. What this ultimately
3944 * means is determined by the _fatal_error_handler() implementation, which
3945 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3946 *
3947 * If this is called from ISR context, the default system fatal error handler
3948 * will treat it as an unrecoverable system error, just like k_panic().
3949 */
3950#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3951
3952/**
3953 * @brief Fatally terminate the system
3954 *
3955 * This should be called when the Zephyr kernel has encountered an
3956 * unrecoverable runtime condition and needs to terminate. What this ultimately
3957 * means is determined by the _fatal_error_handler() implementation, which
3958 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3959 */
3960#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3961
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003962/*
3963 * private APIs that are utilized by one or more public APIs
3964 */
3965
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003966#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003967extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003968#else
3969#define _init_static_threads() do { } while ((0))
3970#endif
3971
3972extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003973extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003974
Andrew Boiedc5d9352017-06-02 12:56:47 -07003975/* arch/cpu.h may declare an architecture or platform-specific macro
3976 * for properly declaring stacks, compatible with MMU/MPU constraints if
3977 * enabled
3978 */
3979#ifdef _ARCH_THREAD_STACK_DEFINE
3980#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
3981#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
3982 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
3983#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
3984#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie507852a2017-07-25 18:47:07 -07003985static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
3986{
3987 return _ARCH_THREAD_STACK_BUFFER(sym);
3988}
Andrew Boiedc5d9352017-06-02 12:56:47 -07003989#else
3990/**
3991 * @brief Declare a toplevel thread stack memory region
3992 *
3993 * This declares a region of memory suitable for use as a thread's stack.
3994 *
3995 * This is the generic, historical definition. Align to STACK_ALIGN and put in
3996 * 'noinit' section so that it isn't zeroed at boot
3997 *
Andrew Boie507852a2017-07-25 18:47:07 -07003998 * The declared symbol will always be a k_thread_stack_t which can be passed to
3999 * k_thread_create, but should otherwise not be manipulated. If the buffer
4000 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004001 *
4002 * It is legal to precede this definition with the 'static' keyword.
4003 *
4004 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4005 * parameter of k_thread_create(), it may not be the same as the
4006 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4007 *
4008 * @param sym Thread stack symbol name
4009 * @param size Size of the stack memory region
4010 */
4011#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004012 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004013
4014/**
4015 * @brief Declare a toplevel array of thread stack memory regions
4016 *
4017 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4018 * definition for additional details and constraints.
4019 *
4020 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4021 * 'noinit' section so that it isn't zeroed at boot
4022 *
4023 * @param sym Thread stack symbol name
4024 * @param nmemb Number of stacks to declare
4025 * @param size Size of the stack memory region
4026 */
4027
4028#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004029 struct _k_thread_stack_element __noinit \
4030 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004031
4032/**
4033 * @brief Declare an embedded stack memory region
4034 *
4035 * Used for stacks embedded within other data structures. Use is highly
4036 * discouraged but in some cases necessary. For memory protection scenarios,
4037 * it is very important that any RAM preceding this member not be writable
4038 * by threads else a stack overflow will lead to silent corruption. In other
4039 * words, the containing data structure should live in RAM owned by the kernel.
4040 *
4041 * @param sym Thread stack symbol name
4042 * @param size Size of the stack memory region
4043 */
4044#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004045 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004046
4047/**
4048 * @brief Return the size in bytes of a stack memory region
4049 *
4050 * Convenience macro for passing the desired stack size to k_thread_create()
4051 * since the underlying implementation may actually create something larger
4052 * (for instance a guard area).
4053 *
4054 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004055 * passed to K_THREAD_STACK_DEFINE.
4056 *
4057 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4058 * it is not guaranteed to return the original value since each array
4059 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004060 *
4061 * @param sym Stack memory symbol
4062 * @return Size of the stack
4063 */
4064#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4065
4066/**
4067 * @brief Get a pointer to the physical stack buffer
4068 *
4069 * Convenience macro to get at the real underlying stack buffer used by
4070 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4071 * This is really only intended for diagnostic tools which want to examine
4072 * stack memory contents.
4073 *
4074 * @param sym Declared stack symbol name
4075 * @return The buffer itself, a char *
4076 */
Andrew Boie507852a2017-07-25 18:47:07 -07004077static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4078{
4079 return (char *)sym;
4080}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004081
4082#endif /* _ARCH_DECLARE_STACK */
4083
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004084#ifdef __cplusplus
4085}
4086#endif
4087
Andrew Boiee004dec2016-11-07 09:01:19 -08004088#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4089/*
4090 * Define new and delete operators.
4091 * At this moment, the operators do nothing since objects are supposed
4092 * to be statically allocated.
4093 */
4094inline void operator delete(void *ptr)
4095{
4096 (void)ptr;
4097}
4098
4099inline void operator delete[](void *ptr)
4100{
4101 (void)ptr;
4102}
4103
4104inline void *operator new(size_t size)
4105{
4106 (void)size;
4107 return NULL;
4108}
4109
4110inline void *operator new[](size_t size)
4111{
4112 (void)size;
4113 return NULL;
4114}
4115
4116/* Placement versions of operator new and delete */
4117inline void operator delete(void *ptr1, void *ptr2)
4118{
4119 (void)ptr1;
4120 (void)ptr2;
4121}
4122
4123inline void operator delete[](void *ptr1, void *ptr2)
4124{
4125 (void)ptr1;
4126 (void)ptr2;
4127}
4128
4129inline void *operator new(size_t size, void *ptr)
4130{
4131 (void)size;
4132 return ptr;
4133}
4134
4135inline void *operator new[](size_t size, void *ptr)
4136{
4137 (void)size;
4138 return ptr;
4139}
4140
4141#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4142
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004143#endif /* !_ASMLANGUAGE */
4144
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004145#endif /* _kernel__h_ */