blob: 5483c84dd965e9d45969ce1af075035c3842381b [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.
499 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400500 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500501 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400502 */
Andrew Boie507852a2017-07-25 18:47:07 -0700503extern __deprecated k_tid_t k_thread_spawn(k_thread_stack_t stack,
504 size_t stack_size, k_thread_entry_t entry,
505 void *p1, void *p2, void *p3,
506 int prio, u32_t options, s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400507
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400508/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700509 * @brief Create a thread.
510 *
511 * This routine initializes a thread, then schedules it for execution.
512 *
513 * The new thread may be scheduled for immediate execution or a delayed start.
514 * If the newly spawned thread does not have a delayed start the kernel
515 * scheduler may preempt the current thread to allow the new thread to
516 * execute.
517 *
518 * Thread options are architecture-specific, and can include K_ESSENTIAL,
519 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
520 * them using "|" (the logical OR operator).
521 *
522 * Historically, users often would use the beginning of the stack memory region
523 * to store the struct k_thread data, although corruption will occur if the
524 * stack overflows this region and stack protection features may not detect this
525 * situation.
526 *
527 * @param new_thread Pointer to uninitialized struct k_thread
528 * @param stack Pointer to the stack space.
529 * @param stack_size Stack size in bytes.
530 * @param entry Thread entry function.
531 * @param p1 1st entry point parameter.
532 * @param p2 2nd entry point parameter.
533 * @param p3 3rd entry point parameter.
534 * @param prio Thread priority.
535 * @param options Thread options.
536 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
537 *
538 * @return ID of new thread.
539 */
Andrew Boie507852a2017-07-25 18:47:07 -0700540extern k_tid_t k_thread_create(struct k_thread *new_thread,
541 k_thread_stack_t stack,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700542 size_t stack_size,
543 void (*entry)(void *, void *, void*),
544 void *p1, void *p2, void *p3,
545 int prio, u32_t options, s32_t delay);
546
547/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500548 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400549 *
Allan Stephensc98da842016-11-11 15:45:03 -0500550 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500551 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400552 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500553 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400554 *
555 * @return N/A
556 */
Kumar Galacc334c72017-04-21 10:55:34 -0500557extern void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400558
559/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500560 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400561 *
562 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500563 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400564 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400565 * @return N/A
566 */
Kumar Galacc334c72017-04-21 10:55:34 -0500567extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400568
569/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500570 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400571 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500572 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400573 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500574 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400575 *
576 * @return N/A
577 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400578extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400579
580/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500581 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400582 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500583 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400584 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500585 * If @a thread is not currently sleeping, the routine has no effect.
586 *
587 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400588 *
589 * @return N/A
590 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400591extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400592
593/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500594 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400595 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500596 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400597 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400598extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400599
600/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500601 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400602 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500603 * This routine prevents @a thread from executing if it has not yet started
604 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400605 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500606 * @param thread ID of thread to cancel.
607 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700608 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500609 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400610 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400611extern int k_thread_cancel(k_tid_t thread);
612
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400613/**
Allan Stephensc98da842016-11-11 15:45:03 -0500614 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400615 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500616 * This routine permanently stops execution of @a thread. The thread is taken
617 * off all kernel queues it is part of (i.e. the ready queue, the timeout
618 * queue, or a kernel object wait queue). However, any kernel resources the
619 * thread might currently own (such as mutexes or memory blocks) are not
620 * released. It is the responsibility of the caller of this routine to ensure
621 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400622 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500623 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400624 *
625 * @return N/A
626 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400627extern void k_thread_abort(k_tid_t thread);
628
Allan Stephensc98da842016-11-11 15:45:03 -0500629/**
630 * @cond INTERNAL_HIDDEN
631 */
632
Benjamin Walshd211a522016-12-06 11:44:01 -0500633/* timeout has timed out and is not on _timeout_q anymore */
634#define _EXPIRED (-2)
635
636/* timeout is not in use */
637#define _INACTIVE (-1)
638
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400639struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700640 struct k_thread *init_thread;
Andrew Boie507852a2017-07-25 18:47:07 -0700641 k_thread_stack_t init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400642 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500643 void (*init_entry)(void *, void *, void *);
644 void *init_p1;
645 void *init_p2;
646 void *init_p3;
647 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500648 u32_t init_options;
649 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500650 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500651 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400652};
653
Andrew Boied26cf2d2017-03-30 13:07:02 -0700654#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400655 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500656 prio, options, delay, abort, groups) \
657 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700658 .init_thread = (thread), \
659 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500660 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400661 .init_entry = (void (*)(void *, void *, void *))entry, \
662 .init_p1 = (void *)p1, \
663 .init_p2 = (void *)p2, \
664 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500665 .init_prio = (prio), \
666 .init_options = (options), \
667 .init_delay = (delay), \
668 .init_abort = (abort), \
669 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400670 }
671
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400672/**
Allan Stephensc98da842016-11-11 15:45:03 -0500673 * INTERNAL_HIDDEN @endcond
674 */
675
676/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500677 * @brief Statically define and initialize a thread.
678 *
679 * The thread may be scheduled for immediate execution or a delayed start.
680 *
681 * Thread options are architecture-specific, and can include K_ESSENTIAL,
682 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
683 * them using "|" (the logical OR operator).
684 *
685 * The ID of the thread can be accessed using:
686 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500687 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500688 *
689 * @param name Name of the thread.
690 * @param stack_size Stack size in bytes.
691 * @param entry Thread entry function.
692 * @param p1 1st entry point parameter.
693 * @param p2 2nd entry point parameter.
694 * @param p3 3rd entry point parameter.
695 * @param prio Thread priority.
696 * @param options Thread options.
697 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400698 *
699 * @internal It has been observed that the x86 compiler by default aligns
700 * these _static_thread_data structures to 32-byte boundaries, thereby
701 * wasting space. To work around this, force a 4-byte alignment.
702 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500703#define K_THREAD_DEFINE(name, stack_size, \
704 entry, p1, p2, p3, \
705 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700706 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700707 struct k_thread _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500708 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500709 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700710 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
711 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500712 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700713 NULL, 0); \
714 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400715
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400716/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500717 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400718 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500719 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400720 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500721 * @param thread ID of thread whose priority is needed.
722 *
723 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400724 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500725extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400726
727/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500728 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400729 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500730 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400731 *
732 * Rescheduling can occur immediately depending on the priority @a thread is
733 * set to:
734 *
735 * - If its priority is raised above the priority of the caller of this
736 * function, and the caller is preemptible, @a thread will be scheduled in.
737 *
738 * - If the caller operates on itself, it lowers its priority below that of
739 * other threads in the system, and the caller is preemptible, the thread of
740 * highest priority will be scheduled in.
741 *
742 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
743 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
744 * highest priority.
745 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500746 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400747 * @param prio New priority.
748 *
749 * @warning Changing the priority of a thread currently involved in mutex
750 * priority inheritance may result in undefined behavior.
751 *
752 * @return N/A
753 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400754extern void k_thread_priority_set(k_tid_t thread, int prio);
755
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400756/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500757 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400758 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500759 * This routine prevents the kernel scheduler from making @a thread the
760 * current thread. All other internal operations on @a thread are still
761 * performed; for example, any timeout it is waiting on keeps ticking,
762 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400763 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500764 * If @a thread is already suspended, the routine has no effect.
765 *
766 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400767 *
768 * @return N/A
769 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400770extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400771
772/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500773 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400774 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500775 * This routine allows the kernel scheduler to make @a thread the current
776 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400777 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500778 * If @a thread is not currently suspended, the routine has no effect.
779 *
780 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400781 *
782 * @return N/A
783 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400784extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400785
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400786/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500787 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400788 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500789 * This routine specifies how the scheduler will perform time slicing of
790 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400791 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500792 * To enable time slicing, @a slice must be non-zero. The scheduler
793 * ensures that no thread runs for more than the specified time limit
794 * before other threads of that priority are given a chance to execute.
795 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700796 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400797 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500798 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400799 * execute. Once the scheduler selects a thread for execution, there is no
800 * minimum guaranteed time the thread will execute before threads of greater or
801 * equal priority are scheduled.
802 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500803 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400804 * for execution, this routine has no effect; the thread is immediately
805 * rescheduled after the slice period expires.
806 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500807 * To disable timeslicing, set both @a slice and @a prio to zero.
808 *
809 * @param slice Maximum time slice length (in milliseconds).
810 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400811 *
812 * @return N/A
813 */
Kumar Galacc334c72017-04-21 10:55:34 -0500814extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400815
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400816/**
Allan Stephensc98da842016-11-11 15:45:03 -0500817 * @} end defgroup thread_apis
818 */
819
820/**
821 * @addtogroup isr_apis
822 * @{
823 */
824
825/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500826 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400827 *
Allan Stephensc98da842016-11-11 15:45:03 -0500828 * This routine allows the caller to customize its actions, depending on
829 * whether it is a thread or an ISR.
830 *
831 * @note Can be called by ISRs.
832 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500833 * @return 0 if invoked by a thread.
834 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400835 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500836extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400837
Benjamin Walsh445830d2016-11-10 15:54:27 -0500838/**
839 * @brief Determine if code is running in a preemptible thread.
840 *
Allan Stephensc98da842016-11-11 15:45:03 -0500841 * This routine allows the caller to customize its actions, depending on
842 * whether it can be preempted by another thread. The routine returns a 'true'
843 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500844 *
Allan Stephensc98da842016-11-11 15:45:03 -0500845 * - The code is running in a thread, not at ISR.
846 * - The thread's priority is in the preemptible range.
847 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500848 *
Allan Stephensc98da842016-11-11 15:45:03 -0500849 * @note Can be called by ISRs.
850 *
851 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500852 * @return Non-zero if invoked by a preemptible thread.
853 */
854extern int k_is_preempt_thread(void);
855
Allan Stephensc98da842016-11-11 15:45:03 -0500856/**
857 * @} end addtogroup isr_apis
858 */
859
860/**
861 * @addtogroup thread_apis
862 * @{
863 */
864
865/**
866 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500867 *
Allan Stephensc98da842016-11-11 15:45:03 -0500868 * This routine prevents the current thread from being preempted by another
869 * thread by instructing the scheduler to treat it as a cooperative thread.
870 * If the thread subsequently performs an operation that makes it unready,
871 * it will be context switched out in the normal manner. When the thread
872 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500873 *
Allan Stephensc98da842016-11-11 15:45:03 -0500874 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500875 *
Allan Stephensc98da842016-11-11 15:45:03 -0500876 * @note k_sched_lock() and k_sched_unlock() should normally be used
877 * when the operation being performed can be safely interrupted by ISRs.
878 * However, if the amount of processing involved is very small, better
879 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500880 *
881 * @return N/A
882 */
883extern void k_sched_lock(void);
884
Allan Stephensc98da842016-11-11 15:45:03 -0500885/**
886 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500887 *
Allan Stephensc98da842016-11-11 15:45:03 -0500888 * This routine reverses the effect of a previous call to k_sched_lock().
889 * A thread must call the routine once for each time it called k_sched_lock()
890 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500891 *
892 * @return N/A
893 */
894extern void k_sched_unlock(void);
895
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400896/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500897 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400898 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500899 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400900 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500901 * Custom data is not used by the kernel itself, and is freely available
902 * for a thread to use as it sees fit. It can be used as a framework
903 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400904 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500905 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400906 *
907 * @return N/A
908 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400909extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400910
911/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500912 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400913 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500914 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400915 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500916 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400917 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400918extern void *k_thread_custom_data_get(void);
919
920/**
Allan Stephensc98da842016-11-11 15:45:03 -0500921 * @} end addtogroup thread_apis
922 */
923
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400924#include <sys_clock.h>
925
Allan Stephensc2f15a42016-11-17 12:24:22 -0500926/**
927 * @addtogroup clock_apis
928 * @{
929 */
930
931/**
932 * @brief Generate null timeout delay.
933 *
934 * This macro generates a timeout delay that that instructs a kernel API
935 * not to wait if the requested operation cannot be performed immediately.
936 *
937 * @return Timeout delay value.
938 */
939#define K_NO_WAIT 0
940
941/**
942 * @brief Generate timeout delay from milliseconds.
943 *
944 * This macro generates a timeout delay that that instructs a kernel API
945 * to wait up to @a ms milliseconds to perform the requested operation.
946 *
947 * @param ms Duration in milliseconds.
948 *
949 * @return Timeout delay value.
950 */
Johan Hedberg14471692016-11-13 10:52:15 +0200951#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500952
953/**
954 * @brief Generate timeout delay from seconds.
955 *
956 * This macro generates a timeout delay that that instructs a kernel API
957 * to wait up to @a s seconds to perform the requested operation.
958 *
959 * @param s Duration in seconds.
960 *
961 * @return Timeout delay value.
962 */
Johan Hedberg14471692016-11-13 10:52:15 +0200963#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500964
965/**
966 * @brief Generate timeout delay from minutes.
967 *
968 * This macro generates a timeout delay that that instructs a kernel API
969 * to wait up to @a m minutes to perform the requested operation.
970 *
971 * @param m Duration in minutes.
972 *
973 * @return Timeout delay value.
974 */
Johan Hedberg14471692016-11-13 10:52:15 +0200975#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500976
977/**
978 * @brief Generate timeout delay from hours.
979 *
980 * This macro generates a timeout delay that that instructs a kernel API
981 * to wait up to @a h hours to perform the requested operation.
982 *
983 * @param h Duration in hours.
984 *
985 * @return Timeout delay value.
986 */
Johan Hedberg14471692016-11-13 10:52:15 +0200987#define K_HOURS(h) K_MINUTES((h) * 60)
988
Allan Stephensc98da842016-11-11 15:45:03 -0500989/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500990 * @brief Generate infinite timeout delay.
991 *
992 * This macro generates a timeout delay that that instructs a kernel API
993 * to wait as long as necessary to perform the requested operation.
994 *
995 * @return Timeout delay value.
996 */
997#define K_FOREVER (-1)
998
999/**
1000 * @} end addtogroup clock_apis
1001 */
1002
1003/**
Allan Stephensc98da842016-11-11 15:45:03 -05001004 * @cond INTERNAL_HIDDEN
1005 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001006
Benjamin Walsh62092182016-12-20 14:39:08 -05001007/* kernel clocks */
1008
1009#if (sys_clock_ticks_per_sec == 1000) || \
1010 (sys_clock_ticks_per_sec == 500) || \
1011 (sys_clock_ticks_per_sec == 250) || \
1012 (sys_clock_ticks_per_sec == 125) || \
1013 (sys_clock_ticks_per_sec == 100) || \
1014 (sys_clock_ticks_per_sec == 50) || \
1015 (sys_clock_ticks_per_sec == 25) || \
1016 (sys_clock_ticks_per_sec == 20) || \
1017 (sys_clock_ticks_per_sec == 10) || \
1018 (sys_clock_ticks_per_sec == 1)
1019
1020 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1021#else
1022 /* yields horrible 64-bit math on many architectures: try to avoid */
1023 #define _NON_OPTIMIZED_TICKS_PER_SEC
1024#endif
1025
1026#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001027extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001028#else
Kumar Galacc334c72017-04-21 10:55:34 -05001029static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001030{
Kumar Galacc334c72017-04-21 10:55:34 -05001031 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001032}
1033#endif
1034
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001035/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001036#ifdef CONFIG_TICKLESS_KERNEL
1037#define _TICK_ALIGN 0
1038#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001039#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001040#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001041
Kumar Galacc334c72017-04-21 10:55:34 -05001042static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001043{
Benjamin Walsh62092182016-12-20 14:39:08 -05001044#ifdef CONFIG_SYS_CLOCK_EXISTS
1045
1046#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001047 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001048#else
Kumar Galacc334c72017-04-21 10:55:34 -05001049 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001050#endif
1051
1052#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001053 __ASSERT(ticks == 0, "");
1054 return 0;
1055#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001056}
1057
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001058struct k_timer {
1059 /*
1060 * _timeout structure must be first here if we want to use
1061 * dynamic timer allocation. timeout.node is used in the double-linked
1062 * list of free timers
1063 */
1064 struct _timeout timeout;
1065
Allan Stephens45bfa372016-10-12 12:39:42 -05001066 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001067 _wait_q_t wait_q;
1068
1069 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001070 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001071
1072 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001073 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001074
1075 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001076 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001077
Allan Stephens45bfa372016-10-12 12:39:42 -05001078 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001079 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001080
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001081 /* user-specific data, also used to support legacy features */
1082 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001083
Anas Nashif2f203c22016-12-18 06:57:45 -05001084 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001085};
1086
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001087#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001088 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001089 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001090 .timeout.wait_q = NULL, \
1091 .timeout.thread = NULL, \
1092 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001093 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001094 .expiry_fn = expiry, \
1095 .stop_fn = stop, \
1096 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001097 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001098 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001099 }
1100
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001101#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1102
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001103/**
Allan Stephensc98da842016-11-11 15:45:03 -05001104 * INTERNAL_HIDDEN @endcond
1105 */
1106
1107/**
1108 * @defgroup timer_apis Timer APIs
1109 * @ingroup kernel_apis
1110 * @{
1111 */
1112
1113/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001114 * @typedef k_timer_expiry_t
1115 * @brief Timer expiry function type.
1116 *
1117 * A timer's expiry function is executed by the system clock interrupt handler
1118 * each time the timer expires. The expiry function is optional, and is only
1119 * invoked if the timer has been initialized with one.
1120 *
1121 * @param timer Address of timer.
1122 *
1123 * @return N/A
1124 */
1125typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1126
1127/**
1128 * @typedef k_timer_stop_t
1129 * @brief Timer stop function type.
1130 *
1131 * A timer's stop function is executed if the timer is stopped prematurely.
1132 * The function runs in the context of the thread that stops the timer.
1133 * The stop function is optional, and is only invoked if the timer has been
1134 * initialized with one.
1135 *
1136 * @param timer Address of timer.
1137 *
1138 * @return N/A
1139 */
1140typedef void (*k_timer_stop_t)(struct k_timer *timer);
1141
1142/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001143 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001144 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001145 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001146 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001147 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001148 *
1149 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001150 * @param expiry_fn Function to invoke each time the timer expires.
1151 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001152 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001153#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001154 struct k_timer name \
1155 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001156 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001157
Allan Stephens45bfa372016-10-12 12:39:42 -05001158/**
1159 * @brief Initialize a timer.
1160 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001161 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001162 *
1163 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001164 * @param expiry_fn Function to invoke each time the timer expires.
1165 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001166 *
1167 * @return N/A
1168 */
1169extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001170 k_timer_expiry_t expiry_fn,
1171 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001172
Allan Stephens45bfa372016-10-12 12:39:42 -05001173/**
1174 * @brief Start a timer.
1175 *
1176 * This routine starts a timer, and resets its status to zero. The timer
1177 * begins counting down using the specified duration and period values.
1178 *
1179 * Attempting to start a timer that is already running is permitted.
1180 * The timer's status is reset to zero and the timer begins counting down
1181 * using the new duration and period values.
1182 *
1183 * @param timer Address of timer.
1184 * @param duration Initial timer duration (in milliseconds).
1185 * @param period Timer period (in milliseconds).
1186 *
1187 * @return N/A
1188 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001189extern void k_timer_start(struct k_timer *timer,
Kumar Galacc334c72017-04-21 10:55:34 -05001190 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001191
1192/**
1193 * @brief Stop a timer.
1194 *
1195 * This routine stops a running timer prematurely. The timer's stop function,
1196 * if one exists, is invoked by the caller.
1197 *
1198 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001199 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001200 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001201 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1202 * if @a k_timer_stop is to be called from ISRs.
1203 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001204 * @param timer Address of timer.
1205 *
1206 * @return N/A
1207 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001208extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001209
1210/**
1211 * @brief Read timer status.
1212 *
1213 * This routine reads the timer's status, which indicates the number of times
1214 * it has expired since its status was last read.
1215 *
1216 * Calling this routine resets the timer's status to zero.
1217 *
1218 * @param timer Address of timer.
1219 *
1220 * @return Timer status.
1221 */
Kumar Galacc334c72017-04-21 10:55:34 -05001222extern u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001223
1224/**
1225 * @brief Synchronize thread to timer expiration.
1226 *
1227 * This routine blocks the calling thread until the timer's status is non-zero
1228 * (indicating that it has expired at least once since it was last examined)
1229 * or the timer is stopped. If the timer status is already non-zero,
1230 * or the timer is already stopped, the caller continues without waiting.
1231 *
1232 * Calling this routine resets the timer's status to zero.
1233 *
1234 * This routine must not be used by interrupt handlers, since they are not
1235 * allowed to block.
1236 *
1237 * @param timer Address of timer.
1238 *
1239 * @return Timer status.
1240 */
Kumar Galacc334c72017-04-21 10:55:34 -05001241extern u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001242
1243/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001244 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001245 *
1246 * This routine computes the (approximate) time remaining before a running
1247 * timer next expires. If the timer is not running, it returns zero.
1248 *
1249 * @param timer Address of timer.
1250 *
1251 * @return Remaining time (in milliseconds).
1252 */
Kumar Galacc334c72017-04-21 10:55:34 -05001253static inline s32_t k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001254{
1255 return _timeout_remaining_get(&timer->timeout);
1256}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001257
Allan Stephensc98da842016-11-11 15:45:03 -05001258/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001259 * @brief Associate user-specific data with a timer.
1260 *
1261 * This routine records the @a user_data with the @a timer, to be retrieved
1262 * later.
1263 *
1264 * It can be used e.g. in a timer handler shared across multiple subsystems to
1265 * retrieve data specific to the subsystem this timer is associated with.
1266 *
1267 * @param timer Address of timer.
1268 * @param user_data User data to associate with the timer.
1269 *
1270 * @return N/A
1271 */
1272static inline void k_timer_user_data_set(struct k_timer *timer,
1273 void *user_data)
1274{
1275 timer->user_data = user_data;
1276}
1277
1278/**
1279 * @brief Retrieve the user-specific data from a timer.
1280 *
1281 * @param timer Address of timer.
1282 *
1283 * @return The user data.
1284 */
1285static inline void *k_timer_user_data_get(struct k_timer *timer)
1286{
1287 return timer->user_data;
1288}
1289
1290/**
Allan Stephensc98da842016-11-11 15:45:03 -05001291 * @} end defgroup timer_apis
1292 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001293
Allan Stephensc98da842016-11-11 15:45:03 -05001294/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001295 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001296 * @{
1297 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001298
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001299/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001300 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001301 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001302 * This routine returns the elapsed time since the system booted,
1303 * in milliseconds.
1304 *
1305 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001306 */
Kumar Galacc334c72017-04-21 10:55:34 -05001307extern s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001308
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001309#ifdef CONFIG_TICKLESS_KERNEL
1310/**
1311 * @brief Enable clock always on in tickless kernel
1312 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001313 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001314 * there are no timer events programmed in tickless kernel
1315 * scheduling. This is necessary if the clock is used to track
1316 * passage of time.
1317 *
1318 * @retval prev_status Previous status of always on flag
1319 */
1320static inline int k_enable_sys_clock_always_on(void)
1321{
1322 int prev_status = _sys_clock_always_on;
1323
1324 _sys_clock_always_on = 1;
1325 _enable_sys_clock();
1326
1327 return prev_status;
1328}
1329
1330/**
1331 * @brief Disable clock always on in tickless kernel
1332 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001333 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001334 * there are no timer events programmed in tickless kernel
1335 * scheduling. To save power, this routine should be called
1336 * immediately when clock is not used to track time.
1337 */
1338static inline void k_disable_sys_clock_always_on(void)
1339{
1340 _sys_clock_always_on = 0;
1341}
1342#else
1343#define k_enable_sys_clock_always_on() do { } while ((0))
1344#define k_disable_sys_clock_always_on() do { } while ((0))
1345#endif
1346
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001347/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001348 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001349 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001350 * This routine returns the lower 32-bits of the elapsed time since the system
1351 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001353 * This routine can be more efficient than k_uptime_get(), as it reduces the
1354 * need for interrupt locking and 64-bit math. However, the 32-bit result
1355 * cannot hold a system uptime time larger than approximately 50 days, so the
1356 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001357 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001358 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001359 */
Kumar Galacc334c72017-04-21 10:55:34 -05001360extern u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001361
1362/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001363 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001364 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001365 * This routine computes the elapsed time between the current system uptime
1366 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001367 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001368 * @param reftime Pointer to a reference time, which is updated to the current
1369 * uptime upon return.
1370 *
1371 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001372 */
Kumar Galacc334c72017-04-21 10:55:34 -05001373extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001374
1375/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001376 * @brief Get elapsed time (32-bit version).
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 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1382 * need for interrupt locking and 64-bit math. However, the 32-bit result
1383 * cannot hold an elapsed time larger than approximately 50 days, so the
1384 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001385 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001386 * @param reftime Pointer to a reference time, which is updated to the current
1387 * uptime upon return.
1388 *
1389 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001390 */
Kumar Galacc334c72017-04-21 10:55:34 -05001391extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001392
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001393/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001394 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001395 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001396 * This routine returns the current time, as measured by the system's hardware
1397 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001398 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001399 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001400 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001401#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001402
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001403/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001404 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001405 */
1406
Allan Stephensc98da842016-11-11 15:45:03 -05001407/**
1408 * @cond INTERNAL_HIDDEN
1409 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001410
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001411struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001412 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001413 union {
1414 _wait_q_t wait_q;
1415
1416 _POLL_EVENT;
1417 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001418
1419 _OBJECT_TRACING_NEXT_PTR(k_queue);
1420};
1421
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001422#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001423 { \
1424 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1425 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001426 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001427 _OBJECT_TRACING_INIT \
1428 }
1429
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001430#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1431
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001432/**
1433 * INTERNAL_HIDDEN @endcond
1434 */
1435
1436/**
1437 * @defgroup queue_apis Queue APIs
1438 * @ingroup kernel_apis
1439 * @{
1440 */
1441
1442/**
1443 * @brief Initialize a queue.
1444 *
1445 * This routine initializes a queue object, prior to its first use.
1446 *
1447 * @param queue Address of the queue.
1448 *
1449 * @return N/A
1450 */
1451extern void k_queue_init(struct k_queue *queue);
1452
1453/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001454 * @brief Cancel waiting on a queue.
1455 *
1456 * This routine causes first thread pending on @a queue, if any, to
1457 * return from k_queue_get() call with NULL value (as if timeout expired).
1458 *
1459 * @note Can be called by ISRs.
1460 *
1461 * @param queue Address of the queue.
1462 *
1463 * @return N/A
1464 */
1465extern void k_queue_cancel_wait(struct k_queue *queue);
1466
1467/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001468 * @brief Append an element to the end of a queue.
1469 *
1470 * This routine appends a data item to @a queue. A queue data item must be
1471 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1472 * reserved for the kernel's use.
1473 *
1474 * @note Can be called by ISRs.
1475 *
1476 * @param queue Address of the queue.
1477 * @param data Address of the data item.
1478 *
1479 * @return N/A
1480 */
1481extern void k_queue_append(struct k_queue *queue, void *data);
1482
1483/**
1484 * @brief Prepend an element to a queue.
1485 *
1486 * This routine prepends a data item to @a queue. A queue data item must be
1487 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1488 * reserved for the kernel's use.
1489 *
1490 * @note Can be called by ISRs.
1491 *
1492 * @param queue Address of the queue.
1493 * @param data Address of the data item.
1494 *
1495 * @return N/A
1496 */
1497extern void k_queue_prepend(struct k_queue *queue, void *data);
1498
1499/**
1500 * @brief Inserts an element to a queue.
1501 *
1502 * This routine inserts a data item to @a queue after previous item. A queue
1503 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1504 * item are reserved for the kernel's use.
1505 *
1506 * @note Can be called by ISRs.
1507 *
1508 * @param queue Address of the queue.
1509 * @param prev Address of the previous data item.
1510 * @param data Address of the data item.
1511 *
1512 * @return N/A
1513 */
1514extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1515
1516/**
1517 * @brief Atomically append a list of elements to a queue.
1518 *
1519 * This routine adds a list of data items to @a queue in one operation.
1520 * The data items must be in a singly-linked list, with the first 32 bits
1521 * in each data item pointing to the next data item; the list must be
1522 * NULL-terminated.
1523 *
1524 * @note Can be called by ISRs.
1525 *
1526 * @param queue Address of the queue.
1527 * @param head Pointer to first node in singly-linked list.
1528 * @param tail Pointer to last node in singly-linked list.
1529 *
1530 * @return N/A
1531 */
1532extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1533
1534/**
1535 * @brief Atomically add a list of elements to a queue.
1536 *
1537 * This routine adds a list of data items to @a queue in one operation.
1538 * The data items must be in a singly-linked list implemented using a
1539 * sys_slist_t object. Upon completion, the original list is empty.
1540 *
1541 * @note Can be called by ISRs.
1542 *
1543 * @param queue Address of the queue.
1544 * @param list Pointer to sys_slist_t object.
1545 *
1546 * @return N/A
1547 */
1548extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1549
1550/**
1551 * @brief Get an element from a queue.
1552 *
1553 * This routine removes first data item from @a queue. The first 32 bits of the
1554 * data item are reserved for the kernel's use.
1555 *
1556 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1557 *
1558 * @param queue Address of the queue.
1559 * @param timeout Waiting period to obtain a data item (in milliseconds),
1560 * or one of the special values K_NO_WAIT and K_FOREVER.
1561 *
1562 * @return Address of the data item if successful; NULL if returned
1563 * without waiting, or waiting period timed out.
1564 */
Kumar Galacc334c72017-04-21 10:55:34 -05001565extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001566
1567/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001568 * @brief Remove an element from a queue.
1569 *
1570 * This routine removes data item from @a queue. The first 32 bits of the
1571 * data item are reserved for the kernel's use. Removing elements from k_queue
1572 * rely on sys_slist_find_and_remove which is not a constant time operation.
1573 *
1574 * @note Can be called by ISRs
1575 *
1576 * @param queue Address of the queue.
1577 * @param data Address of the data item.
1578 *
1579 * @return true if data item was removed
1580 */
1581static inline bool k_queue_remove(struct k_queue *queue, void *data)
1582{
1583 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1584}
1585
1586/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001587 * @brief Query a queue to see if it has data available.
1588 *
1589 * Note that the data might be already gone by the time this function returns
1590 * if other threads are also trying to read from the queue.
1591 *
1592 * @note Can be called by ISRs.
1593 *
1594 * @param queue Address of the queue.
1595 *
1596 * @return Non-zero if the queue is empty.
1597 * @return 0 if data is available.
1598 */
1599static inline int k_queue_is_empty(struct k_queue *queue)
1600{
1601 return (int)sys_slist_is_empty(&queue->data_q);
1602}
1603
1604/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001605 * @brief Peek element at the head of queue.
1606 *
1607 * Return element from the head of queue without removing it.
1608 *
1609 * @param queue Address of the queue.
1610 *
1611 * @return Head element, or NULL if queue is empty.
1612 */
1613static inline void *k_queue_peek_head(struct k_queue *queue)
1614{
1615 return sys_slist_peek_head(&queue->data_q);
1616}
1617
1618/**
1619 * @brief Peek element at the tail of queue.
1620 *
1621 * Return element from the tail of queue without removing it.
1622 *
1623 * @param queue Address of the queue.
1624 *
1625 * @return Tail element, or NULL if queue is empty.
1626 */
1627static inline void *k_queue_peek_tail(struct k_queue *queue)
1628{
1629 return sys_slist_peek_tail(&queue->data_q);
1630}
1631
1632/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001633 * @brief Statically define and initialize a queue.
1634 *
1635 * The queue can be accessed outside the module where it is defined using:
1636 *
1637 * @code extern struct k_queue <name>; @endcode
1638 *
1639 * @param name Name of the queue.
1640 */
1641#define K_QUEUE_DEFINE(name) \
1642 struct k_queue name \
1643 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001644 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001645
1646/**
1647 * @} end defgroup queue_apis
1648 */
1649
1650/**
1651 * @cond INTERNAL_HIDDEN
1652 */
1653
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001654struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001655 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001656};
1657
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001658#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001659 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001660 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001661 }
1662
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001663#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1664
Allan Stephensc98da842016-11-11 15:45:03 -05001665/**
1666 * INTERNAL_HIDDEN @endcond
1667 */
1668
1669/**
1670 * @defgroup fifo_apis Fifo APIs
1671 * @ingroup kernel_apis
1672 * @{
1673 */
1674
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001675/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001676 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001677 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001678 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001679 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001680 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001681 *
1682 * @return N/A
1683 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001684#define k_fifo_init(fifo) \
1685 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001686
1687/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001688 * @brief Cancel waiting on a fifo.
1689 *
1690 * This routine causes first thread pending on @a fifo, if any, to
1691 * return from k_fifo_get() call with NULL value (as if timeout
1692 * expired).
1693 *
1694 * @note Can be called by ISRs.
1695 *
1696 * @param fifo Address of the fifo.
1697 *
1698 * @return N/A
1699 */
1700#define k_fifo_cancel_wait(fifo) \
1701 k_queue_cancel_wait((struct k_queue *) fifo)
1702
1703/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001704 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001705 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001706 * This routine adds a data item to @a fifo. A fifo data item must be
1707 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1708 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001709 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001710 * @note Can be called by ISRs.
1711 *
1712 * @param fifo Address of the fifo.
1713 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001714 *
1715 * @return N/A
1716 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001717#define k_fifo_put(fifo, data) \
1718 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001719
1720/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001721 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001722 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001723 * This routine adds a list of data items to @a fifo in one operation.
1724 * The data items must be in a singly-linked list, with the first 32 bits
1725 * each data item pointing to the next data item; the list must be
1726 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001727 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001728 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001729 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001730 * @param fifo Address of the fifo.
1731 * @param head Pointer to first node in singly-linked list.
1732 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001733 *
1734 * @return N/A
1735 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001736#define k_fifo_put_list(fifo, head, tail) \
1737 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001738
1739/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001740 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001741 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001742 * This routine adds a list of data items to @a fifo in one operation.
1743 * The data items must be in a singly-linked list implemented using a
1744 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001745 * and must be re-initialized via sys_slist_init().
1746 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001747 * @note Can be called by ISRs.
1748 *
1749 * @param fifo Address of the fifo.
1750 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001751 *
1752 * @return N/A
1753 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001754#define k_fifo_put_slist(fifo, list) \
1755 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001756
1757/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001758 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001759 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001760 * This routine removes a data item from @a fifo in a "first in, first out"
1761 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001762 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001763 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1764 *
1765 * @param fifo Address of the fifo.
1766 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001767 * or one of the special values K_NO_WAIT and K_FOREVER.
1768 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001769 * @return Address of the data item if successful; NULL if returned
1770 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001771 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001772#define k_fifo_get(fifo, timeout) \
1773 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001774
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001775/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001776 * @brief Query a fifo to see if it has data available.
1777 *
1778 * Note that the data might be already gone by the time this function returns
1779 * if other threads is also trying to read from the fifo.
1780 *
1781 * @note Can be called by ISRs.
1782 *
1783 * @param fifo Address of the fifo.
1784 *
1785 * @return Non-zero if the fifo is empty.
1786 * @return 0 if data is available.
1787 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001788#define k_fifo_is_empty(fifo) \
1789 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001790
1791/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001792 * @brief Peek element at the head of fifo.
1793 *
1794 * Return element from the head of fifo without removing it. A usecase
1795 * for this is if elements of the fifo are themselves containers. Then
1796 * on each iteration of processing, a head container will be peeked,
1797 * and some data processed out of it, and only if the container is empty,
1798 * it will be completely remove from the fifo.
1799 *
1800 * @param fifo Address of the fifo.
1801 *
1802 * @return Head element, or NULL if the fifo is empty.
1803 */
1804#define k_fifo_peek_head(fifo) \
1805 k_queue_peek_head((struct k_queue *) fifo)
1806
1807/**
1808 * @brief Peek element at the tail of fifo.
1809 *
1810 * Return element from the tail of fifo (without removing it). A usecase
1811 * for this is if elements of the fifo are themselves containers. Then
1812 * it may be useful to add more data to the last container in fifo.
1813 *
1814 * @param fifo Address of the fifo.
1815 *
1816 * @return Tail element, or NULL if fifo is empty.
1817 */
1818#define k_fifo_peek_tail(fifo) \
1819 k_queue_peek_tail((struct k_queue *) fifo)
1820
1821/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001822 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001823 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001824 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001825 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001826 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001827 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001828 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001829 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001830#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001831 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001832 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001833 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001834
Allan Stephensc98da842016-11-11 15:45:03 -05001835/**
1836 * @} end defgroup fifo_apis
1837 */
1838
1839/**
1840 * @cond INTERNAL_HIDDEN
1841 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001842
1843struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001844 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001845};
1846
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001847#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001848 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001849 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001850 }
1851
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001852#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1853
Allan Stephensc98da842016-11-11 15:45:03 -05001854/**
1855 * INTERNAL_HIDDEN @endcond
1856 */
1857
1858/**
1859 * @defgroup lifo_apis Lifo APIs
1860 * @ingroup kernel_apis
1861 * @{
1862 */
1863
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001864/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001865 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001867 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001868 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001869 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001870 *
1871 * @return N/A
1872 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001873#define k_lifo_init(lifo) \
1874 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001875
1876/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001877 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001878 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001879 * This routine adds a data item to @a lifo. A lifo data item must be
1880 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1881 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001883 * @note Can be called by ISRs.
1884 *
1885 * @param lifo Address of the lifo.
1886 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001887 *
1888 * @return N/A
1889 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001890#define k_lifo_put(lifo, data) \
1891 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001892
1893/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001894 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001895 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001896 * This routine removes a data item from @a lifo in a "last in, first out"
1897 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001898 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001899 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1900 *
1901 * @param lifo Address of the lifo.
1902 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001903 * or one of the special values K_NO_WAIT and K_FOREVER.
1904 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001905 * @return Address of the data item if successful; NULL if returned
1906 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001907 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001908#define k_lifo_get(lifo, timeout) \
1909 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001910
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001911/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001912 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001913 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001914 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001915 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001916 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001917 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001918 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001919 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001920#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001921 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001922 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001923 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001924
Allan Stephensc98da842016-11-11 15:45:03 -05001925/**
1926 * @} end defgroup lifo_apis
1927 */
1928
1929/**
1930 * @cond INTERNAL_HIDDEN
1931 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001932
1933struct k_stack {
1934 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001935 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001936
Anas Nashif2f203c22016-12-18 06:57:45 -05001937 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001938};
1939
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001940#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05001941 { \
1942 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1943 .base = stack_buffer, \
1944 .next = stack_buffer, \
1945 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001946 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001947 }
1948
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001949#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
1950
Allan Stephensc98da842016-11-11 15:45:03 -05001951/**
1952 * INTERNAL_HIDDEN @endcond
1953 */
1954
1955/**
1956 * @defgroup stack_apis Stack APIs
1957 * @ingroup kernel_apis
1958 * @{
1959 */
1960
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001961/**
1962 * @brief Initialize a stack.
1963 *
1964 * This routine initializes a stack object, prior to its first use.
1965 *
1966 * @param stack Address of the stack.
1967 * @param buffer Address of array used to hold stacked values.
1968 * @param num_entries Maximum number of values that can be stacked.
1969 *
1970 * @return N/A
1971 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001972extern void k_stack_init(struct k_stack *stack,
Kumar Galacc334c72017-04-21 10:55:34 -05001973 u32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001974
1975/**
1976 * @brief Push an element onto a stack.
1977 *
1978 * This routine adds a 32-bit value @a data to @a stack.
1979 *
1980 * @note Can be called by ISRs.
1981 *
1982 * @param stack Address of the stack.
1983 * @param data Value to push onto the stack.
1984 *
1985 * @return N/A
1986 */
Kumar Galacc334c72017-04-21 10:55:34 -05001987extern void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001988
1989/**
1990 * @brief Pop an element from a stack.
1991 *
1992 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1993 * manner and stores the value in @a data.
1994 *
1995 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1996 *
1997 * @param stack Address of the stack.
1998 * @param data Address of area to hold the value popped from the stack.
1999 * @param timeout Waiting period to obtain a value (in milliseconds),
2000 * or one of the special values K_NO_WAIT and K_FOREVER.
2001 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002002 * @retval 0 Element popped from stack.
2003 * @retval -EBUSY Returned without waiting.
2004 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002005 */
Kumar Galacc334c72017-04-21 10:55:34 -05002006extern int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002008/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002009 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002010 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002011 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002012 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002013 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002014 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002015 * @param name Name of the stack.
2016 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002017 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002018#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002019 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002020 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002021 struct k_stack name \
2022 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002023 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002024 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002025
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002026/**
Allan Stephensc98da842016-11-11 15:45:03 -05002027 * @} end defgroup stack_apis
2028 */
2029
Allan Stephens6bba9b02016-11-16 14:56:54 -05002030struct k_work;
2031
Allan Stephensc98da842016-11-11 15:45:03 -05002032/**
2033 * @defgroup workqueue_apis Workqueue Thread APIs
2034 * @ingroup kernel_apis
2035 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002036 */
2037
Allan Stephens6bba9b02016-11-16 14:56:54 -05002038/**
2039 * @typedef k_work_handler_t
2040 * @brief Work item handler function type.
2041 *
2042 * A work item's handler function is executed by a workqueue's thread
2043 * when the work item is processed by the workqueue.
2044 *
2045 * @param work Address of the work item.
2046 *
2047 * @return N/A
2048 */
2049typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002050
2051/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002052 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002053 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002054
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002055struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002056 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002057 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002058};
2059
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002060enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002061 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002062};
2063
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002064struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002065 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002066 k_work_handler_t handler;
2067 atomic_t flags[1];
2068};
2069
Allan Stephens6bba9b02016-11-16 14:56:54 -05002070struct k_delayed_work {
2071 struct k_work work;
2072 struct _timeout timeout;
2073 struct k_work_q *work_q;
2074};
2075
2076extern struct k_work_q k_sys_work_q;
2077
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002078/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002079 * INTERNAL_HIDDEN @endcond
2080 */
2081
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002082#define _K_WORK_INITIALIZER(work_handler) \
2083 { \
2084 ._reserved = NULL, \
2085 .handler = work_handler, \
2086 .flags = { 0 } \
2087 }
2088
2089#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2090
Allan Stephens6bba9b02016-11-16 14:56:54 -05002091/**
2092 * @brief Initialize a statically-defined work item.
2093 *
2094 * This macro can be used to initialize a statically-defined workqueue work
2095 * item, prior to its first use. For example,
2096 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002097 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002098 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002099 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002100 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002101 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002102#define K_WORK_DEFINE(work, work_handler) \
2103 struct k_work work \
2104 __in_section(_k_work, static, work) = \
2105 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002106
2107/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002108 * @brief Initialize a work item.
2109 *
2110 * This routine initializes a workqueue work item, prior to its first use.
2111 *
2112 * @param work Address of work item.
2113 * @param handler Function to invoke each time work item is processed.
2114 *
2115 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002116 */
2117static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2118{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002119 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002120 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002121 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002122}
2123
2124/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002125 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002126 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002127 * This routine submits work item @a work to be processed by workqueue
2128 * @a work_q. If the work item is already pending in the workqueue's queue
2129 * as a result of an earlier submission, this routine has no effect on the
2130 * work item. If the work item has already been processed, or is currently
2131 * being processed, its work is considered complete and the work item can be
2132 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002133 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002134 * @warning
2135 * A submitted work item must not be modified until it has been processed
2136 * by the workqueue.
2137 *
2138 * @note Can be called by ISRs.
2139 *
2140 * @param work_q Address of workqueue.
2141 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002142 *
2143 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002144 */
2145static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2146 struct k_work *work)
2147{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002148 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002149 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002150 }
2151}
2152
2153/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002154 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002155 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002156 * This routine indicates if work item @a work is pending in a workqueue's
2157 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002158 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002159 * @note Can be called by ISRs.
2160 *
2161 * @param work Address of work item.
2162 *
2163 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002164 */
2165static inline int k_work_pending(struct k_work *work)
2166{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002167 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002168}
2169
2170/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002171 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002172 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002173 * This routine starts workqueue @a work_q. The workqueue spawns its work
2174 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002175 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002176 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002177 * @param stack Pointer to work queue thread's stack space, as defined by
2178 * K_THREAD_STACK_DEFINE()
2179 * @param stack_size Size of the work queue thread's stack (in bytes), which
2180 * should either be the same constant passed to
2181 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002182 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002183 *
2184 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002185 */
Andrew Boie507852a2017-07-25 18:47:07 -07002186extern void k_work_q_start(struct k_work_q *work_q,
2187 k_thread_stack_t stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002188 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002189
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002190/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002191 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002192 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002193 * This routine initializes a workqueue delayed work item, prior to
2194 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002195 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002196 * @param work Address of delayed work item.
2197 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002198 *
2199 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002200 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002201extern void k_delayed_work_init(struct k_delayed_work *work,
2202 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002203
2204/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002205 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002206 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002207 * This routine schedules work item @a work to be processed by workqueue
2208 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002209 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002210 * Only when the countdown completes is the work item actually submitted to
2211 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002212 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002213 * Submitting a previously submitted delayed work item that is still
2214 * counting down cancels the existing submission and restarts the countdown
2215 * using the new delay. If the work item is currently pending on the
2216 * workqueue's queue because the countdown has completed it is too late to
2217 * resubmit the item, and resubmission fails without impacting the work item.
2218 * If the work item has already been processed, or is currently being processed,
2219 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002220 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002221 * @warning
2222 * A delayed work item must not be modified until it has been processed
2223 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002224 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002225 * @note Can be called by ISRs.
2226 *
2227 * @param work_q Address of workqueue.
2228 * @param work Address of delayed work item.
2229 * @param delay Delay before submitting the work item (in milliseconds).
2230 *
2231 * @retval 0 Work item countdown started.
2232 * @retval -EINPROGRESS Work item is already pending.
2233 * @retval -EINVAL Work item is being processed or has completed its work.
2234 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002235 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002236extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2237 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002238 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002239
2240/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002241 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002242 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002243 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002244 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002245 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002246 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002247 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002248 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002249 * @param work Address of delayed work item.
2250 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002251 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002252 * @retval -EINPROGRESS Work item is already pending.
2253 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002254 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002255extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002256
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002257/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002258 * @brief Submit a work item to the system workqueue.
2259 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002260 * This routine submits work item @a work to be processed by the system
2261 * workqueue. If the work item is already pending in the workqueue's queue
2262 * as a result of an earlier submission, this routine has no effect on the
2263 * work item. If the work item has already been processed, or is currently
2264 * being processed, its work is considered complete and the work item can be
2265 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002266 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002267 * @warning
2268 * Work items submitted to the system workqueue should avoid using handlers
2269 * that block or yield since this may prevent the system workqueue from
2270 * processing other work items in a timely manner.
2271 *
2272 * @note Can be called by ISRs.
2273 *
2274 * @param work Address of work item.
2275 *
2276 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002277 */
2278static inline void k_work_submit(struct k_work *work)
2279{
2280 k_work_submit_to_queue(&k_sys_work_q, work);
2281}
2282
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002283/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002284 * @brief Submit a delayed work item to the system workqueue.
2285 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002286 * This routine schedules work item @a work to be processed by the system
2287 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002288 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002289 * Only when the countdown completes is the work item actually submitted to
2290 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002291 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002292 * Submitting a previously submitted delayed work item that is still
2293 * counting down cancels the existing submission and restarts the countdown
2294 * using the new delay. If the work item is currently pending on the
2295 * workqueue's queue because the countdown has completed it is too late to
2296 * resubmit the item, and resubmission fails without impacting the work item.
2297 * If the work item has already been processed, or is currently being processed,
2298 * its work is considered complete and the work item can be resubmitted.
2299 *
2300 * @warning
2301 * Work items submitted to the system workqueue should avoid using handlers
2302 * that block or yield since this may prevent the system workqueue from
2303 * processing other work items in a timely manner.
2304 *
2305 * @note Can be called by ISRs.
2306 *
2307 * @param work Address of delayed work item.
2308 * @param delay Delay before submitting the work item (in milliseconds).
2309 *
2310 * @retval 0 Work item countdown started.
2311 * @retval -EINPROGRESS Work item is already pending.
2312 * @retval -EINVAL Work item is being processed or has completed its work.
2313 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002314 */
2315static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002316 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002317{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002318 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002319}
2320
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002321/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002322 * @brief Get time remaining before a delayed work gets scheduled.
2323 *
2324 * This routine computes the (approximate) time remaining before a
2325 * delayed work gets executed. If the delayed work is not waiting to be
2326 * schedules, it returns zero.
2327 *
2328 * @param work Delayed work item.
2329 *
2330 * @return Remaining time (in milliseconds).
2331 */
Kumar Galacc334c72017-04-21 10:55:34 -05002332static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002333{
2334 return _timeout_remaining_get(&work->timeout);
2335}
2336
2337/**
Allan Stephensc98da842016-11-11 15:45:03 -05002338 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002339 */
2340
Allan Stephensc98da842016-11-11 15:45:03 -05002341/**
2342 * @cond INTERNAL_HIDDEN
2343 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002344
2345struct k_mutex {
2346 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002347 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002348 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002349 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002350
Anas Nashif2f203c22016-12-18 06:57:45 -05002351 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002352};
2353
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002354#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002355 { \
2356 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2357 .owner = NULL, \
2358 .lock_count = 0, \
2359 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002360 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002361 }
2362
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002363#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2364
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002365/**
Allan Stephensc98da842016-11-11 15:45:03 -05002366 * INTERNAL_HIDDEN @endcond
2367 */
2368
2369/**
2370 * @defgroup mutex_apis Mutex APIs
2371 * @ingroup kernel_apis
2372 * @{
2373 */
2374
2375/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002376 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002377 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002378 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002379 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002380 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002381 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002382 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002383 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002384#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002385 struct k_mutex name \
2386 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002387 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002388
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002389/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002390 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002391 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002392 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002393 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002394 * Upon completion, the mutex is available and does not have an owner.
2395 *
2396 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002397 *
2398 * @return N/A
2399 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002400extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002401
2402/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002403 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002404 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002405 * This routine locks @a mutex. If the mutex is locked by another thread,
2406 * the calling thread waits until the mutex becomes available or until
2407 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002408 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002409 * A thread is permitted to lock a mutex it has already locked. The operation
2410 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002411 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002412 * @param mutex Address of the mutex.
2413 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002414 * or one of the special values K_NO_WAIT and K_FOREVER.
2415 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002416 * @retval 0 Mutex locked.
2417 * @retval -EBUSY Returned without waiting.
2418 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002419 */
Kumar Galacc334c72017-04-21 10:55:34 -05002420extern int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002421
2422/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002423 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002424 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002425 * This routine unlocks @a mutex. The mutex must already be locked by the
2426 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002427 *
2428 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002429 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002430 * thread.
2431 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002432 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002433 *
2434 * @return N/A
2435 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002436extern void k_mutex_unlock(struct k_mutex *mutex);
2437
Allan Stephensc98da842016-11-11 15:45:03 -05002438/**
2439 * @} end defgroup mutex_apis
2440 */
2441
2442/**
2443 * @cond INTERNAL_HIDDEN
2444 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002445
2446struct k_sem {
2447 _wait_q_t wait_q;
2448 unsigned int count;
2449 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002450 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002451
Anas Nashif2f203c22016-12-18 06:57:45 -05002452 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002453};
2454
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002455#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002456 { \
2457 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2458 .count = initial_count, \
2459 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002460 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002461 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002462 }
2463
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002464#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2465
Allan Stephensc98da842016-11-11 15:45:03 -05002466/**
2467 * INTERNAL_HIDDEN @endcond
2468 */
2469
2470/**
2471 * @defgroup semaphore_apis Semaphore APIs
2472 * @ingroup kernel_apis
2473 * @{
2474 */
2475
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002476/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002477 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002478 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002479 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002480 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002481 * @param sem Address of the semaphore.
2482 * @param initial_count Initial semaphore count.
2483 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002484 *
2485 * @return N/A
2486 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002487extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2488 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002489
2490/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002491 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002492 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002493 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002494 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002495 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2496 *
2497 * @param sem Address of the semaphore.
2498 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002499 * or one of the special values K_NO_WAIT and K_FOREVER.
2500 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002501 * @note When porting code from the nanokernel legacy API to the new API, be
2502 * careful with the return value of this function. The return value is the
2503 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2504 * non-zero means failure, while the nano_sem_take family returns 1 for success
2505 * and 0 for failure.
2506 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002507 * @retval 0 Semaphore taken.
2508 * @retval -EBUSY Returned without waiting.
2509 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002510 */
Kumar Galacc334c72017-04-21 10:55:34 -05002511extern int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002512
2513/**
2514 * @brief Give a semaphore.
2515 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002516 * This routine gives @a sem, unless the semaphore is already at its maximum
2517 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002518 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002519 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002520 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002521 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002522 *
2523 * @return N/A
2524 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002525extern void k_sem_give(struct k_sem *sem);
2526
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002527/**
2528 * @brief Reset a semaphore's count to zero.
2529 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002530 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002531 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002532 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002533 *
2534 * @return N/A
2535 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04002536static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002537{
2538 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002539}
2540
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002541/**
2542 * @brief Get a semaphore's count.
2543 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002544 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002545 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002546 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002547 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002548 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002549 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02002550static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002551{
2552 return sem->count;
2553}
2554
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002555/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002556 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002557 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002558 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002559 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002560 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002561 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002562 * @param name Name of the semaphore.
2563 * @param initial_count Initial semaphore count.
2564 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002565 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002566#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002567 struct k_sem name \
2568 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002569 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002570
Allan Stephensc98da842016-11-11 15:45:03 -05002571/**
2572 * @} end defgroup semaphore_apis
2573 */
2574
2575/**
2576 * @defgroup alert_apis Alert APIs
2577 * @ingroup kernel_apis
2578 * @{
2579 */
2580
Allan Stephens5eceb852016-11-16 10:16:30 -05002581/**
2582 * @typedef k_alert_handler_t
2583 * @brief Alert handler function type.
2584 *
2585 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002586 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002587 * and is only invoked if the alert has been initialized with one.
2588 *
2589 * @param alert Address of the alert.
2590 *
2591 * @return 0 if alert has been consumed; non-zero if alert should pend.
2592 */
2593typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002594
2595/**
2596 * @} end defgroup alert_apis
2597 */
2598
2599/**
2600 * @cond INTERNAL_HIDDEN
2601 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002602
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002603#define K_ALERT_DEFAULT NULL
2604#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002605
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002606struct k_alert {
2607 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002608 atomic_t send_count;
2609 struct k_work work_item;
2610 struct k_sem sem;
2611
Anas Nashif2f203c22016-12-18 06:57:45 -05002612 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002613};
2614
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002615extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002616
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002617#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002618 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002619 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002620 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002621 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2622 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002623 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002624 }
2625
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002626#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2627
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002628/**
Allan Stephensc98da842016-11-11 15:45:03 -05002629 * INTERNAL_HIDDEN @endcond
2630 */
2631
2632/**
2633 * @addtogroup alert_apis
2634 * @{
2635 */
2636
2637/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002638 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002639 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002640 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002641 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002642 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002643 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002644 * @param name Name of the alert.
2645 * @param alert_handler Action to take when alert is sent. Specify either
2646 * the address of a function to be invoked by the system workqueue
2647 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2648 * K_ALERT_DEFAULT (which causes the alert to pend).
2649 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002650 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002651#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002652 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002653 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002654 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002655 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002656
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002657/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002658 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002659 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002660 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002661 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002662 * @param alert Address of the alert.
2663 * @param handler Action to take when alert is sent. Specify either the address
2664 * of a function to be invoked by the system workqueue thread,
2665 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2666 * K_ALERT_DEFAULT (which causes the alert to pend).
2667 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002668 *
2669 * @return N/A
2670 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002671extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2672 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002673
2674/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002675 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002676 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002677 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002678 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002679 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2680 *
2681 * @param alert Address of the alert.
2682 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002683 * or one of the special values K_NO_WAIT and K_FOREVER.
2684 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002685 * @retval 0 Alert received.
2686 * @retval -EBUSY Returned without waiting.
2687 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002688 */
Kumar Galacc334c72017-04-21 10:55:34 -05002689extern int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002690
2691/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002692 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002694 * This routine signals @a alert. The action specified for @a alert will
2695 * be taken, which may trigger the execution of an alert handler function
2696 * and/or cause the alert to pend (assuming the alert has not reached its
2697 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002698 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002699 * @note Can be called by ISRs.
2700 *
2701 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002702 *
2703 * @return N/A
2704 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002705extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002706
2707/**
Allan Stephensc98da842016-11-11 15:45:03 -05002708 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002709 */
2710
Allan Stephensc98da842016-11-11 15:45:03 -05002711/**
2712 * @cond INTERNAL_HIDDEN
2713 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002714
2715struct k_msgq {
2716 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002717 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002718 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002719 char *buffer_start;
2720 char *buffer_end;
2721 char *read_ptr;
2722 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002723 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002724
Anas Nashif2f203c22016-12-18 06:57:45 -05002725 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002726};
2727
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002728#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002729 { \
2730 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002731 .max_msgs = q_max_msgs, \
2732 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002733 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002734 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002735 .read_ptr = q_buffer, \
2736 .write_ptr = q_buffer, \
2737 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002738 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002739 }
2740
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002741#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2742
Peter Mitsis1da807e2016-10-06 11:36:59 -04002743/**
Allan Stephensc98da842016-11-11 15:45:03 -05002744 * INTERNAL_HIDDEN @endcond
2745 */
2746
2747/**
2748 * @defgroup msgq_apis Message Queue APIs
2749 * @ingroup kernel_apis
2750 * @{
2751 */
2752
2753/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002754 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002755 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002756 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2757 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002758 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2759 * message is similarly aligned to this boundary, @a q_msg_size must also be
2760 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002761 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002762 * The message queue can be accessed outside the module where it is defined
2763 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002764 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002765 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002766 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002767 * @param q_name Name of the message queue.
2768 * @param q_msg_size Message size (in bytes).
2769 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002770 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002771 */
2772#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2773 static char __noinit __aligned(q_align) \
2774 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002775 struct k_msgq q_name \
2776 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002777 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002778 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002779
Peter Mitsisd7a37502016-10-13 11:37:40 -04002780/**
2781 * @brief Initialize a message queue.
2782 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002783 * This routine initializes a message queue object, prior to its first use.
2784 *
Allan Stephensda827222016-11-09 14:23:58 -06002785 * The message queue's ring buffer must contain space for @a max_msgs messages,
2786 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2787 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2788 * that each message is similarly aligned to this boundary, @a q_msg_size
2789 * must also be a multiple of N.
2790 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002791 * @param q Address of the message queue.
2792 * @param buffer Pointer to ring buffer that holds queued messages.
2793 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002794 * @param max_msgs Maximum number of messages that can be queued.
2795 *
2796 * @return N/A
2797 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002798extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002799 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002800
2801/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002802 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002803 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002804 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002805 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002806 * @note Can be called by ISRs.
2807 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002808 * @param q Address of the message queue.
2809 * @param data Pointer to the message.
2810 * @param timeout Waiting period to add the message (in milliseconds),
2811 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002812 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002813 * @retval 0 Message sent.
2814 * @retval -ENOMSG Returned without waiting or queue purged.
2815 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002816 */
Kumar Galacc334c72017-04-21 10:55:34 -05002817extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002818
2819/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002820 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002821 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002822 * This routine receives a message from message queue @a q in a "first in,
2823 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002824 *
Allan Stephensc98da842016-11-11 15:45:03 -05002825 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002826 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002827 * @param q Address of the message queue.
2828 * @param data Address of area to hold the received message.
2829 * @param timeout Waiting period to receive the message (in milliseconds),
2830 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002831 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002832 * @retval 0 Message received.
2833 * @retval -ENOMSG Returned without waiting.
2834 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002835 */
Kumar Galacc334c72017-04-21 10:55:34 -05002836extern int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002837
2838/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002839 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002840 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002841 * This routine discards all unreceived messages in a message queue's ring
2842 * buffer. Any threads that are blocked waiting to send a message to the
2843 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002845 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002846 *
2847 * @return N/A
2848 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002849extern void k_msgq_purge(struct k_msgq *q);
2850
Peter Mitsis67be2492016-10-07 11:44:34 -04002851/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002852 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002853 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002854 * This routine returns the number of unused entries in a message queue's
2855 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002856 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002857 * @param q Address of the message queue.
2858 *
2859 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002860 */
Kumar Galacc334c72017-04-21 10:55:34 -05002861static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002862{
2863 return q->max_msgs - q->used_msgs;
2864}
2865
Peter Mitsisd7a37502016-10-13 11:37:40 -04002866/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002867 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002868 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002869 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002871 * @param q Address of the message queue.
2872 *
2873 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002874 */
Kumar Galacc334c72017-04-21 10:55:34 -05002875static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002876{
2877 return q->used_msgs;
2878}
2879
Allan Stephensc98da842016-11-11 15:45:03 -05002880/**
2881 * @} end defgroup msgq_apis
2882 */
2883
2884/**
2885 * @defgroup mem_pool_apis Memory Pool APIs
2886 * @ingroup kernel_apis
2887 * @{
2888 */
2889
Andy Ross73cb9582017-05-09 10:42:39 -07002890/* Note on sizing: the use of a 20 bit field for block means that,
2891 * assuming a reasonable minimum block size of 16 bytes, we're limited
2892 * to 16M of memory managed by a single pool. Long term it would be
2893 * good to move to a variable bit size based on configuration.
2894 */
2895struct k_mem_block_id {
2896 u32_t pool : 8;
2897 u32_t level : 4;
2898 u32_t block : 20;
2899};
2900
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002901struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002902 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002903 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002904};
2905
Allan Stephensc98da842016-11-11 15:45:03 -05002906/**
2907 * @} end defgroup mem_pool_apis
2908 */
2909
2910/**
2911 * @defgroup mailbox_apis Mailbox APIs
2912 * @ingroup kernel_apis
2913 * @{
2914 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002915
2916struct k_mbox_msg {
2917 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002918 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002919 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002920 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002921 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002922 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002923 /** sender's message data buffer */
2924 void *tx_data;
2925 /** internal use only - needed for legacy API support */
2926 void *_rx_data;
2927 /** message data block descriptor */
2928 struct k_mem_block tx_block;
2929 /** source thread id */
2930 k_tid_t rx_source_thread;
2931 /** target thread id */
2932 k_tid_t tx_target_thread;
2933 /** internal use only - thread waiting on send (may be a dummy) */
2934 k_tid_t _syncing_thread;
2935#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2936 /** internal use only - semaphore used during asynchronous send */
2937 struct k_sem *_async_sem;
2938#endif
2939};
2940
Allan Stephensc98da842016-11-11 15:45:03 -05002941/**
2942 * @cond INTERNAL_HIDDEN
2943 */
2944
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002945struct k_mbox {
2946 _wait_q_t tx_msg_queue;
2947 _wait_q_t rx_msg_queue;
2948
Anas Nashif2f203c22016-12-18 06:57:45 -05002949 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002950};
2951
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002952#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002953 { \
2954 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2955 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002956 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002957 }
2958
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002959#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
2960
Peter Mitsis12092702016-10-14 12:57:23 -04002961/**
Allan Stephensc98da842016-11-11 15:45:03 -05002962 * INTERNAL_HIDDEN @endcond
2963 */
2964
2965/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002966 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002967 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002968 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002969 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002970 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002971 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002972 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002973 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002974#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002975 struct k_mbox name \
2976 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002977 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002978
Peter Mitsis12092702016-10-14 12:57:23 -04002979/**
2980 * @brief Initialize a mailbox.
2981 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002982 * This routine initializes a mailbox object, prior to its first use.
2983 *
2984 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002985 *
2986 * @return N/A
2987 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002988extern void k_mbox_init(struct k_mbox *mbox);
2989
Peter Mitsis12092702016-10-14 12:57:23 -04002990/**
2991 * @brief Send a mailbox message in a synchronous manner.
2992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002993 * This routine sends a message to @a mbox and waits for a receiver to both
2994 * receive and process it. The message data may be in a buffer, in a memory
2995 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002996 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002997 * @param mbox Address of the mailbox.
2998 * @param tx_msg Address of the transmit message descriptor.
2999 * @param timeout Waiting period for the message to be received (in
3000 * milliseconds), or one of the special values K_NO_WAIT
3001 * and K_FOREVER. Once the message has been received,
3002 * this routine waits as long as necessary for the message
3003 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003004 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003005 * @retval 0 Message sent.
3006 * @retval -ENOMSG Returned without waiting.
3007 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003008 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003009extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003010 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003011
Peter Mitsis12092702016-10-14 12:57:23 -04003012/**
3013 * @brief Send a mailbox message in an asynchronous manner.
3014 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003015 * This routine sends a message to @a mbox without waiting for a receiver
3016 * to process it. The message data may be in a buffer, in a memory pool block,
3017 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3018 * will be given when the message has been both received and completely
3019 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003020 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003021 * @param mbox Address of the mailbox.
3022 * @param tx_msg Address of the transmit message descriptor.
3023 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003024 *
3025 * @return N/A
3026 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003027extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003028 struct k_sem *sem);
3029
Peter Mitsis12092702016-10-14 12:57:23 -04003030/**
3031 * @brief Receive a mailbox message.
3032 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003033 * This routine receives a message from @a mbox, then optionally retrieves
3034 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003035 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003036 * @param mbox Address of the mailbox.
3037 * @param rx_msg Address of the receive message descriptor.
3038 * @param buffer Address of the buffer to receive data, or NULL to defer data
3039 * retrieval and message disposal until later.
3040 * @param timeout Waiting period for a message to be received (in
3041 * milliseconds), or one of the special values K_NO_WAIT
3042 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003043 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003044 * @retval 0 Message received.
3045 * @retval -ENOMSG Returned without waiting.
3046 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003047 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003048extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003049 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003050
3051/**
3052 * @brief Retrieve mailbox message data into a buffer.
3053 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003054 * This routine completes the processing of a received message by retrieving
3055 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003056 *
3057 * Alternatively, this routine can be used to dispose of a received message
3058 * without retrieving its data.
3059 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003060 * @param rx_msg Address of the receive message descriptor.
3061 * @param buffer Address of the buffer to receive data, or NULL to discard
3062 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003063 *
3064 * @return N/A
3065 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003066extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003067
3068/**
3069 * @brief Retrieve mailbox message data into a memory pool block.
3070 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003071 * This routine completes the processing of a received message by retrieving
3072 * its data into a memory pool block, then disposing of the message.
3073 * The memory pool block that results from successful retrieval must be
3074 * returned to the pool once the data has been processed, even in cases
3075 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003076 *
3077 * Alternatively, this routine can be used to dispose of a received message
3078 * without retrieving its data. In this case there is no need to return a
3079 * memory pool block to the pool.
3080 *
3081 * This routine allocates a new memory pool block for the data only if the
3082 * data is not already in one. If a new block cannot be allocated, the routine
3083 * returns a failure code and the received message is left unchanged. This
3084 * permits the caller to reattempt data retrieval at a later time or to dispose
3085 * of the received message without retrieving its data.
3086 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003087 * @param rx_msg Address of a receive message descriptor.
3088 * @param pool Address of memory pool, or NULL to discard data.
3089 * @param block Address of the area to hold memory pool block info.
3090 * @param timeout Waiting period to wait for a memory pool block (in
3091 * milliseconds), or one of the special values K_NO_WAIT
3092 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003093 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003094 * @retval 0 Data retrieved.
3095 * @retval -ENOMEM Returned without waiting.
3096 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003097 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003098extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003099 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003100 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003101
Allan Stephensc98da842016-11-11 15:45:03 -05003102/**
3103 * @} end defgroup mailbox_apis
3104 */
3105
3106/**
3107 * @cond INTERNAL_HIDDEN
3108 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003109
3110struct k_pipe {
3111 unsigned char *buffer; /* Pipe buffer: may be NULL */
3112 size_t size; /* Buffer size */
3113 size_t bytes_used; /* # bytes used in buffer */
3114 size_t read_index; /* Where in buffer to read from */
3115 size_t write_index; /* Where in buffer to write */
3116
3117 struct {
3118 _wait_q_t readers; /* Reader wait queue */
3119 _wait_q_t writers; /* Writer wait queue */
3120 } wait_q;
3121
Anas Nashif2f203c22016-12-18 06:57:45 -05003122 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003123};
3124
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003125#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003126 { \
3127 .buffer = pipe_buffer, \
3128 .size = pipe_buffer_size, \
3129 .bytes_used = 0, \
3130 .read_index = 0, \
3131 .write_index = 0, \
3132 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3133 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003134 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003135 }
3136
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003137#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3138
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003139/**
Allan Stephensc98da842016-11-11 15:45:03 -05003140 * INTERNAL_HIDDEN @endcond
3141 */
3142
3143/**
3144 * @defgroup pipe_apis Pipe APIs
3145 * @ingroup kernel_apis
3146 * @{
3147 */
3148
3149/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003150 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003151 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003152 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003153 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003154 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003155 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003156 * @param name Name of the pipe.
3157 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3158 * or zero if no ring buffer is used.
3159 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003160 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003161#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3162 static unsigned char __noinit __aligned(pipe_align) \
3163 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003164 struct k_pipe name \
3165 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003166 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003167
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003168/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003169 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003170 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003171 * This routine initializes a pipe object, prior to its first use.
3172 *
3173 * @param pipe Address of the pipe.
3174 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3175 * is used.
3176 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3177 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003178 *
3179 * @return N/A
3180 */
3181extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3182 size_t size);
3183
3184/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003185 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003186 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003187 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003188 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003189 * @param pipe Address of the pipe.
3190 * @param data Address of data to write.
3191 * @param bytes_to_write Size of data (in bytes).
3192 * @param bytes_written Address of area to hold the number of bytes written.
3193 * @param min_xfer Minimum number of bytes to write.
3194 * @param timeout Waiting period to wait for the data to be written (in
3195 * milliseconds), or one of the special values K_NO_WAIT
3196 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003197 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003198 * @retval 0 At least @a min_xfer bytes of data were written.
3199 * @retval -EIO Returned without waiting; zero data bytes were written.
3200 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003201 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003202 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003203extern int k_pipe_put(struct k_pipe *pipe, void *data,
3204 size_t bytes_to_write, size_t *bytes_written,
Kumar Galacc334c72017-04-21 10:55:34 -05003205 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003206
3207/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003208 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003209 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003210 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003211 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003212 * @param pipe Address of the pipe.
3213 * @param data Address to place the data read from pipe.
3214 * @param bytes_to_read Maximum number of data bytes to read.
3215 * @param bytes_read Address of area to hold the number of bytes read.
3216 * @param min_xfer Minimum number of data bytes to read.
3217 * @param timeout Waiting period to wait for the data to be read (in
3218 * milliseconds), or one of the special values K_NO_WAIT
3219 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003220 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003221 * @retval 0 At least @a min_xfer bytes of data were read.
3222 * @retval -EIO Returned without waiting; zero data bytes were read.
3223 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003224 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003225 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003226extern int k_pipe_get(struct k_pipe *pipe, void *data,
3227 size_t bytes_to_read, size_t *bytes_read,
Kumar Galacc334c72017-04-21 10:55:34 -05003228 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003229
3230/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003231 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003232 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003233 * This routine writes the data contained in a memory block to @a pipe.
3234 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003235 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003236 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003237 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003238 * @param block Memory block containing data to send
3239 * @param size Number of data bytes in memory block to send
3240 * @param sem Semaphore to signal upon completion (else NULL)
3241 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003242 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003243 */
3244extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3245 size_t size, struct k_sem *sem);
3246
3247/**
Allan Stephensc98da842016-11-11 15:45:03 -05003248 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003249 */
3250
Allan Stephensc98da842016-11-11 15:45:03 -05003251/**
3252 * @cond INTERNAL_HIDDEN
3253 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003254
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003255struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003256 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003257 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003258 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003259 char *buffer;
3260 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003261 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003262
Anas Nashif2f203c22016-12-18 06:57:45 -05003263 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003264};
3265
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003266#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003267 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003268 { \
3269 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003270 .num_blocks = slab_num_blocks, \
3271 .block_size = slab_block_size, \
3272 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003273 .free_list = NULL, \
3274 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003275 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003276 }
3277
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003278#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3279
3280
Peter Mitsis578f9112016-10-07 13:50:31 -04003281/**
Allan Stephensc98da842016-11-11 15:45:03 -05003282 * INTERNAL_HIDDEN @endcond
3283 */
3284
3285/**
3286 * @defgroup mem_slab_apis Memory Slab APIs
3287 * @ingroup kernel_apis
3288 * @{
3289 */
3290
3291/**
Allan Stephensda827222016-11-09 14:23:58 -06003292 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003293 *
Allan Stephensda827222016-11-09 14:23:58 -06003294 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003295 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003296 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3297 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003298 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003299 *
Allan Stephensda827222016-11-09 14:23:58 -06003300 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003301 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003302 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003303 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003304 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003305 * @param name Name of the memory slab.
3306 * @param slab_block_size Size of each memory block (in bytes).
3307 * @param slab_num_blocks Number memory blocks.
3308 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003309 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003310#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3311 char __noinit __aligned(slab_align) \
3312 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3313 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003314 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003315 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003316 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003317
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003318/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003319 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003320 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003321 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003322 *
Allan Stephensda827222016-11-09 14:23:58 -06003323 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3324 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3325 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3326 * To ensure that each memory block is similarly aligned to this boundary,
3327 * @a slab_block_size must also be a multiple of N.
3328 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003329 * @param slab Address of the memory slab.
3330 * @param buffer Pointer to buffer used for the memory blocks.
3331 * @param block_size Size of each memory block (in bytes).
3332 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003333 *
3334 * @return N/A
3335 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003336extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003337 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003338
3339/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003340 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003341 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003342 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003343 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003344 * @param slab Address of the memory slab.
3345 * @param mem Pointer to block address area.
3346 * @param timeout Maximum time to wait for operation to complete
3347 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3348 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003349 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003350 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003351 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003352 * @retval -ENOMEM Returned without waiting.
3353 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003354 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003355extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003356 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003357
3358/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003359 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003360 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003361 * This routine releases a previously allocated memory block back to its
3362 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003363 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003364 * @param slab Address of the memory slab.
3365 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003366 *
3367 * @return N/A
3368 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003369extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003370
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003371/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003372 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003373 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003374 * This routine gets the number of memory blocks that are currently
3375 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003376 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003377 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003378 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003379 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003380 */
Kumar Galacc334c72017-04-21 10:55:34 -05003381static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003382{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003383 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003384}
3385
Peter Mitsisc001aa82016-10-13 13:53:37 -04003386/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003387 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003388 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003389 * This routine gets the number of memory blocks that are currently
3390 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003391 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003392 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003393 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003394 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003395 */
Kumar Galacc334c72017-04-21 10:55:34 -05003396static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003397{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003398 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003399}
3400
Allan Stephensc98da842016-11-11 15:45:03 -05003401/**
3402 * @} end defgroup mem_slab_apis
3403 */
3404
3405/**
3406 * @cond INTERNAL_HIDDEN
3407 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003408
Andy Ross73cb9582017-05-09 10:42:39 -07003409struct k_mem_pool_lvl {
3410 union {
3411 u32_t *bits_p;
3412 u32_t bits;
3413 };
3414 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003415};
3416
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003417struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003418 void *buf;
3419 size_t max_sz;
3420 u16_t n_max;
3421 u8_t n_levels;
3422 u8_t max_inline_level;
3423 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003424 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003425};
3426
Andy Ross73cb9582017-05-09 10:42:39 -07003427#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003428
Andy Ross73cb9582017-05-09 10:42:39 -07003429#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3430
3431#define _MPOOL_LVLS(maxsz, minsz) \
3432 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3433 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3434 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3435 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3436 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3437 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3438 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3439 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3440 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3441 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3442 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3443 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3444 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3445 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3446 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3447 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3448
3449/* Rounds the needed bits up to integer multiples of u32_t */
3450#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3451 ((((n_max) << (2*(l))) + 31) / 32)
3452
3453/* One word gets stored free unioned with the pointer, otherwise the
3454 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003455 */
Andy Ross73cb9582017-05-09 10:42:39 -07003456#define _MPOOL_LBIT_WORDS(n_max, l) \
3457 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3458 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003459
Andy Ross73cb9582017-05-09 10:42:39 -07003460/* How many bytes for the bitfields of a single level? */
3461#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3462 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3463 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003464
Andy Ross73cb9582017-05-09 10:42:39 -07003465/* Size of the bitmap array that follows the buffer in allocated memory */
3466#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3467 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3468 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3469 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3470 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3471 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3472 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3473 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3474 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3475 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3476 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3477 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3478 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3479 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3480 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3481 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3482 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003483
3484/**
Allan Stephensc98da842016-11-11 15:45:03 -05003485 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003486 */
3487
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003488/**
Allan Stephensc98da842016-11-11 15:45:03 -05003489 * @addtogroup mem_pool_apis
3490 * @{
3491 */
3492
3493/**
3494 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003495 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003496 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3497 * long. The memory pool allows blocks to be repeatedly partitioned into
3498 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003499 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003500 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003501 * If the pool is to be accessed outside the module where it is defined, it
3502 * can be declared via
3503 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003504 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003505 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003506 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003507 * @param minsz Size of the smallest blocks in the pool (in bytes).
3508 * @param maxsz Size of the largest blocks in the pool (in bytes).
3509 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003510 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003511 */
Andy Ross73cb9582017-05-09 10:42:39 -07003512#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3513 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3514 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3515 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3516 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3517 .buf = _mpool_buf_##name, \
3518 .max_sz = maxsz, \
3519 .n_max = nmax, \
3520 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3521 .levels = _mpool_lvls_##name, \
3522 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003523
Peter Mitsis937042c2016-10-13 13:18:26 -04003524/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003525 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003526 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003527 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003528 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003529 * @param pool Address of the memory pool.
3530 * @param block Pointer to block descriptor for the allocated memory.
3531 * @param size Amount of memory to allocate (in bytes).
3532 * @param timeout Maximum time to wait for operation to complete
3533 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3534 * or K_FOREVER to wait as long as necessary.
3535 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003536 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003537 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003538 * @retval -ENOMEM Returned without waiting.
3539 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003540 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003541extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003542 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003543
3544/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003545 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003546 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003547 * This routine releases a previously allocated memory block back to its
3548 * memory pool.
3549 *
3550 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003551 *
3552 * @return N/A
3553 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003554extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003555
3556/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003557 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003558 *
Andy Ross73cb9582017-05-09 10:42:39 -07003559 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003560 *
Andy Ross73cb9582017-05-09 10:42:39 -07003561 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003562 *
3563 * @return N/A
3564 */
Andy Ross73cb9582017-05-09 10:42:39 -07003565static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003566
3567/**
Allan Stephensc98da842016-11-11 15:45:03 -05003568 * @} end addtogroup mem_pool_apis
3569 */
3570
3571/**
3572 * @defgroup heap_apis Heap Memory Pool APIs
3573 * @ingroup kernel_apis
3574 * @{
3575 */
3576
3577/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003578 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003579 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003580 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003581 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003582 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003583 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003584 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003585 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003586 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003587extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003588
3589/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003590 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003591 *
3592 * This routine provides traditional free() semantics. The memory being
3593 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003594 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003595 * If @a ptr is NULL, no operation is performed.
3596 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003597 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003598 *
3599 * @return N/A
3600 */
3601extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003602
Allan Stephensc98da842016-11-11 15:45:03 -05003603/**
3604 * @} end defgroup heap_apis
3605 */
3606
Benjamin Walshacc68c12017-01-29 18:57:45 -05003607/* polling API - PRIVATE */
3608
Benjamin Walshb0179862017-02-02 16:39:57 -05003609#ifdef CONFIG_POLL
3610#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3611#else
3612#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3613#endif
3614
Benjamin Walshacc68c12017-01-29 18:57:45 -05003615/* private - implementation data created as needed, per-type */
3616struct _poller {
3617 struct k_thread *thread;
3618};
3619
3620/* private - types bit positions */
3621enum _poll_types_bits {
3622 /* can be used to ignore an event */
3623 _POLL_TYPE_IGNORE,
3624
3625 /* to be signaled by k_poll_signal() */
3626 _POLL_TYPE_SIGNAL,
3627
3628 /* semaphore availability */
3629 _POLL_TYPE_SEM_AVAILABLE,
3630
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003631 /* queue/fifo/lifo data availability */
3632 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003633
3634 _POLL_NUM_TYPES
3635};
3636
3637#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3638
3639/* private - states bit positions */
3640enum _poll_states_bits {
3641 /* default state when creating event */
3642 _POLL_STATE_NOT_READY,
3643
Benjamin Walshacc68c12017-01-29 18:57:45 -05003644 /* signaled by k_poll_signal() */
3645 _POLL_STATE_SIGNALED,
3646
3647 /* semaphore is available */
3648 _POLL_STATE_SEM_AVAILABLE,
3649
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003650 /* data is available to read on queue/fifo/lifo */
3651 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003652
3653 _POLL_NUM_STATES
3654};
3655
3656#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3657
3658#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003659 (32 - (0 \
3660 + 8 /* tag */ \
3661 + _POLL_NUM_TYPES \
3662 + _POLL_NUM_STATES \
3663 + 1 /* modes */ \
3664 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003665
3666#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3667#error overflow of 32-bit word in struct k_poll_event
3668#endif
3669
3670/* end of polling API - PRIVATE */
3671
3672
3673/**
3674 * @defgroup poll_apis Async polling APIs
3675 * @ingroup kernel_apis
3676 * @{
3677 */
3678
3679/* Public polling API */
3680
3681/* public - values for k_poll_event.type bitfield */
3682#define K_POLL_TYPE_IGNORE 0
3683#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3684#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003685#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3686#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003687
3688/* public - polling modes */
3689enum k_poll_modes {
3690 /* polling thread does not take ownership of objects when available */
3691 K_POLL_MODE_NOTIFY_ONLY = 0,
3692
3693 K_POLL_NUM_MODES
3694};
3695
3696/* public - values for k_poll_event.state bitfield */
3697#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003698#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3699#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003700#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3701#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003702
3703/* public - poll signal object */
3704struct k_poll_signal {
3705 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003706 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003707
3708 /*
3709 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3710 * user resets it to 0.
3711 */
3712 unsigned int signaled;
3713
3714 /* custom result value passed to k_poll_signal() if needed */
3715 int result;
3716};
3717
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003718#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003719 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003720 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003721 .signaled = 0, \
3722 .result = 0, \
3723 }
3724
3725struct k_poll_event {
3726 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003727 sys_dnode_t _node;
3728
3729 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003730 struct _poller *poller;
3731
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003732 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003733 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003734
Benjamin Walshacc68c12017-01-29 18:57:45 -05003735 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003736 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003737
3738 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003739 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003740
3741 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003742 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003743
3744 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003745 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003746
3747 /* per-type data */
3748 union {
3749 void *obj;
3750 struct k_poll_signal *signal;
3751 struct k_sem *sem;
3752 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003753 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003754 };
3755};
3756
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003757#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003758 { \
3759 .poller = NULL, \
3760 .type = event_type, \
3761 .state = K_POLL_STATE_NOT_READY, \
3762 .mode = event_mode, \
3763 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003764 { .obj = event_obj }, \
3765 }
3766
3767#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3768 event_tag) \
3769 { \
3770 .type = event_type, \
3771 .tag = event_tag, \
3772 .state = K_POLL_STATE_NOT_READY, \
3773 .mode = event_mode, \
3774 .unused = 0, \
3775 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003776 }
3777
3778/**
3779 * @brief Initialize one struct k_poll_event instance
3780 *
3781 * After this routine is called on a poll event, the event it ready to be
3782 * placed in an event array to be passed to k_poll().
3783 *
3784 * @param event The event to initialize.
3785 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3786 * values. Only values that apply to the same object being polled
3787 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3788 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003789 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003790 * @param obj Kernel object or poll signal.
3791 *
3792 * @return N/A
3793 */
3794
Kumar Galacc334c72017-04-21 10:55:34 -05003795extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003796 int mode, void *obj);
3797
3798/**
3799 * @brief Wait for one or many of multiple poll events to occur
3800 *
3801 * This routine allows a thread to wait concurrently for one or many of
3802 * multiple poll events to have occurred. Such events can be a kernel object
3803 * being available, like a semaphore, or a poll signal event.
3804 *
3805 * When an event notifies that a kernel object is available, the kernel object
3806 * is not "given" to the thread calling k_poll(): it merely signals the fact
3807 * that the object was available when the k_poll() call was in effect. Also,
3808 * all threads trying to acquire an object the regular way, i.e. by pending on
3809 * the object, have precedence over the thread polling on the object. This
3810 * means that the polling thread will never get the poll event on an object
3811 * until the object becomes available and its pend queue is empty. For this
3812 * reason, the k_poll() call is more effective when the objects being polled
3813 * only have one thread, the polling thread, trying to acquire them.
3814 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003815 * When k_poll() returns 0, the caller should loop on all the events that were
3816 * passed to k_poll() and check the state field for the values that were
3817 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003818 *
3819 * Before being reused for another call to k_poll(), the user has to reset the
3820 * state field to K_POLL_STATE_NOT_READY.
3821 *
3822 * @param events An array of pointers to events to be polled for.
3823 * @param num_events The number of events in the array.
3824 * @param timeout Waiting period for an event to be ready (in milliseconds),
3825 * or one of the special values K_NO_WAIT and K_FOREVER.
3826 *
3827 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003828 * @retval -EAGAIN Waiting period timed out.
3829 */
3830
3831extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003832 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003833
3834/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003835 * @brief Initialize a poll signal object.
3836 *
3837 * Ready a poll signal object to be signaled via k_poll_signal().
3838 *
3839 * @param signal A poll signal.
3840 *
3841 * @return N/A
3842 */
3843
3844extern void k_poll_signal_init(struct k_poll_signal *signal);
3845
3846/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003847 * @brief Signal a poll signal object.
3848 *
3849 * This routine makes ready a poll signal, which is basically a poll event of
3850 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3851 * made ready to run. A @a result value can be specified.
3852 *
3853 * The poll signal contains a 'signaled' field that, when set by
3854 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3855 * be reset by the user before being passed again to k_poll() or k_poll() will
3856 * consider it being signaled, and will return immediately.
3857 *
3858 * @param signal A poll signal.
3859 * @param result The value to store in the result field of the signal.
3860 *
3861 * @retval 0 The signal was delivered successfully.
3862 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3863 */
3864
3865extern int k_poll_signal(struct k_poll_signal *signal, int result);
3866
3867/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003868extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003869
3870/**
3871 * @} end defgroup poll_apis
3872 */
3873
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003874/**
3875 * @brief Make the CPU idle.
3876 *
3877 * This function makes the CPU idle until an event wakes it up.
3878 *
3879 * In a regular system, the idle thread should be the only thread responsible
3880 * for making the CPU idle and triggering any type of power management.
3881 * However, in some more constrained systems, such as a single-threaded system,
3882 * the only thread would be responsible for this if needed.
3883 *
3884 * @return N/A
3885 */
3886extern void k_cpu_idle(void);
3887
3888/**
3889 * @brief Make the CPU idle in an atomic fashion.
3890 *
3891 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3892 * must be done atomically before making the CPU idle.
3893 *
3894 * @param key Interrupt locking key obtained from irq_lock().
3895 *
3896 * @return N/A
3897 */
3898extern void k_cpu_atomic_idle(unsigned int key);
3899
Kumar Galacc334c72017-04-21 10:55:34 -05003900extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003901
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003902#include <arch/cpu.h>
3903
Andrew Boiecdb94d62017-04-18 15:22:05 -07003904#ifdef _ARCH_EXCEPT
3905/* This archtecture has direct support for triggering a CPU exception */
3906#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3907#else
3908
3909#include <misc/printk.h>
3910
3911/* NOTE: This is the implementation for arches that do not implement
3912 * _ARCH_EXCEPT() to generate a real CPU exception.
3913 *
3914 * We won't have a real exception frame to determine the PC value when
3915 * the oops occurred, so print file and line number before we jump into
3916 * the fatal error handler.
3917 */
3918#define _k_except_reason(reason) do { \
3919 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3920 _NanoFatalErrorHandler(reason, &_default_esf); \
3921 CODE_UNREACHABLE; \
3922 } while (0)
3923
3924#endif /* _ARCH__EXCEPT */
3925
3926/**
3927 * @brief Fatally terminate a thread
3928 *
3929 * This should be called when a thread has encountered an unrecoverable
3930 * runtime condition and needs to terminate. What this ultimately
3931 * means is determined by the _fatal_error_handler() implementation, which
3932 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3933 *
3934 * If this is called from ISR context, the default system fatal error handler
3935 * will treat it as an unrecoverable system error, just like k_panic().
3936 */
3937#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3938
3939/**
3940 * @brief Fatally terminate the system
3941 *
3942 * This should be called when the Zephyr kernel has encountered an
3943 * unrecoverable runtime condition and needs to terminate. What this ultimately
3944 * means is determined by the _fatal_error_handler() implementation, which
3945 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3946 */
3947#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3948
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003949/*
3950 * private APIs that are utilized by one or more public APIs
3951 */
3952
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003953#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003954extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003955#else
3956#define _init_static_threads() do { } while ((0))
3957#endif
3958
3959extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003960extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003961
Andrew Boiedc5d9352017-06-02 12:56:47 -07003962/* arch/cpu.h may declare an architecture or platform-specific macro
3963 * for properly declaring stacks, compatible with MMU/MPU constraints if
3964 * enabled
3965 */
3966#ifdef _ARCH_THREAD_STACK_DEFINE
3967#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
3968#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
3969 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
3970#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
3971#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie507852a2017-07-25 18:47:07 -07003972static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
3973{
3974 return _ARCH_THREAD_STACK_BUFFER(sym);
3975}
Andrew Boiedc5d9352017-06-02 12:56:47 -07003976#else
3977/**
3978 * @brief Declare a toplevel thread stack memory region
3979 *
3980 * This declares a region of memory suitable for use as a thread's stack.
3981 *
3982 * This is the generic, historical definition. Align to STACK_ALIGN and put in
3983 * 'noinit' section so that it isn't zeroed at boot
3984 *
Andrew Boie507852a2017-07-25 18:47:07 -07003985 * The declared symbol will always be a k_thread_stack_t which can be passed to
3986 * k_thread_create, but should otherwise not be manipulated. If the buffer
3987 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07003988 *
3989 * It is legal to precede this definition with the 'static' keyword.
3990 *
3991 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
3992 * parameter of k_thread_create(), it may not be the same as the
3993 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
3994 *
3995 * @param sym Thread stack symbol name
3996 * @param size Size of the stack memory region
3997 */
3998#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07003999 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004000
4001/**
4002 * @brief Declare a toplevel array of thread stack memory regions
4003 *
4004 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4005 * definition for additional details and constraints.
4006 *
4007 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4008 * 'noinit' section so that it isn't zeroed at boot
4009 *
4010 * @param sym Thread stack symbol name
4011 * @param nmemb Number of stacks to declare
4012 * @param size Size of the stack memory region
4013 */
4014
4015#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004016 struct _k_thread_stack_element __noinit \
4017 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004018
4019/**
4020 * @brief Declare an embedded stack memory region
4021 *
4022 * Used for stacks embedded within other data structures. Use is highly
4023 * discouraged but in some cases necessary. For memory protection scenarios,
4024 * it is very important that any RAM preceding this member not be writable
4025 * by threads else a stack overflow will lead to silent corruption. In other
4026 * words, the containing data structure should live in RAM owned by the kernel.
4027 *
4028 * @param sym Thread stack symbol name
4029 * @param size Size of the stack memory region
4030 */
4031#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004032 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004033
4034/**
4035 * @brief Return the size in bytes of a stack memory region
4036 *
4037 * Convenience macro for passing the desired stack size to k_thread_create()
4038 * since the underlying implementation may actually create something larger
4039 * (for instance a guard area).
4040 *
4041 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004042 * passed to K_THREAD_STACK_DEFINE.
4043 *
4044 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4045 * it is not guaranteed to return the original value since each array
4046 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004047 *
4048 * @param sym Stack memory symbol
4049 * @return Size of the stack
4050 */
4051#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4052
4053/**
4054 * @brief Get a pointer to the physical stack buffer
4055 *
4056 * Convenience macro to get at the real underlying stack buffer used by
4057 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4058 * This is really only intended for diagnostic tools which want to examine
4059 * stack memory contents.
4060 *
4061 * @param sym Declared stack symbol name
4062 * @return The buffer itself, a char *
4063 */
Andrew Boie507852a2017-07-25 18:47:07 -07004064static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4065{
4066 return (char *)sym;
4067}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004068
4069#endif /* _ARCH_DECLARE_STACK */
4070
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004071#ifdef __cplusplus
4072}
4073#endif
4074
Andrew Boiee004dec2016-11-07 09:01:19 -08004075#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4076/*
4077 * Define new and delete operators.
4078 * At this moment, the operators do nothing since objects are supposed
4079 * to be statically allocated.
4080 */
4081inline void operator delete(void *ptr)
4082{
4083 (void)ptr;
4084}
4085
4086inline void operator delete[](void *ptr)
4087{
4088 (void)ptr;
4089}
4090
4091inline void *operator new(size_t size)
4092{
4093 (void)size;
4094 return NULL;
4095}
4096
4097inline void *operator new[](size_t size)
4098{
4099 (void)size;
4100 return NULL;
4101}
4102
4103/* Placement versions of operator new and delete */
4104inline void operator delete(void *ptr1, void *ptr2)
4105{
4106 (void)ptr1;
4107 (void)ptr2;
4108}
4109
4110inline void operator delete[](void *ptr1, void *ptr2)
4111{
4112 (void)ptr1;
4113 (void)ptr2;
4114}
4115
4116inline void *operator new(size_t size, void *ptr)
4117{
4118 (void)size;
4119 return ptr;
4120}
4121
4122inline void *operator new[](size_t size, void *ptr)
4123{
4124 (void)size;
4125 return ptr;
4126}
4127
4128#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4129
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004130#endif /* !_ASMLANGUAGE */
4131
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004132#endif /* _kernel__h_ */