blob: 131e7641f83971dc3558534768ef2689412ceca7 [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
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700238/**
239 * @typedef k_thread_entry_t
240 * @brief Thread entry point function type.
241 *
242 * A thread's entry point function is invoked when the thread starts executing.
243 * Up to 3 argument values can be passed to the function.
244 *
245 * The thread terminates execution permanently if the entry point function
246 * returns. The thread is responsible for releasing any shared resources
247 * it may own (such as mutexes and dynamically allocated memory), prior to
248 * returning.
249 *
250 * @param p1 First argument.
251 * @param p2 Second argument.
252 * @param p3 Third argument.
253 *
254 * @return N/A
255 */
256typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700257
258#ifdef CONFIG_THREAD_MONITOR
259struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700260 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700261 void *parameter1;
262 void *parameter2;
263 void *parameter3;
264};
265#endif
266
267/* can be used for creating 'dummy' threads, e.g. for pending on objects */
268struct _thread_base {
269
270 /* this thread's entry in a ready/wait queue */
271 sys_dnode_t k_q_node;
272
273 /* user facing 'thread options'; values defined in include/kernel.h */
274 u8_t user_options;
275
276 /* thread state */
277 u8_t thread_state;
278
279 /*
280 * scheduler lock count and thread priority
281 *
282 * These two fields control the preemptibility of a thread.
283 *
284 * When the scheduler is locked, sched_locked is decremented, which
285 * means that the scheduler is locked for values from 0xff to 0x01. A
286 * thread is coop if its prio is negative, thus 0x80 to 0xff when
287 * looked at the value as unsigned.
288 *
289 * By putting them end-to-end, this means that a thread is
290 * non-preemptible if the bundled value is greater than or equal to
291 * 0x0080.
292 */
293 union {
294 struct {
295#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
296 u8_t sched_locked;
297 s8_t prio;
298#else /* LITTLE and PDP */
299 s8_t prio;
300 u8_t sched_locked;
301#endif
302 };
303 u16_t preempt;
304 };
305
306 /* data returned by APIs */
307 void *swap_data;
308
309#ifdef CONFIG_SYS_CLOCK_EXISTS
310 /* this thread's entry in a timeout queue */
311 struct _timeout timeout;
312#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700313};
314
315typedef struct _thread_base _thread_base_t;
316
317#if defined(CONFIG_THREAD_STACK_INFO)
318/* Contains the stack information of a thread */
319struct _thread_stack_info {
320 /* Stack Start */
321 u32_t start;
322 /* Stack Size */
323 u32_t size;
324};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700325
326typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700327#endif /* CONFIG_THREAD_STACK_INFO */
328
329struct k_thread {
330
331 struct _thread_base base;
332
333 /* defined by the architecture, but all archs need these */
334 struct _caller_saved caller_saved;
335 struct _callee_saved callee_saved;
336
337 /* static thread init data */
338 void *init_data;
339
340 /* abort function */
341 void (*fn_abort)(void);
342
343#if defined(CONFIG_THREAD_MONITOR)
344 /* thread entry and parameters description */
345 struct __thread_entry *entry;
346
347 /* next item in list of all threads */
348 struct k_thread *next_thread;
349#endif
350
351#ifdef CONFIG_THREAD_CUSTOM_DATA
352 /* crude thread-local storage */
353 void *custom_data;
354#endif
355
356#ifdef CONFIG_ERRNO
357 /* per-thread errno variable */
358 int errno_var;
359#endif
360
361#if defined(CONFIG_THREAD_STACK_INFO)
362 /* Stack Info */
363 struct _thread_stack_info stack_info;
364#endif /* CONFIG_THREAD_STACK_INFO */
365
366 /* arch-specifics: must always be at the end */
367 struct _thread_arch arch;
368};
369
370typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400371typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700372#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400373
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400374enum execution_context_types {
375 K_ISR = 0,
376 K_COOP_THREAD,
377 K_PREEMPT_THREAD,
378};
379
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400380/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100381 * @defgroup profiling_apis Profiling APIs
382 * @ingroup kernel_apis
383 * @{
384 */
385
386/**
387 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
388 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700389 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100390 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
391 *
392 * CONFIG_MAIN_STACK_SIZE
393 * CONFIG_IDLE_STACK_SIZE
394 * CONFIG_ISR_STACK_SIZE
395 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
396 *
397 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
398 * produce output.
399 *
400 * @return N/A
401 */
402extern void k_call_stacks_analyze(void);
403
404/**
405 * @} end defgroup profiling_apis
406 */
407
408/**
Allan Stephensc98da842016-11-11 15:45:03 -0500409 * @defgroup thread_apis Thread APIs
410 * @ingroup kernel_apis
411 * @{
412 */
413
Benjamin Walshed240f22017-01-22 13:05:08 -0500414#endif /* !_ASMLANGUAGE */
415
416
417/*
418 * Thread user options. May be needed by assembly code. Common part uses low
419 * bits, arch-specific use high bits.
420 */
421
422/* system thread that must not abort */
423#define K_ESSENTIAL (1 << 0)
424
425#if defined(CONFIG_FP_SHARING)
426/* thread uses floating point registers */
427#define K_FP_REGS (1 << 1)
428#endif
429
430#ifdef CONFIG_X86
431/* x86 Bitmask definitions for threads user options */
432
433#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
434/* thread uses SSEx (and also FP) registers */
435#define K_SSE_REGS (1 << 7)
436#endif
437#endif
438
439/* end - thread options */
440
441#if !defined(_ASMLANGUAGE)
442
Andrew Boie507852a2017-07-25 18:47:07 -0700443/* Using typedef deliberately here, this is quite intended to be an opaque
444 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
445 *
446 * The purpose of this data type is to clearly distinguish between the
447 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
448 * buffer which composes the stack data actually used by the underlying
449 * thread; they cannot be used interchangably as some arches precede the
450 * stack buffer region with guard areas that trigger a MPU or MMU fault
451 * if written to.
452 *
453 * APIs that want to work with the buffer inside should continue to use
454 * char *.
455 *
456 * Stacks should always be created with K_THREAD_STACK_DEFINE().
457 */
458struct __packed _k_thread_stack_element {
459 char data;
460};
461typedef struct _k_thread_stack_element *k_thread_stack_t;
462
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400463
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400464/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700465 * @brief Create a thread.
466 *
467 * This routine initializes a thread, then schedules it for execution.
468 *
469 * The new thread may be scheduled for immediate execution or a delayed start.
470 * If the newly spawned thread does not have a delayed start the kernel
471 * scheduler may preempt the current thread to allow the new thread to
472 * execute.
473 *
474 * Thread options are architecture-specific, and can include K_ESSENTIAL,
475 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
476 * them using "|" (the logical OR operator).
477 *
478 * Historically, users often would use the beginning of the stack memory region
479 * to store the struct k_thread data, although corruption will occur if the
480 * stack overflows this region and stack protection features may not detect this
481 * situation.
482 *
483 * @param new_thread Pointer to uninitialized struct k_thread
484 * @param stack Pointer to the stack space.
485 * @param stack_size Stack size in bytes.
486 * @param entry Thread entry function.
487 * @param p1 1st entry point parameter.
488 * @param p2 2nd entry point parameter.
489 * @param p3 3rd entry point parameter.
490 * @param prio Thread priority.
491 * @param options Thread options.
492 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
493 *
494 * @return ID of new thread.
495 */
Andrew Boie507852a2017-07-25 18:47:07 -0700496extern k_tid_t k_thread_create(struct k_thread *new_thread,
497 k_thread_stack_t stack,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700498 size_t stack_size,
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700499 k_thread_entry_t entry,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700500 void *p1, void *p2, void *p3,
501 int prio, u32_t options, s32_t delay);
502
503/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500504 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400505 *
Allan Stephensc98da842016-11-11 15:45:03 -0500506 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500507 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400508 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500509 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400510 *
511 * @return N/A
512 */
Kumar Galacc334c72017-04-21 10:55:34 -0500513extern void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400514
515/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500516 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400517 *
518 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500519 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400520 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400521 * @return N/A
522 */
Kumar Galacc334c72017-04-21 10:55:34 -0500523extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400524
525/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500526 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400527 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500528 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400529 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500530 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400531 *
532 * @return N/A
533 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400534extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400535
536/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500537 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400538 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500539 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400540 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500541 * If @a thread is not currently sleeping, the routine has no effect.
542 *
543 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400544 *
545 * @return N/A
546 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400547extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400548
549/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500550 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400551 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500552 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400553 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400554extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400555
556/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500557 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400558 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500559 * This routine prevents @a thread from executing if it has not yet started
560 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400561 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500562 * @param thread ID of thread to cancel.
563 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700564 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500565 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400566 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400567extern int k_thread_cancel(k_tid_t thread);
568
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400569/**
Allan Stephensc98da842016-11-11 15:45:03 -0500570 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400571 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500572 * This routine permanently stops execution of @a thread. The thread is taken
573 * off all kernel queues it is part of (i.e. the ready queue, the timeout
574 * queue, or a kernel object wait queue). However, any kernel resources the
575 * thread might currently own (such as mutexes or memory blocks) are not
576 * released. It is the responsibility of the caller of this routine to ensure
577 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400578 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500579 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400580 *
581 * @return N/A
582 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400583extern void k_thread_abort(k_tid_t thread);
584
Andrew Boie7d627c52017-08-30 11:01:56 -0700585
586/**
587 * @brief Start an inactive thread
588 *
589 * If a thread was created with K_FOREVER in the delay parameter, it will
590 * not be added to the scheduling queue until this function is called
591 * on it.
592 *
593 * @param thread thread to start
594 */
595extern void k_thread_start(k_tid_t thread);
596
Allan Stephensc98da842016-11-11 15:45:03 -0500597/**
598 * @cond INTERNAL_HIDDEN
599 */
600
Benjamin Walshd211a522016-12-06 11:44:01 -0500601/* timeout has timed out and is not on _timeout_q anymore */
602#define _EXPIRED (-2)
603
604/* timeout is not in use */
605#define _INACTIVE (-1)
606
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400607struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700608 struct k_thread *init_thread;
Andrew Boie507852a2017-07-25 18:47:07 -0700609 k_thread_stack_t init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400610 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700611 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500612 void *init_p1;
613 void *init_p2;
614 void *init_p3;
615 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500616 u32_t init_options;
617 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500618 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500619 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400620};
621
Andrew Boied26cf2d2017-03-30 13:07:02 -0700622#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400623 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500624 prio, options, delay, abort, groups) \
625 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700626 .init_thread = (thread), \
627 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500628 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700629 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400630 .init_p1 = (void *)p1, \
631 .init_p2 = (void *)p2, \
632 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500633 .init_prio = (prio), \
634 .init_options = (options), \
635 .init_delay = (delay), \
636 .init_abort = (abort), \
637 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400638 }
639
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400640/**
Allan Stephensc98da842016-11-11 15:45:03 -0500641 * INTERNAL_HIDDEN @endcond
642 */
643
644/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500645 * @brief Statically define and initialize a thread.
646 *
647 * The thread may be scheduled for immediate execution or a delayed start.
648 *
649 * Thread options are architecture-specific, and can include K_ESSENTIAL,
650 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
651 * them using "|" (the logical OR operator).
652 *
653 * The ID of the thread can be accessed using:
654 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500655 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500656 *
657 * @param name Name of the thread.
658 * @param stack_size Stack size in bytes.
659 * @param entry Thread entry function.
660 * @param p1 1st entry point parameter.
661 * @param p2 2nd entry point parameter.
662 * @param p3 3rd entry point parameter.
663 * @param prio Thread priority.
664 * @param options Thread options.
665 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400666 *
667 * @internal It has been observed that the x86 compiler by default aligns
668 * these _static_thread_data structures to 32-byte boundaries, thereby
669 * wasting space. To work around this, force a 4-byte alignment.
670 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500671#define K_THREAD_DEFINE(name, stack_size, \
672 entry, p1, p2, p3, \
673 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700674 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700675 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500676 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500677 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700678 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
679 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500680 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700681 NULL, 0); \
682 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400683
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400684/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500685 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400686 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500687 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400688 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500689 * @param thread ID of thread whose priority is needed.
690 *
691 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400692 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500693extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400694
695/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500696 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400697 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500698 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400699 *
700 * Rescheduling can occur immediately depending on the priority @a thread is
701 * set to:
702 *
703 * - If its priority is raised above the priority of the caller of this
704 * function, and the caller is preemptible, @a thread will be scheduled in.
705 *
706 * - If the caller operates on itself, it lowers its priority below that of
707 * other threads in the system, and the caller is preemptible, the thread of
708 * highest priority will be scheduled in.
709 *
710 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
711 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
712 * highest priority.
713 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500714 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400715 * @param prio New priority.
716 *
717 * @warning Changing the priority of a thread currently involved in mutex
718 * priority inheritance may result in undefined behavior.
719 *
720 * @return N/A
721 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400722extern void k_thread_priority_set(k_tid_t thread, int prio);
723
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400724/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500725 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400726 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500727 * This routine prevents the kernel scheduler from making @a thread the
728 * current thread. All other internal operations on @a thread are still
729 * performed; for example, any timeout it is waiting on keeps ticking,
730 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400731 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500732 * If @a thread is already suspended, the routine has no effect.
733 *
734 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400735 *
736 * @return N/A
737 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400738extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400739
740/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500741 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400742 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500743 * This routine allows the kernel scheduler to make @a thread the current
744 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400745 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500746 * If @a thread is not currently suspended, the routine has no effect.
747 *
748 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400749 *
750 * @return N/A
751 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400752extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400753
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400754/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500755 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400756 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500757 * This routine specifies how the scheduler will perform time slicing of
758 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400759 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500760 * To enable time slicing, @a slice must be non-zero. The scheduler
761 * ensures that no thread runs for more than the specified time limit
762 * before other threads of that priority are given a chance to execute.
763 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700764 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400765 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500766 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400767 * execute. Once the scheduler selects a thread for execution, there is no
768 * minimum guaranteed time the thread will execute before threads of greater or
769 * equal priority are scheduled.
770 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500771 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400772 * for execution, this routine has no effect; the thread is immediately
773 * rescheduled after the slice period expires.
774 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500775 * To disable timeslicing, set both @a slice and @a prio to zero.
776 *
777 * @param slice Maximum time slice length (in milliseconds).
778 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400779 *
780 * @return N/A
781 */
Kumar Galacc334c72017-04-21 10:55:34 -0500782extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400783
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400784/**
Allan Stephensc98da842016-11-11 15:45:03 -0500785 * @} end defgroup thread_apis
786 */
787
788/**
789 * @addtogroup isr_apis
790 * @{
791 */
792
793/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500794 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400795 *
Allan Stephensc98da842016-11-11 15:45:03 -0500796 * This routine allows the caller to customize its actions, depending on
797 * whether it is a thread or an ISR.
798 *
799 * @note Can be called by ISRs.
800 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500801 * @return 0 if invoked by a thread.
802 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400803 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500804extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400805
Benjamin Walsh445830d2016-11-10 15:54:27 -0500806/**
807 * @brief Determine if code is running in a preemptible thread.
808 *
Allan Stephensc98da842016-11-11 15:45:03 -0500809 * This routine allows the caller to customize its actions, depending on
810 * whether it can be preempted by another thread. The routine returns a 'true'
811 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500812 *
Allan Stephensc98da842016-11-11 15:45:03 -0500813 * - The code is running in a thread, not at ISR.
814 * - The thread's priority is in the preemptible range.
815 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500816 *
Allan Stephensc98da842016-11-11 15:45:03 -0500817 * @note Can be called by ISRs.
818 *
819 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500820 * @return Non-zero if invoked by a preemptible thread.
821 */
822extern int k_is_preempt_thread(void);
823
Allan Stephensc98da842016-11-11 15:45:03 -0500824/**
825 * @} end addtogroup isr_apis
826 */
827
828/**
829 * @addtogroup thread_apis
830 * @{
831 */
832
833/**
834 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500835 *
Allan Stephensc98da842016-11-11 15:45:03 -0500836 * This routine prevents the current thread from being preempted by another
837 * thread by instructing the scheduler to treat it as a cooperative thread.
838 * If the thread subsequently performs an operation that makes it unready,
839 * it will be context switched out in the normal manner. When the thread
840 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500841 *
Allan Stephensc98da842016-11-11 15:45:03 -0500842 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500843 *
Allan Stephensc98da842016-11-11 15:45:03 -0500844 * @note k_sched_lock() and k_sched_unlock() should normally be used
845 * when the operation being performed can be safely interrupted by ISRs.
846 * However, if the amount of processing involved is very small, better
847 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500848 *
849 * @return N/A
850 */
851extern void k_sched_lock(void);
852
Allan Stephensc98da842016-11-11 15:45:03 -0500853/**
854 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500855 *
Allan Stephensc98da842016-11-11 15:45:03 -0500856 * This routine reverses the effect of a previous call to k_sched_lock().
857 * A thread must call the routine once for each time it called k_sched_lock()
858 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500859 *
860 * @return N/A
861 */
862extern void k_sched_unlock(void);
863
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400864/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500865 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500867 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400868 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500869 * Custom data is not used by the kernel itself, and is freely available
870 * for a thread to use as it sees fit. It can be used as a framework
871 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400872 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500873 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400874 *
875 * @return N/A
876 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400877extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400878
879/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500880 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500882 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500884 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400885 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400886extern void *k_thread_custom_data_get(void);
887
888/**
Allan Stephensc98da842016-11-11 15:45:03 -0500889 * @} end addtogroup thread_apis
890 */
891
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400892#include <sys_clock.h>
893
Allan Stephensc2f15a42016-11-17 12:24:22 -0500894/**
895 * @addtogroup clock_apis
896 * @{
897 */
898
899/**
900 * @brief Generate null timeout delay.
901 *
902 * This macro generates a timeout delay that that instructs a kernel API
903 * not to wait if the requested operation cannot be performed immediately.
904 *
905 * @return Timeout delay value.
906 */
907#define K_NO_WAIT 0
908
909/**
910 * @brief Generate timeout delay from milliseconds.
911 *
912 * This macro generates a timeout delay that that instructs a kernel API
913 * to wait up to @a ms milliseconds to perform the requested operation.
914 *
915 * @param ms Duration in milliseconds.
916 *
917 * @return Timeout delay value.
918 */
Johan Hedberg14471692016-11-13 10:52:15 +0200919#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500920
921/**
922 * @brief Generate timeout delay from seconds.
923 *
924 * This macro generates a timeout delay that that instructs a kernel API
925 * to wait up to @a s seconds to perform the requested operation.
926 *
927 * @param s Duration in seconds.
928 *
929 * @return Timeout delay value.
930 */
Johan Hedberg14471692016-11-13 10:52:15 +0200931#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500932
933/**
934 * @brief Generate timeout delay from minutes.
935 *
936 * This macro generates a timeout delay that that instructs a kernel API
937 * to wait up to @a m minutes to perform the requested operation.
938 *
939 * @param m Duration in minutes.
940 *
941 * @return Timeout delay value.
942 */
Johan Hedberg14471692016-11-13 10:52:15 +0200943#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500944
945/**
946 * @brief Generate timeout delay from hours.
947 *
948 * This macro generates a timeout delay that that instructs a kernel API
949 * to wait up to @a h hours to perform the requested operation.
950 *
951 * @param h Duration in hours.
952 *
953 * @return Timeout delay value.
954 */
Johan Hedberg14471692016-11-13 10:52:15 +0200955#define K_HOURS(h) K_MINUTES((h) * 60)
956
Allan Stephensc98da842016-11-11 15:45:03 -0500957/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500958 * @brief Generate infinite timeout delay.
959 *
960 * This macro generates a timeout delay that that instructs a kernel API
961 * to wait as long as necessary to perform the requested operation.
962 *
963 * @return Timeout delay value.
964 */
965#define K_FOREVER (-1)
966
967/**
968 * @} end addtogroup clock_apis
969 */
970
971/**
Allan Stephensc98da842016-11-11 15:45:03 -0500972 * @cond INTERNAL_HIDDEN
973 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400974
Benjamin Walsh62092182016-12-20 14:39:08 -0500975/* kernel clocks */
976
977#if (sys_clock_ticks_per_sec == 1000) || \
978 (sys_clock_ticks_per_sec == 500) || \
979 (sys_clock_ticks_per_sec == 250) || \
980 (sys_clock_ticks_per_sec == 125) || \
981 (sys_clock_ticks_per_sec == 100) || \
982 (sys_clock_ticks_per_sec == 50) || \
983 (sys_clock_ticks_per_sec == 25) || \
984 (sys_clock_ticks_per_sec == 20) || \
985 (sys_clock_ticks_per_sec == 10) || \
986 (sys_clock_ticks_per_sec == 1)
987
988 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
989#else
990 /* yields horrible 64-bit math on many architectures: try to avoid */
991 #define _NON_OPTIMIZED_TICKS_PER_SEC
992#endif
993
994#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -0500995extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -0500996#else
Kumar Galacc334c72017-04-21 10:55:34 -0500997static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -0500998{
Kumar Galacc334c72017-04-21 10:55:34 -0500999 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001000}
1001#endif
1002
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001003/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001004#ifdef CONFIG_TICKLESS_KERNEL
1005#define _TICK_ALIGN 0
1006#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001007#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001008#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001009
Kumar Galacc334c72017-04-21 10:55:34 -05001010static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001011{
Benjamin Walsh62092182016-12-20 14:39:08 -05001012#ifdef CONFIG_SYS_CLOCK_EXISTS
1013
1014#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001015 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001016#else
Kumar Galacc334c72017-04-21 10:55:34 -05001017 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001018#endif
1019
1020#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001021 __ASSERT(ticks == 0, "");
1022 return 0;
1023#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001024}
1025
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001026struct k_timer {
1027 /*
1028 * _timeout structure must be first here if we want to use
1029 * dynamic timer allocation. timeout.node is used in the double-linked
1030 * list of free timers
1031 */
1032 struct _timeout timeout;
1033
Allan Stephens45bfa372016-10-12 12:39:42 -05001034 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001035 _wait_q_t wait_q;
1036
1037 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001038 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001039
1040 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001041 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001042
1043 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001044 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001045
Allan Stephens45bfa372016-10-12 12:39:42 -05001046 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001047 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001048
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001049 /* user-specific data, also used to support legacy features */
1050 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001051
Anas Nashif2f203c22016-12-18 06:57:45 -05001052 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001053};
1054
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001055#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001056 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001057 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001058 .timeout.wait_q = NULL, \
1059 .timeout.thread = NULL, \
1060 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001061 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001062 .expiry_fn = expiry, \
1063 .stop_fn = stop, \
1064 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001065 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001066 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001067 }
1068
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001069#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1070
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001071/**
Allan Stephensc98da842016-11-11 15:45:03 -05001072 * INTERNAL_HIDDEN @endcond
1073 */
1074
1075/**
1076 * @defgroup timer_apis Timer APIs
1077 * @ingroup kernel_apis
1078 * @{
1079 */
1080
1081/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001082 * @typedef k_timer_expiry_t
1083 * @brief Timer expiry function type.
1084 *
1085 * A timer's expiry function is executed by the system clock interrupt handler
1086 * each time the timer expires. The expiry function is optional, and is only
1087 * invoked if the timer has been initialized with one.
1088 *
1089 * @param timer Address of timer.
1090 *
1091 * @return N/A
1092 */
1093typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1094
1095/**
1096 * @typedef k_timer_stop_t
1097 * @brief Timer stop function type.
1098 *
1099 * A timer's stop function is executed if the timer is stopped prematurely.
1100 * The function runs in the context of the thread that stops the timer.
1101 * The stop function is optional, and is only invoked if the timer has been
1102 * initialized with one.
1103 *
1104 * @param timer Address of timer.
1105 *
1106 * @return N/A
1107 */
1108typedef void (*k_timer_stop_t)(struct k_timer *timer);
1109
1110/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001111 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001112 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001113 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001114 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001115 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001116 *
1117 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001118 * @param expiry_fn Function to invoke each time the timer expires.
1119 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001120 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001121#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001122 struct k_timer name \
1123 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001124 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001125
Allan Stephens45bfa372016-10-12 12:39:42 -05001126/**
1127 * @brief Initialize a timer.
1128 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001129 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001130 *
1131 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001132 * @param expiry_fn Function to invoke each time the timer expires.
1133 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001134 *
1135 * @return N/A
1136 */
1137extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001138 k_timer_expiry_t expiry_fn,
1139 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001140
Allan Stephens45bfa372016-10-12 12:39:42 -05001141/**
1142 * @brief Start a timer.
1143 *
1144 * This routine starts a timer, and resets its status to zero. The timer
1145 * begins counting down using the specified duration and period values.
1146 *
1147 * Attempting to start a timer that is already running is permitted.
1148 * The timer's status is reset to zero and the timer begins counting down
1149 * using the new duration and period values.
1150 *
1151 * @param timer Address of timer.
1152 * @param duration Initial timer duration (in milliseconds).
1153 * @param period Timer period (in milliseconds).
1154 *
1155 * @return N/A
1156 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001157extern void k_timer_start(struct k_timer *timer,
Kumar Galacc334c72017-04-21 10:55:34 -05001158 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001159
1160/**
1161 * @brief Stop a timer.
1162 *
1163 * This routine stops a running timer prematurely. The timer's stop function,
1164 * if one exists, is invoked by the caller.
1165 *
1166 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001167 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001168 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001169 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1170 * if @a k_timer_stop is to be called from ISRs.
1171 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001172 * @param timer Address of timer.
1173 *
1174 * @return N/A
1175 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001176extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001177
1178/**
1179 * @brief Read timer status.
1180 *
1181 * This routine reads the timer's status, which indicates the number of times
1182 * it has expired since its status was last read.
1183 *
1184 * Calling this routine resets the timer's status to zero.
1185 *
1186 * @param timer Address of timer.
1187 *
1188 * @return Timer status.
1189 */
Kumar Galacc334c72017-04-21 10:55:34 -05001190extern u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001191
1192/**
1193 * @brief Synchronize thread to timer expiration.
1194 *
1195 * This routine blocks the calling thread until the timer's status is non-zero
1196 * (indicating that it has expired at least once since it was last examined)
1197 * or the timer is stopped. If the timer status is already non-zero,
1198 * or the timer is already stopped, the caller continues without waiting.
1199 *
1200 * Calling this routine resets the timer's status to zero.
1201 *
1202 * This routine must not be used by interrupt handlers, since they are not
1203 * allowed to block.
1204 *
1205 * @param timer Address of timer.
1206 *
1207 * @return Timer status.
1208 */
Kumar Galacc334c72017-04-21 10:55:34 -05001209extern u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001210
1211/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001212 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001213 *
1214 * This routine computes the (approximate) time remaining before a running
1215 * timer next expires. If the timer is not running, it returns zero.
1216 *
1217 * @param timer Address of timer.
1218 *
1219 * @return Remaining time (in milliseconds).
1220 */
Kumar Galacc334c72017-04-21 10:55:34 -05001221static inline s32_t k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001222{
1223 return _timeout_remaining_get(&timer->timeout);
1224}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001225
Allan Stephensc98da842016-11-11 15:45:03 -05001226/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001227 * @brief Associate user-specific data with a timer.
1228 *
1229 * This routine records the @a user_data with the @a timer, to be retrieved
1230 * later.
1231 *
1232 * It can be used e.g. in a timer handler shared across multiple subsystems to
1233 * retrieve data specific to the subsystem this timer is associated with.
1234 *
1235 * @param timer Address of timer.
1236 * @param user_data User data to associate with the timer.
1237 *
1238 * @return N/A
1239 */
1240static inline void k_timer_user_data_set(struct k_timer *timer,
1241 void *user_data)
1242{
1243 timer->user_data = user_data;
1244}
1245
1246/**
1247 * @brief Retrieve the user-specific data from a timer.
1248 *
1249 * @param timer Address of timer.
1250 *
1251 * @return The user data.
1252 */
1253static inline void *k_timer_user_data_get(struct k_timer *timer)
1254{
1255 return timer->user_data;
1256}
1257
1258/**
Allan Stephensc98da842016-11-11 15:45:03 -05001259 * @} end defgroup timer_apis
1260 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001261
Allan Stephensc98da842016-11-11 15:45:03 -05001262/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001263 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001264 * @{
1265 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001266
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001267/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001268 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001269 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001270 * This routine returns the elapsed time since the system booted,
1271 * in milliseconds.
1272 *
1273 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001274 */
Kumar Galacc334c72017-04-21 10:55:34 -05001275extern s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001276
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001277#ifdef CONFIG_TICKLESS_KERNEL
1278/**
1279 * @brief Enable clock always on in tickless kernel
1280 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001281 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001282 * there are no timer events programmed in tickless kernel
1283 * scheduling. This is necessary if the clock is used to track
1284 * passage of time.
1285 *
1286 * @retval prev_status Previous status of always on flag
1287 */
1288static inline int k_enable_sys_clock_always_on(void)
1289{
1290 int prev_status = _sys_clock_always_on;
1291
1292 _sys_clock_always_on = 1;
1293 _enable_sys_clock();
1294
1295 return prev_status;
1296}
1297
1298/**
1299 * @brief Disable clock always on in tickless kernel
1300 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001301 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001302 * there are no timer events programmed in tickless kernel
1303 * scheduling. To save power, this routine should be called
1304 * immediately when clock is not used to track time.
1305 */
1306static inline void k_disable_sys_clock_always_on(void)
1307{
1308 _sys_clock_always_on = 0;
1309}
1310#else
1311#define k_enable_sys_clock_always_on() do { } while ((0))
1312#define k_disable_sys_clock_always_on() do { } while ((0))
1313#endif
1314
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001315/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001316 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001317 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001318 * This routine returns the lower 32-bits of the elapsed time since the system
1319 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001320 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001321 * This routine can be more efficient than k_uptime_get(), as it reduces the
1322 * need for interrupt locking and 64-bit math. However, the 32-bit result
1323 * cannot hold a system uptime time larger than approximately 50 days, so the
1324 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001325 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001326 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001327 */
Kumar Galacc334c72017-04-21 10:55:34 -05001328extern u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001329
1330/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001331 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001332 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001333 * This routine computes the elapsed time between the current system uptime
1334 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001335 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001336 * @param reftime Pointer to a reference time, which is updated to the current
1337 * uptime upon return.
1338 *
1339 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001340 */
Kumar Galacc334c72017-04-21 10:55:34 -05001341extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001342
1343/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001344 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001345 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001346 * This routine computes the elapsed time between the current system uptime
1347 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001348 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001349 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1350 * need for interrupt locking and 64-bit math. However, the 32-bit result
1351 * cannot hold an elapsed time larger than approximately 50 days, so the
1352 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001353 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001354 * @param reftime Pointer to a reference time, which is updated to the current
1355 * uptime upon return.
1356 *
1357 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001358 */
Kumar Galacc334c72017-04-21 10:55:34 -05001359extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001360
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001361/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001362 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001363 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001364 * This routine returns the current time, as measured by the system's hardware
1365 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001366 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001367 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001368 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001369#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001370
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001371/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001372 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001373 */
1374
Allan Stephensc98da842016-11-11 15:45:03 -05001375/**
1376 * @cond INTERNAL_HIDDEN
1377 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001378
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001379struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001380 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001381 union {
1382 _wait_q_t wait_q;
1383
1384 _POLL_EVENT;
1385 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001386
1387 _OBJECT_TRACING_NEXT_PTR(k_queue);
1388};
1389
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001390#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001391 { \
1392 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1393 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001394 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001395 _OBJECT_TRACING_INIT \
1396 }
1397
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001398#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1399
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001400/**
1401 * INTERNAL_HIDDEN @endcond
1402 */
1403
1404/**
1405 * @defgroup queue_apis Queue APIs
1406 * @ingroup kernel_apis
1407 * @{
1408 */
1409
1410/**
1411 * @brief Initialize a queue.
1412 *
1413 * This routine initializes a queue object, prior to its first use.
1414 *
1415 * @param queue Address of the queue.
1416 *
1417 * @return N/A
1418 */
1419extern void k_queue_init(struct k_queue *queue);
1420
1421/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001422 * @brief Cancel waiting on a queue.
1423 *
1424 * This routine causes first thread pending on @a queue, if any, to
1425 * return from k_queue_get() call with NULL value (as if timeout expired).
1426 *
1427 * @note Can be called by ISRs.
1428 *
1429 * @param queue Address of the queue.
1430 *
1431 * @return N/A
1432 */
1433extern void k_queue_cancel_wait(struct k_queue *queue);
1434
1435/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001436 * @brief Append an element to the end of a queue.
1437 *
1438 * This routine appends a data item to @a queue. A queue data item must be
1439 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1440 * reserved for the kernel's use.
1441 *
1442 * @note Can be called by ISRs.
1443 *
1444 * @param queue Address of the queue.
1445 * @param data Address of the data item.
1446 *
1447 * @return N/A
1448 */
1449extern void k_queue_append(struct k_queue *queue, void *data);
1450
1451/**
1452 * @brief Prepend an element to a queue.
1453 *
1454 * This routine prepends a data item to @a queue. A queue data item must be
1455 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1456 * reserved for the kernel's use.
1457 *
1458 * @note Can be called by ISRs.
1459 *
1460 * @param queue Address of the queue.
1461 * @param data Address of the data item.
1462 *
1463 * @return N/A
1464 */
1465extern void k_queue_prepend(struct k_queue *queue, void *data);
1466
1467/**
1468 * @brief Inserts an element to a queue.
1469 *
1470 * This routine inserts a data item to @a queue after previous item. A queue
1471 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1472 * item are reserved for the kernel's use.
1473 *
1474 * @note Can be called by ISRs.
1475 *
1476 * @param queue Address of the queue.
1477 * @param prev Address of the previous data item.
1478 * @param data Address of the data item.
1479 *
1480 * @return N/A
1481 */
1482extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1483
1484/**
1485 * @brief Atomically append a list of elements to a queue.
1486 *
1487 * This routine adds a list of data items to @a queue in one operation.
1488 * The data items must be in a singly-linked list, with the first 32 bits
1489 * in each data item pointing to the next data item; the list must be
1490 * NULL-terminated.
1491 *
1492 * @note Can be called by ISRs.
1493 *
1494 * @param queue Address of the queue.
1495 * @param head Pointer to first node in singly-linked list.
1496 * @param tail Pointer to last node in singly-linked list.
1497 *
1498 * @return N/A
1499 */
1500extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1501
1502/**
1503 * @brief Atomically add a list of elements to a queue.
1504 *
1505 * This routine adds a list of data items to @a queue in one operation.
1506 * The data items must be in a singly-linked list implemented using a
1507 * sys_slist_t object. Upon completion, the original list is empty.
1508 *
1509 * @note Can be called by ISRs.
1510 *
1511 * @param queue Address of the queue.
1512 * @param list Pointer to sys_slist_t object.
1513 *
1514 * @return N/A
1515 */
1516extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1517
1518/**
1519 * @brief Get an element from a queue.
1520 *
1521 * This routine removes first data item from @a queue. The first 32 bits of the
1522 * data item are reserved for the kernel's use.
1523 *
1524 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1525 *
1526 * @param queue Address of the queue.
1527 * @param timeout Waiting period to obtain a data item (in milliseconds),
1528 * or one of the special values K_NO_WAIT and K_FOREVER.
1529 *
1530 * @return Address of the data item if successful; NULL if returned
1531 * without waiting, or waiting period timed out.
1532 */
Kumar Galacc334c72017-04-21 10:55:34 -05001533extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001534
1535/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001536 * @brief Remove an element from a queue.
1537 *
1538 * This routine removes data item from @a queue. The first 32 bits of the
1539 * data item are reserved for the kernel's use. Removing elements from k_queue
1540 * rely on sys_slist_find_and_remove which is not a constant time operation.
1541 *
1542 * @note Can be called by ISRs
1543 *
1544 * @param queue Address of the queue.
1545 * @param data Address of the data item.
1546 *
1547 * @return true if data item was removed
1548 */
1549static inline bool k_queue_remove(struct k_queue *queue, void *data)
1550{
1551 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1552}
1553
1554/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001555 * @brief Query a queue to see if it has data available.
1556 *
1557 * Note that the data might be already gone by the time this function returns
1558 * if other threads are also trying to read from the queue.
1559 *
1560 * @note Can be called by ISRs.
1561 *
1562 * @param queue Address of the queue.
1563 *
1564 * @return Non-zero if the queue is empty.
1565 * @return 0 if data is available.
1566 */
1567static inline int k_queue_is_empty(struct k_queue *queue)
1568{
1569 return (int)sys_slist_is_empty(&queue->data_q);
1570}
1571
1572/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001573 * @brief Peek element at the head of queue.
1574 *
1575 * Return element from the head of queue without removing it.
1576 *
1577 * @param queue Address of the queue.
1578 *
1579 * @return Head element, or NULL if queue is empty.
1580 */
1581static inline void *k_queue_peek_head(struct k_queue *queue)
1582{
1583 return sys_slist_peek_head(&queue->data_q);
1584}
1585
1586/**
1587 * @brief Peek element at the tail of queue.
1588 *
1589 * Return element from the tail of queue without removing it.
1590 *
1591 * @param queue Address of the queue.
1592 *
1593 * @return Tail element, or NULL if queue is empty.
1594 */
1595static inline void *k_queue_peek_tail(struct k_queue *queue)
1596{
1597 return sys_slist_peek_tail(&queue->data_q);
1598}
1599
1600/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001601 * @brief Statically define and initialize a queue.
1602 *
1603 * The queue can be accessed outside the module where it is defined using:
1604 *
1605 * @code extern struct k_queue <name>; @endcode
1606 *
1607 * @param name Name of the queue.
1608 */
1609#define K_QUEUE_DEFINE(name) \
1610 struct k_queue name \
1611 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001612 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001613
1614/**
1615 * @} end defgroup queue_apis
1616 */
1617
1618/**
1619 * @cond INTERNAL_HIDDEN
1620 */
1621
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001622struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001623 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001624};
1625
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001626#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001627 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001628 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001629 }
1630
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001631#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1632
Allan Stephensc98da842016-11-11 15:45:03 -05001633/**
1634 * INTERNAL_HIDDEN @endcond
1635 */
1636
1637/**
1638 * @defgroup fifo_apis Fifo APIs
1639 * @ingroup kernel_apis
1640 * @{
1641 */
1642
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001643/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001644 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001645 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001646 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001647 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001648 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001649 *
1650 * @return N/A
1651 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001652#define k_fifo_init(fifo) \
1653 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001654
1655/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001656 * @brief Cancel waiting on a fifo.
1657 *
1658 * This routine causes first thread pending on @a fifo, if any, to
1659 * return from k_fifo_get() call with NULL value (as if timeout
1660 * expired).
1661 *
1662 * @note Can be called by ISRs.
1663 *
1664 * @param fifo Address of the fifo.
1665 *
1666 * @return N/A
1667 */
1668#define k_fifo_cancel_wait(fifo) \
1669 k_queue_cancel_wait((struct k_queue *) fifo)
1670
1671/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001672 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001673 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001674 * This routine adds a data item to @a fifo. A fifo data item must be
1675 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1676 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001677 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001678 * @note Can be called by ISRs.
1679 *
1680 * @param fifo Address of the fifo.
1681 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001682 *
1683 * @return N/A
1684 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001685#define k_fifo_put(fifo, data) \
1686 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001687
1688/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001689 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001690 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001691 * This routine adds a list of data items to @a fifo in one operation.
1692 * The data items must be in a singly-linked list, with the first 32 bits
1693 * each data item pointing to the next data item; the list must be
1694 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001695 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001696 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001697 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001698 * @param fifo Address of the fifo.
1699 * @param head Pointer to first node in singly-linked list.
1700 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001701 *
1702 * @return N/A
1703 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001704#define k_fifo_put_list(fifo, head, tail) \
1705 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001706
1707/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001708 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001709 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001710 * This routine adds a list of data items to @a fifo in one operation.
1711 * The data items must be in a singly-linked list implemented using a
1712 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001713 * and must be re-initialized via sys_slist_init().
1714 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001715 * @note Can be called by ISRs.
1716 *
1717 * @param fifo Address of the fifo.
1718 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001719 *
1720 * @return N/A
1721 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001722#define k_fifo_put_slist(fifo, list) \
1723 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001724
1725/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001726 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001727 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001728 * This routine removes a data item from @a fifo in a "first in, first out"
1729 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001730 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001731 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1732 *
1733 * @param fifo Address of the fifo.
1734 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001735 * or one of the special values K_NO_WAIT and K_FOREVER.
1736 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001737 * @return Address of the data item if successful; NULL if returned
1738 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001739 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001740#define k_fifo_get(fifo, timeout) \
1741 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001742
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001743/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001744 * @brief Query a fifo to see if it has data available.
1745 *
1746 * Note that the data might be already gone by the time this function returns
1747 * if other threads is also trying to read from the fifo.
1748 *
1749 * @note Can be called by ISRs.
1750 *
1751 * @param fifo Address of the fifo.
1752 *
1753 * @return Non-zero if the fifo is empty.
1754 * @return 0 if data is available.
1755 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001756#define k_fifo_is_empty(fifo) \
1757 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001758
1759/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001760 * @brief Peek element at the head of fifo.
1761 *
1762 * Return element from the head of fifo without removing it. A usecase
1763 * for this is if elements of the fifo are themselves containers. Then
1764 * on each iteration of processing, a head container will be peeked,
1765 * and some data processed out of it, and only if the container is empty,
1766 * it will be completely remove from the fifo.
1767 *
1768 * @param fifo Address of the fifo.
1769 *
1770 * @return Head element, or NULL if the fifo is empty.
1771 */
1772#define k_fifo_peek_head(fifo) \
1773 k_queue_peek_head((struct k_queue *) fifo)
1774
1775/**
1776 * @brief Peek element at the tail of fifo.
1777 *
1778 * Return element from the tail of fifo (without removing it). A usecase
1779 * for this is if elements of the fifo are themselves containers. Then
1780 * it may be useful to add more data to the last container in fifo.
1781 *
1782 * @param fifo Address of the fifo.
1783 *
1784 * @return Tail element, or NULL if fifo is empty.
1785 */
1786#define k_fifo_peek_tail(fifo) \
1787 k_queue_peek_tail((struct k_queue *) fifo)
1788
1789/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001790 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001791 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001792 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001793 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001794 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001795 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001796 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001797 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001798#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001799 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001800 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001801 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001802
Allan Stephensc98da842016-11-11 15:45:03 -05001803/**
1804 * @} end defgroup fifo_apis
1805 */
1806
1807/**
1808 * @cond INTERNAL_HIDDEN
1809 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001810
1811struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001812 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001813};
1814
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001815#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001816 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001817 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001818 }
1819
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001820#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1821
Allan Stephensc98da842016-11-11 15:45:03 -05001822/**
1823 * INTERNAL_HIDDEN @endcond
1824 */
1825
1826/**
1827 * @defgroup lifo_apis Lifo APIs
1828 * @ingroup kernel_apis
1829 * @{
1830 */
1831
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001832/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001833 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001834 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001835 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001836 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001837 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001838 *
1839 * @return N/A
1840 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001841#define k_lifo_init(lifo) \
1842 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001843
1844/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001845 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001846 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001847 * This routine adds a data item to @a lifo. A lifo data item must be
1848 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1849 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001850 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001851 * @note Can be called by ISRs.
1852 *
1853 * @param lifo Address of the lifo.
1854 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001855 *
1856 * @return N/A
1857 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001858#define k_lifo_put(lifo, data) \
1859 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001860
1861/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001862 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001863 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001864 * This routine removes a data item from @a lifo in a "last in, first out"
1865 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001867 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1868 *
1869 * @param lifo Address of the lifo.
1870 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001871 * or one of the special values K_NO_WAIT and K_FOREVER.
1872 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001873 * @return Address of the data item if successful; NULL if returned
1874 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001875 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001876#define k_lifo_get(lifo, timeout) \
1877 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001878
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001879/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001880 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001882 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001883 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001884 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001885 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001886 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001887 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001888#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001889 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001890 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001891 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001892
Allan Stephensc98da842016-11-11 15:45:03 -05001893/**
1894 * @} end defgroup lifo_apis
1895 */
1896
1897/**
1898 * @cond INTERNAL_HIDDEN
1899 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001900
1901struct k_stack {
1902 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001903 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001904
Anas Nashif2f203c22016-12-18 06:57:45 -05001905 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001906};
1907
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001908#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05001909 { \
1910 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1911 .base = stack_buffer, \
1912 .next = stack_buffer, \
1913 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001914 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001915 }
1916
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001917#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
1918
Allan Stephensc98da842016-11-11 15:45:03 -05001919/**
1920 * INTERNAL_HIDDEN @endcond
1921 */
1922
1923/**
1924 * @defgroup stack_apis Stack APIs
1925 * @ingroup kernel_apis
1926 * @{
1927 */
1928
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001929/**
1930 * @brief Initialize a stack.
1931 *
1932 * This routine initializes a stack object, prior to its first use.
1933 *
1934 * @param stack Address of the stack.
1935 * @param buffer Address of array used to hold stacked values.
1936 * @param num_entries Maximum number of values that can be stacked.
1937 *
1938 * @return N/A
1939 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001940extern void k_stack_init(struct k_stack *stack,
Kumar Galacc334c72017-04-21 10:55:34 -05001941 u32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001942
1943/**
1944 * @brief Push an element onto a stack.
1945 *
1946 * This routine adds a 32-bit value @a data to @a stack.
1947 *
1948 * @note Can be called by ISRs.
1949 *
1950 * @param stack Address of the stack.
1951 * @param data Value to push onto the stack.
1952 *
1953 * @return N/A
1954 */
Kumar Galacc334c72017-04-21 10:55:34 -05001955extern void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001956
1957/**
1958 * @brief Pop an element from a stack.
1959 *
1960 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1961 * manner and stores the value in @a data.
1962 *
1963 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1964 *
1965 * @param stack Address of the stack.
1966 * @param data Address of area to hold the value popped from the stack.
1967 * @param timeout Waiting period to obtain a value (in milliseconds),
1968 * or one of the special values K_NO_WAIT and K_FOREVER.
1969 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001970 * @retval 0 Element popped from stack.
1971 * @retval -EBUSY Returned without waiting.
1972 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001973 */
Kumar Galacc334c72017-04-21 10:55:34 -05001974extern int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001975
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001976/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001977 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001978 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001979 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001980 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001981 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001983 * @param name Name of the stack.
1984 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001985 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001986#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05001987 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001988 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001989 struct k_stack name \
1990 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001991 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001992 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001993
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001994/**
Allan Stephensc98da842016-11-11 15:45:03 -05001995 * @} end defgroup stack_apis
1996 */
1997
Allan Stephens6bba9b02016-11-16 14:56:54 -05001998struct k_work;
1999
Allan Stephensc98da842016-11-11 15:45:03 -05002000/**
2001 * @defgroup workqueue_apis Workqueue Thread APIs
2002 * @ingroup kernel_apis
2003 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002004 */
2005
Allan Stephens6bba9b02016-11-16 14:56:54 -05002006/**
2007 * @typedef k_work_handler_t
2008 * @brief Work item handler function type.
2009 *
2010 * A work item's handler function is executed by a workqueue's thread
2011 * when the work item is processed by the workqueue.
2012 *
2013 * @param work Address of the work item.
2014 *
2015 * @return N/A
2016 */
2017typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002018
2019/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002020 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002021 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002022
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002023struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002024 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002025 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002026};
2027
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002028enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002029 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002030};
2031
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002032struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002033 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002034 k_work_handler_t handler;
2035 atomic_t flags[1];
2036};
2037
Allan Stephens6bba9b02016-11-16 14:56:54 -05002038struct k_delayed_work {
2039 struct k_work work;
2040 struct _timeout timeout;
2041 struct k_work_q *work_q;
2042};
2043
2044extern struct k_work_q k_sys_work_q;
2045
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002046/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002047 * INTERNAL_HIDDEN @endcond
2048 */
2049
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002050#define _K_WORK_INITIALIZER(work_handler) \
2051 { \
2052 ._reserved = NULL, \
2053 .handler = work_handler, \
2054 .flags = { 0 } \
2055 }
2056
2057#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2058
Allan Stephens6bba9b02016-11-16 14:56:54 -05002059/**
2060 * @brief Initialize a statically-defined work item.
2061 *
2062 * This macro can be used to initialize a statically-defined workqueue work
2063 * item, prior to its first use. For example,
2064 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002065 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002066 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002067 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002068 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002069 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002070#define K_WORK_DEFINE(work, work_handler) \
2071 struct k_work work \
2072 __in_section(_k_work, static, work) = \
2073 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002074
2075/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002076 * @brief Initialize a work item.
2077 *
2078 * This routine initializes a workqueue work item, prior to its first use.
2079 *
2080 * @param work Address of work item.
2081 * @param handler Function to invoke each time work item is processed.
2082 *
2083 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002084 */
2085static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2086{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002087 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002088 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002089 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002090}
2091
2092/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002093 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002094 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002095 * This routine submits work item @a work to be processed by workqueue
2096 * @a work_q. If the work item is already pending in the workqueue's queue
2097 * as a result of an earlier submission, this routine has no effect on the
2098 * work item. If the work item has already been processed, or is currently
2099 * being processed, its work is considered complete and the work item can be
2100 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002101 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002102 * @warning
2103 * A submitted work item must not be modified until it has been processed
2104 * by the workqueue.
2105 *
2106 * @note Can be called by ISRs.
2107 *
2108 * @param work_q Address of workqueue.
2109 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002110 *
2111 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002112 */
2113static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2114 struct k_work *work)
2115{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002116 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002117 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002118 }
2119}
2120
2121/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002122 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002123 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002124 * This routine indicates if work item @a work is pending in a workqueue's
2125 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002126 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002127 * @note Can be called by ISRs.
2128 *
2129 * @param work Address of work item.
2130 *
2131 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002132 */
2133static inline int k_work_pending(struct k_work *work)
2134{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002135 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002136}
2137
2138/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002139 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002140 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002141 * This routine starts workqueue @a work_q. The workqueue spawns its work
2142 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002143 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002144 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002145 * @param stack Pointer to work queue thread's stack space, as defined by
2146 * K_THREAD_STACK_DEFINE()
2147 * @param stack_size Size of the work queue thread's stack (in bytes), which
2148 * should either be the same constant passed to
2149 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002150 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002151 *
2152 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002153 */
Andrew Boie507852a2017-07-25 18:47:07 -07002154extern void k_work_q_start(struct k_work_q *work_q,
2155 k_thread_stack_t stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002156 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002157
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002158/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002159 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002160 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002161 * This routine initializes a workqueue delayed work item, prior to
2162 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002163 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002164 * @param work Address of delayed work item.
2165 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002166 *
2167 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002168 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002169extern void k_delayed_work_init(struct k_delayed_work *work,
2170 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002171
2172/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002173 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002174 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002175 * This routine schedules work item @a work to be processed by workqueue
2176 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002177 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002178 * Only when the countdown completes is the work item actually submitted to
2179 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002180 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002181 * Submitting a previously submitted delayed work item that is still
2182 * counting down cancels the existing submission and restarts the countdown
2183 * using the new delay. If the work item is currently pending on the
2184 * workqueue's queue because the countdown has completed it is too late to
2185 * resubmit the item, and resubmission fails without impacting the work item.
2186 * If the work item has already been processed, or is currently being processed,
2187 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002188 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002189 * @warning
2190 * A delayed work item must not be modified until it has been processed
2191 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002192 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002193 * @note Can be called by ISRs.
2194 *
2195 * @param work_q Address of workqueue.
2196 * @param work Address of delayed work item.
2197 * @param delay Delay before submitting the work item (in milliseconds).
2198 *
2199 * @retval 0 Work item countdown started.
2200 * @retval -EINPROGRESS Work item is already pending.
2201 * @retval -EINVAL Work item is being processed or has completed its work.
2202 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002203 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002204extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2205 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002206 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002207
2208/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002209 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002210 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002211 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002212 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002213 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002214 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002215 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002216 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002217 * @param work Address of delayed work item.
2218 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002219 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002220 * @retval -EINPROGRESS Work item is already pending.
2221 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002222 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002223extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002224
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002225/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002226 * @brief Submit a work item to the system workqueue.
2227 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002228 * This routine submits work item @a work to be processed by the system
2229 * workqueue. If the work item is already pending in the workqueue's queue
2230 * as a result of an earlier submission, this routine has no effect on the
2231 * work item. If the work item has already been processed, or is currently
2232 * being processed, its work is considered complete and the work item can be
2233 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002234 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002235 * @warning
2236 * Work items submitted to the system workqueue should avoid using handlers
2237 * that block or yield since this may prevent the system workqueue from
2238 * processing other work items in a timely manner.
2239 *
2240 * @note Can be called by ISRs.
2241 *
2242 * @param work Address of work item.
2243 *
2244 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002245 */
2246static inline void k_work_submit(struct k_work *work)
2247{
2248 k_work_submit_to_queue(&k_sys_work_q, work);
2249}
2250
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002251/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002252 * @brief Submit a delayed work item to the system workqueue.
2253 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002254 * This routine schedules work item @a work to be processed by the system
2255 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002256 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002257 * Only when the countdown completes is the work item actually submitted to
2258 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002259 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002260 * Submitting a previously submitted delayed work item that is still
2261 * counting down cancels the existing submission and restarts the countdown
2262 * using the new delay. If the work item is currently pending on the
2263 * workqueue's queue because the countdown has completed it is too late to
2264 * resubmit the item, and resubmission fails without impacting the work item.
2265 * If the work item has already been processed, or is currently being processed,
2266 * its work is considered complete and the work item can be resubmitted.
2267 *
2268 * @warning
2269 * Work items submitted to the system workqueue should avoid using handlers
2270 * that block or yield since this may prevent the system workqueue from
2271 * processing other work items in a timely manner.
2272 *
2273 * @note Can be called by ISRs.
2274 *
2275 * @param work Address of delayed work item.
2276 * @param delay Delay before submitting the work item (in milliseconds).
2277 *
2278 * @retval 0 Work item countdown started.
2279 * @retval -EINPROGRESS Work item is already pending.
2280 * @retval -EINVAL Work item is being processed or has completed its work.
2281 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002282 */
2283static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002284 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002285{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002286 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002287}
2288
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002289/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002290 * @brief Get time remaining before a delayed work gets scheduled.
2291 *
2292 * This routine computes the (approximate) time remaining before a
2293 * delayed work gets executed. If the delayed work is not waiting to be
2294 * schedules, it returns zero.
2295 *
2296 * @param work Delayed work item.
2297 *
2298 * @return Remaining time (in milliseconds).
2299 */
Kumar Galacc334c72017-04-21 10:55:34 -05002300static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002301{
2302 return _timeout_remaining_get(&work->timeout);
2303}
2304
2305/**
Allan Stephensc98da842016-11-11 15:45:03 -05002306 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002307 */
2308
Allan Stephensc98da842016-11-11 15:45:03 -05002309/**
2310 * @cond INTERNAL_HIDDEN
2311 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002312
2313struct k_mutex {
2314 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002315 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002316 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002317 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002318
Anas Nashif2f203c22016-12-18 06:57:45 -05002319 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002320};
2321
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002322#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002323 { \
2324 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2325 .owner = NULL, \
2326 .lock_count = 0, \
2327 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002328 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002329 }
2330
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002331#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2332
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002333/**
Allan Stephensc98da842016-11-11 15:45:03 -05002334 * INTERNAL_HIDDEN @endcond
2335 */
2336
2337/**
2338 * @defgroup mutex_apis Mutex APIs
2339 * @ingroup kernel_apis
2340 * @{
2341 */
2342
2343/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002344 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002345 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002346 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002347 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002348 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002349 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002350 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002351 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002352#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002353 struct k_mutex name \
2354 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002355 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002356
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002357/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002358 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002359 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002360 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002361 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002362 * Upon completion, the mutex is available and does not have an owner.
2363 *
2364 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002365 *
2366 * @return N/A
2367 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002368extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002369
2370/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002371 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002372 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002373 * This routine locks @a mutex. If the mutex is locked by another thread,
2374 * the calling thread waits until the mutex becomes available or until
2375 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002376 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002377 * A thread is permitted to lock a mutex it has already locked. The operation
2378 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002379 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002380 * @param mutex Address of the mutex.
2381 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002382 * or one of the special values K_NO_WAIT and K_FOREVER.
2383 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002384 * @retval 0 Mutex locked.
2385 * @retval -EBUSY Returned without waiting.
2386 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002387 */
Kumar Galacc334c72017-04-21 10:55:34 -05002388extern int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002389
2390/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002391 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002392 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002393 * This routine unlocks @a mutex. The mutex must already be locked by the
2394 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002395 *
2396 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002397 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002398 * thread.
2399 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002400 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002401 *
2402 * @return N/A
2403 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002404extern void k_mutex_unlock(struct k_mutex *mutex);
2405
Allan Stephensc98da842016-11-11 15:45:03 -05002406/**
2407 * @} end defgroup mutex_apis
2408 */
2409
2410/**
2411 * @cond INTERNAL_HIDDEN
2412 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002413
2414struct k_sem {
2415 _wait_q_t wait_q;
2416 unsigned int count;
2417 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002418 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002419
Anas Nashif2f203c22016-12-18 06:57:45 -05002420 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002421};
2422
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002423#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002424 { \
2425 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2426 .count = initial_count, \
2427 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002428 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002429 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002430 }
2431
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002432#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2433
Allan Stephensc98da842016-11-11 15:45:03 -05002434/**
2435 * INTERNAL_HIDDEN @endcond
2436 */
2437
2438/**
2439 * @defgroup semaphore_apis Semaphore APIs
2440 * @ingroup kernel_apis
2441 * @{
2442 */
2443
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002444/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002445 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002446 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002447 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002448 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002449 * @param sem Address of the semaphore.
2450 * @param initial_count Initial semaphore count.
2451 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002452 *
2453 * @return N/A
2454 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002455extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2456 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002457
2458/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002459 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002460 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002461 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002462 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002463 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2464 *
2465 * @param sem Address of the semaphore.
2466 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002467 * or one of the special values K_NO_WAIT and K_FOREVER.
2468 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002469 * @note When porting code from the nanokernel legacy API to the new API, be
2470 * careful with the return value of this function. The return value is the
2471 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2472 * non-zero means failure, while the nano_sem_take family returns 1 for success
2473 * and 0 for failure.
2474 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002475 * @retval 0 Semaphore taken.
2476 * @retval -EBUSY Returned without waiting.
2477 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002478 */
Kumar Galacc334c72017-04-21 10:55:34 -05002479extern int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002480
2481/**
2482 * @brief Give a semaphore.
2483 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002484 * This routine gives @a sem, unless the semaphore is already at its maximum
2485 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002486 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002487 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002488 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002489 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002490 *
2491 * @return N/A
2492 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002493extern void k_sem_give(struct k_sem *sem);
2494
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002495/**
2496 * @brief Reset a semaphore's count to zero.
2497 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002498 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002499 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002500 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002501 *
2502 * @return N/A
2503 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04002504static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002505{
2506 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002507}
2508
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002509/**
2510 * @brief Get a semaphore's count.
2511 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002512 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002513 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002514 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002515 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002516 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002517 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02002518static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002519{
2520 return sem->count;
2521}
2522
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002523/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002524 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002525 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002526 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002527 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002528 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002529 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002530 * @param name Name of the semaphore.
2531 * @param initial_count Initial semaphore count.
2532 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002533 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002534#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002535 struct k_sem name \
2536 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002537 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002538
Allan Stephensc98da842016-11-11 15:45:03 -05002539/**
2540 * @} end defgroup semaphore_apis
2541 */
2542
2543/**
2544 * @defgroup alert_apis Alert APIs
2545 * @ingroup kernel_apis
2546 * @{
2547 */
2548
Allan Stephens5eceb852016-11-16 10:16:30 -05002549/**
2550 * @typedef k_alert_handler_t
2551 * @brief Alert handler function type.
2552 *
2553 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002554 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002555 * and is only invoked if the alert has been initialized with one.
2556 *
2557 * @param alert Address of the alert.
2558 *
2559 * @return 0 if alert has been consumed; non-zero if alert should pend.
2560 */
2561typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002562
2563/**
2564 * @} end defgroup alert_apis
2565 */
2566
2567/**
2568 * @cond INTERNAL_HIDDEN
2569 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002570
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002571#define K_ALERT_DEFAULT NULL
2572#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002573
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002574struct k_alert {
2575 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002576 atomic_t send_count;
2577 struct k_work work_item;
2578 struct k_sem sem;
2579
Anas Nashif2f203c22016-12-18 06:57:45 -05002580 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002581};
2582
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002583extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002584
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002585#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002586 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002587 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002588 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002589 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2590 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002591 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002592 }
2593
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002594#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2595
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002596/**
Allan Stephensc98da842016-11-11 15:45:03 -05002597 * INTERNAL_HIDDEN @endcond
2598 */
2599
2600/**
2601 * @addtogroup alert_apis
2602 * @{
2603 */
2604
2605/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002606 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002607 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002608 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002609 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002610 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002611 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002612 * @param name Name of the alert.
2613 * @param alert_handler Action to take when alert is sent. Specify either
2614 * the address of a function to be invoked by the system workqueue
2615 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2616 * K_ALERT_DEFAULT (which causes the alert to pend).
2617 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002618 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002619#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002620 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002621 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002622 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002623 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002624
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002625/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002626 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002627 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002628 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002629 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002630 * @param alert Address of the alert.
2631 * @param handler Action to take when alert is sent. Specify either the address
2632 * of a function to be invoked by the system workqueue thread,
2633 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2634 * K_ALERT_DEFAULT (which causes the alert to pend).
2635 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002636 *
2637 * @return N/A
2638 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002639extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2640 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002641
2642/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002643 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002644 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002645 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002646 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002647 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2648 *
2649 * @param alert Address of the alert.
2650 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002651 * or one of the special values K_NO_WAIT and K_FOREVER.
2652 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002653 * @retval 0 Alert received.
2654 * @retval -EBUSY Returned without waiting.
2655 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002656 */
Kumar Galacc334c72017-04-21 10:55:34 -05002657extern int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002658
2659/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002660 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002661 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002662 * This routine signals @a alert. The action specified for @a alert will
2663 * be taken, which may trigger the execution of an alert handler function
2664 * and/or cause the alert to pend (assuming the alert has not reached its
2665 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002666 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002667 * @note Can be called by ISRs.
2668 *
2669 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002670 *
2671 * @return N/A
2672 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002673extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002674
2675/**
Allan Stephensc98da842016-11-11 15:45:03 -05002676 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002677 */
2678
Allan Stephensc98da842016-11-11 15:45:03 -05002679/**
2680 * @cond INTERNAL_HIDDEN
2681 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002682
2683struct k_msgq {
2684 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002685 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002686 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002687 char *buffer_start;
2688 char *buffer_end;
2689 char *read_ptr;
2690 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002691 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002692
Anas Nashif2f203c22016-12-18 06:57:45 -05002693 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002694};
2695
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002696#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002697 { \
2698 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002699 .max_msgs = q_max_msgs, \
2700 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002701 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002702 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002703 .read_ptr = q_buffer, \
2704 .write_ptr = q_buffer, \
2705 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002706 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002707 }
2708
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002709#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2710
Peter Mitsis1da807e2016-10-06 11:36:59 -04002711/**
Allan Stephensc98da842016-11-11 15:45:03 -05002712 * INTERNAL_HIDDEN @endcond
2713 */
2714
2715/**
2716 * @defgroup msgq_apis Message Queue APIs
2717 * @ingroup kernel_apis
2718 * @{
2719 */
2720
2721/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002722 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002723 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002724 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2725 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002726 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2727 * message is similarly aligned to this boundary, @a q_msg_size must also be
2728 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002729 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002730 * The message queue can be accessed outside the module where it is defined
2731 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002732 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002733 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002734 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002735 * @param q_name Name of the message queue.
2736 * @param q_msg_size Message size (in bytes).
2737 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002738 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002739 */
2740#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2741 static char __noinit __aligned(q_align) \
2742 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002743 struct k_msgq q_name \
2744 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002745 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002746 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002747
Peter Mitsisd7a37502016-10-13 11:37:40 -04002748/**
2749 * @brief Initialize a message queue.
2750 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002751 * This routine initializes a message queue object, prior to its first use.
2752 *
Allan Stephensda827222016-11-09 14:23:58 -06002753 * The message queue's ring buffer must contain space for @a max_msgs messages,
2754 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2755 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2756 * that each message is similarly aligned to this boundary, @a q_msg_size
2757 * must also be a multiple of N.
2758 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002759 * @param q Address of the message queue.
2760 * @param buffer Pointer to ring buffer that holds queued messages.
2761 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002762 * @param max_msgs Maximum number of messages that can be queued.
2763 *
2764 * @return N/A
2765 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002766extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002767 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002768
2769/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002770 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002771 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002772 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002773 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002774 * @note Can be called by ISRs.
2775 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002776 * @param q Address of the message queue.
2777 * @param data Pointer to the message.
2778 * @param timeout Waiting period to add the message (in milliseconds),
2779 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002780 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002781 * @retval 0 Message sent.
2782 * @retval -ENOMSG Returned without waiting or queue purged.
2783 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002784 */
Kumar Galacc334c72017-04-21 10:55:34 -05002785extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002786
2787/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002788 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002789 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002790 * This routine receives a message from message queue @a q in a "first in,
2791 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002792 *
Allan Stephensc98da842016-11-11 15:45:03 -05002793 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002794 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002795 * @param q Address of the message queue.
2796 * @param data Address of area to hold the received message.
2797 * @param timeout Waiting period to receive the message (in milliseconds),
2798 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002799 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002800 * @retval 0 Message received.
2801 * @retval -ENOMSG Returned without waiting.
2802 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002803 */
Kumar Galacc334c72017-04-21 10:55:34 -05002804extern int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002805
2806/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002807 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002808 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002809 * This routine discards all unreceived messages in a message queue's ring
2810 * buffer. Any threads that are blocked waiting to send a message to the
2811 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002812 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002813 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002814 *
2815 * @return N/A
2816 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002817extern void k_msgq_purge(struct k_msgq *q);
2818
Peter Mitsis67be2492016-10-07 11:44:34 -04002819/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002820 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002821 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002822 * This routine returns the number of unused entries in a message queue's
2823 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002824 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002825 * @param q Address of the message queue.
2826 *
2827 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002828 */
Kumar Galacc334c72017-04-21 10:55:34 -05002829static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002830{
2831 return q->max_msgs - q->used_msgs;
2832}
2833
Peter Mitsisd7a37502016-10-13 11:37:40 -04002834/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002835 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002836 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002837 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002838 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002839 * @param q Address of the message queue.
2840 *
2841 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002842 */
Kumar Galacc334c72017-04-21 10:55:34 -05002843static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002844{
2845 return q->used_msgs;
2846}
2847
Allan Stephensc98da842016-11-11 15:45:03 -05002848/**
2849 * @} end defgroup msgq_apis
2850 */
2851
2852/**
2853 * @defgroup mem_pool_apis Memory Pool APIs
2854 * @ingroup kernel_apis
2855 * @{
2856 */
2857
Andy Ross73cb9582017-05-09 10:42:39 -07002858/* Note on sizing: the use of a 20 bit field for block means that,
2859 * assuming a reasonable minimum block size of 16 bytes, we're limited
2860 * to 16M of memory managed by a single pool. Long term it would be
2861 * good to move to a variable bit size based on configuration.
2862 */
2863struct k_mem_block_id {
2864 u32_t pool : 8;
2865 u32_t level : 4;
2866 u32_t block : 20;
2867};
2868
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002869struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002870 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002871 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002872};
2873
Allan Stephensc98da842016-11-11 15:45:03 -05002874/**
2875 * @} end defgroup mem_pool_apis
2876 */
2877
2878/**
2879 * @defgroup mailbox_apis Mailbox APIs
2880 * @ingroup kernel_apis
2881 * @{
2882 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002883
2884struct k_mbox_msg {
2885 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002886 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002887 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002888 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002889 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002890 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002891 /** sender's message data buffer */
2892 void *tx_data;
2893 /** internal use only - needed for legacy API support */
2894 void *_rx_data;
2895 /** message data block descriptor */
2896 struct k_mem_block tx_block;
2897 /** source thread id */
2898 k_tid_t rx_source_thread;
2899 /** target thread id */
2900 k_tid_t tx_target_thread;
2901 /** internal use only - thread waiting on send (may be a dummy) */
2902 k_tid_t _syncing_thread;
2903#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2904 /** internal use only - semaphore used during asynchronous send */
2905 struct k_sem *_async_sem;
2906#endif
2907};
2908
Allan Stephensc98da842016-11-11 15:45:03 -05002909/**
2910 * @cond INTERNAL_HIDDEN
2911 */
2912
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002913struct k_mbox {
2914 _wait_q_t tx_msg_queue;
2915 _wait_q_t rx_msg_queue;
2916
Anas Nashif2f203c22016-12-18 06:57:45 -05002917 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002918};
2919
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002920#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002921 { \
2922 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2923 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002924 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002925 }
2926
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002927#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
2928
Peter Mitsis12092702016-10-14 12:57:23 -04002929/**
Allan Stephensc98da842016-11-11 15:45:03 -05002930 * INTERNAL_HIDDEN @endcond
2931 */
2932
2933/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002934 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002935 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002936 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002937 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002938 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002939 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002940 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002941 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002942#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002943 struct k_mbox name \
2944 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002945 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002946
Peter Mitsis12092702016-10-14 12:57:23 -04002947/**
2948 * @brief Initialize a mailbox.
2949 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002950 * This routine initializes a mailbox object, prior to its first use.
2951 *
2952 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002953 *
2954 * @return N/A
2955 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002956extern void k_mbox_init(struct k_mbox *mbox);
2957
Peter Mitsis12092702016-10-14 12:57:23 -04002958/**
2959 * @brief Send a mailbox message in a synchronous manner.
2960 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002961 * This routine sends a message to @a mbox and waits for a receiver to both
2962 * receive and process it. The message data may be in a buffer, in a memory
2963 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002964 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002965 * @param mbox Address of the mailbox.
2966 * @param tx_msg Address of the transmit message descriptor.
2967 * @param timeout Waiting period for the message to be received (in
2968 * milliseconds), or one of the special values K_NO_WAIT
2969 * and K_FOREVER. Once the message has been received,
2970 * this routine waits as long as necessary for the message
2971 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002972 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002973 * @retval 0 Message sent.
2974 * @retval -ENOMSG Returned without waiting.
2975 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002976 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002977extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002978 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002979
Peter Mitsis12092702016-10-14 12:57:23 -04002980/**
2981 * @brief Send a mailbox message in an asynchronous manner.
2982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002983 * This routine sends a message to @a mbox without waiting for a receiver
2984 * to process it. The message data may be in a buffer, in a memory pool block,
2985 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2986 * will be given when the message has been both received and completely
2987 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002988 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002989 * @param mbox Address of the mailbox.
2990 * @param tx_msg Address of the transmit message descriptor.
2991 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002992 *
2993 * @return N/A
2994 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002995extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002996 struct k_sem *sem);
2997
Peter Mitsis12092702016-10-14 12:57:23 -04002998/**
2999 * @brief Receive a mailbox message.
3000 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003001 * This routine receives a message from @a mbox, then optionally retrieves
3002 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003003 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003004 * @param mbox Address of the mailbox.
3005 * @param rx_msg Address of the receive message descriptor.
3006 * @param buffer Address of the buffer to receive data, or NULL to defer data
3007 * retrieval and message disposal until later.
3008 * @param timeout Waiting period for a message to be received (in
3009 * milliseconds), or one of the special values K_NO_WAIT
3010 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003011 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003012 * @retval 0 Message received.
3013 * @retval -ENOMSG Returned without waiting.
3014 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003015 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003016extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003017 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003018
3019/**
3020 * @brief Retrieve mailbox message data into a buffer.
3021 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003022 * This routine completes the processing of a received message by retrieving
3023 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003024 *
3025 * Alternatively, this routine can be used to dispose of a received message
3026 * without retrieving its data.
3027 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003028 * @param rx_msg Address of the receive message descriptor.
3029 * @param buffer Address of the buffer to receive data, or NULL to discard
3030 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003031 *
3032 * @return N/A
3033 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003034extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003035
3036/**
3037 * @brief Retrieve mailbox message data into a memory pool block.
3038 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003039 * This routine completes the processing of a received message by retrieving
3040 * its data into a memory pool block, then disposing of the message.
3041 * The memory pool block that results from successful retrieval must be
3042 * returned to the pool once the data has been processed, even in cases
3043 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003044 *
3045 * Alternatively, this routine can be used to dispose of a received message
3046 * without retrieving its data. In this case there is no need to return a
3047 * memory pool block to the pool.
3048 *
3049 * This routine allocates a new memory pool block for the data only if the
3050 * data is not already in one. If a new block cannot be allocated, the routine
3051 * returns a failure code and the received message is left unchanged. This
3052 * permits the caller to reattempt data retrieval at a later time or to dispose
3053 * of the received message without retrieving its data.
3054 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003055 * @param rx_msg Address of a receive message descriptor.
3056 * @param pool Address of memory pool, or NULL to discard data.
3057 * @param block Address of the area to hold memory pool block info.
3058 * @param timeout Waiting period to wait for a memory pool block (in
3059 * milliseconds), or one of the special values K_NO_WAIT
3060 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003061 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003062 * @retval 0 Data retrieved.
3063 * @retval -ENOMEM Returned without waiting.
3064 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003065 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003066extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003067 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003068 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003069
Allan Stephensc98da842016-11-11 15:45:03 -05003070/**
3071 * @} end defgroup mailbox_apis
3072 */
3073
3074/**
3075 * @cond INTERNAL_HIDDEN
3076 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003077
3078struct k_pipe {
3079 unsigned char *buffer; /* Pipe buffer: may be NULL */
3080 size_t size; /* Buffer size */
3081 size_t bytes_used; /* # bytes used in buffer */
3082 size_t read_index; /* Where in buffer to read from */
3083 size_t write_index; /* Where in buffer to write */
3084
3085 struct {
3086 _wait_q_t readers; /* Reader wait queue */
3087 _wait_q_t writers; /* Writer wait queue */
3088 } wait_q;
3089
Anas Nashif2f203c22016-12-18 06:57:45 -05003090 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003091};
3092
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003093#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003094 { \
3095 .buffer = pipe_buffer, \
3096 .size = pipe_buffer_size, \
3097 .bytes_used = 0, \
3098 .read_index = 0, \
3099 .write_index = 0, \
3100 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3101 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003102 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003103 }
3104
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003105#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3106
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003107/**
Allan Stephensc98da842016-11-11 15:45:03 -05003108 * INTERNAL_HIDDEN @endcond
3109 */
3110
3111/**
3112 * @defgroup pipe_apis Pipe APIs
3113 * @ingroup kernel_apis
3114 * @{
3115 */
3116
3117/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003118 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003119 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003120 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003121 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003122 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003123 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003124 * @param name Name of the pipe.
3125 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3126 * or zero if no ring buffer is used.
3127 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003128 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003129#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3130 static unsigned char __noinit __aligned(pipe_align) \
3131 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003132 struct k_pipe name \
3133 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003134 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003135
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003136/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003137 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003139 * This routine initializes a pipe object, prior to its first use.
3140 *
3141 * @param pipe Address of the pipe.
3142 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3143 * is used.
3144 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3145 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003146 *
3147 * @return N/A
3148 */
3149extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3150 size_t size);
3151
3152/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003153 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003154 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003155 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003156 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003157 * @param pipe Address of the pipe.
3158 * @param data Address of data to write.
3159 * @param bytes_to_write Size of data (in bytes).
3160 * @param bytes_written Address of area to hold the number of bytes written.
3161 * @param min_xfer Minimum number of bytes to write.
3162 * @param timeout Waiting period to wait for the data to be written (in
3163 * milliseconds), or one of the special values K_NO_WAIT
3164 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003165 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003166 * @retval 0 At least @a min_xfer bytes of data were written.
3167 * @retval -EIO Returned without waiting; zero data bytes were written.
3168 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003169 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003170 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003171extern int k_pipe_put(struct k_pipe *pipe, void *data,
3172 size_t bytes_to_write, size_t *bytes_written,
Kumar Galacc334c72017-04-21 10:55:34 -05003173 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003174
3175/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003176 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003177 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003178 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003179 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003180 * @param pipe Address of the pipe.
3181 * @param data Address to place the data read from pipe.
3182 * @param bytes_to_read Maximum number of data bytes to read.
3183 * @param bytes_read Address of area to hold the number of bytes read.
3184 * @param min_xfer Minimum number of data bytes to read.
3185 * @param timeout Waiting period to wait for the data to be read (in
3186 * milliseconds), or one of the special values K_NO_WAIT
3187 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003188 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003189 * @retval 0 At least @a min_xfer bytes of data were read.
3190 * @retval -EIO Returned without waiting; zero data bytes were read.
3191 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003192 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003193 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003194extern int k_pipe_get(struct k_pipe *pipe, void *data,
3195 size_t bytes_to_read, size_t *bytes_read,
Kumar Galacc334c72017-04-21 10:55:34 -05003196 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003197
3198/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003199 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003200 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003201 * This routine writes the data contained in a memory block to @a pipe.
3202 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003203 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003204 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003205 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003206 * @param block Memory block containing data to send
3207 * @param size Number of data bytes in memory block to send
3208 * @param sem Semaphore to signal upon completion (else NULL)
3209 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003210 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003211 */
3212extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3213 size_t size, struct k_sem *sem);
3214
3215/**
Allan Stephensc98da842016-11-11 15:45:03 -05003216 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003217 */
3218
Allan Stephensc98da842016-11-11 15:45:03 -05003219/**
3220 * @cond INTERNAL_HIDDEN
3221 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003222
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003223struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003224 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003225 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003226 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003227 char *buffer;
3228 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003229 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003230
Anas Nashif2f203c22016-12-18 06:57:45 -05003231 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003232};
3233
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003234#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003235 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003236 { \
3237 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003238 .num_blocks = slab_num_blocks, \
3239 .block_size = slab_block_size, \
3240 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003241 .free_list = NULL, \
3242 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003243 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003244 }
3245
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003246#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3247
3248
Peter Mitsis578f9112016-10-07 13:50:31 -04003249/**
Allan Stephensc98da842016-11-11 15:45:03 -05003250 * INTERNAL_HIDDEN @endcond
3251 */
3252
3253/**
3254 * @defgroup mem_slab_apis Memory Slab APIs
3255 * @ingroup kernel_apis
3256 * @{
3257 */
3258
3259/**
Allan Stephensda827222016-11-09 14:23:58 -06003260 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003261 *
Allan Stephensda827222016-11-09 14:23:58 -06003262 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003263 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003264 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3265 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003266 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003267 *
Allan Stephensda827222016-11-09 14:23:58 -06003268 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003269 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003270 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003271 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003272 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003273 * @param name Name of the memory slab.
3274 * @param slab_block_size Size of each memory block (in bytes).
3275 * @param slab_num_blocks Number memory blocks.
3276 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003277 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003278#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3279 char __noinit __aligned(slab_align) \
3280 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3281 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003282 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003283 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003284 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003285
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003286/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003287 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003288 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003289 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003290 *
Allan Stephensda827222016-11-09 14:23:58 -06003291 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3292 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3293 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3294 * To ensure that each memory block is similarly aligned to this boundary,
3295 * @a slab_block_size must also be a multiple of N.
3296 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003297 * @param slab Address of the memory slab.
3298 * @param buffer Pointer to buffer used for the memory blocks.
3299 * @param block_size Size of each memory block (in bytes).
3300 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003301 *
3302 * @return N/A
3303 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003304extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003305 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003306
3307/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003308 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003309 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003310 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003311 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003312 * @param slab Address of the memory slab.
3313 * @param mem Pointer to block address area.
3314 * @param timeout Maximum time to wait for operation to complete
3315 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3316 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003317 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003318 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003319 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003320 * @retval -ENOMEM Returned without waiting.
3321 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003322 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003323extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003324 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003325
3326/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003327 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003328 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003329 * This routine releases a previously allocated memory block back to its
3330 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003331 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003332 * @param slab Address of the memory slab.
3333 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003334 *
3335 * @return N/A
3336 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003337extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003338
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003339/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003340 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003341 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003342 * This routine gets the number of memory blocks that are currently
3343 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003344 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003345 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003346 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003347 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003348 */
Kumar Galacc334c72017-04-21 10:55:34 -05003349static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003350{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003351 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003352}
3353
Peter Mitsisc001aa82016-10-13 13:53:37 -04003354/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003355 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003356 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003357 * This routine gets the number of memory blocks that are currently
3358 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003359 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003360 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003361 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003362 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003363 */
Kumar Galacc334c72017-04-21 10:55:34 -05003364static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003365{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003366 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003367}
3368
Allan Stephensc98da842016-11-11 15:45:03 -05003369/**
3370 * @} end defgroup mem_slab_apis
3371 */
3372
3373/**
3374 * @cond INTERNAL_HIDDEN
3375 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003376
Andy Ross73cb9582017-05-09 10:42:39 -07003377struct k_mem_pool_lvl {
3378 union {
3379 u32_t *bits_p;
3380 u32_t bits;
3381 };
3382 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003383};
3384
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003385struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003386 void *buf;
3387 size_t max_sz;
3388 u16_t n_max;
3389 u8_t n_levels;
3390 u8_t max_inline_level;
3391 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003392 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003393};
3394
Andy Ross73cb9582017-05-09 10:42:39 -07003395#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003396
Andy Ross73cb9582017-05-09 10:42:39 -07003397#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3398
3399#define _MPOOL_LVLS(maxsz, minsz) \
3400 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3401 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3402 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3403 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3404 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3405 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3406 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3407 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3408 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3409 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3410 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3411 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3412 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3413 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3414 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3415 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3416
3417/* Rounds the needed bits up to integer multiples of u32_t */
3418#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3419 ((((n_max) << (2*(l))) + 31) / 32)
3420
3421/* One word gets stored free unioned with the pointer, otherwise the
3422 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003423 */
Andy Ross73cb9582017-05-09 10:42:39 -07003424#define _MPOOL_LBIT_WORDS(n_max, l) \
3425 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3426 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003427
Andy Ross73cb9582017-05-09 10:42:39 -07003428/* How many bytes for the bitfields of a single level? */
3429#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3430 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3431 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003432
Andy Ross73cb9582017-05-09 10:42:39 -07003433/* Size of the bitmap array that follows the buffer in allocated memory */
3434#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3435 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3436 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3437 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3438 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3439 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3440 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3441 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3442 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3443 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3444 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3445 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3446 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3447 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3448 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3449 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3450 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003451
3452/**
Allan Stephensc98da842016-11-11 15:45:03 -05003453 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003454 */
3455
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003456/**
Allan Stephensc98da842016-11-11 15:45:03 -05003457 * @addtogroup mem_pool_apis
3458 * @{
3459 */
3460
3461/**
3462 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003463 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003464 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3465 * long. The memory pool allows blocks to be repeatedly partitioned into
3466 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003467 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003468 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003469 * If the pool is to be accessed outside the module where it is defined, it
3470 * can be declared via
3471 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003472 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003473 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003474 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003475 * @param minsz Size of the smallest blocks in the pool (in bytes).
3476 * @param maxsz Size of the largest blocks in the pool (in bytes).
3477 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003478 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003479 */
Andy Ross73cb9582017-05-09 10:42:39 -07003480#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3481 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3482 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3483 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3484 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3485 .buf = _mpool_buf_##name, \
3486 .max_sz = maxsz, \
3487 .n_max = nmax, \
3488 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3489 .levels = _mpool_lvls_##name, \
3490 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003491
Peter Mitsis937042c2016-10-13 13:18:26 -04003492/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003493 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003494 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003495 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003496 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003497 * @param pool Address of the memory pool.
3498 * @param block Pointer to block descriptor for the allocated memory.
3499 * @param size Amount of memory to allocate (in bytes).
3500 * @param timeout Maximum time to wait for operation to complete
3501 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3502 * or K_FOREVER to wait as long as necessary.
3503 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003504 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003505 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003506 * @retval -ENOMEM Returned without waiting.
3507 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003508 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003509extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003510 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003511
3512/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003513 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003514 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003515 * This routine releases a previously allocated memory block back to its
3516 * memory pool.
3517 *
3518 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003519 *
3520 * @return N/A
3521 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003522extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003523
3524/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003525 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003526 *
Andy Ross73cb9582017-05-09 10:42:39 -07003527 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003528 *
Andy Ross73cb9582017-05-09 10:42:39 -07003529 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003530 *
3531 * @return N/A
3532 */
Andy Ross73cb9582017-05-09 10:42:39 -07003533static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003534
3535/**
Allan Stephensc98da842016-11-11 15:45:03 -05003536 * @} end addtogroup mem_pool_apis
3537 */
3538
3539/**
3540 * @defgroup heap_apis Heap Memory Pool APIs
3541 * @ingroup kernel_apis
3542 * @{
3543 */
3544
3545/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003546 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003547 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003548 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003549 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003550 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003551 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003552 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003553 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003554 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003555extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003556
3557/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003558 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003559 *
3560 * This routine provides traditional free() semantics. The memory being
3561 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003562 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003563 * If @a ptr is NULL, no operation is performed.
3564 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003565 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003566 *
3567 * @return N/A
3568 */
3569extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003570
Allan Stephensc98da842016-11-11 15:45:03 -05003571/**
3572 * @} end defgroup heap_apis
3573 */
3574
Benjamin Walshacc68c12017-01-29 18:57:45 -05003575/* polling API - PRIVATE */
3576
Benjamin Walshb0179862017-02-02 16:39:57 -05003577#ifdef CONFIG_POLL
3578#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3579#else
3580#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3581#endif
3582
Benjamin Walshacc68c12017-01-29 18:57:45 -05003583/* private - implementation data created as needed, per-type */
3584struct _poller {
3585 struct k_thread *thread;
3586};
3587
3588/* private - types bit positions */
3589enum _poll_types_bits {
3590 /* can be used to ignore an event */
3591 _POLL_TYPE_IGNORE,
3592
3593 /* to be signaled by k_poll_signal() */
3594 _POLL_TYPE_SIGNAL,
3595
3596 /* semaphore availability */
3597 _POLL_TYPE_SEM_AVAILABLE,
3598
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003599 /* queue/fifo/lifo data availability */
3600 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003601
3602 _POLL_NUM_TYPES
3603};
3604
3605#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3606
3607/* private - states bit positions */
3608enum _poll_states_bits {
3609 /* default state when creating event */
3610 _POLL_STATE_NOT_READY,
3611
Benjamin Walshacc68c12017-01-29 18:57:45 -05003612 /* signaled by k_poll_signal() */
3613 _POLL_STATE_SIGNALED,
3614
3615 /* semaphore is available */
3616 _POLL_STATE_SEM_AVAILABLE,
3617
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003618 /* data is available to read on queue/fifo/lifo */
3619 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003620
3621 _POLL_NUM_STATES
3622};
3623
3624#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3625
3626#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003627 (32 - (0 \
3628 + 8 /* tag */ \
3629 + _POLL_NUM_TYPES \
3630 + _POLL_NUM_STATES \
3631 + 1 /* modes */ \
3632 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003633
3634#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3635#error overflow of 32-bit word in struct k_poll_event
3636#endif
3637
3638/* end of polling API - PRIVATE */
3639
3640
3641/**
3642 * @defgroup poll_apis Async polling APIs
3643 * @ingroup kernel_apis
3644 * @{
3645 */
3646
3647/* Public polling API */
3648
3649/* public - values for k_poll_event.type bitfield */
3650#define K_POLL_TYPE_IGNORE 0
3651#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3652#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003653#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3654#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003655
3656/* public - polling modes */
3657enum k_poll_modes {
3658 /* polling thread does not take ownership of objects when available */
3659 K_POLL_MODE_NOTIFY_ONLY = 0,
3660
3661 K_POLL_NUM_MODES
3662};
3663
3664/* public - values for k_poll_event.state bitfield */
3665#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003666#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3667#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003668#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3669#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003670
3671/* public - poll signal object */
3672struct k_poll_signal {
3673 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003674 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003675
3676 /*
3677 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3678 * user resets it to 0.
3679 */
3680 unsigned int signaled;
3681
3682 /* custom result value passed to k_poll_signal() if needed */
3683 int result;
3684};
3685
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003686#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003687 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003688 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003689 .signaled = 0, \
3690 .result = 0, \
3691 }
3692
3693struct k_poll_event {
3694 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003695 sys_dnode_t _node;
3696
3697 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003698 struct _poller *poller;
3699
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003700 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003701 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003702
Benjamin Walshacc68c12017-01-29 18:57:45 -05003703 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003704 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003705
3706 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003707 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003708
3709 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003710 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003711
3712 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003713 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003714
3715 /* per-type data */
3716 union {
3717 void *obj;
3718 struct k_poll_signal *signal;
3719 struct k_sem *sem;
3720 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003721 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003722 };
3723};
3724
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003725#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003726 { \
3727 .poller = NULL, \
3728 .type = event_type, \
3729 .state = K_POLL_STATE_NOT_READY, \
3730 .mode = event_mode, \
3731 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003732 { .obj = event_obj }, \
3733 }
3734
3735#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3736 event_tag) \
3737 { \
3738 .type = event_type, \
3739 .tag = event_tag, \
3740 .state = K_POLL_STATE_NOT_READY, \
3741 .mode = event_mode, \
3742 .unused = 0, \
3743 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003744 }
3745
3746/**
3747 * @brief Initialize one struct k_poll_event instance
3748 *
3749 * After this routine is called on a poll event, the event it ready to be
3750 * placed in an event array to be passed to k_poll().
3751 *
3752 * @param event The event to initialize.
3753 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3754 * values. Only values that apply to the same object being polled
3755 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3756 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003757 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003758 * @param obj Kernel object or poll signal.
3759 *
3760 * @return N/A
3761 */
3762
Kumar Galacc334c72017-04-21 10:55:34 -05003763extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003764 int mode, void *obj);
3765
3766/**
3767 * @brief Wait for one or many of multiple poll events to occur
3768 *
3769 * This routine allows a thread to wait concurrently for one or many of
3770 * multiple poll events to have occurred. Such events can be a kernel object
3771 * being available, like a semaphore, or a poll signal event.
3772 *
3773 * When an event notifies that a kernel object is available, the kernel object
3774 * is not "given" to the thread calling k_poll(): it merely signals the fact
3775 * that the object was available when the k_poll() call was in effect. Also,
3776 * all threads trying to acquire an object the regular way, i.e. by pending on
3777 * the object, have precedence over the thread polling on the object. This
3778 * means that the polling thread will never get the poll event on an object
3779 * until the object becomes available and its pend queue is empty. For this
3780 * reason, the k_poll() call is more effective when the objects being polled
3781 * only have one thread, the polling thread, trying to acquire them.
3782 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003783 * When k_poll() returns 0, the caller should loop on all the events that were
3784 * passed to k_poll() and check the state field for the values that were
3785 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003786 *
3787 * Before being reused for another call to k_poll(), the user has to reset the
3788 * state field to K_POLL_STATE_NOT_READY.
3789 *
3790 * @param events An array of pointers to events to be polled for.
3791 * @param num_events The number of events in the array.
3792 * @param timeout Waiting period for an event to be ready (in milliseconds),
3793 * or one of the special values K_NO_WAIT and K_FOREVER.
3794 *
3795 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003796 * @retval -EAGAIN Waiting period timed out.
3797 */
3798
3799extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003800 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003801
3802/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003803 * @brief Initialize a poll signal object.
3804 *
3805 * Ready a poll signal object to be signaled via k_poll_signal().
3806 *
3807 * @param signal A poll signal.
3808 *
3809 * @return N/A
3810 */
3811
3812extern void k_poll_signal_init(struct k_poll_signal *signal);
3813
3814/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003815 * @brief Signal a poll signal object.
3816 *
3817 * This routine makes ready a poll signal, which is basically a poll event of
3818 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3819 * made ready to run. A @a result value can be specified.
3820 *
3821 * The poll signal contains a 'signaled' field that, when set by
3822 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3823 * be reset by the user before being passed again to k_poll() or k_poll() will
3824 * consider it being signaled, and will return immediately.
3825 *
3826 * @param signal A poll signal.
3827 * @param result The value to store in the result field of the signal.
3828 *
3829 * @retval 0 The signal was delivered successfully.
3830 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3831 */
3832
3833extern int k_poll_signal(struct k_poll_signal *signal, int result);
3834
3835/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003836extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003837
3838/**
3839 * @} end defgroup poll_apis
3840 */
3841
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003842/**
3843 * @brief Make the CPU idle.
3844 *
3845 * This function makes the CPU idle until an event wakes it up.
3846 *
3847 * In a regular system, the idle thread should be the only thread responsible
3848 * for making the CPU idle and triggering any type of power management.
3849 * However, in some more constrained systems, such as a single-threaded system,
3850 * the only thread would be responsible for this if needed.
3851 *
3852 * @return N/A
3853 */
3854extern void k_cpu_idle(void);
3855
3856/**
3857 * @brief Make the CPU idle in an atomic fashion.
3858 *
3859 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3860 * must be done atomically before making the CPU idle.
3861 *
3862 * @param key Interrupt locking key obtained from irq_lock().
3863 *
3864 * @return N/A
3865 */
3866extern void k_cpu_atomic_idle(unsigned int key);
3867
Kumar Galacc334c72017-04-21 10:55:34 -05003868extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003869
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003870#include <arch/cpu.h>
3871
Andrew Boiecdb94d62017-04-18 15:22:05 -07003872#ifdef _ARCH_EXCEPT
3873/* This archtecture has direct support for triggering a CPU exception */
3874#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3875#else
3876
3877#include <misc/printk.h>
3878
3879/* NOTE: This is the implementation for arches that do not implement
3880 * _ARCH_EXCEPT() to generate a real CPU exception.
3881 *
3882 * We won't have a real exception frame to determine the PC value when
3883 * the oops occurred, so print file and line number before we jump into
3884 * the fatal error handler.
3885 */
3886#define _k_except_reason(reason) do { \
3887 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3888 _NanoFatalErrorHandler(reason, &_default_esf); \
3889 CODE_UNREACHABLE; \
3890 } while (0)
3891
3892#endif /* _ARCH__EXCEPT */
3893
3894/**
3895 * @brief Fatally terminate a thread
3896 *
3897 * This should be called when a thread has encountered an unrecoverable
3898 * runtime condition and needs to terminate. What this ultimately
3899 * means is determined by the _fatal_error_handler() implementation, which
3900 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3901 *
3902 * If this is called from ISR context, the default system fatal error handler
3903 * will treat it as an unrecoverable system error, just like k_panic().
3904 */
3905#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3906
3907/**
3908 * @brief Fatally terminate the system
3909 *
3910 * This should be called when the Zephyr kernel has encountered an
3911 * unrecoverable runtime condition and needs to terminate. What this ultimately
3912 * means is determined by the _fatal_error_handler() implementation, which
3913 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3914 */
3915#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3916
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003917/*
3918 * private APIs that are utilized by one or more public APIs
3919 */
3920
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003921#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003922extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003923#else
3924#define _init_static_threads() do { } while ((0))
3925#endif
3926
3927extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003928extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003929
Andrew Boiedc5d9352017-06-02 12:56:47 -07003930/* arch/cpu.h may declare an architecture or platform-specific macro
3931 * for properly declaring stacks, compatible with MMU/MPU constraints if
3932 * enabled
3933 */
3934#ifdef _ARCH_THREAD_STACK_DEFINE
3935#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
3936#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
3937 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
3938#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
3939#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie507852a2017-07-25 18:47:07 -07003940static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
3941{
3942 return _ARCH_THREAD_STACK_BUFFER(sym);
3943}
Andrew Boiedc5d9352017-06-02 12:56:47 -07003944#else
3945/**
3946 * @brief Declare a toplevel thread stack memory region
3947 *
3948 * This declares a region of memory suitable for use as a thread's stack.
3949 *
3950 * This is the generic, historical definition. Align to STACK_ALIGN and put in
3951 * 'noinit' section so that it isn't zeroed at boot
3952 *
Andrew Boie507852a2017-07-25 18:47:07 -07003953 * The declared symbol will always be a k_thread_stack_t which can be passed to
3954 * k_thread_create, but should otherwise not be manipulated. If the buffer
3955 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07003956 *
3957 * It is legal to precede this definition with the 'static' keyword.
3958 *
3959 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
3960 * parameter of k_thread_create(), it may not be the same as the
3961 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
3962 *
3963 * @param sym Thread stack symbol name
3964 * @param size Size of the stack memory region
3965 */
3966#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07003967 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07003968
3969/**
3970 * @brief Declare a toplevel array of thread stack memory regions
3971 *
3972 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
3973 * definition for additional details and constraints.
3974 *
3975 * This is the generic, historical definition. Align to STACK_ALIGN and put in
3976 * 'noinit' section so that it isn't zeroed at boot
3977 *
3978 * @param sym Thread stack symbol name
3979 * @param nmemb Number of stacks to declare
3980 * @param size Size of the stack memory region
3981 */
3982
3983#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07003984 struct _k_thread_stack_element __noinit \
3985 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07003986
3987/**
3988 * @brief Declare an embedded stack memory region
3989 *
3990 * Used for stacks embedded within other data structures. Use is highly
3991 * discouraged but in some cases necessary. For memory protection scenarios,
3992 * it is very important that any RAM preceding this member not be writable
3993 * by threads else a stack overflow will lead to silent corruption. In other
3994 * words, the containing data structure should live in RAM owned by the kernel.
3995 *
3996 * @param sym Thread stack symbol name
3997 * @param size Size of the stack memory region
3998 */
3999#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004000 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004001
4002/**
4003 * @brief Return the size in bytes of a stack memory region
4004 *
4005 * Convenience macro for passing the desired stack size to k_thread_create()
4006 * since the underlying implementation may actually create something larger
4007 * (for instance a guard area).
4008 *
4009 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004010 * passed to K_THREAD_STACK_DEFINE.
4011 *
4012 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4013 * it is not guaranteed to return the original value since each array
4014 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004015 *
4016 * @param sym Stack memory symbol
4017 * @return Size of the stack
4018 */
4019#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4020
4021/**
4022 * @brief Get a pointer to the physical stack buffer
4023 *
4024 * Convenience macro to get at the real underlying stack buffer used by
4025 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4026 * This is really only intended for diagnostic tools which want to examine
4027 * stack memory contents.
4028 *
4029 * @param sym Declared stack symbol name
4030 * @return The buffer itself, a char *
4031 */
Andrew Boie507852a2017-07-25 18:47:07 -07004032static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4033{
4034 return (char *)sym;
4035}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004036
4037#endif /* _ARCH_DECLARE_STACK */
4038
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004039#ifdef __cplusplus
4040}
4041#endif
4042
Andrew Boiee004dec2016-11-07 09:01:19 -08004043#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4044/*
4045 * Define new and delete operators.
4046 * At this moment, the operators do nothing since objects are supposed
4047 * to be statically allocated.
4048 */
4049inline void operator delete(void *ptr)
4050{
4051 (void)ptr;
4052}
4053
4054inline void operator delete[](void *ptr)
4055{
4056 (void)ptr;
4057}
4058
4059inline void *operator new(size_t size)
4060{
4061 (void)size;
4062 return NULL;
4063}
4064
4065inline void *operator new[](size_t size)
4066{
4067 (void)size;
4068 return NULL;
4069}
4070
4071/* Placement versions of operator new and delete */
4072inline void operator delete(void *ptr1, void *ptr2)
4073{
4074 (void)ptr1;
4075 (void)ptr2;
4076}
4077
4078inline void operator delete[](void *ptr1, void *ptr2)
4079{
4080 (void)ptr1;
4081 (void)ptr2;
4082}
4083
4084inline void *operator new(size_t size, void *ptr)
4085{
4086 (void)size;
4087 return ptr;
4088}
4089
4090inline void *operator new[](size_t size, void *ptr)
4091{
4092 (void)size;
4093 return ptr;
4094}
4095
4096#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4097
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004098#endif /* !_ASMLANGUAGE */
4099
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004100#endif /* _kernel__h_ */