blob: a052b98df2bf3f216d0a794184eff945cc7b6746 [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
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700430/* This thread has dropped from supervisor mode to user mode and consequently
431 * has additional restrictions
432 */
433#define K_USER (1 << 2)
434
Benjamin Walshed240f22017-01-22 13:05:08 -0500435#ifdef CONFIG_X86
436/* x86 Bitmask definitions for threads user options */
437
438#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
439/* thread uses SSEx (and also FP) registers */
440#define K_SSE_REGS (1 << 7)
441#endif
442#endif
443
444/* end - thread options */
445
446#if !defined(_ASMLANGUAGE)
447
Andrew Boie507852a2017-07-25 18:47:07 -0700448/* Using typedef deliberately here, this is quite intended to be an opaque
449 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
450 *
451 * The purpose of this data type is to clearly distinguish between the
452 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
453 * buffer which composes the stack data actually used by the underlying
454 * thread; they cannot be used interchangably as some arches precede the
455 * stack buffer region with guard areas that trigger a MPU or MMU fault
456 * if written to.
457 *
458 * APIs that want to work with the buffer inside should continue to use
459 * char *.
460 *
461 * Stacks should always be created with K_THREAD_STACK_DEFINE().
462 */
463struct __packed _k_thread_stack_element {
464 char data;
465};
466typedef struct _k_thread_stack_element *k_thread_stack_t;
467
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400468
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400469/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700470 * @brief Create a thread.
471 *
472 * This routine initializes a thread, then schedules it for execution.
473 *
474 * The new thread may be scheduled for immediate execution or a delayed start.
475 * If the newly spawned thread does not have a delayed start the kernel
476 * scheduler may preempt the current thread to allow the new thread to
477 * execute.
478 *
479 * Thread options are architecture-specific, and can include K_ESSENTIAL,
480 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
481 * them using "|" (the logical OR operator).
482 *
483 * Historically, users often would use the beginning of the stack memory region
484 * to store the struct k_thread data, although corruption will occur if the
485 * stack overflows this region and stack protection features may not detect this
486 * situation.
487 *
488 * @param new_thread Pointer to uninitialized struct k_thread
489 * @param stack Pointer to the stack space.
490 * @param stack_size Stack size in bytes.
491 * @param entry Thread entry function.
492 * @param p1 1st entry point parameter.
493 * @param p2 2nd entry point parameter.
494 * @param p3 3rd entry point parameter.
495 * @param prio Thread priority.
496 * @param options Thread options.
497 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
498 *
499 * @return ID of new thread.
500 */
Andrew Boie507852a2017-07-25 18:47:07 -0700501extern k_tid_t k_thread_create(struct k_thread *new_thread,
502 k_thread_stack_t stack,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700503 size_t stack_size,
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700504 k_thread_entry_t entry,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700505 void *p1, void *p2, void *p3,
506 int prio, u32_t options, s32_t delay);
507
508/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500509 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400510 *
Allan Stephensc98da842016-11-11 15:45:03 -0500511 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500512 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400513 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500514 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400515 *
516 * @return N/A
517 */
Kumar Galacc334c72017-04-21 10:55:34 -0500518extern void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400519
520/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500521 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400522 *
523 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500524 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400525 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400526 * @return N/A
527 */
Kumar Galacc334c72017-04-21 10:55:34 -0500528extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400529
530/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500531 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400532 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500533 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400534 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500535 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400536 *
537 * @return N/A
538 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400539extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400540
541/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500542 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400543 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500544 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400545 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500546 * If @a thread is not currently sleeping, the routine has no effect.
547 *
548 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400549 *
550 * @return N/A
551 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400552extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400553
554/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500555 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400556 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500557 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400558 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400559extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400560
561/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500562 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400563 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500564 * This routine prevents @a thread from executing if it has not yet started
565 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400566 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500567 * @param thread ID of thread to cancel.
568 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700569 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500570 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400571 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400572extern int k_thread_cancel(k_tid_t thread);
573
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400574/**
Allan Stephensc98da842016-11-11 15:45:03 -0500575 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400576 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500577 * This routine permanently stops execution of @a thread. The thread is taken
578 * off all kernel queues it is part of (i.e. the ready queue, the timeout
579 * queue, or a kernel object wait queue). However, any kernel resources the
580 * thread might currently own (such as mutexes or memory blocks) are not
581 * released. It is the responsibility of the caller of this routine to ensure
582 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400583 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500584 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400585 *
586 * @return N/A
587 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400588extern void k_thread_abort(k_tid_t thread);
589
Andrew Boie7d627c52017-08-30 11:01:56 -0700590
591/**
592 * @brief Start an inactive thread
593 *
594 * If a thread was created with K_FOREVER in the delay parameter, it will
595 * not be added to the scheduling queue until this function is called
596 * on it.
597 *
598 * @param thread thread to start
599 */
600extern void k_thread_start(k_tid_t thread);
601
Allan Stephensc98da842016-11-11 15:45:03 -0500602/**
603 * @cond INTERNAL_HIDDEN
604 */
605
Benjamin Walshd211a522016-12-06 11:44:01 -0500606/* timeout has timed out and is not on _timeout_q anymore */
607#define _EXPIRED (-2)
608
609/* timeout is not in use */
610#define _INACTIVE (-1)
611
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400612struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700613 struct k_thread *init_thread;
Andrew Boie507852a2017-07-25 18:47:07 -0700614 k_thread_stack_t init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400615 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700616 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500617 void *init_p1;
618 void *init_p2;
619 void *init_p3;
620 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500621 u32_t init_options;
622 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500623 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500624 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400625};
626
Andrew Boied26cf2d2017-03-30 13:07:02 -0700627#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400628 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500629 prio, options, delay, abort, groups) \
630 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700631 .init_thread = (thread), \
632 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500633 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700634 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400635 .init_p1 = (void *)p1, \
636 .init_p2 = (void *)p2, \
637 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500638 .init_prio = (prio), \
639 .init_options = (options), \
640 .init_delay = (delay), \
641 .init_abort = (abort), \
642 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400643 }
644
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400645/**
Allan Stephensc98da842016-11-11 15:45:03 -0500646 * INTERNAL_HIDDEN @endcond
647 */
648
649/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500650 * @brief Statically define and initialize a thread.
651 *
652 * The thread may be scheduled for immediate execution or a delayed start.
653 *
654 * Thread options are architecture-specific, and can include K_ESSENTIAL,
655 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
656 * them using "|" (the logical OR operator).
657 *
658 * The ID of the thread can be accessed using:
659 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500660 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500661 *
662 * @param name Name of the thread.
663 * @param stack_size Stack size in bytes.
664 * @param entry Thread entry function.
665 * @param p1 1st entry point parameter.
666 * @param p2 2nd entry point parameter.
667 * @param p3 3rd entry point parameter.
668 * @param prio Thread priority.
669 * @param options Thread options.
670 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400671 *
672 * @internal It has been observed that the x86 compiler by default aligns
673 * these _static_thread_data structures to 32-byte boundaries, thereby
674 * wasting space. To work around this, force a 4-byte alignment.
675 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500676#define K_THREAD_DEFINE(name, stack_size, \
677 entry, p1, p2, p3, \
678 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700679 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700680 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500681 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500682 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700683 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
684 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500685 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700686 NULL, 0); \
687 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400688
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400689/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500690 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400691 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500692 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500694 * @param thread ID of thread whose priority is needed.
695 *
696 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400697 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500698extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400699
700/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500701 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400702 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500703 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400704 *
705 * Rescheduling can occur immediately depending on the priority @a thread is
706 * set to:
707 *
708 * - If its priority is raised above the priority of the caller of this
709 * function, and the caller is preemptible, @a thread will be scheduled in.
710 *
711 * - If the caller operates on itself, it lowers its priority below that of
712 * other threads in the system, and the caller is preemptible, the thread of
713 * highest priority will be scheduled in.
714 *
715 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
716 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
717 * highest priority.
718 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500719 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400720 * @param prio New priority.
721 *
722 * @warning Changing the priority of a thread currently involved in mutex
723 * priority inheritance may result in undefined behavior.
724 *
725 * @return N/A
726 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400727extern void k_thread_priority_set(k_tid_t thread, int prio);
728
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400729/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500730 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400731 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500732 * This routine prevents the kernel scheduler from making @a thread the
733 * current thread. All other internal operations on @a thread are still
734 * performed; for example, any timeout it is waiting on keeps ticking,
735 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400736 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500737 * If @a thread is already suspended, the routine has no effect.
738 *
739 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400740 *
741 * @return N/A
742 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400743extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400744
745/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500746 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400747 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500748 * This routine allows the kernel scheduler to make @a thread the current
749 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400750 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500751 * If @a thread is not currently suspended, the routine has no effect.
752 *
753 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400754 *
755 * @return N/A
756 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400757extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400758
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400759/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500760 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400761 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500762 * This routine specifies how the scheduler will perform time slicing of
763 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400764 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500765 * To enable time slicing, @a slice must be non-zero. The scheduler
766 * ensures that no thread runs for more than the specified time limit
767 * before other threads of that priority are given a chance to execute.
768 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700769 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400770 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500771 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400772 * execute. Once the scheduler selects a thread for execution, there is no
773 * minimum guaranteed time the thread will execute before threads of greater or
774 * equal priority are scheduled.
775 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500776 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400777 * for execution, this routine has no effect; the thread is immediately
778 * rescheduled after the slice period expires.
779 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500780 * To disable timeslicing, set both @a slice and @a prio to zero.
781 *
782 * @param slice Maximum time slice length (in milliseconds).
783 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400784 *
785 * @return N/A
786 */
Kumar Galacc334c72017-04-21 10:55:34 -0500787extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400788
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400789/**
Allan Stephensc98da842016-11-11 15:45:03 -0500790 * @} end defgroup thread_apis
791 */
792
793/**
794 * @addtogroup isr_apis
795 * @{
796 */
797
798/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500799 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400800 *
Allan Stephensc98da842016-11-11 15:45:03 -0500801 * This routine allows the caller to customize its actions, depending on
802 * whether it is a thread or an ISR.
803 *
804 * @note Can be called by ISRs.
805 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500806 * @return 0 if invoked by a thread.
807 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400808 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500809extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400810
Benjamin Walsh445830d2016-11-10 15:54:27 -0500811/**
812 * @brief Determine if code is running in a preemptible thread.
813 *
Allan Stephensc98da842016-11-11 15:45:03 -0500814 * This routine allows the caller to customize its actions, depending on
815 * whether it can be preempted by another thread. The routine returns a 'true'
816 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500817 *
Allan Stephensc98da842016-11-11 15:45:03 -0500818 * - The code is running in a thread, not at ISR.
819 * - The thread's priority is in the preemptible range.
820 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500821 *
Allan Stephensc98da842016-11-11 15:45:03 -0500822 * @note Can be called by ISRs.
823 *
824 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500825 * @return Non-zero if invoked by a preemptible thread.
826 */
827extern int k_is_preempt_thread(void);
828
Allan Stephensc98da842016-11-11 15:45:03 -0500829/**
830 * @} end addtogroup isr_apis
831 */
832
833/**
834 * @addtogroup thread_apis
835 * @{
836 */
837
838/**
839 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500840 *
Allan Stephensc98da842016-11-11 15:45:03 -0500841 * This routine prevents the current thread from being preempted by another
842 * thread by instructing the scheduler to treat it as a cooperative thread.
843 * If the thread subsequently performs an operation that makes it unready,
844 * it will be context switched out in the normal manner. When the thread
845 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500846 *
Allan Stephensc98da842016-11-11 15:45:03 -0500847 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500848 *
Allan Stephensc98da842016-11-11 15:45:03 -0500849 * @note k_sched_lock() and k_sched_unlock() should normally be used
850 * when the operation being performed can be safely interrupted by ISRs.
851 * However, if the amount of processing involved is very small, better
852 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500853 *
854 * @return N/A
855 */
856extern void k_sched_lock(void);
857
Allan Stephensc98da842016-11-11 15:45:03 -0500858/**
859 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500860 *
Allan Stephensc98da842016-11-11 15:45:03 -0500861 * This routine reverses the effect of a previous call to k_sched_lock().
862 * A thread must call the routine once for each time it called k_sched_lock()
863 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500864 *
865 * @return N/A
866 */
867extern void k_sched_unlock(void);
868
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400869/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500870 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400871 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500872 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400873 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500874 * Custom data is not used by the kernel itself, and is freely available
875 * for a thread to use as it sees fit. It can be used as a framework
876 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400877 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500878 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400879 *
880 * @return N/A
881 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400882extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400883
884/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500885 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400886 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500887 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400888 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500889 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400890 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400891extern void *k_thread_custom_data_get(void);
892
893/**
Allan Stephensc98da842016-11-11 15:45:03 -0500894 * @} end addtogroup thread_apis
895 */
896
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400897#include <sys_clock.h>
898
Allan Stephensc2f15a42016-11-17 12:24:22 -0500899/**
900 * @addtogroup clock_apis
901 * @{
902 */
903
904/**
905 * @brief Generate null timeout delay.
906 *
907 * This macro generates a timeout delay that that instructs a kernel API
908 * not to wait if the requested operation cannot be performed immediately.
909 *
910 * @return Timeout delay value.
911 */
912#define K_NO_WAIT 0
913
914/**
915 * @brief Generate timeout delay from milliseconds.
916 *
917 * This macro generates a timeout delay that that instructs a kernel API
918 * to wait up to @a ms milliseconds to perform the requested operation.
919 *
920 * @param ms Duration in milliseconds.
921 *
922 * @return Timeout delay value.
923 */
Johan Hedberg14471692016-11-13 10:52:15 +0200924#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500925
926/**
927 * @brief Generate timeout delay from seconds.
928 *
929 * This macro generates a timeout delay that that instructs a kernel API
930 * to wait up to @a s seconds to perform the requested operation.
931 *
932 * @param s Duration in seconds.
933 *
934 * @return Timeout delay value.
935 */
Johan Hedberg14471692016-11-13 10:52:15 +0200936#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500937
938/**
939 * @brief Generate timeout delay from minutes.
940 *
941 * This macro generates a timeout delay that that instructs a kernel API
942 * to wait up to @a m minutes to perform the requested operation.
943 *
944 * @param m Duration in minutes.
945 *
946 * @return Timeout delay value.
947 */
Johan Hedberg14471692016-11-13 10:52:15 +0200948#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500949
950/**
951 * @brief Generate timeout delay from hours.
952 *
953 * This macro generates a timeout delay that that instructs a kernel API
954 * to wait up to @a h hours to perform the requested operation.
955 *
956 * @param h Duration in hours.
957 *
958 * @return Timeout delay value.
959 */
Johan Hedberg14471692016-11-13 10:52:15 +0200960#define K_HOURS(h) K_MINUTES((h) * 60)
961
Allan Stephensc98da842016-11-11 15:45:03 -0500962/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500963 * @brief Generate infinite timeout delay.
964 *
965 * This macro generates a timeout delay that that instructs a kernel API
966 * to wait as long as necessary to perform the requested operation.
967 *
968 * @return Timeout delay value.
969 */
970#define K_FOREVER (-1)
971
972/**
973 * @} end addtogroup clock_apis
974 */
975
976/**
Allan Stephensc98da842016-11-11 15:45:03 -0500977 * @cond INTERNAL_HIDDEN
978 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400979
Benjamin Walsh62092182016-12-20 14:39:08 -0500980/* kernel clocks */
981
982#if (sys_clock_ticks_per_sec == 1000) || \
983 (sys_clock_ticks_per_sec == 500) || \
984 (sys_clock_ticks_per_sec == 250) || \
985 (sys_clock_ticks_per_sec == 125) || \
986 (sys_clock_ticks_per_sec == 100) || \
987 (sys_clock_ticks_per_sec == 50) || \
988 (sys_clock_ticks_per_sec == 25) || \
989 (sys_clock_ticks_per_sec == 20) || \
990 (sys_clock_ticks_per_sec == 10) || \
991 (sys_clock_ticks_per_sec == 1)
992
993 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
994#else
995 /* yields horrible 64-bit math on many architectures: try to avoid */
996 #define _NON_OPTIMIZED_TICKS_PER_SEC
997#endif
998
999#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001000extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001001#else
Kumar Galacc334c72017-04-21 10:55:34 -05001002static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001003{
Kumar Galacc334c72017-04-21 10:55:34 -05001004 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001005}
1006#endif
1007
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001008/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001009#ifdef CONFIG_TICKLESS_KERNEL
1010#define _TICK_ALIGN 0
1011#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001012#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001013#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001014
Kumar Galacc334c72017-04-21 10:55:34 -05001015static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001016{
Benjamin Walsh62092182016-12-20 14:39:08 -05001017#ifdef CONFIG_SYS_CLOCK_EXISTS
1018
1019#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001020 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001021#else
Kumar Galacc334c72017-04-21 10:55:34 -05001022 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001023#endif
1024
1025#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001026 __ASSERT(ticks == 0, "");
1027 return 0;
1028#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001029}
1030
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001031struct k_timer {
1032 /*
1033 * _timeout structure must be first here if we want to use
1034 * dynamic timer allocation. timeout.node is used in the double-linked
1035 * list of free timers
1036 */
1037 struct _timeout timeout;
1038
Allan Stephens45bfa372016-10-12 12:39:42 -05001039 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001040 _wait_q_t wait_q;
1041
1042 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001043 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001044
1045 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001046 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001047
1048 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001049 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001050
Allan Stephens45bfa372016-10-12 12:39:42 -05001051 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001052 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001053
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001054 /* user-specific data, also used to support legacy features */
1055 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001056
Anas Nashif2f203c22016-12-18 06:57:45 -05001057 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001058};
1059
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001060#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001061 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001062 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001063 .timeout.wait_q = NULL, \
1064 .timeout.thread = NULL, \
1065 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001066 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001067 .expiry_fn = expiry, \
1068 .stop_fn = stop, \
1069 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001070 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001071 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001072 }
1073
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001074#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1075
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001076/**
Allan Stephensc98da842016-11-11 15:45:03 -05001077 * INTERNAL_HIDDEN @endcond
1078 */
1079
1080/**
1081 * @defgroup timer_apis Timer APIs
1082 * @ingroup kernel_apis
1083 * @{
1084 */
1085
1086/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001087 * @typedef k_timer_expiry_t
1088 * @brief Timer expiry function type.
1089 *
1090 * A timer's expiry function is executed by the system clock interrupt handler
1091 * each time the timer expires. The expiry function is optional, and is only
1092 * invoked if the timer has been initialized with one.
1093 *
1094 * @param timer Address of timer.
1095 *
1096 * @return N/A
1097 */
1098typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1099
1100/**
1101 * @typedef k_timer_stop_t
1102 * @brief Timer stop function type.
1103 *
1104 * A timer's stop function is executed if the timer is stopped prematurely.
1105 * The function runs in the context of the thread that stops the timer.
1106 * The stop function is optional, and is only invoked if the timer has been
1107 * initialized with one.
1108 *
1109 * @param timer Address of timer.
1110 *
1111 * @return N/A
1112 */
1113typedef void (*k_timer_stop_t)(struct k_timer *timer);
1114
1115/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001116 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001117 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001118 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001119 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001120 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001121 *
1122 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001123 * @param expiry_fn Function to invoke each time the timer expires.
1124 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001125 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001126#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001127 struct k_timer name \
1128 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001129 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001130
Allan Stephens45bfa372016-10-12 12:39:42 -05001131/**
1132 * @brief Initialize a timer.
1133 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001134 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001135 *
1136 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001137 * @param expiry_fn Function to invoke each time the timer expires.
1138 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001139 *
1140 * @return N/A
1141 */
1142extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001143 k_timer_expiry_t expiry_fn,
1144 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001145
Allan Stephens45bfa372016-10-12 12:39:42 -05001146/**
1147 * @brief Start a timer.
1148 *
1149 * This routine starts a timer, and resets its status to zero. The timer
1150 * begins counting down using the specified duration and period values.
1151 *
1152 * Attempting to start a timer that is already running is permitted.
1153 * The timer's status is reset to zero and the timer begins counting down
1154 * using the new duration and period values.
1155 *
1156 * @param timer Address of timer.
1157 * @param duration Initial timer duration (in milliseconds).
1158 * @param period Timer period (in milliseconds).
1159 *
1160 * @return N/A
1161 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001162extern void k_timer_start(struct k_timer *timer,
Kumar Galacc334c72017-04-21 10:55:34 -05001163 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001164
1165/**
1166 * @brief Stop a timer.
1167 *
1168 * This routine stops a running timer prematurely. The timer's stop function,
1169 * if one exists, is invoked by the caller.
1170 *
1171 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001172 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001173 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001174 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1175 * if @a k_timer_stop is to be called from ISRs.
1176 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001177 * @param timer Address of timer.
1178 *
1179 * @return N/A
1180 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001181extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001182
1183/**
1184 * @brief Read timer status.
1185 *
1186 * This routine reads the timer's status, which indicates the number of times
1187 * it has expired since its status was last read.
1188 *
1189 * Calling this routine resets the timer's status to zero.
1190 *
1191 * @param timer Address of timer.
1192 *
1193 * @return Timer status.
1194 */
Kumar Galacc334c72017-04-21 10:55:34 -05001195extern u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001196
1197/**
1198 * @brief Synchronize thread to timer expiration.
1199 *
1200 * This routine blocks the calling thread until the timer's status is non-zero
1201 * (indicating that it has expired at least once since it was last examined)
1202 * or the timer is stopped. If the timer status is already non-zero,
1203 * or the timer is already stopped, the caller continues without waiting.
1204 *
1205 * Calling this routine resets the timer's status to zero.
1206 *
1207 * This routine must not be used by interrupt handlers, since they are not
1208 * allowed to block.
1209 *
1210 * @param timer Address of timer.
1211 *
1212 * @return Timer status.
1213 */
Kumar Galacc334c72017-04-21 10:55:34 -05001214extern u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001215
1216/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001217 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001218 *
1219 * This routine computes the (approximate) time remaining before a running
1220 * timer next expires. If the timer is not running, it returns zero.
1221 *
1222 * @param timer Address of timer.
1223 *
1224 * @return Remaining time (in milliseconds).
1225 */
Kumar Galacc334c72017-04-21 10:55:34 -05001226static inline s32_t k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001227{
1228 return _timeout_remaining_get(&timer->timeout);
1229}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001230
Allan Stephensc98da842016-11-11 15:45:03 -05001231/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001232 * @brief Associate user-specific data with a timer.
1233 *
1234 * This routine records the @a user_data with the @a timer, to be retrieved
1235 * later.
1236 *
1237 * It can be used e.g. in a timer handler shared across multiple subsystems to
1238 * retrieve data specific to the subsystem this timer is associated with.
1239 *
1240 * @param timer Address of timer.
1241 * @param user_data User data to associate with the timer.
1242 *
1243 * @return N/A
1244 */
1245static inline void k_timer_user_data_set(struct k_timer *timer,
1246 void *user_data)
1247{
1248 timer->user_data = user_data;
1249}
1250
1251/**
1252 * @brief Retrieve the user-specific data from a timer.
1253 *
1254 * @param timer Address of timer.
1255 *
1256 * @return The user data.
1257 */
1258static inline void *k_timer_user_data_get(struct k_timer *timer)
1259{
1260 return timer->user_data;
1261}
1262
1263/**
Allan Stephensc98da842016-11-11 15:45:03 -05001264 * @} end defgroup timer_apis
1265 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001266
Allan Stephensc98da842016-11-11 15:45:03 -05001267/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001268 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001269 * @{
1270 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001271
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001272/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001273 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001274 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001275 * This routine returns the elapsed time since the system booted,
1276 * in milliseconds.
1277 *
1278 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001279 */
Kumar Galacc334c72017-04-21 10:55:34 -05001280extern s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001281
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001282#ifdef CONFIG_TICKLESS_KERNEL
1283/**
1284 * @brief Enable clock always on in tickless kernel
1285 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001286 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001287 * there are no timer events programmed in tickless kernel
1288 * scheduling. This is necessary if the clock is used to track
1289 * passage of time.
1290 *
1291 * @retval prev_status Previous status of always on flag
1292 */
1293static inline int k_enable_sys_clock_always_on(void)
1294{
1295 int prev_status = _sys_clock_always_on;
1296
1297 _sys_clock_always_on = 1;
1298 _enable_sys_clock();
1299
1300 return prev_status;
1301}
1302
1303/**
1304 * @brief Disable clock always on in tickless kernel
1305 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001306 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001307 * there are no timer events programmed in tickless kernel
1308 * scheduling. To save power, this routine should be called
1309 * immediately when clock is not used to track time.
1310 */
1311static inline void k_disable_sys_clock_always_on(void)
1312{
1313 _sys_clock_always_on = 0;
1314}
1315#else
1316#define k_enable_sys_clock_always_on() do { } while ((0))
1317#define k_disable_sys_clock_always_on() do { } while ((0))
1318#endif
1319
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001320/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001321 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001322 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001323 * This routine returns the lower 32-bits of the elapsed time since the system
1324 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001325 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001326 * This routine can be more efficient than k_uptime_get(), as it reduces the
1327 * need for interrupt locking and 64-bit math. However, the 32-bit result
1328 * cannot hold a system uptime time larger than approximately 50 days, so the
1329 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001330 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001331 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001332 */
Kumar Galacc334c72017-04-21 10:55:34 -05001333extern u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001334
1335/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001336 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001337 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001338 * This routine computes the elapsed time between the current system uptime
1339 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001340 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001341 * @param reftime Pointer to a reference time, which is updated to the current
1342 * uptime upon return.
1343 *
1344 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001345 */
Kumar Galacc334c72017-04-21 10:55:34 -05001346extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001347
1348/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001349 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001350 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001351 * This routine computes the elapsed time between the current system uptime
1352 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001353 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001354 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1355 * need for interrupt locking and 64-bit math. However, the 32-bit result
1356 * cannot hold an elapsed time larger than approximately 50 days, so the
1357 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001358 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001359 * @param reftime Pointer to a reference time, which is updated to the current
1360 * uptime upon return.
1361 *
1362 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001363 */
Kumar Galacc334c72017-04-21 10:55:34 -05001364extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001365
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001366/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001367 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001368 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001369 * This routine returns the current time, as measured by the system's hardware
1370 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001371 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001372 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001373 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001374#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001375
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001376/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001377 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001378 */
1379
Allan Stephensc98da842016-11-11 15:45:03 -05001380/**
1381 * @cond INTERNAL_HIDDEN
1382 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001383
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001384struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001385 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001386 union {
1387 _wait_q_t wait_q;
1388
1389 _POLL_EVENT;
1390 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001391
1392 _OBJECT_TRACING_NEXT_PTR(k_queue);
1393};
1394
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001395#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001396 { \
1397 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1398 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001399 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001400 _OBJECT_TRACING_INIT \
1401 }
1402
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001403#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1404
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001405/**
1406 * INTERNAL_HIDDEN @endcond
1407 */
1408
1409/**
1410 * @defgroup queue_apis Queue APIs
1411 * @ingroup kernel_apis
1412 * @{
1413 */
1414
1415/**
1416 * @brief Initialize a queue.
1417 *
1418 * This routine initializes a queue object, prior to its first use.
1419 *
1420 * @param queue Address of the queue.
1421 *
1422 * @return N/A
1423 */
1424extern void k_queue_init(struct k_queue *queue);
1425
1426/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001427 * @brief Cancel waiting on a queue.
1428 *
1429 * This routine causes first thread pending on @a queue, if any, to
1430 * return from k_queue_get() call with NULL value (as if timeout expired).
1431 *
1432 * @note Can be called by ISRs.
1433 *
1434 * @param queue Address of the queue.
1435 *
1436 * @return N/A
1437 */
1438extern void k_queue_cancel_wait(struct k_queue *queue);
1439
1440/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001441 * @brief Append an element to the end of a queue.
1442 *
1443 * This routine appends a data item to @a queue. A queue data item must be
1444 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1445 * reserved for the kernel's use.
1446 *
1447 * @note Can be called by ISRs.
1448 *
1449 * @param queue Address of the queue.
1450 * @param data Address of the data item.
1451 *
1452 * @return N/A
1453 */
1454extern void k_queue_append(struct k_queue *queue, void *data);
1455
1456/**
1457 * @brief Prepend an element to a queue.
1458 *
1459 * This routine prepends a data item to @a queue. A queue data item must be
1460 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1461 * reserved for the kernel's use.
1462 *
1463 * @note Can be called by ISRs.
1464 *
1465 * @param queue Address of the queue.
1466 * @param data Address of the data item.
1467 *
1468 * @return N/A
1469 */
1470extern void k_queue_prepend(struct k_queue *queue, void *data);
1471
1472/**
1473 * @brief Inserts an element to a queue.
1474 *
1475 * This routine inserts a data item to @a queue after previous item. A queue
1476 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1477 * item are reserved for the kernel's use.
1478 *
1479 * @note Can be called by ISRs.
1480 *
1481 * @param queue Address of the queue.
1482 * @param prev Address of the previous data item.
1483 * @param data Address of the data item.
1484 *
1485 * @return N/A
1486 */
1487extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1488
1489/**
1490 * @brief Atomically append a list of elements to a queue.
1491 *
1492 * This routine adds a list of data items to @a queue in one operation.
1493 * The data items must be in a singly-linked list, with the first 32 bits
1494 * in each data item pointing to the next data item; the list must be
1495 * NULL-terminated.
1496 *
1497 * @note Can be called by ISRs.
1498 *
1499 * @param queue Address of the queue.
1500 * @param head Pointer to first node in singly-linked list.
1501 * @param tail Pointer to last node in singly-linked list.
1502 *
1503 * @return N/A
1504 */
1505extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1506
1507/**
1508 * @brief Atomically add a list of elements to a queue.
1509 *
1510 * This routine adds a list of data items to @a queue in one operation.
1511 * The data items must be in a singly-linked list implemented using a
1512 * sys_slist_t object. Upon completion, the original list is empty.
1513 *
1514 * @note Can be called by ISRs.
1515 *
1516 * @param queue Address of the queue.
1517 * @param list Pointer to sys_slist_t object.
1518 *
1519 * @return N/A
1520 */
1521extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1522
1523/**
1524 * @brief Get an element from a queue.
1525 *
1526 * This routine removes first data item from @a queue. The first 32 bits of the
1527 * data item are reserved for the kernel's use.
1528 *
1529 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1530 *
1531 * @param queue Address of the queue.
1532 * @param timeout Waiting period to obtain a data item (in milliseconds),
1533 * or one of the special values K_NO_WAIT and K_FOREVER.
1534 *
1535 * @return Address of the data item if successful; NULL if returned
1536 * without waiting, or waiting period timed out.
1537 */
Kumar Galacc334c72017-04-21 10:55:34 -05001538extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001539
1540/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001541 * @brief Remove an element from a queue.
1542 *
1543 * This routine removes data item from @a queue. The first 32 bits of the
1544 * data item are reserved for the kernel's use. Removing elements from k_queue
1545 * rely on sys_slist_find_and_remove which is not a constant time operation.
1546 *
1547 * @note Can be called by ISRs
1548 *
1549 * @param queue Address of the queue.
1550 * @param data Address of the data item.
1551 *
1552 * @return true if data item was removed
1553 */
1554static inline bool k_queue_remove(struct k_queue *queue, void *data)
1555{
1556 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1557}
1558
1559/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001560 * @brief Query a queue to see if it has data available.
1561 *
1562 * Note that the data might be already gone by the time this function returns
1563 * if other threads are also trying to read from the queue.
1564 *
1565 * @note Can be called by ISRs.
1566 *
1567 * @param queue Address of the queue.
1568 *
1569 * @return Non-zero if the queue is empty.
1570 * @return 0 if data is available.
1571 */
1572static inline int k_queue_is_empty(struct k_queue *queue)
1573{
1574 return (int)sys_slist_is_empty(&queue->data_q);
1575}
1576
1577/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001578 * @brief Peek element at the head of queue.
1579 *
1580 * Return element from the head of queue without removing it.
1581 *
1582 * @param queue Address of the queue.
1583 *
1584 * @return Head element, or NULL if queue is empty.
1585 */
1586static inline void *k_queue_peek_head(struct k_queue *queue)
1587{
1588 return sys_slist_peek_head(&queue->data_q);
1589}
1590
1591/**
1592 * @brief Peek element at the tail of queue.
1593 *
1594 * Return element from the tail of queue without removing it.
1595 *
1596 * @param queue Address of the queue.
1597 *
1598 * @return Tail element, or NULL if queue is empty.
1599 */
1600static inline void *k_queue_peek_tail(struct k_queue *queue)
1601{
1602 return sys_slist_peek_tail(&queue->data_q);
1603}
1604
1605/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001606 * @brief Statically define and initialize a queue.
1607 *
1608 * The queue can be accessed outside the module where it is defined using:
1609 *
1610 * @code extern struct k_queue <name>; @endcode
1611 *
1612 * @param name Name of the queue.
1613 */
1614#define K_QUEUE_DEFINE(name) \
1615 struct k_queue name \
1616 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001617 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001618
1619/**
1620 * @} end defgroup queue_apis
1621 */
1622
1623/**
1624 * @cond INTERNAL_HIDDEN
1625 */
1626
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001627struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001628 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001629};
1630
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001631#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001632 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001633 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001634 }
1635
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001636#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1637
Allan Stephensc98da842016-11-11 15:45:03 -05001638/**
1639 * INTERNAL_HIDDEN @endcond
1640 */
1641
1642/**
1643 * @defgroup fifo_apis Fifo APIs
1644 * @ingroup kernel_apis
1645 * @{
1646 */
1647
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001648/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001649 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001650 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001651 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001652 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001653 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001654 *
1655 * @return N/A
1656 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001657#define k_fifo_init(fifo) \
1658 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001659
1660/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001661 * @brief Cancel waiting on a fifo.
1662 *
1663 * This routine causes first thread pending on @a fifo, if any, to
1664 * return from k_fifo_get() call with NULL value (as if timeout
1665 * expired).
1666 *
1667 * @note Can be called by ISRs.
1668 *
1669 * @param fifo Address of the fifo.
1670 *
1671 * @return N/A
1672 */
1673#define k_fifo_cancel_wait(fifo) \
1674 k_queue_cancel_wait((struct k_queue *) fifo)
1675
1676/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001677 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001678 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001679 * This routine adds a data item to @a fifo. A fifo data item must be
1680 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1681 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001682 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001683 * @note Can be called by ISRs.
1684 *
1685 * @param fifo Address of the fifo.
1686 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001687 *
1688 * @return N/A
1689 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001690#define k_fifo_put(fifo, data) \
1691 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001692
1693/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001694 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001695 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001696 * This routine adds a list of data items to @a fifo in one operation.
1697 * The data items must be in a singly-linked list, with the first 32 bits
1698 * each data item pointing to the next data item; the list must be
1699 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001700 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001701 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001702 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001703 * @param fifo Address of the fifo.
1704 * @param head Pointer to first node in singly-linked list.
1705 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001706 *
1707 * @return N/A
1708 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001709#define k_fifo_put_list(fifo, head, tail) \
1710 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001711
1712/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001713 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001714 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001715 * This routine adds a list of data items to @a fifo in one operation.
1716 * The data items must be in a singly-linked list implemented using a
1717 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001718 * and must be re-initialized via sys_slist_init().
1719 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001720 * @note Can be called by ISRs.
1721 *
1722 * @param fifo Address of the fifo.
1723 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001724 *
1725 * @return N/A
1726 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001727#define k_fifo_put_slist(fifo, list) \
1728 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001729
1730/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001731 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001732 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001733 * This routine removes a data item from @a fifo in a "first in, first out"
1734 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001735 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001736 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1737 *
1738 * @param fifo Address of the fifo.
1739 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001740 * or one of the special values K_NO_WAIT and K_FOREVER.
1741 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001742 * @return Address of the data item if successful; NULL if returned
1743 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001744 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001745#define k_fifo_get(fifo, timeout) \
1746 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001747
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001748/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001749 * @brief Query a fifo to see if it has data available.
1750 *
1751 * Note that the data might be already gone by the time this function returns
1752 * if other threads is also trying to read from the fifo.
1753 *
1754 * @note Can be called by ISRs.
1755 *
1756 * @param fifo Address of the fifo.
1757 *
1758 * @return Non-zero if the fifo is empty.
1759 * @return 0 if data is available.
1760 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001761#define k_fifo_is_empty(fifo) \
1762 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001763
1764/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001765 * @brief Peek element at the head of fifo.
1766 *
1767 * Return element from the head of fifo without removing it. A usecase
1768 * for this is if elements of the fifo are themselves containers. Then
1769 * on each iteration of processing, a head container will be peeked,
1770 * and some data processed out of it, and only if the container is empty,
1771 * it will be completely remove from the fifo.
1772 *
1773 * @param fifo Address of the fifo.
1774 *
1775 * @return Head element, or NULL if the fifo is empty.
1776 */
1777#define k_fifo_peek_head(fifo) \
1778 k_queue_peek_head((struct k_queue *) fifo)
1779
1780/**
1781 * @brief Peek element at the tail of fifo.
1782 *
1783 * Return element from the tail of fifo (without removing it). A usecase
1784 * for this is if elements of the fifo are themselves containers. Then
1785 * it may be useful to add more data to the last container in fifo.
1786 *
1787 * @param fifo Address of the fifo.
1788 *
1789 * @return Tail element, or NULL if fifo is empty.
1790 */
1791#define k_fifo_peek_tail(fifo) \
1792 k_queue_peek_tail((struct k_queue *) fifo)
1793
1794/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001795 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001796 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001797 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001798 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001799 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001800 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001801 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001802 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001803#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001804 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001805 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001806 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001807
Allan Stephensc98da842016-11-11 15:45:03 -05001808/**
1809 * @} end defgroup fifo_apis
1810 */
1811
1812/**
1813 * @cond INTERNAL_HIDDEN
1814 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001815
1816struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001817 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001818};
1819
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001820#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001821 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001822 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001823 }
1824
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001825#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1826
Allan Stephensc98da842016-11-11 15:45:03 -05001827/**
1828 * INTERNAL_HIDDEN @endcond
1829 */
1830
1831/**
1832 * @defgroup lifo_apis Lifo APIs
1833 * @ingroup kernel_apis
1834 * @{
1835 */
1836
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001837/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001838 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001839 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001840 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001841 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001842 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001843 *
1844 * @return N/A
1845 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001846#define k_lifo_init(lifo) \
1847 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001848
1849/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001850 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001852 * This routine adds a data item to @a lifo. A lifo data item must be
1853 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1854 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001856 * @note Can be called by ISRs.
1857 *
1858 * @param lifo Address of the lifo.
1859 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001860 *
1861 * @return N/A
1862 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001863#define k_lifo_put(lifo, data) \
1864 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001865
1866/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001867 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001868 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001869 * This routine removes a data item from @a lifo in a "last in, first out"
1870 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001871 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001872 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1873 *
1874 * @param lifo Address of the lifo.
1875 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001876 * or one of the special values K_NO_WAIT and K_FOREVER.
1877 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001878 * @return Address of the data item if successful; NULL if returned
1879 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001880 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001881#define k_lifo_get(lifo, timeout) \
1882 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001883
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001884/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001885 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001886 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001887 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001888 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001889 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001890 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001891 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001892 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001893#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001894 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001895 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001896 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001897
Allan Stephensc98da842016-11-11 15:45:03 -05001898/**
1899 * @} end defgroup lifo_apis
1900 */
1901
1902/**
1903 * @cond INTERNAL_HIDDEN
1904 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001905
1906struct k_stack {
1907 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001908 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001909
Anas Nashif2f203c22016-12-18 06:57:45 -05001910 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001911};
1912
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001913#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05001914 { \
1915 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1916 .base = stack_buffer, \
1917 .next = stack_buffer, \
1918 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001919 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001920 }
1921
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001922#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
1923
Allan Stephensc98da842016-11-11 15:45:03 -05001924/**
1925 * INTERNAL_HIDDEN @endcond
1926 */
1927
1928/**
1929 * @defgroup stack_apis Stack APIs
1930 * @ingroup kernel_apis
1931 * @{
1932 */
1933
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001934/**
1935 * @brief Initialize a stack.
1936 *
1937 * This routine initializes a stack object, prior to its first use.
1938 *
1939 * @param stack Address of the stack.
1940 * @param buffer Address of array used to hold stacked values.
1941 * @param num_entries Maximum number of values that can be stacked.
1942 *
1943 * @return N/A
1944 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001945extern void k_stack_init(struct k_stack *stack,
Kumar Galacc334c72017-04-21 10:55:34 -05001946 u32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001947
1948/**
1949 * @brief Push an element onto a stack.
1950 *
1951 * This routine adds a 32-bit value @a data to @a stack.
1952 *
1953 * @note Can be called by ISRs.
1954 *
1955 * @param stack Address of the stack.
1956 * @param data Value to push onto the stack.
1957 *
1958 * @return N/A
1959 */
Kumar Galacc334c72017-04-21 10:55:34 -05001960extern void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001961
1962/**
1963 * @brief Pop an element from a stack.
1964 *
1965 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1966 * manner and stores the value in @a data.
1967 *
1968 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1969 *
1970 * @param stack Address of the stack.
1971 * @param data Address of area to hold the value popped from the stack.
1972 * @param timeout Waiting period to obtain a value (in milliseconds),
1973 * or one of the special values K_NO_WAIT and K_FOREVER.
1974 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001975 * @retval 0 Element popped from stack.
1976 * @retval -EBUSY Returned without waiting.
1977 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001978 */
Kumar Galacc334c72017-04-21 10:55:34 -05001979extern int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001980
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001981/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001982 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001983 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001984 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001985 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001986 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001987 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001988 * @param name Name of the stack.
1989 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001990 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001991#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05001992 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001993 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001994 struct k_stack name \
1995 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001996 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001997 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001998
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001999/**
Allan Stephensc98da842016-11-11 15:45:03 -05002000 * @} end defgroup stack_apis
2001 */
2002
Allan Stephens6bba9b02016-11-16 14:56:54 -05002003struct k_work;
2004
Allan Stephensc98da842016-11-11 15:45:03 -05002005/**
2006 * @defgroup workqueue_apis Workqueue Thread APIs
2007 * @ingroup kernel_apis
2008 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002009 */
2010
Allan Stephens6bba9b02016-11-16 14:56:54 -05002011/**
2012 * @typedef k_work_handler_t
2013 * @brief Work item handler function type.
2014 *
2015 * A work item's handler function is executed by a workqueue's thread
2016 * when the work item is processed by the workqueue.
2017 *
2018 * @param work Address of the work item.
2019 *
2020 * @return N/A
2021 */
2022typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002023
2024/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002025 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002026 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002027
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002028struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002029 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002030 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002031};
2032
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002033enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002034 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002035};
2036
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002037struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002038 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002039 k_work_handler_t handler;
2040 atomic_t flags[1];
2041};
2042
Allan Stephens6bba9b02016-11-16 14:56:54 -05002043struct k_delayed_work {
2044 struct k_work work;
2045 struct _timeout timeout;
2046 struct k_work_q *work_q;
2047};
2048
2049extern struct k_work_q k_sys_work_q;
2050
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002051/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002052 * INTERNAL_HIDDEN @endcond
2053 */
2054
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002055#define _K_WORK_INITIALIZER(work_handler) \
2056 { \
2057 ._reserved = NULL, \
2058 .handler = work_handler, \
2059 .flags = { 0 } \
2060 }
2061
2062#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2063
Allan Stephens6bba9b02016-11-16 14:56:54 -05002064/**
2065 * @brief Initialize a statically-defined work item.
2066 *
2067 * This macro can be used to initialize a statically-defined workqueue work
2068 * item, prior to its first use. For example,
2069 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002070 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002071 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002072 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002073 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002074 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002075#define K_WORK_DEFINE(work, work_handler) \
2076 struct k_work work \
2077 __in_section(_k_work, static, work) = \
2078 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002079
2080/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002081 * @brief Initialize a work item.
2082 *
2083 * This routine initializes a workqueue work item, prior to its first use.
2084 *
2085 * @param work Address of work item.
2086 * @param handler Function to invoke each time work item is processed.
2087 *
2088 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002089 */
2090static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2091{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002092 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002093 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002094 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002095}
2096
2097/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002098 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002099 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002100 * This routine submits work item @a work to be processed by workqueue
2101 * @a work_q. If the work item is already pending in the workqueue's queue
2102 * as a result of an earlier submission, this routine has no effect on the
2103 * work item. If the work item has already been processed, or is currently
2104 * being processed, its work is considered complete and the work item can be
2105 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002106 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002107 * @warning
2108 * A submitted work item must not be modified until it has been processed
2109 * by the workqueue.
2110 *
2111 * @note Can be called by ISRs.
2112 *
2113 * @param work_q Address of workqueue.
2114 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002115 *
2116 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002117 */
2118static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2119 struct k_work *work)
2120{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002121 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002122 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002123 }
2124}
2125
2126/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002127 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002128 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002129 * This routine indicates if work item @a work is pending in a workqueue's
2130 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002131 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002132 * @note Can be called by ISRs.
2133 *
2134 * @param work Address of work item.
2135 *
2136 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002137 */
2138static inline int k_work_pending(struct k_work *work)
2139{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002140 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002141}
2142
2143/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002144 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002145 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002146 * This routine starts workqueue @a work_q. The workqueue spawns its work
2147 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002148 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002149 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002150 * @param stack Pointer to work queue thread's stack space, as defined by
2151 * K_THREAD_STACK_DEFINE()
2152 * @param stack_size Size of the work queue thread's stack (in bytes), which
2153 * should either be the same constant passed to
2154 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002155 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002156 *
2157 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002158 */
Andrew Boie507852a2017-07-25 18:47:07 -07002159extern void k_work_q_start(struct k_work_q *work_q,
2160 k_thread_stack_t stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002161 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002162
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002163/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002164 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002165 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002166 * This routine initializes a workqueue delayed work item, prior to
2167 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002168 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002169 * @param work Address of delayed work item.
2170 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002171 *
2172 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002173 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002174extern void k_delayed_work_init(struct k_delayed_work *work,
2175 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002176
2177/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002178 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002179 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002180 * This routine schedules work item @a work to be processed by workqueue
2181 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002182 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002183 * Only when the countdown completes is the work item actually submitted to
2184 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002185 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002186 * Submitting a previously submitted delayed work item that is still
2187 * counting down cancels the existing submission and restarts the countdown
2188 * using the new delay. If the work item is currently pending on the
2189 * workqueue's queue because the countdown has completed it is too late to
2190 * resubmit the item, and resubmission fails without impacting the work item.
2191 * If the work item has already been processed, or is currently being processed,
2192 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002193 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002194 * @warning
2195 * A delayed work item must not be modified until it has been processed
2196 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002197 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002198 * @note Can be called by ISRs.
2199 *
2200 * @param work_q Address of workqueue.
2201 * @param work Address of delayed work item.
2202 * @param delay Delay before submitting the work item (in milliseconds).
2203 *
2204 * @retval 0 Work item countdown started.
2205 * @retval -EINPROGRESS Work item is already pending.
2206 * @retval -EINVAL Work item is being processed or has completed its work.
2207 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002208 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002209extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2210 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002211 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002212
2213/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002214 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002215 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002216 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002217 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002218 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002219 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002220 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002221 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002222 * @param work Address of delayed work item.
2223 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002224 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002225 * @retval -EINPROGRESS Work item is already pending.
2226 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002227 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002228extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002229
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002230/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002231 * @brief Submit a work item to the system workqueue.
2232 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002233 * This routine submits work item @a work to be processed by the system
2234 * workqueue. If the work item is already pending in the workqueue's queue
2235 * as a result of an earlier submission, this routine has no effect on the
2236 * work item. If the work item has already been processed, or is currently
2237 * being processed, its work is considered complete and the work item can be
2238 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002239 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002240 * @warning
2241 * Work items submitted to the system workqueue should avoid using handlers
2242 * that block or yield since this may prevent the system workqueue from
2243 * processing other work items in a timely manner.
2244 *
2245 * @note Can be called by ISRs.
2246 *
2247 * @param work Address of work item.
2248 *
2249 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002250 */
2251static inline void k_work_submit(struct k_work *work)
2252{
2253 k_work_submit_to_queue(&k_sys_work_q, work);
2254}
2255
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002256/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002257 * @brief Submit a delayed work item to the system workqueue.
2258 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002259 * This routine schedules work item @a work to be processed by the system
2260 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002261 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002262 * Only when the countdown completes is the work item actually submitted to
2263 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002264 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002265 * Submitting a previously submitted delayed work item that is still
2266 * counting down cancels the existing submission and restarts the countdown
2267 * using the new delay. If the work item is currently pending on the
2268 * workqueue's queue because the countdown has completed it is too late to
2269 * resubmit the item, and resubmission fails without impacting the work item.
2270 * If the work item has already been processed, or is currently being processed,
2271 * its work is considered complete and the work item can be resubmitted.
2272 *
2273 * @warning
2274 * Work items submitted to the system workqueue should avoid using handlers
2275 * that block or yield since this may prevent the system workqueue from
2276 * processing other work items in a timely manner.
2277 *
2278 * @note Can be called by ISRs.
2279 *
2280 * @param work Address of delayed work item.
2281 * @param delay Delay before submitting the work item (in milliseconds).
2282 *
2283 * @retval 0 Work item countdown started.
2284 * @retval -EINPROGRESS Work item is already pending.
2285 * @retval -EINVAL Work item is being processed or has completed its work.
2286 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002287 */
2288static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002289 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002290{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002291 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002292}
2293
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002294/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002295 * @brief Get time remaining before a delayed work gets scheduled.
2296 *
2297 * This routine computes the (approximate) time remaining before a
2298 * delayed work gets executed. If the delayed work is not waiting to be
2299 * schedules, it returns zero.
2300 *
2301 * @param work Delayed work item.
2302 *
2303 * @return Remaining time (in milliseconds).
2304 */
Kumar Galacc334c72017-04-21 10:55:34 -05002305static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002306{
2307 return _timeout_remaining_get(&work->timeout);
2308}
2309
2310/**
Allan Stephensc98da842016-11-11 15:45:03 -05002311 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002312 */
2313
Allan Stephensc98da842016-11-11 15:45:03 -05002314/**
2315 * @cond INTERNAL_HIDDEN
2316 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002317
2318struct k_mutex {
2319 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002320 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002321 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002322 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002323
Anas Nashif2f203c22016-12-18 06:57:45 -05002324 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002325};
2326
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002327#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002328 { \
2329 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2330 .owner = NULL, \
2331 .lock_count = 0, \
2332 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002333 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002334 }
2335
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002336#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2337
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002338/**
Allan Stephensc98da842016-11-11 15:45:03 -05002339 * INTERNAL_HIDDEN @endcond
2340 */
2341
2342/**
2343 * @defgroup mutex_apis Mutex APIs
2344 * @ingroup kernel_apis
2345 * @{
2346 */
2347
2348/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002349 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002350 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002351 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002352 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002353 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002355 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002356 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002357#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002358 struct k_mutex name \
2359 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002360 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002361
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002362/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002363 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002364 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002365 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002366 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002367 * Upon completion, the mutex is available and does not have an owner.
2368 *
2369 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002370 *
2371 * @return N/A
2372 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002373extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002374
2375/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002376 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002377 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002378 * This routine locks @a mutex. If the mutex is locked by another thread,
2379 * the calling thread waits until the mutex becomes available or until
2380 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002381 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002382 * A thread is permitted to lock a mutex it has already locked. The operation
2383 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002384 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002385 * @param mutex Address of the mutex.
2386 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002387 * or one of the special values K_NO_WAIT and K_FOREVER.
2388 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002389 * @retval 0 Mutex locked.
2390 * @retval -EBUSY Returned without waiting.
2391 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002392 */
Kumar Galacc334c72017-04-21 10:55:34 -05002393extern int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002394
2395/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002396 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002397 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002398 * This routine unlocks @a mutex. The mutex must already be locked by the
2399 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002400 *
2401 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002402 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002403 * thread.
2404 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002405 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002406 *
2407 * @return N/A
2408 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002409extern void k_mutex_unlock(struct k_mutex *mutex);
2410
Allan Stephensc98da842016-11-11 15:45:03 -05002411/**
2412 * @} end defgroup mutex_apis
2413 */
2414
2415/**
2416 * @cond INTERNAL_HIDDEN
2417 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002418
2419struct k_sem {
2420 _wait_q_t wait_q;
2421 unsigned int count;
2422 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002423 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002424
Anas Nashif2f203c22016-12-18 06:57:45 -05002425 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002426};
2427
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002428#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002429 { \
2430 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2431 .count = initial_count, \
2432 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002433 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002434 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002435 }
2436
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002437#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2438
Allan Stephensc98da842016-11-11 15:45:03 -05002439/**
2440 * INTERNAL_HIDDEN @endcond
2441 */
2442
2443/**
2444 * @defgroup semaphore_apis Semaphore APIs
2445 * @ingroup kernel_apis
2446 * @{
2447 */
2448
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002449/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002450 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002451 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002452 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002453 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002454 * @param sem Address of the semaphore.
2455 * @param initial_count Initial semaphore count.
2456 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002457 *
2458 * @return N/A
2459 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002460extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2461 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002462
2463/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002464 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002465 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002466 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002467 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002468 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2469 *
2470 * @param sem Address of the semaphore.
2471 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002472 * or one of the special values K_NO_WAIT and K_FOREVER.
2473 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002474 * @note When porting code from the nanokernel legacy API to the new API, be
2475 * careful with the return value of this function. The return value is the
2476 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2477 * non-zero means failure, while the nano_sem_take family returns 1 for success
2478 * and 0 for failure.
2479 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002480 * @retval 0 Semaphore taken.
2481 * @retval -EBUSY Returned without waiting.
2482 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002483 */
Kumar Galacc334c72017-04-21 10:55:34 -05002484extern int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002485
2486/**
2487 * @brief Give a semaphore.
2488 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002489 * This routine gives @a sem, unless the semaphore is already at its maximum
2490 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002491 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002492 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002493 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002494 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002495 *
2496 * @return N/A
2497 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002498extern void k_sem_give(struct k_sem *sem);
2499
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002500/**
2501 * @brief Reset a semaphore's count to zero.
2502 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002503 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002504 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002505 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002506 *
2507 * @return N/A
2508 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04002509static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002510{
2511 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002512}
2513
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002514/**
2515 * @brief Get a semaphore's count.
2516 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002517 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002518 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002519 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002520 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002521 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002522 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02002523static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002524{
2525 return sem->count;
2526}
2527
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002528/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002529 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002530 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002531 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002532 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002533 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002534 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002535 * @param name Name of the semaphore.
2536 * @param initial_count Initial semaphore count.
2537 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002538 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002539#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002540 struct k_sem name \
2541 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002542 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002543
Allan Stephensc98da842016-11-11 15:45:03 -05002544/**
2545 * @} end defgroup semaphore_apis
2546 */
2547
2548/**
2549 * @defgroup alert_apis Alert APIs
2550 * @ingroup kernel_apis
2551 * @{
2552 */
2553
Allan Stephens5eceb852016-11-16 10:16:30 -05002554/**
2555 * @typedef k_alert_handler_t
2556 * @brief Alert handler function type.
2557 *
2558 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002559 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002560 * and is only invoked if the alert has been initialized with one.
2561 *
2562 * @param alert Address of the alert.
2563 *
2564 * @return 0 if alert has been consumed; non-zero if alert should pend.
2565 */
2566typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002567
2568/**
2569 * @} end defgroup alert_apis
2570 */
2571
2572/**
2573 * @cond INTERNAL_HIDDEN
2574 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002575
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002576#define K_ALERT_DEFAULT NULL
2577#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002578
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002579struct k_alert {
2580 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002581 atomic_t send_count;
2582 struct k_work work_item;
2583 struct k_sem sem;
2584
Anas Nashif2f203c22016-12-18 06:57:45 -05002585 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002586};
2587
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002588extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002589
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002590#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002591 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002592 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002593 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002594 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2595 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002596 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002597 }
2598
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002599#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2600
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002601/**
Allan Stephensc98da842016-11-11 15:45:03 -05002602 * INTERNAL_HIDDEN @endcond
2603 */
2604
2605/**
2606 * @addtogroup alert_apis
2607 * @{
2608 */
2609
2610/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002611 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002612 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002613 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002614 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002615 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002616 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002617 * @param name Name of the alert.
2618 * @param alert_handler Action to take when alert is sent. Specify either
2619 * the address of a function to be invoked by the system workqueue
2620 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2621 * K_ALERT_DEFAULT (which causes the alert to pend).
2622 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002623 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002624#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002625 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002626 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002627 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002628 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002629
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002630/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002631 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002632 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002633 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002634 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002635 * @param alert Address of the alert.
2636 * @param handler Action to take when alert is sent. Specify either the address
2637 * of a function to be invoked by the system workqueue thread,
2638 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2639 * K_ALERT_DEFAULT (which causes the alert to pend).
2640 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002641 *
2642 * @return N/A
2643 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002644extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2645 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002646
2647/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002648 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002649 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002650 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002651 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002652 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2653 *
2654 * @param alert Address of the alert.
2655 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002656 * or one of the special values K_NO_WAIT and K_FOREVER.
2657 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002658 * @retval 0 Alert received.
2659 * @retval -EBUSY Returned without waiting.
2660 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002661 */
Kumar Galacc334c72017-04-21 10:55:34 -05002662extern int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002663
2664/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002665 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002666 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002667 * This routine signals @a alert. The action specified for @a alert will
2668 * be taken, which may trigger the execution of an alert handler function
2669 * and/or cause the alert to pend (assuming the alert has not reached its
2670 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002671 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002672 * @note Can be called by ISRs.
2673 *
2674 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002675 *
2676 * @return N/A
2677 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002678extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002679
2680/**
Allan Stephensc98da842016-11-11 15:45:03 -05002681 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002682 */
2683
Allan Stephensc98da842016-11-11 15:45:03 -05002684/**
2685 * @cond INTERNAL_HIDDEN
2686 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002687
2688struct k_msgq {
2689 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002690 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002691 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002692 char *buffer_start;
2693 char *buffer_end;
2694 char *read_ptr;
2695 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002696 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002697
Anas Nashif2f203c22016-12-18 06:57:45 -05002698 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002699};
2700
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002701#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002702 { \
2703 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002704 .max_msgs = q_max_msgs, \
2705 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002706 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002707 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002708 .read_ptr = q_buffer, \
2709 .write_ptr = q_buffer, \
2710 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002711 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002712 }
2713
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002714#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2715
Peter Mitsis1da807e2016-10-06 11:36:59 -04002716/**
Allan Stephensc98da842016-11-11 15:45:03 -05002717 * INTERNAL_HIDDEN @endcond
2718 */
2719
2720/**
2721 * @defgroup msgq_apis Message Queue APIs
2722 * @ingroup kernel_apis
2723 * @{
2724 */
2725
2726/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002727 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002728 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002729 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2730 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002731 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2732 * message is similarly aligned to this boundary, @a q_msg_size must also be
2733 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002734 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002735 * The message queue can be accessed outside the module where it is defined
2736 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002737 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002738 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002739 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002740 * @param q_name Name of the message queue.
2741 * @param q_msg_size Message size (in bytes).
2742 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002743 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002744 */
2745#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2746 static char __noinit __aligned(q_align) \
2747 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002748 struct k_msgq q_name \
2749 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002750 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002751 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002752
Peter Mitsisd7a37502016-10-13 11:37:40 -04002753/**
2754 * @brief Initialize a message queue.
2755 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002756 * This routine initializes a message queue object, prior to its first use.
2757 *
Allan Stephensda827222016-11-09 14:23:58 -06002758 * The message queue's ring buffer must contain space for @a max_msgs messages,
2759 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2760 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2761 * that each message is similarly aligned to this boundary, @a q_msg_size
2762 * must also be a multiple of N.
2763 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002764 * @param q Address of the message queue.
2765 * @param buffer Pointer to ring buffer that holds queued messages.
2766 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002767 * @param max_msgs Maximum number of messages that can be queued.
2768 *
2769 * @return N/A
2770 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002771extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002772 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002773
2774/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002775 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002776 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002777 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002778 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002779 * @note Can be called by ISRs.
2780 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002781 * @param q Address of the message queue.
2782 * @param data Pointer to the message.
2783 * @param timeout Waiting period to add the message (in milliseconds),
2784 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002785 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002786 * @retval 0 Message sent.
2787 * @retval -ENOMSG Returned without waiting or queue purged.
2788 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002789 */
Kumar Galacc334c72017-04-21 10:55:34 -05002790extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002791
2792/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002793 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002794 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002795 * This routine receives a message from message queue @a q in a "first in,
2796 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002797 *
Allan Stephensc98da842016-11-11 15:45:03 -05002798 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002799 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002800 * @param q Address of the message queue.
2801 * @param data Address of area to hold the received message.
2802 * @param timeout Waiting period to receive the message (in milliseconds),
2803 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002804 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002805 * @retval 0 Message received.
2806 * @retval -ENOMSG Returned without waiting.
2807 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002808 */
Kumar Galacc334c72017-04-21 10:55:34 -05002809extern int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002810
2811/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002812 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002813 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002814 * This routine discards all unreceived messages in a message queue's ring
2815 * buffer. Any threads that are blocked waiting to send a message to the
2816 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002817 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002818 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002819 *
2820 * @return N/A
2821 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002822extern void k_msgq_purge(struct k_msgq *q);
2823
Peter Mitsis67be2492016-10-07 11:44:34 -04002824/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002825 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002826 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002827 * This routine returns the number of unused entries in a message queue's
2828 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002829 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002830 * @param q Address of the message queue.
2831 *
2832 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002833 */
Kumar Galacc334c72017-04-21 10:55:34 -05002834static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002835{
2836 return q->max_msgs - q->used_msgs;
2837}
2838
Peter Mitsisd7a37502016-10-13 11:37:40 -04002839/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002840 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002841 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002842 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002843 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002844 * @param q Address of the message queue.
2845 *
2846 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002847 */
Kumar Galacc334c72017-04-21 10:55:34 -05002848static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002849{
2850 return q->used_msgs;
2851}
2852
Allan Stephensc98da842016-11-11 15:45:03 -05002853/**
2854 * @} end defgroup msgq_apis
2855 */
2856
2857/**
2858 * @defgroup mem_pool_apis Memory Pool APIs
2859 * @ingroup kernel_apis
2860 * @{
2861 */
2862
Andy Ross73cb9582017-05-09 10:42:39 -07002863/* Note on sizing: the use of a 20 bit field for block means that,
2864 * assuming a reasonable minimum block size of 16 bytes, we're limited
2865 * to 16M of memory managed by a single pool. Long term it would be
2866 * good to move to a variable bit size based on configuration.
2867 */
2868struct k_mem_block_id {
2869 u32_t pool : 8;
2870 u32_t level : 4;
2871 u32_t block : 20;
2872};
2873
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002874struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002875 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002876 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002877};
2878
Allan Stephensc98da842016-11-11 15:45:03 -05002879/**
2880 * @} end defgroup mem_pool_apis
2881 */
2882
2883/**
2884 * @defgroup mailbox_apis Mailbox APIs
2885 * @ingroup kernel_apis
2886 * @{
2887 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002888
2889struct k_mbox_msg {
2890 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002891 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002892 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002893 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002894 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002895 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002896 /** sender's message data buffer */
2897 void *tx_data;
2898 /** internal use only - needed for legacy API support */
2899 void *_rx_data;
2900 /** message data block descriptor */
2901 struct k_mem_block tx_block;
2902 /** source thread id */
2903 k_tid_t rx_source_thread;
2904 /** target thread id */
2905 k_tid_t tx_target_thread;
2906 /** internal use only - thread waiting on send (may be a dummy) */
2907 k_tid_t _syncing_thread;
2908#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2909 /** internal use only - semaphore used during asynchronous send */
2910 struct k_sem *_async_sem;
2911#endif
2912};
2913
Allan Stephensc98da842016-11-11 15:45:03 -05002914/**
2915 * @cond INTERNAL_HIDDEN
2916 */
2917
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002918struct k_mbox {
2919 _wait_q_t tx_msg_queue;
2920 _wait_q_t rx_msg_queue;
2921
Anas Nashif2f203c22016-12-18 06:57:45 -05002922 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002923};
2924
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002925#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002926 { \
2927 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2928 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002929 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002930 }
2931
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002932#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
2933
Peter Mitsis12092702016-10-14 12:57:23 -04002934/**
Allan Stephensc98da842016-11-11 15:45:03 -05002935 * INTERNAL_HIDDEN @endcond
2936 */
2937
2938/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002939 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002940 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002941 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002942 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002943 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002944 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002945 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002946 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002947#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002948 struct k_mbox name \
2949 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002950 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002951
Peter Mitsis12092702016-10-14 12:57:23 -04002952/**
2953 * @brief Initialize a mailbox.
2954 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002955 * This routine initializes a mailbox object, prior to its first use.
2956 *
2957 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002958 *
2959 * @return N/A
2960 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002961extern void k_mbox_init(struct k_mbox *mbox);
2962
Peter Mitsis12092702016-10-14 12:57:23 -04002963/**
2964 * @brief Send a mailbox message in a synchronous manner.
2965 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002966 * This routine sends a message to @a mbox and waits for a receiver to both
2967 * receive and process it. The message data may be in a buffer, in a memory
2968 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002969 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002970 * @param mbox Address of the mailbox.
2971 * @param tx_msg Address of the transmit message descriptor.
2972 * @param timeout Waiting period for the message to be received (in
2973 * milliseconds), or one of the special values K_NO_WAIT
2974 * and K_FOREVER. Once the message has been received,
2975 * this routine waits as long as necessary for the message
2976 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002977 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002978 * @retval 0 Message sent.
2979 * @retval -ENOMSG Returned without waiting.
2980 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002981 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002982extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002983 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002984
Peter Mitsis12092702016-10-14 12:57:23 -04002985/**
2986 * @brief Send a mailbox message in an asynchronous manner.
2987 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002988 * This routine sends a message to @a mbox without waiting for a receiver
2989 * to process it. The message data may be in a buffer, in a memory pool block,
2990 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2991 * will be given when the message has been both received and completely
2992 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002994 * @param mbox Address of the mailbox.
2995 * @param tx_msg Address of the transmit message descriptor.
2996 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002997 *
2998 * @return N/A
2999 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003000extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003001 struct k_sem *sem);
3002
Peter Mitsis12092702016-10-14 12:57:23 -04003003/**
3004 * @brief Receive a mailbox message.
3005 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003006 * This routine receives a message from @a mbox, then optionally retrieves
3007 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003008 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003009 * @param mbox Address of the mailbox.
3010 * @param rx_msg Address of the receive message descriptor.
3011 * @param buffer Address of the buffer to receive data, or NULL to defer data
3012 * retrieval and message disposal until later.
3013 * @param timeout Waiting period for a message to be received (in
3014 * milliseconds), or one of the special values K_NO_WAIT
3015 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003016 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003017 * @retval 0 Message received.
3018 * @retval -ENOMSG Returned without waiting.
3019 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003020 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003021extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003022 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003023
3024/**
3025 * @brief Retrieve mailbox message data into a buffer.
3026 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003027 * This routine completes the processing of a received message by retrieving
3028 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003029 *
3030 * Alternatively, this routine can be used to dispose of a received message
3031 * without retrieving its data.
3032 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003033 * @param rx_msg Address of the receive message descriptor.
3034 * @param buffer Address of the buffer to receive data, or NULL to discard
3035 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003036 *
3037 * @return N/A
3038 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003039extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003040
3041/**
3042 * @brief Retrieve mailbox message data into a memory pool block.
3043 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003044 * This routine completes the processing of a received message by retrieving
3045 * its data into a memory pool block, then disposing of the message.
3046 * The memory pool block that results from successful retrieval must be
3047 * returned to the pool once the data has been processed, even in cases
3048 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003049 *
3050 * Alternatively, this routine can be used to dispose of a received message
3051 * without retrieving its data. In this case there is no need to return a
3052 * memory pool block to the pool.
3053 *
3054 * This routine allocates a new memory pool block for the data only if the
3055 * data is not already in one. If a new block cannot be allocated, the routine
3056 * returns a failure code and the received message is left unchanged. This
3057 * permits the caller to reattempt data retrieval at a later time or to dispose
3058 * of the received message without retrieving its data.
3059 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003060 * @param rx_msg Address of a receive message descriptor.
3061 * @param pool Address of memory pool, or NULL to discard data.
3062 * @param block Address of the area to hold memory pool block info.
3063 * @param timeout Waiting period to wait for a memory pool block (in
3064 * milliseconds), or one of the special values K_NO_WAIT
3065 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003066 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003067 * @retval 0 Data retrieved.
3068 * @retval -ENOMEM Returned without waiting.
3069 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003070 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003071extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003072 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003073 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003074
Allan Stephensc98da842016-11-11 15:45:03 -05003075/**
3076 * @} end defgroup mailbox_apis
3077 */
3078
3079/**
3080 * @cond INTERNAL_HIDDEN
3081 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003082
3083struct k_pipe {
3084 unsigned char *buffer; /* Pipe buffer: may be NULL */
3085 size_t size; /* Buffer size */
3086 size_t bytes_used; /* # bytes used in buffer */
3087 size_t read_index; /* Where in buffer to read from */
3088 size_t write_index; /* Where in buffer to write */
3089
3090 struct {
3091 _wait_q_t readers; /* Reader wait queue */
3092 _wait_q_t writers; /* Writer wait queue */
3093 } wait_q;
3094
Anas Nashif2f203c22016-12-18 06:57:45 -05003095 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003096};
3097
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003098#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003099 { \
3100 .buffer = pipe_buffer, \
3101 .size = pipe_buffer_size, \
3102 .bytes_used = 0, \
3103 .read_index = 0, \
3104 .write_index = 0, \
3105 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3106 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003107 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003108 }
3109
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003110#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3111
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003112/**
Allan Stephensc98da842016-11-11 15:45:03 -05003113 * INTERNAL_HIDDEN @endcond
3114 */
3115
3116/**
3117 * @defgroup pipe_apis Pipe APIs
3118 * @ingroup kernel_apis
3119 * @{
3120 */
3121
3122/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003123 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003124 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003125 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003126 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003127 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003128 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003129 * @param name Name of the pipe.
3130 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3131 * or zero if no ring buffer is used.
3132 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003133 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003134#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3135 static unsigned char __noinit __aligned(pipe_align) \
3136 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003137 struct k_pipe name \
3138 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003139 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003140
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003141/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003142 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003144 * This routine initializes a pipe object, prior to its first use.
3145 *
3146 * @param pipe Address of the pipe.
3147 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3148 * is used.
3149 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3150 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003151 *
3152 * @return N/A
3153 */
3154extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3155 size_t size);
3156
3157/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003158 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003159 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003160 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003161 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003162 * @param pipe Address of the pipe.
3163 * @param data Address of data to write.
3164 * @param bytes_to_write Size of data (in bytes).
3165 * @param bytes_written Address of area to hold the number of bytes written.
3166 * @param min_xfer Minimum number of bytes to write.
3167 * @param timeout Waiting period to wait for the data to be written (in
3168 * milliseconds), or one of the special values K_NO_WAIT
3169 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003170 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003171 * @retval 0 At least @a min_xfer bytes of data were written.
3172 * @retval -EIO Returned without waiting; zero data bytes were written.
3173 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003174 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003175 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003176extern int k_pipe_put(struct k_pipe *pipe, void *data,
3177 size_t bytes_to_write, size_t *bytes_written,
Kumar Galacc334c72017-04-21 10:55:34 -05003178 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003179
3180/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003181 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003182 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003183 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003184 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003185 * @param pipe Address of the pipe.
3186 * @param data Address to place the data read from pipe.
3187 * @param bytes_to_read Maximum number of data bytes to read.
3188 * @param bytes_read Address of area to hold the number of bytes read.
3189 * @param min_xfer Minimum number of data bytes to read.
3190 * @param timeout Waiting period to wait for the data to be read (in
3191 * milliseconds), or one of the special values K_NO_WAIT
3192 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003193 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003194 * @retval 0 At least @a min_xfer bytes of data were read.
3195 * @retval -EIO Returned without waiting; zero data bytes were read.
3196 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003197 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003198 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003199extern int k_pipe_get(struct k_pipe *pipe, void *data,
3200 size_t bytes_to_read, size_t *bytes_read,
Kumar Galacc334c72017-04-21 10:55:34 -05003201 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003202
3203/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003204 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003205 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003206 * This routine writes the data contained in a memory block to @a pipe.
3207 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003208 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003209 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003210 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003211 * @param block Memory block containing data to send
3212 * @param size Number of data bytes in memory block to send
3213 * @param sem Semaphore to signal upon completion (else NULL)
3214 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003215 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003216 */
3217extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3218 size_t size, struct k_sem *sem);
3219
3220/**
Allan Stephensc98da842016-11-11 15:45:03 -05003221 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003222 */
3223
Allan Stephensc98da842016-11-11 15:45:03 -05003224/**
3225 * @cond INTERNAL_HIDDEN
3226 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003227
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003228struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003229 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003230 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003231 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003232 char *buffer;
3233 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003234 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003235
Anas Nashif2f203c22016-12-18 06:57:45 -05003236 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003237};
3238
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003239#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003240 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003241 { \
3242 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003243 .num_blocks = slab_num_blocks, \
3244 .block_size = slab_block_size, \
3245 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003246 .free_list = NULL, \
3247 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003248 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003249 }
3250
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003251#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3252
3253
Peter Mitsis578f9112016-10-07 13:50:31 -04003254/**
Allan Stephensc98da842016-11-11 15:45:03 -05003255 * INTERNAL_HIDDEN @endcond
3256 */
3257
3258/**
3259 * @defgroup mem_slab_apis Memory Slab APIs
3260 * @ingroup kernel_apis
3261 * @{
3262 */
3263
3264/**
Allan Stephensda827222016-11-09 14:23:58 -06003265 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003266 *
Allan Stephensda827222016-11-09 14:23:58 -06003267 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003268 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003269 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3270 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003271 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003272 *
Allan Stephensda827222016-11-09 14:23:58 -06003273 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003274 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003275 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003276 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003277 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003278 * @param name Name of the memory slab.
3279 * @param slab_block_size Size of each memory block (in bytes).
3280 * @param slab_num_blocks Number memory blocks.
3281 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003282 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003283#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3284 char __noinit __aligned(slab_align) \
3285 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3286 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003287 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003288 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003289 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003290
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003291/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003292 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003293 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003294 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003295 *
Allan Stephensda827222016-11-09 14:23:58 -06003296 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3297 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3298 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3299 * To ensure that each memory block is similarly aligned to this boundary,
3300 * @a slab_block_size must also be a multiple of N.
3301 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003302 * @param slab Address of the memory slab.
3303 * @param buffer Pointer to buffer used for the memory blocks.
3304 * @param block_size Size of each memory block (in bytes).
3305 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003306 *
3307 * @return N/A
3308 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003309extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003310 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003311
3312/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003313 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003314 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003315 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003316 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003317 * @param slab Address of the memory slab.
3318 * @param mem Pointer to block address area.
3319 * @param timeout Maximum time to wait for operation to complete
3320 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3321 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003322 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003323 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003324 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003325 * @retval -ENOMEM Returned without waiting.
3326 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003327 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003328extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003329 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003330
3331/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003332 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003333 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003334 * This routine releases a previously allocated memory block back to its
3335 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003336 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003337 * @param slab Address of the memory slab.
3338 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003339 *
3340 * @return N/A
3341 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003342extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003343
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003344/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003345 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003346 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003347 * This routine gets the number of memory blocks that are currently
3348 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003349 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003350 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003351 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003352 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003353 */
Kumar Galacc334c72017-04-21 10:55:34 -05003354static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003355{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003356 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003357}
3358
Peter Mitsisc001aa82016-10-13 13:53:37 -04003359/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003360 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003361 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003362 * This routine gets the number of memory blocks that are currently
3363 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003364 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003365 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003366 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003367 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003368 */
Kumar Galacc334c72017-04-21 10:55:34 -05003369static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003370{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003371 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003372}
3373
Allan Stephensc98da842016-11-11 15:45:03 -05003374/**
3375 * @} end defgroup mem_slab_apis
3376 */
3377
3378/**
3379 * @cond INTERNAL_HIDDEN
3380 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003381
Andy Ross73cb9582017-05-09 10:42:39 -07003382struct k_mem_pool_lvl {
3383 union {
3384 u32_t *bits_p;
3385 u32_t bits;
3386 };
3387 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003388};
3389
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003390struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003391 void *buf;
3392 size_t max_sz;
3393 u16_t n_max;
3394 u8_t n_levels;
3395 u8_t max_inline_level;
3396 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003397 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003398};
3399
Andy Ross73cb9582017-05-09 10:42:39 -07003400#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003401
Andy Ross73cb9582017-05-09 10:42:39 -07003402#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3403
3404#define _MPOOL_LVLS(maxsz, minsz) \
3405 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3406 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3407 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3408 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3409 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3410 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3411 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3412 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3413 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3414 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3415 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3416 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3417 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3418 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3419 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3420 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3421
3422/* Rounds the needed bits up to integer multiples of u32_t */
3423#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3424 ((((n_max) << (2*(l))) + 31) / 32)
3425
3426/* One word gets stored free unioned with the pointer, otherwise the
3427 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003428 */
Andy Ross73cb9582017-05-09 10:42:39 -07003429#define _MPOOL_LBIT_WORDS(n_max, l) \
3430 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3431 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003432
Andy Ross73cb9582017-05-09 10:42:39 -07003433/* How many bytes for the bitfields of a single level? */
3434#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3435 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3436 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003437
Andy Ross73cb9582017-05-09 10:42:39 -07003438/* Size of the bitmap array that follows the buffer in allocated memory */
3439#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3440 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3441 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3442 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3443 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3444 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3445 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3446 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3447 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3448 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3449 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3450 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3451 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3452 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3453 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3454 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3455 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003456
3457/**
Allan Stephensc98da842016-11-11 15:45:03 -05003458 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003459 */
3460
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003461/**
Allan Stephensc98da842016-11-11 15:45:03 -05003462 * @addtogroup mem_pool_apis
3463 * @{
3464 */
3465
3466/**
3467 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003468 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003469 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3470 * long. The memory pool allows blocks to be repeatedly partitioned into
3471 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003472 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003473 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003474 * If the pool is to be accessed outside the module where it is defined, it
3475 * can be declared via
3476 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003477 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003478 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003479 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003480 * @param minsz Size of the smallest blocks in the pool (in bytes).
3481 * @param maxsz Size of the largest blocks in the pool (in bytes).
3482 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003483 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003484 */
Andy Ross73cb9582017-05-09 10:42:39 -07003485#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3486 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3487 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3488 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3489 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3490 .buf = _mpool_buf_##name, \
3491 .max_sz = maxsz, \
3492 .n_max = nmax, \
3493 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3494 .levels = _mpool_lvls_##name, \
3495 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003496
Peter Mitsis937042c2016-10-13 13:18:26 -04003497/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003498 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003499 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003500 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003501 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003502 * @param pool Address of the memory pool.
3503 * @param block Pointer to block descriptor for the allocated memory.
3504 * @param size Amount of memory to allocate (in bytes).
3505 * @param timeout Maximum time to wait for operation to complete
3506 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3507 * or K_FOREVER to wait as long as necessary.
3508 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003509 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003510 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003511 * @retval -ENOMEM Returned without waiting.
3512 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003513 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003514extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003515 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003516
3517/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003518 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003519 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003520 * This routine releases a previously allocated memory block back to its
3521 * memory pool.
3522 *
3523 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003524 *
3525 * @return N/A
3526 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003527extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003528
3529/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003530 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003531 *
Andy Ross73cb9582017-05-09 10:42:39 -07003532 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003533 *
Andy Ross73cb9582017-05-09 10:42:39 -07003534 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003535 *
3536 * @return N/A
3537 */
Andy Ross73cb9582017-05-09 10:42:39 -07003538static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003539
3540/**
Allan Stephensc98da842016-11-11 15:45:03 -05003541 * @} end addtogroup mem_pool_apis
3542 */
3543
3544/**
3545 * @defgroup heap_apis Heap Memory Pool APIs
3546 * @ingroup kernel_apis
3547 * @{
3548 */
3549
3550/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003551 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003552 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003553 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003554 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003555 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003556 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003557 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003558 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003559 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003560extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003561
3562/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003563 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003564 *
3565 * This routine provides traditional free() semantics. The memory being
3566 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003567 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003568 * If @a ptr is NULL, no operation is performed.
3569 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003570 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003571 *
3572 * @return N/A
3573 */
3574extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003575
Allan Stephensc98da842016-11-11 15:45:03 -05003576/**
3577 * @} end defgroup heap_apis
3578 */
3579
Benjamin Walshacc68c12017-01-29 18:57:45 -05003580/* polling API - PRIVATE */
3581
Benjamin Walshb0179862017-02-02 16:39:57 -05003582#ifdef CONFIG_POLL
3583#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3584#else
3585#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3586#endif
3587
Benjamin Walshacc68c12017-01-29 18:57:45 -05003588/* private - implementation data created as needed, per-type */
3589struct _poller {
3590 struct k_thread *thread;
3591};
3592
3593/* private - types bit positions */
3594enum _poll_types_bits {
3595 /* can be used to ignore an event */
3596 _POLL_TYPE_IGNORE,
3597
3598 /* to be signaled by k_poll_signal() */
3599 _POLL_TYPE_SIGNAL,
3600
3601 /* semaphore availability */
3602 _POLL_TYPE_SEM_AVAILABLE,
3603
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003604 /* queue/fifo/lifo data availability */
3605 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003606
3607 _POLL_NUM_TYPES
3608};
3609
3610#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3611
3612/* private - states bit positions */
3613enum _poll_states_bits {
3614 /* default state when creating event */
3615 _POLL_STATE_NOT_READY,
3616
Benjamin Walshacc68c12017-01-29 18:57:45 -05003617 /* signaled by k_poll_signal() */
3618 _POLL_STATE_SIGNALED,
3619
3620 /* semaphore is available */
3621 _POLL_STATE_SEM_AVAILABLE,
3622
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003623 /* data is available to read on queue/fifo/lifo */
3624 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003625
3626 _POLL_NUM_STATES
3627};
3628
3629#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3630
3631#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003632 (32 - (0 \
3633 + 8 /* tag */ \
3634 + _POLL_NUM_TYPES \
3635 + _POLL_NUM_STATES \
3636 + 1 /* modes */ \
3637 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003638
3639#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3640#error overflow of 32-bit word in struct k_poll_event
3641#endif
3642
3643/* end of polling API - PRIVATE */
3644
3645
3646/**
3647 * @defgroup poll_apis Async polling APIs
3648 * @ingroup kernel_apis
3649 * @{
3650 */
3651
3652/* Public polling API */
3653
3654/* public - values for k_poll_event.type bitfield */
3655#define K_POLL_TYPE_IGNORE 0
3656#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3657#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003658#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3659#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003660
3661/* public - polling modes */
3662enum k_poll_modes {
3663 /* polling thread does not take ownership of objects when available */
3664 K_POLL_MODE_NOTIFY_ONLY = 0,
3665
3666 K_POLL_NUM_MODES
3667};
3668
3669/* public - values for k_poll_event.state bitfield */
3670#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003671#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3672#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003673#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3674#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003675
3676/* public - poll signal object */
3677struct k_poll_signal {
3678 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003679 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003680
3681 /*
3682 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3683 * user resets it to 0.
3684 */
3685 unsigned int signaled;
3686
3687 /* custom result value passed to k_poll_signal() if needed */
3688 int result;
3689};
3690
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003691#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003692 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003693 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003694 .signaled = 0, \
3695 .result = 0, \
3696 }
3697
3698struct k_poll_event {
3699 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003700 sys_dnode_t _node;
3701
3702 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003703 struct _poller *poller;
3704
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003705 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003706 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003707
Benjamin Walshacc68c12017-01-29 18:57:45 -05003708 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003709 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003710
3711 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003712 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003713
3714 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003715 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003716
3717 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003718 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003719
3720 /* per-type data */
3721 union {
3722 void *obj;
3723 struct k_poll_signal *signal;
3724 struct k_sem *sem;
3725 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003726 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003727 };
3728};
3729
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003730#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003731 { \
3732 .poller = NULL, \
3733 .type = event_type, \
3734 .state = K_POLL_STATE_NOT_READY, \
3735 .mode = event_mode, \
3736 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003737 { .obj = event_obj }, \
3738 }
3739
3740#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3741 event_tag) \
3742 { \
3743 .type = event_type, \
3744 .tag = event_tag, \
3745 .state = K_POLL_STATE_NOT_READY, \
3746 .mode = event_mode, \
3747 .unused = 0, \
3748 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003749 }
3750
3751/**
3752 * @brief Initialize one struct k_poll_event instance
3753 *
3754 * After this routine is called on a poll event, the event it ready to be
3755 * placed in an event array to be passed to k_poll().
3756 *
3757 * @param event The event to initialize.
3758 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3759 * values. Only values that apply to the same object being polled
3760 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3761 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003762 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003763 * @param obj Kernel object or poll signal.
3764 *
3765 * @return N/A
3766 */
3767
Kumar Galacc334c72017-04-21 10:55:34 -05003768extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003769 int mode, void *obj);
3770
3771/**
3772 * @brief Wait for one or many of multiple poll events to occur
3773 *
3774 * This routine allows a thread to wait concurrently for one or many of
3775 * multiple poll events to have occurred. Such events can be a kernel object
3776 * being available, like a semaphore, or a poll signal event.
3777 *
3778 * When an event notifies that a kernel object is available, the kernel object
3779 * is not "given" to the thread calling k_poll(): it merely signals the fact
3780 * that the object was available when the k_poll() call was in effect. Also,
3781 * all threads trying to acquire an object the regular way, i.e. by pending on
3782 * the object, have precedence over the thread polling on the object. This
3783 * means that the polling thread will never get the poll event on an object
3784 * until the object becomes available and its pend queue is empty. For this
3785 * reason, the k_poll() call is more effective when the objects being polled
3786 * only have one thread, the polling thread, trying to acquire them.
3787 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003788 * When k_poll() returns 0, the caller should loop on all the events that were
3789 * passed to k_poll() and check the state field for the values that were
3790 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003791 *
3792 * Before being reused for another call to k_poll(), the user has to reset the
3793 * state field to K_POLL_STATE_NOT_READY.
3794 *
3795 * @param events An array of pointers to events to be polled for.
3796 * @param num_events The number of events in the array.
3797 * @param timeout Waiting period for an event to be ready (in milliseconds),
3798 * or one of the special values K_NO_WAIT and K_FOREVER.
3799 *
3800 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003801 * @retval -EAGAIN Waiting period timed out.
3802 */
3803
3804extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003805 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003806
3807/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003808 * @brief Initialize a poll signal object.
3809 *
3810 * Ready a poll signal object to be signaled via k_poll_signal().
3811 *
3812 * @param signal A poll signal.
3813 *
3814 * @return N/A
3815 */
3816
3817extern void k_poll_signal_init(struct k_poll_signal *signal);
3818
3819/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003820 * @brief Signal a poll signal object.
3821 *
3822 * This routine makes ready a poll signal, which is basically a poll event of
3823 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3824 * made ready to run. A @a result value can be specified.
3825 *
3826 * The poll signal contains a 'signaled' field that, when set by
3827 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3828 * be reset by the user before being passed again to k_poll() or k_poll() will
3829 * consider it being signaled, and will return immediately.
3830 *
3831 * @param signal A poll signal.
3832 * @param result The value to store in the result field of the signal.
3833 *
3834 * @retval 0 The signal was delivered successfully.
3835 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3836 */
3837
3838extern int k_poll_signal(struct k_poll_signal *signal, int result);
3839
3840/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003841extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003842
3843/**
3844 * @} end defgroup poll_apis
3845 */
3846
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003847/**
3848 * @brief Make the CPU idle.
3849 *
3850 * This function makes the CPU idle until an event wakes it up.
3851 *
3852 * In a regular system, the idle thread should be the only thread responsible
3853 * for making the CPU idle and triggering any type of power management.
3854 * However, in some more constrained systems, such as a single-threaded system,
3855 * the only thread would be responsible for this if needed.
3856 *
3857 * @return N/A
3858 */
3859extern void k_cpu_idle(void);
3860
3861/**
3862 * @brief Make the CPU idle in an atomic fashion.
3863 *
3864 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3865 * must be done atomically before making the CPU idle.
3866 *
3867 * @param key Interrupt locking key obtained from irq_lock().
3868 *
3869 * @return N/A
3870 */
3871extern void k_cpu_atomic_idle(unsigned int key);
3872
Kumar Galacc334c72017-04-21 10:55:34 -05003873extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003874
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003875#include <arch/cpu.h>
3876
Andrew Boie1f32d092017-08-30 14:00:26 -07003877#ifdef CONFIG_USERSPACE
3878/* Architecture-specific inline functions that may be indirectly called by
3879 * application code due to their appearance in macros or other inline functions.
3880 *
3881 * Each arch should implement these in <arch/cpu.h>
3882 */
3883
3884/* Indicate whether we are currently running in user mode
3885 *
3886 * @return nonzero if the CPU is currently running with user permissions
3887 */
3888static inline int _arch_is_user_context(void);
3889
3890/* Interfaces for invoking system calls */
3891static inline u32_t _arch_syscall_invoke5(u32_t arg1, u32_t arg2, u32_t arg3,
3892 u32_t arg4, u32_t arg5,
3893 u32_t call_id);
3894
3895static inline u32_t _arch_syscall_invoke4(u32_t arg1, u32_t arg2, u32_t arg3,
3896 u32_t arg4, u32_t call_id);
3897
3898static inline u32_t _arch_syscall_invoke3(u32_t arg1, u32_t arg2, u32_t arg3,
3899 u32_t call_id);
3900
3901static inline u32_t _arch_syscall_invoke2(u32_t arg1, u32_t arg2,
3902 u32_t call_id);
3903
3904static inline u32_t _arch_syscall_invoke1(u32_t arg1, u32_t call_id);
3905
3906static inline u32_t _arch_syscall_invoke0(u32_t call_id);
3907#endif
3908
Andrew Boiecdb94d62017-04-18 15:22:05 -07003909#ifdef _ARCH_EXCEPT
3910/* This archtecture has direct support for triggering a CPU exception */
3911#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3912#else
3913
3914#include <misc/printk.h>
3915
3916/* NOTE: This is the implementation for arches that do not implement
3917 * _ARCH_EXCEPT() to generate a real CPU exception.
3918 *
3919 * We won't have a real exception frame to determine the PC value when
3920 * the oops occurred, so print file and line number before we jump into
3921 * the fatal error handler.
3922 */
3923#define _k_except_reason(reason) do { \
3924 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3925 _NanoFatalErrorHandler(reason, &_default_esf); \
3926 CODE_UNREACHABLE; \
3927 } while (0)
3928
3929#endif /* _ARCH__EXCEPT */
3930
3931/**
3932 * @brief Fatally terminate a thread
3933 *
3934 * This should be called when a thread has encountered an unrecoverable
3935 * runtime condition and needs to terminate. What this ultimately
3936 * means is determined by the _fatal_error_handler() implementation, which
3937 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3938 *
3939 * If this is called from ISR context, the default system fatal error handler
3940 * will treat it as an unrecoverable system error, just like k_panic().
3941 */
3942#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3943
3944/**
3945 * @brief Fatally terminate the system
3946 *
3947 * This should be called when the Zephyr kernel has encountered an
3948 * unrecoverable runtime condition and needs to terminate. What this ultimately
3949 * means is determined by the _fatal_error_handler() implementation, which
3950 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3951 */
3952#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3953
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003954/*
3955 * private APIs that are utilized by one or more public APIs
3956 */
3957
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003958#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003959extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003960#else
3961#define _init_static_threads() do { } while ((0))
3962#endif
3963
3964extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003965extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003966
Andrew Boiedc5d9352017-06-02 12:56:47 -07003967/* arch/cpu.h may declare an architecture or platform-specific macro
3968 * for properly declaring stacks, compatible with MMU/MPU constraints if
3969 * enabled
3970 */
3971#ifdef _ARCH_THREAD_STACK_DEFINE
3972#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
3973#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
3974 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
3975#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
3976#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie507852a2017-07-25 18:47:07 -07003977static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
3978{
3979 return _ARCH_THREAD_STACK_BUFFER(sym);
3980}
Andrew Boiedc5d9352017-06-02 12:56:47 -07003981#else
3982/**
3983 * @brief Declare a toplevel thread stack memory region
3984 *
3985 * This declares a region of memory suitable for use as a thread's stack.
3986 *
3987 * This is the generic, historical definition. Align to STACK_ALIGN and put in
3988 * 'noinit' section so that it isn't zeroed at boot
3989 *
Andrew Boie507852a2017-07-25 18:47:07 -07003990 * The declared symbol will always be a k_thread_stack_t which can be passed to
3991 * k_thread_create, but should otherwise not be manipulated. If the buffer
3992 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07003993 *
3994 * It is legal to precede this definition with the 'static' keyword.
3995 *
3996 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
3997 * parameter of k_thread_create(), it may not be the same as the
3998 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
3999 *
4000 * @param sym Thread stack symbol name
4001 * @param size Size of the stack memory region
4002 */
4003#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004004 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004005
4006/**
4007 * @brief Declare a toplevel array of thread stack memory regions
4008 *
4009 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4010 * definition for additional details and constraints.
4011 *
4012 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4013 * 'noinit' section so that it isn't zeroed at boot
4014 *
4015 * @param sym Thread stack symbol name
4016 * @param nmemb Number of stacks to declare
4017 * @param size Size of the stack memory region
4018 */
4019
4020#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004021 struct _k_thread_stack_element __noinit \
4022 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004023
4024/**
4025 * @brief Declare an embedded stack memory region
4026 *
4027 * Used for stacks embedded within other data structures. Use is highly
4028 * discouraged but in some cases necessary. For memory protection scenarios,
4029 * it is very important that any RAM preceding this member not be writable
4030 * by threads else a stack overflow will lead to silent corruption. In other
4031 * words, the containing data structure should live in RAM owned by the kernel.
4032 *
4033 * @param sym Thread stack symbol name
4034 * @param size Size of the stack memory region
4035 */
4036#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004037 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004038
4039/**
4040 * @brief Return the size in bytes of a stack memory region
4041 *
4042 * Convenience macro for passing the desired stack size to k_thread_create()
4043 * since the underlying implementation may actually create something larger
4044 * (for instance a guard area).
4045 *
4046 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004047 * passed to K_THREAD_STACK_DEFINE.
4048 *
4049 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4050 * it is not guaranteed to return the original value since each array
4051 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004052 *
4053 * @param sym Stack memory symbol
4054 * @return Size of the stack
4055 */
4056#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4057
4058/**
4059 * @brief Get a pointer to the physical stack buffer
4060 *
4061 * Convenience macro to get at the real underlying stack buffer used by
4062 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4063 * This is really only intended for diagnostic tools which want to examine
4064 * stack memory contents.
4065 *
4066 * @param sym Declared stack symbol name
4067 * @return The buffer itself, a char *
4068 */
Andrew Boie507852a2017-07-25 18:47:07 -07004069static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4070{
4071 return (char *)sym;
4072}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004073
4074#endif /* _ARCH_DECLARE_STACK */
4075
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004076#ifdef __cplusplus
4077}
4078#endif
4079
Andrew Boiee004dec2016-11-07 09:01:19 -08004080#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4081/*
4082 * Define new and delete operators.
4083 * At this moment, the operators do nothing since objects are supposed
4084 * to be statically allocated.
4085 */
4086inline void operator delete(void *ptr)
4087{
4088 (void)ptr;
4089}
4090
4091inline void operator delete[](void *ptr)
4092{
4093 (void)ptr;
4094}
4095
4096inline void *operator new(size_t size)
4097{
4098 (void)size;
4099 return NULL;
4100}
4101
4102inline void *operator new[](size_t size)
4103{
4104 (void)size;
4105 return NULL;
4106}
4107
4108/* Placement versions of operator new and delete */
4109inline void operator delete(void *ptr1, void *ptr2)
4110{
4111 (void)ptr1;
4112 (void)ptr2;
4113}
4114
4115inline void operator delete[](void *ptr1, void *ptr2)
4116{
4117 (void)ptr1;
4118 (void)ptr2;
4119}
4120
4121inline void *operator new(size_t size, void *ptr)
4122{
4123 (void)size;
4124 return ptr;
4125}
4126
4127inline void *operator new[](size_t size, void *ptr)
4128{
4129 (void)size;
4130 return ptr;
4131}
4132
4133#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4134
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004135#endif /* !_ASMLANGUAGE */
4136
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004137#endif /* _kernel__h_ */