blob: 33128329468508ff4f77166783ef8c7412b7e847 [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>
Andy Ross1acd8c22018-05-03 14:51:49 -070025#include <sched_priq.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040026#include <misc/dlist.h>
27#include <misc/slist.h>
Andrew Boie2b9b4b22018-04-27 13:21:22 -070028#include <misc/sflist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050029#include <misc/util.h>
Andrew Boieaa6de292018-03-06 17:12:37 -080030#include <misc/mempool_base.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050031#include <kernel_version.h>
Leandro Pereiraadce1d12017-10-13 15:45:02 -070032#include <random/rand32.h>
Andrew Boie73abd322017-04-04 13:19:13 -070033#include <kernel_arch_thread.h>
Andrew Boie13ca6fe2017-09-23 12:05:49 -070034#include <syscall.h>
Andrew Boie43263fc2017-11-02 11:07:31 -070035#include <misc/printk.h>
36#include <arch/cpu.h>
Andy Ross1acd8c22018-05-03 14:51:49 -070037#include <misc/rb.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040038
39#ifdef __cplusplus
40extern "C" {
41#endif
42
Anas Nashifbbb157d2017-01-15 08:46:31 -050043/**
44 * @brief Kernel APIs
45 * @defgroup kernel_apis Kernel APIs
46 * @{
47 * @}
48 */
49
Anas Nashif61f4b242016-11-18 10:53:59 -050050#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040051#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
52#else
53#define K_DEBUG(fmt, ...)
54#endif
55
Benjamin Walsh2f280412017-01-14 19:23:46 -050056#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
57#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
58#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
59#elif defined(CONFIG_COOP_ENABLED)
60#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
61#define _NUM_PREEMPT_PRIO (0)
62#elif defined(CONFIG_PREEMPT_ENABLED)
63#define _NUM_COOP_PRIO (0)
64#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
65#else
66#error "invalid configuration"
67#endif
68
69#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040070#define K_PRIO_PREEMPT(x) (x)
71
Benjamin Walsh456c6da2016-09-02 18:55:39 -040072#define K_ANY NULL
73#define K_END NULL
74
Benjamin Walshedb35702017-01-14 18:47:22 -050075#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040076#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050077#elif defined(CONFIG_COOP_ENABLED)
78#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
79#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040080#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050081#else
82#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040083#endif
84
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050085#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040086#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
87#else
88#define K_LOWEST_THREAD_PRIO -1
89#endif
90
Benjamin Walshfab8d922016-11-08 15:36:36 -050091#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
92
Benjamin Walsh456c6da2016-09-02 18:55:39 -040093#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
94#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
95
Andy Ross1acd8c22018-05-03 14:51:49 -070096#ifdef CONFIG_WAITQ_FAST
97
98typedef struct {
99 struct _priq_rb waitq;
100} _wait_q_t;
101
102extern int _priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
103
104#define _WAIT_Q_INIT(wait_q) { { { .lessthan_fn = _priq_rb_lessthan } } }
105
106#else
107
Andy Rossccf3bf72018-05-10 11:10:34 -0700108typedef struct {
109 sys_dlist_t waitq;
110} _wait_q_t;
111
112#define _WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) }
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400113
Andy Ross1acd8c22018-05-03 14:51:49 -0700114#endif
115
Anas Nashif2f203c22016-12-18 06:57:45 -0500116#ifdef CONFIG_OBJECT_TRACING
117#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
118#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400119#else
Anas Nashif2f203c22016-12-18 06:57:45 -0500120#define _OBJECT_TRACING_INIT
121#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400122#endif
123
Benjamin Walshacc68c12017-01-29 18:57:45 -0500124#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300125#define _POLL_EVENT_OBJ_INIT(obj) \
126 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
127#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500128#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300129#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500130#define _POLL_EVENT
131#endif
132
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500133struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400134struct k_mutex;
135struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400136struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400137struct k_msgq;
138struct k_mbox;
139struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200140struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400141struct k_fifo;
142struct k_lifo;
143struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400144struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400145struct k_mem_pool;
146struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500147struct k_poll_event;
148struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800149struct k_mem_domain;
150struct k_mem_partition;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400151
Andrew Boie5bd891d2017-09-27 12:59:28 -0700152/* This enumeration needs to be kept in sync with the lists of kernel objects
153 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
154 * function in kernel/userspace.c
155 */
Andrew Boie945af952017-08-22 13:15:23 -0700156enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700157 K_OBJ_ANY,
158
Leandro Pereirac2003672018-04-04 13:50:32 -0700159 /** @cond
160 * Doxygen should ignore this build-time generated include file
161 * when genrating API documentation. Enumeration values are
162 * generated during build by gen_kobject_list.py. It includes
163 * basic kernel objects (e.g. pipes and mutexes) and driver types.
164 */
165#include <kobj-types-enum.h>
166 /** @endcond
167 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700168
Andrew Boie945af952017-08-22 13:15:23 -0700169 K_OBJ_LAST
170};
171
172#ifdef CONFIG_USERSPACE
173/* Table generated by gperf, these objects are retrieved via
174 * _k_object_find() */
175struct _k_object {
176 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700177 u8_t perms[CONFIG_MAX_THREAD_BYTES];
178 u8_t type;
179 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700180 u32_t data;
Andrew Boie945af952017-08-22 13:15:23 -0700181} __packed;
182
Andrew Boie877f82e2017-10-17 11:20:22 -0700183struct _k_object_assignment {
184 struct k_thread *thread;
185 void * const *objects;
186};
187
188/**
189 * @brief Grant a static thread access to a list of kernel objects
190 *
191 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
192 * a set of kernel objects. These objects do not need to be in an initialized
193 * state. The permissions will be granted when the threads are initialized
194 * in the early boot sequence.
195 *
196 * All arguments beyond the first must be pointers to kernel objects.
197 *
198 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
199 */
200#define K_THREAD_ACCESS_GRANT(name_, ...) \
201 static void * const _CONCAT(_object_list_, name_)[] = \
202 { __VA_ARGS__, NULL }; \
203 static __used __in_section_unique(object_access) \
204 const struct _k_object_assignment \
205 _CONCAT(_object_access_, name_) = \
206 { (&_k_thread_obj_ ## name_), \
207 (_CONCAT(_object_list_, name_)) }
208
Andrew Boie945af952017-08-22 13:15:23 -0700209#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700210#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie97bf0012018-04-24 17:01:37 -0700211#define K_OBJ_FLAG_ALLOC BIT(2)
Andrew Boie945af952017-08-22 13:15:23 -0700212
213/**
214 * Lookup a kernel object and init its metadata if it exists
215 *
216 * Calling this on an object will make it usable from userspace.
217 * Intended to be called as the last statement in kernel object init
218 * functions.
219 *
220 * @param object Address of the kernel object
221 */
222void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700223#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700224
225#define K_THREAD_ACCESS_GRANT(thread, ...)
226
Anas Nashif954d5502018-02-25 08:37:28 -0600227/**
228 * @internal
229 */
Andrew Boie743e4682017-10-04 12:25:50 -0700230static inline void _k_object_init(void *obj)
231{
232 ARG_UNUSED(obj);
233}
234
Anas Nashif954d5502018-02-25 08:37:28 -0600235/**
236 * @internal
237 */
Andrew Boie743e4682017-10-04 12:25:50 -0700238static inline void _impl_k_object_access_grant(void *object,
239 struct k_thread *thread)
240{
241 ARG_UNUSED(object);
242 ARG_UNUSED(thread);
243}
244
Anas Nashif954d5502018-02-25 08:37:28 -0600245/**
246 * @internal
247 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700248static inline void k_object_access_revoke(void *object,
249 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700250{
251 ARG_UNUSED(object);
252 ARG_UNUSED(thread);
253}
254
Andrew Boiee9cfc542018-04-13 13:15:28 -0700255/**
256 * @internal
257 */
258static inline void _impl_k_object_release(void *object)
259{
260 ARG_UNUSED(object);
261}
262
Andrew Boie41bab6e2017-10-14 14:42:23 -0700263static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700264{
265 ARG_UNUSED(object);
266}
267#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700268
269/**
270 * grant a thread access to a kernel object
271 *
272 * The thread will be granted access to the object if the caller is from
273 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700274 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700275 *
276 * @param object Address of kernel object
277 * @param thread Thread to grant access to the object
278 */
Andrew Boie743e4682017-10-04 12:25:50 -0700279__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700280
Andrew Boiea89bf012017-10-09 14:47:55 -0700281/**
282 * grant a thread access to a kernel object
283 *
284 * The thread will lose access to the object if the caller is from
285 * supervisor mode, or the caller is from user mode AND has permissions
286 * on both the object and the thread whose access is being revoked.
287 *
288 * @param object Address of kernel object
289 * @param thread Thread to remove access to the object
290 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700291void k_object_access_revoke(void *object, struct k_thread *thread);
292
293
294__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700295
296/**
297 * grant all present and future threads access to an object
298 *
299 * If the caller is from supervisor mode, or the caller is from user mode and
300 * have sufficient permissions on the object, then that object will have
301 * permissions granted to it for *all* current and future threads running in
302 * the system, effectively becoming a public kernel object.
303 *
304 * Use of this API should be avoided on systems that are running untrusted code
305 * as it is possible for such code to derive the addresses of kernel objects
306 * and perform unwanted operations on them.
307 *
Andrew Boie04caa672017-10-13 13:57:07 -0700308 * It is not possible to revoke permissions on public objects; once public,
309 * any thread may use it.
310 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700311 * @param object Address of kernel object
312 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700313void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700314
Andrew Boie31bdfc02017-11-08 16:38:03 -0800315/**
316 * Allocate a kernel object of a designated type
317 *
318 * This will instantiate at runtime a kernel object of the specified type,
319 * returning a pointer to it. The object will be returned in an uninitialized
320 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700321 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800322 *
323 * Currently, allocation of thread stacks is not supported.
324 *
325 * @param otype Requested kernel object type
326 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
327 * available
328 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700329__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800330
Andrew Boie97bf0012018-04-24 17:01:37 -0700331#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800332/**
333 * Free a kernel object previously allocated with k_object_alloc()
334 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700335 * This will return memory for a kernel object back to resource pool it was
336 * allocated from. Care must be exercised that the object will not be used
337 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800338 *
339 * @param obj Pointer to the kernel object memory address.
340 */
341void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700342#else
343static inline void *_impl_k_object_alloc(enum k_objects otype)
344{
Kumar Gala85699f72018-05-17 09:26:03 -0500345 ARG_UNUSED(otype);
346
Andrew Boie97bf0012018-04-24 17:01:37 -0700347 return NULL;
348}
349
350static inline void k_obj_free(void *obj)
351{
352 ARG_UNUSED(obj);
353}
Andrew Boie31bdfc02017-11-08 16:38:03 -0800354#endif /* CONFIG_DYNAMIC_OBJECTS */
355
Andrew Boiebca15da2017-10-15 14:17:48 -0700356/* Using typedef deliberately here, this is quite intended to be an opaque
357 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
358 *
359 * The purpose of this data type is to clearly distinguish between the
360 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
361 * buffer which composes the stack data actually used by the underlying
362 * thread; they cannot be used interchangably as some arches precede the
363 * stack buffer region with guard areas that trigger a MPU or MMU fault
364 * if written to.
365 *
366 * APIs that want to work with the buffer inside should continue to use
367 * char *.
368 *
369 * Stacks should always be created with K_THREAD_STACK_DEFINE().
370 */
371struct __packed _k_thread_stack_element {
372 char data;
373};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700374typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700375
Andrew Boie73abd322017-04-04 13:19:13 -0700376/* timeouts */
377
378struct _timeout;
379typedef void (*_timeout_func_t)(struct _timeout *t);
380
381struct _timeout {
382 sys_dnode_t node;
383 struct k_thread *thread;
384 sys_dlist_t *wait_q;
385 s32_t delta_ticks_from_prev;
386 _timeout_func_t func;
387};
388
389extern s32_t _timeout_remaining_get(struct _timeout *timeout);
390
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700391/**
392 * @typedef k_thread_entry_t
393 * @brief Thread entry point function type.
394 *
395 * A thread's entry point function is invoked when the thread starts executing.
396 * Up to 3 argument values can be passed to the function.
397 *
398 * The thread terminates execution permanently if the entry point function
399 * returns. The thread is responsible for releasing any shared resources
400 * it may own (such as mutexes and dynamically allocated memory), prior to
401 * returning.
402 *
403 * @param p1 First argument.
404 * @param p2 Second argument.
405 * @param p3 Third argument.
406 *
407 * @return N/A
408 */
409typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700410
411#ifdef CONFIG_THREAD_MONITOR
412struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700413 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700414 void *parameter1;
415 void *parameter2;
416 void *parameter3;
417};
418#endif
419
420/* can be used for creating 'dummy' threads, e.g. for pending on objects */
421struct _thread_base {
422
423 /* this thread's entry in a ready/wait queue */
Andy Ross1acd8c22018-05-03 14:51:49 -0700424 union {
425 sys_dlist_t qnode_dlist;
426 struct rbnode qnode_rb;
427 };
428
429#ifdef CONFIG_WAITQ_FAST
430 /* wait queue on which the thread is pended (needed only for
431 * trees, not dumb lists)
432 */
433 _wait_q_t *pended_on;
434#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700435
436 /* user facing 'thread options'; values defined in include/kernel.h */
437 u8_t user_options;
438
439 /* thread state */
440 u8_t thread_state;
441
442 /*
443 * scheduler lock count and thread priority
444 *
445 * These two fields control the preemptibility of a thread.
446 *
447 * When the scheduler is locked, sched_locked is decremented, which
448 * means that the scheduler is locked for values from 0xff to 0x01. A
449 * thread is coop if its prio is negative, thus 0x80 to 0xff when
450 * looked at the value as unsigned.
451 *
452 * By putting them end-to-end, this means that a thread is
453 * non-preemptible if the bundled value is greater than or equal to
454 * 0x0080.
455 */
456 union {
457 struct {
458#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
459 u8_t sched_locked;
460 s8_t prio;
461#else /* LITTLE and PDP */
462 s8_t prio;
463 u8_t sched_locked;
464#endif
465 };
466 u16_t preempt;
467 };
468
Andy Ross4a2e50f2018-05-15 11:06:25 -0700469#ifdef CONFIG_SCHED_DEADLINE
470 int prio_deadline;
471#endif
472
Andy Ross1acd8c22018-05-03 14:51:49 -0700473 u32_t order_key;
474
Andy Ross2724fd12018-01-29 14:55:20 -0800475#ifdef CONFIG_SMP
476 /* True for the per-CPU idle threads */
477 u8_t is_idle;
478
Andy Ross2724fd12018-01-29 14:55:20 -0800479 /* CPU index on which thread was last run */
480 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700481
482 /* Recursive count of irq_lock() calls */
483 u8_t global_lock_count;
Andy Ross2724fd12018-01-29 14:55:20 -0800484#endif
485
Andrew Boie73abd322017-04-04 13:19:13 -0700486 /* data returned by APIs */
487 void *swap_data;
488
489#ifdef CONFIG_SYS_CLOCK_EXISTS
490 /* this thread's entry in a timeout queue */
491 struct _timeout timeout;
492#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700493};
494
495typedef struct _thread_base _thread_base_t;
496
497#if defined(CONFIG_THREAD_STACK_INFO)
498/* Contains the stack information of a thread */
499struct _thread_stack_info {
500 /* Stack Start */
501 u32_t start;
502 /* Stack Size */
503 u32_t size;
504};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700505
506typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700507#endif /* CONFIG_THREAD_STACK_INFO */
508
Chunlin Hane9c97022017-07-07 20:29:30 +0800509#if defined(CONFIG_USERSPACE)
510struct _mem_domain_info {
511 /* memory domain queue node */
512 sys_dnode_t mem_domain_q_node;
513 /* memory domain of the thread */
514 struct k_mem_domain *mem_domain;
515};
516
517#endif /* CONFIG_USERSPACE */
518
Andrew Boie73abd322017-04-04 13:19:13 -0700519struct k_thread {
520
521 struct _thread_base base;
522
523 /* defined by the architecture, but all archs need these */
524 struct _caller_saved caller_saved;
525 struct _callee_saved callee_saved;
526
527 /* static thread init data */
528 void *init_data;
529
530 /* abort function */
531 void (*fn_abort)(void);
532
533#if defined(CONFIG_THREAD_MONITOR)
534 /* thread entry and parameters description */
535 struct __thread_entry *entry;
536
537 /* next item in list of all threads */
538 struct k_thread *next_thread;
539#endif
540
541#ifdef CONFIG_THREAD_CUSTOM_DATA
542 /* crude thread-local storage */
543 void *custom_data;
544#endif
545
546#ifdef CONFIG_ERRNO
547 /* per-thread errno variable */
548 int errno_var;
549#endif
550
551#if defined(CONFIG_THREAD_STACK_INFO)
552 /* Stack Info */
553 struct _thread_stack_info stack_info;
554#endif /* CONFIG_THREAD_STACK_INFO */
555
Chunlin Hane9c97022017-07-07 20:29:30 +0800556#if defined(CONFIG_USERSPACE)
557 /* memory domain info of the thread */
558 struct _mem_domain_info mem_domain_info;
Andrew Boiebca15da2017-10-15 14:17:48 -0700559 /* Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700560 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800561#endif /* CONFIG_USERSPACE */
562
Andy Ross042d8ec2017-12-09 08:37:20 -0800563#if defined(CONFIG_USE_SWITCH)
564 /* When using __switch() a few previously arch-specific items
565 * become part of the core OS
566 */
567
568 /* _Swap() return value */
569 int swap_retval;
570
571 /* Context handle returned via _arch_switch() */
572 void *switch_handle;
573#endif
Andrew Boie92e5bd72018-04-12 17:12:15 -0700574 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800575
Andrew Boie73abd322017-04-04 13:19:13 -0700576 /* arch-specifics: must always be at the end */
577 struct _thread_arch arch;
578};
579
580typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400581typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700582#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400583
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400584enum execution_context_types {
585 K_ISR = 0,
586 K_COOP_THREAD,
587 K_PREEMPT_THREAD,
588};
589
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400590/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100591 * @defgroup profiling_apis Profiling APIs
592 * @ingroup kernel_apis
593 * @{
594 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530595typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
596 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100597
598/**
599 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
600 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700601 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100602 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
603 *
604 * CONFIG_MAIN_STACK_SIZE
605 * CONFIG_IDLE_STACK_SIZE
606 * CONFIG_ISR_STACK_SIZE
607 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
608 *
609 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
610 * produce output.
611 *
612 * @return N/A
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530613 *
614 * @deprecated This API is deprecated. Use k_thread_foreach().
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100615 */
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530616__deprecated extern void k_call_stacks_analyze(void);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100617
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530618/**
619 * @brief Iterate over all the threads in the system.
620 *
621 * This routine iterates over all the threads in the system and
622 * calls the user_cb function for each thread.
623 *
624 * @param user_cb Pointer to the user callback function.
625 * @param user_data Pointer to user data.
626 *
627 * @note CONFIG_THREAD_MONITOR must be set for this function
628 * to be effective. Also this API uses irq_lock to protect the
629 * _kernel.threads list which means creation of new threads and
630 * terminations of existing threads are blocked until this
631 * API returns.
632 *
633 * @return N/A
634 */
635extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
636
Anas Nashif166f5192018-02-25 08:02:36 -0600637/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100638
639/**
Allan Stephensc98da842016-11-11 15:45:03 -0500640 * @defgroup thread_apis Thread APIs
641 * @ingroup kernel_apis
642 * @{
643 */
644
Benjamin Walshed240f22017-01-22 13:05:08 -0500645#endif /* !_ASMLANGUAGE */
646
647
648/*
649 * Thread user options. May be needed by assembly code. Common part uses low
650 * bits, arch-specific use high bits.
651 */
652
653/* system thread that must not abort */
654#define K_ESSENTIAL (1 << 0)
655
656#if defined(CONFIG_FP_SHARING)
657/* thread uses floating point registers */
658#define K_FP_REGS (1 << 1)
659#endif
660
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700661/* This thread has dropped from supervisor mode to user mode and consequently
662 * has additional restrictions
663 */
664#define K_USER (1 << 2)
665
Andrew Boie47f8fd12017-10-05 11:11:02 -0700666/* Indicates that the thread being created should inherit all kernel object
667 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
668 * is not enabled.
669 */
670#define K_INHERIT_PERMS (1 << 3)
671
Benjamin Walshed240f22017-01-22 13:05:08 -0500672#ifdef CONFIG_X86
673/* x86 Bitmask definitions for threads user options */
674
675#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
676/* thread uses SSEx (and also FP) registers */
677#define K_SSE_REGS (1 << 7)
678#endif
679#endif
680
681/* end - thread options */
682
683#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400684/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700685 * @brief Create a thread.
686 *
687 * This routine initializes a thread, then schedules it for execution.
688 *
689 * The new thread may be scheduled for immediate execution or a delayed start.
690 * If the newly spawned thread does not have a delayed start the kernel
691 * scheduler may preempt the current thread to allow the new thread to
692 * execute.
693 *
694 * Thread options are architecture-specific, and can include K_ESSENTIAL,
695 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
696 * them using "|" (the logical OR operator).
697 *
698 * Historically, users often would use the beginning of the stack memory region
699 * to store the struct k_thread data, although corruption will occur if the
700 * stack overflows this region and stack protection features may not detect this
701 * situation.
702 *
703 * @param new_thread Pointer to uninitialized struct k_thread
704 * @param stack Pointer to the stack space.
705 * @param stack_size Stack size in bytes.
706 * @param entry Thread entry function.
707 * @param p1 1st entry point parameter.
708 * @param p2 2nd entry point parameter.
709 * @param p3 3rd entry point parameter.
710 * @param prio Thread priority.
711 * @param options Thread options.
712 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
713 *
714 * @return ID of new thread.
715 */
Andrew Boie662c3452017-10-02 10:51:18 -0700716__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700717 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700718 size_t stack_size,
719 k_thread_entry_t entry,
720 void *p1, void *p2, void *p3,
721 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700722
Andrew Boie3f091b52017-08-30 14:34:14 -0700723/**
724 * @brief Drop a thread's privileges permanently to user mode
725 *
726 * @param entry Function to start executing from
727 * @param p1 1st entry point parameter
728 * @param p2 2nd entry point parameter
729 * @param p3 3rd entry point parameter
730 */
731extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
732 void *p1, void *p2,
733 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700734
Andrew Boied26cf2d2017-03-30 13:07:02 -0700735/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700736 * @brief Grant a thread access to a NULL-terminated set of kernel objects
737 *
738 * This is a convenience function. For the provided thread, grant access to
739 * the remaining arguments, which must be pointers to kernel objects.
740 * The final argument must be a NULL.
741 *
742 * The thread object must be initialized (i.e. running). The objects don't
743 * need to be.
744 *
745 * @param thread Thread to grant access to objects
746 * @param ... NULL-terminated list of kernel object pointers
747 */
748extern void __attribute__((sentinel))
749 k_thread_access_grant(struct k_thread *thread, ...);
750
751/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700752 * @brief Assign a resource memory pool to a thread
753 *
754 * By default, threads have no resource pool assigned unless their parent
755 * thread has a resource pool, in which case it is inherited. Multiple
756 * threads may be assigned to the same memory pool.
757 *
758 * Changing a thread's resource pool will not migrate allocations from the
759 * previous pool.
760 *
761 * @param thread Target thread to assign a memory pool for resource requests,
762 * or NULL if the thread should no longer have a memory pool.
763 * @param pool Memory pool to use for resources.
764 */
765static inline void k_thread_resource_pool_assign(struct k_thread *thread,
766 struct k_mem_pool *pool)
767{
768 thread->resource_pool = pool;
769}
770
771#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
772/**
773 * @brief Assign the system heap as a thread's resource pool
774 *
775 * Similar to k_thread_resource_pool_assign(), but the thread will use
776 * the kernel heap to draw memory.
777 *
778 * Use with caution, as a malicious thread could perform DoS attacks on the
779 * kernel heap.
780 *
781 * @param thread Target thread to assign the system heap for resource requests
782 */
783void k_thread_system_pool_assign(struct k_thread *thread);
784#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
785
786/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500787 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400788 *
Allan Stephensc98da842016-11-11 15:45:03 -0500789 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500790 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400791 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500792 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400793 *
794 * @return N/A
795 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700796__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400797
798/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500799 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400800 *
801 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500802 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400803 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400804 * @return N/A
805 */
Kumar Galacc334c72017-04-21 10:55:34 -0500806extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400807
808/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500809 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400810 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500811 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400812 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500813 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400814 *
815 * @return N/A
816 */
Andrew Boie468190a2017-09-29 14:00:48 -0700817__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400818
819/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500820 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400821 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500822 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400823 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500824 * If @a thread is not currently sleeping, the routine has no effect.
825 *
826 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400827 *
828 * @return N/A
829 */
Andrew Boie468190a2017-09-29 14:00:48 -0700830__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400831
832/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500833 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400834 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500835 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400836 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700837__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400838
839/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500840 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400841 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500842 * This routine prevents @a thread from executing if it has not yet started
843 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500845 * @param thread ID of thread to cancel.
846 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700847 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500848 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700849 *
850 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400851 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700852__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400853
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400854/**
Allan Stephensc98da842016-11-11 15:45:03 -0500855 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400856 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500857 * This routine permanently stops execution of @a thread. The thread is taken
858 * off all kernel queues it is part of (i.e. the ready queue, the timeout
859 * queue, or a kernel object wait queue). However, any kernel resources the
860 * thread might currently own (such as mutexes or memory blocks) are not
861 * released. It is the responsibility of the caller of this routine to ensure
862 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400863 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500864 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400865 *
866 * @return N/A
867 */
Andrew Boie468190a2017-09-29 14:00:48 -0700868__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400869
Andrew Boie7d627c52017-08-30 11:01:56 -0700870
871/**
872 * @brief Start an inactive thread
873 *
874 * If a thread was created with K_FOREVER in the delay parameter, it will
875 * not be added to the scheduling queue until this function is called
876 * on it.
877 *
878 * @param thread thread to start
879 */
Andrew Boie468190a2017-09-29 14:00:48 -0700880__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700881
Allan Stephensc98da842016-11-11 15:45:03 -0500882/**
883 * @cond INTERNAL_HIDDEN
884 */
885
Benjamin Walshd211a522016-12-06 11:44:01 -0500886/* timeout has timed out and is not on _timeout_q anymore */
887#define _EXPIRED (-2)
888
889/* timeout is not in use */
890#define _INACTIVE (-1)
891
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400892struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700893 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700894 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400895 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700896 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500897 void *init_p1;
898 void *init_p2;
899 void *init_p3;
900 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500901 u32_t init_options;
902 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500903 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400904};
905
Andrew Boied26cf2d2017-03-30 13:07:02 -0700906#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400907 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500908 prio, options, delay, abort, groups) \
909 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700910 .init_thread = (thread), \
911 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500912 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700913 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400914 .init_p1 = (void *)p1, \
915 .init_p2 = (void *)p2, \
916 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500917 .init_prio = (prio), \
918 .init_options = (options), \
919 .init_delay = (delay), \
920 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400921 }
922
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400923/**
Allan Stephensc98da842016-11-11 15:45:03 -0500924 * INTERNAL_HIDDEN @endcond
925 */
926
927/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500928 * @brief Statically define and initialize a thread.
929 *
930 * The thread may be scheduled for immediate execution or a delayed start.
931 *
932 * Thread options are architecture-specific, and can include K_ESSENTIAL,
933 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
934 * them using "|" (the logical OR operator).
935 *
936 * The ID of the thread can be accessed using:
937 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500938 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500939 *
940 * @param name Name of the thread.
941 * @param stack_size Stack size in bytes.
942 * @param entry Thread entry function.
943 * @param p1 1st entry point parameter.
944 * @param p2 2nd entry point parameter.
945 * @param p3 3rd entry point parameter.
946 * @param prio Thread priority.
947 * @param options Thread options.
948 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400949 *
950 * @internal It has been observed that the x86 compiler by default aligns
951 * these _static_thread_data structures to 32-byte boundaries, thereby
952 * wasting space. To work around this, force a 4-byte alignment.
953 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500954#define K_THREAD_DEFINE(name, stack_size, \
955 entry, p1, p2, p3, \
956 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700957 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700958 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500959 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500960 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700961 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
962 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500963 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700964 NULL, 0); \
965 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400966
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400967/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500968 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400969 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500970 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400971 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500972 * @param thread ID of thread whose priority is needed.
973 *
974 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400975 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700976__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400977
978/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500979 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400980 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500981 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400982 *
983 * Rescheduling can occur immediately depending on the priority @a thread is
984 * set to:
985 *
986 * - If its priority is raised above the priority of the caller of this
987 * function, and the caller is preemptible, @a thread will be scheduled in.
988 *
989 * - If the caller operates on itself, it lowers its priority below that of
990 * other threads in the system, and the caller is preemptible, the thread of
991 * highest priority will be scheduled in.
992 *
993 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
994 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
995 * highest priority.
996 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500997 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400998 * @param prio New priority.
999 *
1000 * @warning Changing the priority of a thread currently involved in mutex
1001 * priority inheritance may result in undefined behavior.
1002 *
1003 * @return N/A
1004 */
Andrew Boie468190a2017-09-29 14:00:48 -07001005__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001006
Andy Ross4a2e50f2018-05-15 11:06:25 -07001007
1008#ifdef CONFIG_SCHED_DEADLINE
1009/**
1010 * @brief Set deadline expiration time for scheduler
1011 *
1012 * This sets the "deadline" expiration as a time delta from the
1013 * current time, in the same units used by k_cycle_get_32(). The
1014 * scheduler (when deadline scheduling is enabled) will choose the
1015 * next expiring thread when selecting between threads at the same
1016 * static priority. Threads at different priorities will be scheduled
1017 * according to their static priority.
1018 *
1019 * @note Deadlines that are negative (i.e. in the past) are still seen
1020 * as higher priority than others, even if the thread has "finished"
1021 * its work. If you don't want it scheduled anymore, you have to
1022 * reset the deadline into the future, block/pend the thread, or
1023 * modify its priority with k_thread_priority_set().
1024 *
1025 * @note Despite the API naming, the scheduler makes no guarantees the
1026 * the thread WILL be scheduled within that deadline, nor does it take
1027 * extra metadata (like e.g. the "runtime" and "period" parameters in
1028 * Linux sched_setattr()) that allows the kernel to validate the
1029 * scheduling for achievability. Such features could be implemented
1030 * above this call, which is simply input to the priority selection
1031 * logic.
1032 *
1033 * @param thread A thread on which to set the deadline
1034 * @param deadline A time delta, in cycle units
1035 */
1036__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1037#endif
1038
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001039/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001040 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001041 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001042 * This routine prevents the kernel scheduler from making @a thread the
1043 * current thread. All other internal operations on @a thread are still
1044 * performed; for example, any timeout it is waiting on keeps ticking,
1045 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001046 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001047 * If @a thread is already suspended, the routine has no effect.
1048 *
1049 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001050 *
1051 * @return N/A
1052 */
Andrew Boie468190a2017-09-29 14:00:48 -07001053__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001054
1055/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001056 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001058 * This routine allows the kernel scheduler to make @a thread the current
1059 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001060 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001061 * If @a thread is not currently suspended, the routine has no effect.
1062 *
1063 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001064 *
1065 * @return N/A
1066 */
Andrew Boie468190a2017-09-29 14:00:48 -07001067__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001068
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001069/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001070 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001071 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001072 * This routine specifies how the scheduler will perform time slicing of
1073 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001074 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001075 * To enable time slicing, @a slice must be non-zero. The scheduler
1076 * ensures that no thread runs for more than the specified time limit
1077 * before other threads of that priority are given a chance to execute.
1078 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001079 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001080 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001081 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001082 * execute. Once the scheduler selects a thread for execution, there is no
1083 * minimum guaranteed time the thread will execute before threads of greater or
1084 * equal priority are scheduled.
1085 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001086 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001087 * for execution, this routine has no effect; the thread is immediately
1088 * rescheduled after the slice period expires.
1089 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001090 * To disable timeslicing, set both @a slice and @a prio to zero.
1091 *
1092 * @param slice Maximum time slice length (in milliseconds).
1093 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001094 *
1095 * @return N/A
1096 */
Kumar Galacc334c72017-04-21 10:55:34 -05001097extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001098
Anas Nashif166f5192018-02-25 08:02:36 -06001099/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001100
1101/**
1102 * @addtogroup isr_apis
1103 * @{
1104 */
1105
1106/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001107 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001108 *
Allan Stephensc98da842016-11-11 15:45:03 -05001109 * This routine allows the caller to customize its actions, depending on
1110 * whether it is a thread or an ISR.
1111 *
1112 * @note Can be called by ISRs.
1113 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001114 * @return 0 if invoked by a thread.
1115 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001116 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -05001117extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001118
Benjamin Walsh445830d2016-11-10 15:54:27 -05001119/**
1120 * @brief Determine if code is running in a preemptible thread.
1121 *
Allan Stephensc98da842016-11-11 15:45:03 -05001122 * This routine allows the caller to customize its actions, depending on
1123 * whether it can be preempted by another thread. The routine returns a 'true'
1124 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001125 *
Allan Stephensc98da842016-11-11 15:45:03 -05001126 * - The code is running in a thread, not at ISR.
1127 * - The thread's priority is in the preemptible range.
1128 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001129 *
Allan Stephensc98da842016-11-11 15:45:03 -05001130 * @note Can be called by ISRs.
1131 *
1132 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001133 * @return Non-zero if invoked by a preemptible thread.
1134 */
Andrew Boie468190a2017-09-29 14:00:48 -07001135__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001136
Allan Stephensc98da842016-11-11 15:45:03 -05001137/**
Anas Nashif166f5192018-02-25 08:02:36 -06001138 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001139 */
1140
1141/**
1142 * @addtogroup thread_apis
1143 * @{
1144 */
1145
1146/**
1147 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001148 *
Allan Stephensc98da842016-11-11 15:45:03 -05001149 * This routine prevents the current thread from being preempted by another
1150 * thread by instructing the scheduler to treat it as a cooperative thread.
1151 * If the thread subsequently performs an operation that makes it unready,
1152 * it will be context switched out in the normal manner. When the thread
1153 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001154 *
Allan Stephensc98da842016-11-11 15:45:03 -05001155 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001156 *
Allan Stephensc98da842016-11-11 15:45:03 -05001157 * @note k_sched_lock() and k_sched_unlock() should normally be used
1158 * when the operation being performed can be safely interrupted by ISRs.
1159 * However, if the amount of processing involved is very small, better
1160 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001161 *
1162 * @return N/A
1163 */
1164extern void k_sched_lock(void);
1165
Allan Stephensc98da842016-11-11 15:45:03 -05001166/**
1167 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001168 *
Allan Stephensc98da842016-11-11 15:45:03 -05001169 * This routine reverses the effect of a previous call to k_sched_lock().
1170 * A thread must call the routine once for each time it called k_sched_lock()
1171 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001172 *
1173 * @return N/A
1174 */
1175extern void k_sched_unlock(void);
1176
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001177/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001178 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001179 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001180 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001181 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001182 * Custom data is not used by the kernel itself, and is freely available
1183 * for a thread to use as it sees fit. It can be used as a framework
1184 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001185 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001186 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001187 *
1188 * @return N/A
1189 */
Andrew Boie468190a2017-09-29 14:00:48 -07001190__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001191
1192/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001193 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001194 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001195 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001196 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001197 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001198 */
Andrew Boie468190a2017-09-29 14:00:48 -07001199__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001200
1201/**
Anas Nashif166f5192018-02-25 08:02:36 -06001202 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001203 */
1204
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001205#include <sys_clock.h>
1206
Allan Stephensc2f15a42016-11-17 12:24:22 -05001207/**
1208 * @addtogroup clock_apis
1209 * @{
1210 */
1211
1212/**
1213 * @brief Generate null timeout delay.
1214 *
1215 * This macro generates a timeout delay that that instructs a kernel API
1216 * not to wait if the requested operation cannot be performed immediately.
1217 *
1218 * @return Timeout delay value.
1219 */
1220#define K_NO_WAIT 0
1221
1222/**
1223 * @brief Generate timeout delay from milliseconds.
1224 *
1225 * This macro generates a timeout delay that that instructs a kernel API
1226 * to wait up to @a ms milliseconds to perform the requested operation.
1227 *
1228 * @param ms Duration in milliseconds.
1229 *
1230 * @return Timeout delay value.
1231 */
Johan Hedberg14471692016-11-13 10:52:15 +02001232#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001233
1234/**
1235 * @brief Generate timeout delay from seconds.
1236 *
1237 * This macro generates a timeout delay that that instructs a kernel API
1238 * to wait up to @a s seconds to perform the requested operation.
1239 *
1240 * @param s Duration in seconds.
1241 *
1242 * @return Timeout delay value.
1243 */
Johan Hedberg14471692016-11-13 10:52:15 +02001244#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001245
1246/**
1247 * @brief Generate timeout delay from minutes.
1248 *
1249 * This macro generates a timeout delay that that instructs a kernel API
1250 * to wait up to @a m minutes to perform the requested operation.
1251 *
1252 * @param m Duration in minutes.
1253 *
1254 * @return Timeout delay value.
1255 */
Johan Hedberg14471692016-11-13 10:52:15 +02001256#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001257
1258/**
1259 * @brief Generate timeout delay from hours.
1260 *
1261 * This macro generates a timeout delay that that instructs a kernel API
1262 * to wait up to @a h hours to perform the requested operation.
1263 *
1264 * @param h Duration in hours.
1265 *
1266 * @return Timeout delay value.
1267 */
Johan Hedberg14471692016-11-13 10:52:15 +02001268#define K_HOURS(h) K_MINUTES((h) * 60)
1269
Allan Stephensc98da842016-11-11 15:45:03 -05001270/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001271 * @brief Generate infinite timeout delay.
1272 *
1273 * This macro generates a timeout delay that that instructs a kernel API
1274 * to wait as long as necessary to perform the requested operation.
1275 *
1276 * @return Timeout delay value.
1277 */
1278#define K_FOREVER (-1)
1279
1280/**
Anas Nashif166f5192018-02-25 08:02:36 -06001281 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001282 */
1283
1284/**
Allan Stephensc98da842016-11-11 15:45:03 -05001285 * @cond INTERNAL_HIDDEN
1286 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001287
Benjamin Walsh62092182016-12-20 14:39:08 -05001288/* kernel clocks */
1289
1290#if (sys_clock_ticks_per_sec == 1000) || \
1291 (sys_clock_ticks_per_sec == 500) || \
1292 (sys_clock_ticks_per_sec == 250) || \
1293 (sys_clock_ticks_per_sec == 125) || \
1294 (sys_clock_ticks_per_sec == 100) || \
1295 (sys_clock_ticks_per_sec == 50) || \
1296 (sys_clock_ticks_per_sec == 25) || \
1297 (sys_clock_ticks_per_sec == 20) || \
1298 (sys_clock_ticks_per_sec == 10) || \
1299 (sys_clock_ticks_per_sec == 1)
1300
1301 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1302#else
1303 /* yields horrible 64-bit math on many architectures: try to avoid */
1304 #define _NON_OPTIMIZED_TICKS_PER_SEC
1305#endif
1306
1307#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001308extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001309#else
Kumar Galacc334c72017-04-21 10:55:34 -05001310static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001311{
Kumar Galacc334c72017-04-21 10:55:34 -05001312 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001313}
1314#endif
1315
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001316/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001317#ifdef CONFIG_TICKLESS_KERNEL
1318#define _TICK_ALIGN 0
1319#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001320#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001321#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001322
Kumar Galacc334c72017-04-21 10:55:34 -05001323static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001324{
Benjamin Walsh62092182016-12-20 14:39:08 -05001325#ifdef CONFIG_SYS_CLOCK_EXISTS
1326
1327#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001328 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001329#else
Kumar Galacc334c72017-04-21 10:55:34 -05001330 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001331#endif
1332
1333#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001334 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001335 return 0;
1336#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001337}
1338
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001339struct k_timer {
1340 /*
1341 * _timeout structure must be first here if we want to use
1342 * dynamic timer allocation. timeout.node is used in the double-linked
1343 * list of free timers
1344 */
1345 struct _timeout timeout;
1346
Allan Stephens45bfa372016-10-12 12:39:42 -05001347 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001348 _wait_q_t wait_q;
1349
1350 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001351 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001352
1353 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001354 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001355
1356 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001357 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001358
Allan Stephens45bfa372016-10-12 12:39:42 -05001359 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001360 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001361
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001362 /* user-specific data, also used to support legacy features */
1363 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001364
Anas Nashif2f203c22016-12-18 06:57:45 -05001365 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001366};
1367
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001368#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001369 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001370 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001371 .timeout.wait_q = NULL, \
1372 .timeout.thread = NULL, \
1373 .timeout.func = _timer_expiration_handler, \
Andy Rossccf3bf72018-05-10 11:10:34 -07001374 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001375 .expiry_fn = expiry, \
1376 .stop_fn = stop, \
1377 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001378 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001379 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001380 }
1381
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001382#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1383
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001384/**
Allan Stephensc98da842016-11-11 15:45:03 -05001385 * INTERNAL_HIDDEN @endcond
1386 */
1387
1388/**
1389 * @defgroup timer_apis Timer APIs
1390 * @ingroup kernel_apis
1391 * @{
1392 */
1393
1394/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001395 * @typedef k_timer_expiry_t
1396 * @brief Timer expiry function type.
1397 *
1398 * A timer's expiry function is executed by the system clock interrupt handler
1399 * each time the timer expires. The expiry function is optional, and is only
1400 * invoked if the timer has been initialized with one.
1401 *
1402 * @param timer Address of timer.
1403 *
1404 * @return N/A
1405 */
1406typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1407
1408/**
1409 * @typedef k_timer_stop_t
1410 * @brief Timer stop function type.
1411 *
1412 * A timer's stop function is executed if the timer is stopped prematurely.
1413 * The function runs in the context of the thread that stops the timer.
1414 * The stop function is optional, and is only invoked if the timer has been
1415 * initialized with one.
1416 *
1417 * @param timer Address of timer.
1418 *
1419 * @return N/A
1420 */
1421typedef void (*k_timer_stop_t)(struct k_timer *timer);
1422
1423/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001424 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001425 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001426 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001427 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001428 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001429 *
1430 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001431 * @param expiry_fn Function to invoke each time the timer expires.
1432 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001433 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001434#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001435 struct k_timer name \
1436 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001437 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001438
Allan Stephens45bfa372016-10-12 12:39:42 -05001439/**
1440 * @brief Initialize a timer.
1441 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001442 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001443 *
1444 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001445 * @param expiry_fn Function to invoke each time the timer expires.
1446 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001447 *
1448 * @return N/A
1449 */
1450extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001451 k_timer_expiry_t expiry_fn,
1452 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001453
Allan Stephens45bfa372016-10-12 12:39:42 -05001454/**
1455 * @brief Start a timer.
1456 *
1457 * This routine starts a timer, and resets its status to zero. The timer
1458 * begins counting down using the specified duration and period values.
1459 *
1460 * Attempting to start a timer that is already running is permitted.
1461 * The timer's status is reset to zero and the timer begins counting down
1462 * using the new duration and period values.
1463 *
1464 * @param timer Address of timer.
1465 * @param duration Initial timer duration (in milliseconds).
1466 * @param period Timer period (in milliseconds).
1467 *
1468 * @return N/A
1469 */
Andrew Boiea354d492017-09-29 16:22:28 -07001470__syscall void k_timer_start(struct k_timer *timer,
1471 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001472
1473/**
1474 * @brief Stop a timer.
1475 *
1476 * This routine stops a running timer prematurely. The timer's stop function,
1477 * if one exists, is invoked by the caller.
1478 *
1479 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001480 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001481 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001482 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1483 * if @a k_timer_stop is to be called from ISRs.
1484 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001485 * @param timer Address of timer.
1486 *
1487 * @return N/A
1488 */
Andrew Boiea354d492017-09-29 16:22:28 -07001489__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001490
1491/**
1492 * @brief Read timer status.
1493 *
1494 * This routine reads the timer's status, which indicates the number of times
1495 * it has expired since its status was last read.
1496 *
1497 * Calling this routine resets the timer's status to zero.
1498 *
1499 * @param timer Address of timer.
1500 *
1501 * @return Timer status.
1502 */
Andrew Boiea354d492017-09-29 16:22:28 -07001503__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001504
1505/**
1506 * @brief Synchronize thread to timer expiration.
1507 *
1508 * This routine blocks the calling thread until the timer's status is non-zero
1509 * (indicating that it has expired at least once since it was last examined)
1510 * or the timer is stopped. If the timer status is already non-zero,
1511 * or the timer is already stopped, the caller continues without waiting.
1512 *
1513 * Calling this routine resets the timer's status to zero.
1514 *
1515 * This routine must not be used by interrupt handlers, since they are not
1516 * allowed to block.
1517 *
1518 * @param timer Address of timer.
1519 *
1520 * @return Timer status.
1521 */
Andrew Boiea354d492017-09-29 16:22:28 -07001522__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001523
1524/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001525 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001526 *
1527 * This routine computes the (approximate) time remaining before a running
1528 * timer next expires. If the timer is not running, it returns zero.
1529 *
1530 * @param timer Address of timer.
1531 *
1532 * @return Remaining time (in milliseconds).
1533 */
Andrew Boiea354d492017-09-29 16:22:28 -07001534__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1535
1536static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001537{
1538 return _timeout_remaining_get(&timer->timeout);
1539}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001540
Allan Stephensc98da842016-11-11 15:45:03 -05001541/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001542 * @brief Associate user-specific data with a timer.
1543 *
1544 * This routine records the @a user_data with the @a timer, to be retrieved
1545 * later.
1546 *
1547 * It can be used e.g. in a timer handler shared across multiple subsystems to
1548 * retrieve data specific to the subsystem this timer is associated with.
1549 *
1550 * @param timer Address of timer.
1551 * @param user_data User data to associate with the timer.
1552 *
1553 * @return N/A
1554 */
Andrew Boiea354d492017-09-29 16:22:28 -07001555__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1556
Anas Nashif954d5502018-02-25 08:37:28 -06001557/**
1558 * @internal
1559 */
Andrew Boiea354d492017-09-29 16:22:28 -07001560static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1561 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001562{
1563 timer->user_data = user_data;
1564}
1565
1566/**
1567 * @brief Retrieve the user-specific data from a timer.
1568 *
1569 * @param timer Address of timer.
1570 *
1571 * @return The user data.
1572 */
Andrew Boiea354d492017-09-29 16:22:28 -07001573__syscall void *k_timer_user_data_get(struct k_timer *timer);
1574
1575static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001576{
1577 return timer->user_data;
1578}
1579
Anas Nashif166f5192018-02-25 08:02:36 -06001580/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001581
Allan Stephensc98da842016-11-11 15:45:03 -05001582/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001583 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001584 * @{
1585 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001586
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001587/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001588 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001589 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001590 * This routine returns the elapsed time since the system booted,
1591 * in milliseconds.
1592 *
1593 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001594 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001595__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001596
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001597/**
1598 * @brief Enable clock always on in tickless kernel
1599 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001600 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001601 * there are no timer events programmed in tickless kernel
1602 * scheduling. This is necessary if the clock is used to track
1603 * passage of time.
1604 *
1605 * @retval prev_status Previous status of always on flag
1606 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301607#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001608static inline int k_enable_sys_clock_always_on(void)
1609{
1610 int prev_status = _sys_clock_always_on;
1611
1612 _sys_clock_always_on = 1;
1613 _enable_sys_clock();
1614
1615 return prev_status;
1616}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301617#else
1618#define k_enable_sys_clock_always_on() do { } while ((0))
1619#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001620
1621/**
1622 * @brief Disable clock always on in tickless kernel
1623 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001624 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001625 * there are no timer events programmed in tickless kernel
1626 * scheduling. To save power, this routine should be called
1627 * immediately when clock is not used to track time.
1628 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301629#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001630static inline void k_disable_sys_clock_always_on(void)
1631{
1632 _sys_clock_always_on = 0;
1633}
1634#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001635#define k_disable_sys_clock_always_on() do { } while ((0))
1636#endif
1637
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001638/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001639 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001640 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001641 * This routine returns the lower 32-bits of the elapsed time since the system
1642 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001643 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001644 * This routine can be more efficient than k_uptime_get(), as it reduces the
1645 * need for interrupt locking and 64-bit math. However, the 32-bit result
1646 * cannot hold a system uptime time larger than approximately 50 days, so the
1647 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001648 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001649 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001650 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001651__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001652
1653/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001654 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001655 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001656 * This routine computes the elapsed time between the current system uptime
1657 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001658 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001659 * @param reftime Pointer to a reference time, which is updated to the current
1660 * uptime upon return.
1661 *
1662 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001663 */
Kumar Galacc334c72017-04-21 10:55:34 -05001664extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001665
1666/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001667 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001668 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001669 * This routine computes the elapsed time between the current system uptime
1670 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001671 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001672 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1673 * need for interrupt locking and 64-bit math. However, the 32-bit result
1674 * cannot hold an elapsed time larger than approximately 50 days, so the
1675 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001676 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001677 * @param reftime Pointer to a reference time, which is updated to the current
1678 * uptime upon return.
1679 *
1680 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001681 */
Kumar Galacc334c72017-04-21 10:55:34 -05001682extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001683
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001684/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001685 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001686 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001687 * This routine returns the current time, as measured by the system's hardware
1688 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001689 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001690 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001691 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001692#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001693
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001694/**
Anas Nashif166f5192018-02-25 08:02:36 -06001695 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001696 */
1697
Allan Stephensc98da842016-11-11 15:45:03 -05001698/**
1699 * @cond INTERNAL_HIDDEN
1700 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001701
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001702struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001703 sys_sflist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001704 union {
1705 _wait_q_t wait_q;
1706
1707 _POLL_EVENT;
1708 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001709
1710 _OBJECT_TRACING_NEXT_PTR(k_queue);
1711};
1712
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001713#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001714 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001715 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Andy Rossccf3bf72018-05-10 11:10:34 -07001716 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001717 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001718 _OBJECT_TRACING_INIT \
1719 }
1720
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001721#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1722
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001723extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1724
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001725/**
1726 * INTERNAL_HIDDEN @endcond
1727 */
1728
1729/**
1730 * @defgroup queue_apis Queue APIs
1731 * @ingroup kernel_apis
1732 * @{
1733 */
1734
1735/**
1736 * @brief Initialize a queue.
1737 *
1738 * This routine initializes a queue object, prior to its first use.
1739 *
1740 * @param queue Address of the queue.
1741 *
1742 * @return N/A
1743 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001744__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001745
1746/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001747 * @brief Cancel waiting on a queue.
1748 *
1749 * This routine causes first thread pending on @a queue, if any, to
1750 * return from k_queue_get() call with NULL value (as if timeout expired).
1751 *
1752 * @note Can be called by ISRs.
1753 *
1754 * @param queue Address of the queue.
1755 *
1756 * @return N/A
1757 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001758__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001759
1760/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001761 * @brief Append an element to the end of a queue.
1762 *
1763 * This routine appends a data item to @a queue. A queue data item must be
1764 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1765 * reserved for the kernel's use.
1766 *
1767 * @note Can be called by ISRs.
1768 *
1769 * @param queue Address of the queue.
1770 * @param data Address of the data item.
1771 *
1772 * @return N/A
1773 */
1774extern void k_queue_append(struct k_queue *queue, void *data);
1775
1776/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001777 * @brief Append an element to a queue.
1778 *
1779 * This routine appends a data item to @a queue. There is an implicit
1780 * memory allocation from the calling thread's resource pool, which is
1781 * automatically freed when the item is removed from the queue.
1782 *
1783 * @note Can be called by ISRs.
1784 *
1785 * @param queue Address of the queue.
1786 * @param data Address of the data item.
1787 *
1788 * @retval 0 on success
1789 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1790 */
1791__syscall int k_queue_alloc_append(struct k_queue *queue, void *data);
1792
1793/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001794 * @brief Prepend an element to a queue.
1795 *
1796 * This routine prepends a data item to @a queue. A queue data item must be
1797 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1798 * reserved for the kernel's use.
1799 *
1800 * @note Can be called by ISRs.
1801 *
1802 * @param queue Address of the queue.
1803 * @param data Address of the data item.
1804 *
1805 * @return N/A
1806 */
1807extern void k_queue_prepend(struct k_queue *queue, void *data);
1808
1809/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001810 * @brief Prepend an element to a queue.
1811 *
1812 * This routine prepends a data item to @a queue. There is an implicit
1813 * memory allocation from the calling thread's resource pool, which is
1814 * automatically freed when the item is removed from the queue.
1815 *
1816 * @note Can be called by ISRs.
1817 *
1818 * @param queue Address of the queue.
1819 * @param data Address of the data item.
1820 *
1821 * @retval 0 on success
1822 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1823 */
1824__syscall int k_queue_alloc_prepend(struct k_queue *queue, void *data);
1825
1826/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001827 * @brief Inserts an element to a queue.
1828 *
1829 * This routine inserts a data item to @a queue after previous item. A queue
1830 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1831 * item are reserved for the kernel's use.
1832 *
1833 * @note Can be called by ISRs.
1834 *
1835 * @param queue Address of the queue.
1836 * @param prev Address of the previous data item.
1837 * @param data Address of the data item.
1838 *
1839 * @return N/A
1840 */
1841extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1842
1843/**
1844 * @brief Atomically append a list of elements to a queue.
1845 *
1846 * This routine adds a list of data items to @a queue in one operation.
1847 * The data items must be in a singly-linked list, with the first 32 bits
1848 * in each data item pointing to the next data item; the list must be
1849 * NULL-terminated.
1850 *
1851 * @note Can be called by ISRs.
1852 *
1853 * @param queue Address of the queue.
1854 * @param head Pointer to first node in singly-linked list.
1855 * @param tail Pointer to last node in singly-linked list.
1856 *
1857 * @return N/A
1858 */
1859extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1860
1861/**
1862 * @brief Atomically add a list of elements to a queue.
1863 *
1864 * This routine adds a list of data items to @a queue in one operation.
1865 * The data items must be in a singly-linked list implemented using a
1866 * sys_slist_t object. Upon completion, the original list is empty.
1867 *
1868 * @note Can be called by ISRs.
1869 *
1870 * @param queue Address of the queue.
1871 * @param list Pointer to sys_slist_t object.
1872 *
1873 * @return N/A
1874 */
1875extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1876
1877/**
1878 * @brief Get an element from a queue.
1879 *
1880 * This routine removes first data item from @a queue. The first 32 bits of the
1881 * data item are reserved for the kernel's use.
1882 *
1883 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1884 *
1885 * @param queue Address of the queue.
1886 * @param timeout Waiting period to obtain a data item (in milliseconds),
1887 * or one of the special values K_NO_WAIT and K_FOREVER.
1888 *
1889 * @return Address of the data item if successful; NULL if returned
1890 * without waiting, or waiting period timed out.
1891 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001892__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001893
1894/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001895 * @brief Remove an element from a queue.
1896 *
1897 * This routine removes data item from @a queue. The first 32 bits of the
1898 * data item are reserved for the kernel's use. Removing elements from k_queue
1899 * rely on sys_slist_find_and_remove which is not a constant time operation.
1900 *
1901 * @note Can be called by ISRs
1902 *
1903 * @param queue Address of the queue.
1904 * @param data Address of the data item.
1905 *
1906 * @return true if data item was removed
1907 */
1908static inline bool k_queue_remove(struct k_queue *queue, void *data)
1909{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001910 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001911}
1912
1913/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001914 * @brief Query a queue to see if it has data available.
1915 *
1916 * Note that the data might be already gone by the time this function returns
1917 * if other threads are also trying to read from the queue.
1918 *
1919 * @note Can be called by ISRs.
1920 *
1921 * @param queue Address of the queue.
1922 *
1923 * @return Non-zero if the queue is empty.
1924 * @return 0 if data is available.
1925 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001926__syscall int k_queue_is_empty(struct k_queue *queue);
1927
1928static inline int _impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001929{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001930 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001931}
1932
1933/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001934 * @brief Peek element at the head of queue.
1935 *
1936 * Return element from the head of queue without removing it.
1937 *
1938 * @param queue Address of the queue.
1939 *
1940 * @return Head element, or NULL if queue is empty.
1941 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001942__syscall void *k_queue_peek_head(struct k_queue *queue);
1943
1944static inline void *_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001945{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001946 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001947}
1948
1949/**
1950 * @brief Peek element at the tail of queue.
1951 *
1952 * Return element from the tail of queue without removing it.
1953 *
1954 * @param queue Address of the queue.
1955 *
1956 * @return Tail element, or NULL if queue is empty.
1957 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001958__syscall void *k_queue_peek_tail(struct k_queue *queue);
1959
1960static inline void *_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001961{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001962 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001963}
1964
1965/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001966 * @brief Statically define and initialize a queue.
1967 *
1968 * The queue can be accessed outside the module where it is defined using:
1969 *
1970 * @code extern struct k_queue <name>; @endcode
1971 *
1972 * @param name Name of the queue.
1973 */
1974#define K_QUEUE_DEFINE(name) \
1975 struct k_queue name \
1976 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001977 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001978
Anas Nashif166f5192018-02-25 08:02:36 -06001979/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001980
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001981struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001982 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001983};
1984
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04001985/**
1986 * @cond INTERNAL_HIDDEN
1987 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001988#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001989 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001990 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001991 }
1992
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001993#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1994
Allan Stephensc98da842016-11-11 15:45:03 -05001995/**
1996 * INTERNAL_HIDDEN @endcond
1997 */
1998
1999/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002000 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002001 * @ingroup kernel_apis
2002 * @{
2003 */
2004
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002005/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002006 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002007 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002008 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002009 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002010 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002011 *
2012 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002013 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002014 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002015#define k_fifo_init(fifo) \
2016 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002017
2018/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002019 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002020 *
2021 * This routine causes first thread pending on @a fifo, if any, to
2022 * return from k_fifo_get() call with NULL value (as if timeout
2023 * expired).
2024 *
2025 * @note Can be called by ISRs.
2026 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002027 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002028 *
2029 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002030 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002031 */
2032#define k_fifo_cancel_wait(fifo) \
2033 k_queue_cancel_wait((struct k_queue *) fifo)
2034
2035/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002036 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002037 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002038 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002039 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2040 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002041 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002042 * @note Can be called by ISRs.
2043 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002044 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002045 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002046 *
2047 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002048 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002049 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002050#define k_fifo_put(fifo, data) \
2051 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002052
2053/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002054 * @brief Add an element to a FIFO queue.
2055 *
2056 * This routine adds a data item to @a fifo. There is an implicit
2057 * memory allocation from the calling thread's resource pool, which is
2058 * automatically freed when the item is removed.
2059 *
2060 * @note Can be called by ISRs.
2061 *
2062 * @param fifo Address of the FIFO.
2063 * @param data Address of the data item.
2064 *
2065 * @retval 0 on success
2066 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002067 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002068 */
2069#define k_fifo_alloc_put(fifo, data) \
2070 k_queue_alloc_append((struct k_queue *) fifo, data)
2071
2072/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002073 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002074 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002075 * This routine adds a list of data items to @a fifo in one operation.
2076 * The data items must be in a singly-linked list, with the first 32 bits
2077 * each data item pointing to the next data item; the list must be
2078 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002079 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002080 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002081 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002082 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002083 * @param head Pointer to first node in singly-linked list.
2084 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002085 *
2086 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002087 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002088 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002089#define k_fifo_put_list(fifo, head, tail) \
2090 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002091
2092/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002093 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002094 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002095 * This routine adds a list of data items to @a fifo in one operation.
2096 * The data items must be in a singly-linked list implemented using a
2097 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002098 * and must be re-initialized via sys_slist_init().
2099 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002100 * @note Can be called by ISRs.
2101 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002102 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002103 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002104 *
2105 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002106 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002107 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002108#define k_fifo_put_slist(fifo, list) \
2109 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002110
2111/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002112 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002113 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002114 * This routine removes a data item from @a fifo in a "first in, first out"
2115 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002116 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002117 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2118 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002119 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002120 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002121 * or one of the special values K_NO_WAIT and K_FOREVER.
2122 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002123 * @return Address of the data item if successful; NULL if returned
2124 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002125 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002126 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002127#define k_fifo_get(fifo, timeout) \
2128 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002129
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002130/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002131 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002132 *
2133 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002134 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002135 *
2136 * @note Can be called by ISRs.
2137 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002138 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002139 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002140 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002141 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002142 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002143 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002144#define k_fifo_is_empty(fifo) \
2145 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002146
2147/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002148 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002149 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002150 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302151 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002152 * on each iteration of processing, a head container will be peeked,
2153 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002154 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002155 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002156 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002157 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002158 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002159 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002160 */
2161#define k_fifo_peek_head(fifo) \
2162 k_queue_peek_head((struct k_queue *) fifo)
2163
2164/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002165 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002166 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002167 * Return element from the tail of FIFO queue (without removing it). A usecase
2168 * for this is if elements of the FIFO queue are themselves containers. Then
2169 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002170 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002171 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002172 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002173 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002174 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002175 */
2176#define k_fifo_peek_tail(fifo) \
2177 k_queue_peek_tail((struct k_queue *) fifo)
2178
2179/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002180 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002181 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002182 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002183 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002184 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002185 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002186 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002187 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002188 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002189#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002190 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002191 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002192 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002193
Anas Nashif166f5192018-02-25 08:02:36 -06002194/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002195
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002196struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002197 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002198};
2199
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002200/**
2201 * @cond INTERNAL_HIDDEN
2202 */
2203
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002204#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002205 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002206 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002207 }
2208
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002209#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2210
Allan Stephensc98da842016-11-11 15:45:03 -05002211/**
2212 * INTERNAL_HIDDEN @endcond
2213 */
2214
2215/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002216 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002217 * @ingroup kernel_apis
2218 * @{
2219 */
2220
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002221/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002222 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002223 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002224 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002225 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002226 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002227 *
2228 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002229 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002230 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002231#define k_lifo_init(lifo) \
2232 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002233
2234/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002235 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002236 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002237 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002238 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2239 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002240 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002241 * @note Can be called by ISRs.
2242 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002243 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002244 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002245 *
2246 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002247 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002248 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002249#define k_lifo_put(lifo, data) \
2250 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002251
2252/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002253 * @brief Add an element to a LIFO queue.
2254 *
2255 * This routine adds a data item to @a lifo. There is an implicit
2256 * memory allocation from the calling thread's resource pool, which is
2257 * automatically freed when the item is removed.
2258 *
2259 * @note Can be called by ISRs.
2260 *
2261 * @param lifo Address of the LIFO.
2262 * @param data Address of the data item.
2263 *
2264 * @retval 0 on success
2265 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002266 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002267 */
2268#define k_lifo_alloc_put(lifo, data) \
2269 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2270
2271/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002272 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002273 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002274 * This routine removes a data item from @a lifo in a "last in, first out"
2275 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002276 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002277 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2278 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002279 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002280 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002281 * or one of the special values K_NO_WAIT and K_FOREVER.
2282 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002283 * @return Address of the data item if successful; NULL if returned
2284 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002285 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002286 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002287#define k_lifo_get(lifo, timeout) \
2288 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002289
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002290/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002291 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002292 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002293 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002294 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002295 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002296 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002297 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002298 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002299 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002300#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002301 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002302 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002303 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002304
Anas Nashif166f5192018-02-25 08:02:36 -06002305/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002306
2307/**
2308 * @cond INTERNAL_HIDDEN
2309 */
Andrew Boief3bee952018-05-02 17:44:39 -07002310#define K_STACK_FLAG_ALLOC BIT(0) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002311
2312struct k_stack {
2313 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002314 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002315
Anas Nashif2f203c22016-12-18 06:57:45 -05002316 _OBJECT_TRACING_NEXT_PTR(k_stack);
Andrew Boief3bee952018-05-02 17:44:39 -07002317 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002318};
2319
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002320#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002321 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002322 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002323 .base = stack_buffer, \
2324 .next = stack_buffer, \
2325 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002326 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002327 }
2328
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002329#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2330
Allan Stephensc98da842016-11-11 15:45:03 -05002331/**
2332 * INTERNAL_HIDDEN @endcond
2333 */
2334
2335/**
2336 * @defgroup stack_apis Stack APIs
2337 * @ingroup kernel_apis
2338 * @{
2339 */
2340
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002341/**
2342 * @brief Initialize a stack.
2343 *
2344 * This routine initializes a stack object, prior to its first use.
2345 *
2346 * @param stack Address of the stack.
2347 * @param buffer Address of array used to hold stacked values.
2348 * @param num_entries Maximum number of values that can be stacked.
2349 *
2350 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002351 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002352 */
Andrew Boief3bee952018-05-02 17:44:39 -07002353void k_stack_init(struct k_stack *stack,
2354 u32_t *buffer, unsigned int num_entries);
2355
2356
2357/**
2358 * @brief Initialize a stack.
2359 *
2360 * This routine initializes a stack object, prior to its first use. Internal
2361 * buffers will be allocated from the calling thread's resource pool.
2362 * This memory will be released if k_stack_cleanup() is called, or
2363 * userspace is enabled and the stack object loses all references to it.
2364 *
2365 * @param stack Address of the stack.
2366 * @param num_entries Maximum number of values that can be stacked.
2367 *
2368 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002369 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002370 */
2371
2372__syscall int k_stack_alloc_init(struct k_stack *stack,
2373 unsigned int num_entries);
2374
2375/**
2376 * @brief Release a stack's allocated buffer
2377 *
2378 * If a stack object was given a dynamically allocated buffer via
2379 * k_stack_alloc_init(), this will free it. This function does nothing
2380 * if the buffer wasn't dynamically allocated.
2381 *
2382 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002383 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002384 */
2385void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002386
2387/**
2388 * @brief Push an element onto a stack.
2389 *
2390 * This routine adds a 32-bit value @a data to @a stack.
2391 *
2392 * @note Can be called by ISRs.
2393 *
2394 * @param stack Address of the stack.
2395 * @param data Value to push onto the stack.
2396 *
2397 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002398 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002399 */
Andrew Boiee8734462017-09-29 16:42:07 -07002400__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002401
2402/**
2403 * @brief Pop an element from a stack.
2404 *
2405 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2406 * manner and stores the value in @a data.
2407 *
2408 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2409 *
2410 * @param stack Address of the stack.
2411 * @param data Address of area to hold the value popped from the stack.
2412 * @param timeout Waiting period to obtain a value (in milliseconds),
2413 * or one of the special values K_NO_WAIT and K_FOREVER.
2414 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002415 * @retval 0 Element popped from stack.
2416 * @retval -EBUSY Returned without waiting.
2417 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002418 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002419 */
Andrew Boiee8734462017-09-29 16:42:07 -07002420__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002421
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002422/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002423 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002424 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002425 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002426 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002427 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002428 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002429 * @param name Name of the stack.
2430 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002431 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002432 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002433#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002434 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002435 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002436 struct k_stack name \
2437 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002438 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002439 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002440
Anas Nashif166f5192018-02-25 08:02:36 -06002441/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002442
Allan Stephens6bba9b02016-11-16 14:56:54 -05002443struct k_work;
2444
Allan Stephensc98da842016-11-11 15:45:03 -05002445/**
2446 * @defgroup workqueue_apis Workqueue Thread APIs
2447 * @ingroup kernel_apis
2448 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002449 */
2450
Allan Stephens6bba9b02016-11-16 14:56:54 -05002451/**
2452 * @typedef k_work_handler_t
2453 * @brief Work item handler function type.
2454 *
2455 * A work item's handler function is executed by a workqueue's thread
2456 * when the work item is processed by the workqueue.
2457 *
2458 * @param work Address of the work item.
2459 *
2460 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002461 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002462 */
2463typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002464
2465/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002466 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002467 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002468
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002469struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002470 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002471 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002472};
2473
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002474enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002475 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002476};
2477
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002478struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002479 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002480 k_work_handler_t handler;
2481 atomic_t flags[1];
2482};
2483
Allan Stephens6bba9b02016-11-16 14:56:54 -05002484struct k_delayed_work {
2485 struct k_work work;
2486 struct _timeout timeout;
2487 struct k_work_q *work_q;
2488};
2489
2490extern struct k_work_q k_sys_work_q;
2491
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002492/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002493 * INTERNAL_HIDDEN @endcond
2494 */
2495
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002496#define _K_WORK_INITIALIZER(work_handler) \
2497 { \
2498 ._reserved = NULL, \
2499 .handler = work_handler, \
2500 .flags = { 0 } \
2501 }
2502
2503#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2504
Allan Stephens6bba9b02016-11-16 14:56:54 -05002505/**
2506 * @brief Initialize a statically-defined work item.
2507 *
2508 * This macro can be used to initialize a statically-defined workqueue work
2509 * item, prior to its first use. For example,
2510 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002511 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002512 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002513 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002514 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002515 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002516 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002517#define K_WORK_DEFINE(work, work_handler) \
2518 struct k_work work \
2519 __in_section(_k_work, static, work) = \
2520 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002521
2522/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002523 * @brief Initialize a work item.
2524 *
2525 * This routine initializes a workqueue work item, prior to its first use.
2526 *
2527 * @param work Address of work item.
2528 * @param handler Function to invoke each time work item is processed.
2529 *
2530 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002531 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002532 */
2533static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2534{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002535 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002536 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002537 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002538}
2539
2540/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002541 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002542 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002543 * This routine submits work item @a work to be processed by workqueue
2544 * @a work_q. If the work item is already pending in the workqueue's queue
2545 * as a result of an earlier submission, this routine has no effect on the
2546 * work item. If the work item has already been processed, or is currently
2547 * being processed, its work is considered complete and the work item can be
2548 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002549 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002550 * @warning
2551 * A submitted work item must not be modified until it has been processed
2552 * by the workqueue.
2553 *
2554 * @note Can be called by ISRs.
2555 *
2556 * @param work_q Address of workqueue.
2557 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002558 *
2559 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002560 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002561 */
2562static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2563 struct k_work *work)
2564{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002565 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002566 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002567 }
2568}
2569
2570/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002571 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002572 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002573 * This routine indicates if work item @a work is pending in a workqueue's
2574 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002575 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002576 * @note Can be called by ISRs.
2577 *
2578 * @param work Address of work item.
2579 *
2580 * @return 1 if work item is pending, or 0 if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002581 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002582 */
2583static inline int k_work_pending(struct k_work *work)
2584{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002585 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002586}
2587
2588/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002589 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002590 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002591 * This routine starts workqueue @a work_q. The workqueue spawns its work
2592 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002593 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002594 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002595 * @param stack Pointer to work queue thread's stack space, as defined by
2596 * K_THREAD_STACK_DEFINE()
2597 * @param stack_size Size of the work queue thread's stack (in bytes), which
2598 * should either be the same constant passed to
2599 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002600 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002601 *
2602 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002603 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002604 */
Andrew Boie507852a2017-07-25 18:47:07 -07002605extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002606 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002607 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002608
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002609/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002610 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002611 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002612 * This routine initializes a workqueue delayed work item, prior to
2613 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002614 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002615 * @param work Address of delayed work item.
2616 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002617 *
2618 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002619 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002620 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002621extern void k_delayed_work_init(struct k_delayed_work *work,
2622 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002623
2624/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002625 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002626 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002627 * This routine schedules work item @a work to be processed by workqueue
2628 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002629 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002630 * Only when the countdown completes is the work item actually submitted to
2631 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002632 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002633 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002634 * counting down cancels the existing submission and restarts the
2635 * countdown using the new delay. Note that this behavior is
2636 * inherently subject to race conditions with the pre-existing
2637 * timeouts and work queue, so care must be taken to synchronize such
2638 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002639 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002640 * @warning
2641 * A delayed work item must not be modified until it has been processed
2642 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002643 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002644 * @note Can be called by ISRs.
2645 *
2646 * @param work_q Address of workqueue.
2647 * @param work Address of delayed work item.
2648 * @param delay Delay before submitting the work item (in milliseconds).
2649 *
2650 * @retval 0 Work item countdown started.
2651 * @retval -EINPROGRESS Work item is already pending.
2652 * @retval -EINVAL Work item is being processed or has completed its work.
2653 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002654 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002655 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002656extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2657 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002658 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002659
2660/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002661 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002662 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002663 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002664 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002665 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002666 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002667 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002668 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002669 * @param work Address of delayed work item.
2670 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002671 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002672 * @retval -EINPROGRESS Work item is already pending.
2673 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002674 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002675 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002676extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002677
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002678/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002679 * @brief Submit a work item to the system workqueue.
2680 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002681 * This routine submits work item @a work to be processed by the system
2682 * workqueue. If the work item is already pending in the workqueue's queue
2683 * as a result of an earlier submission, this routine has no effect on the
2684 * work item. If the work item has already been processed, or is currently
2685 * being processed, its work is considered complete and the work item can be
2686 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002687 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002688 * @warning
2689 * Work items submitted to the system workqueue should avoid using handlers
2690 * that block or yield since this may prevent the system workqueue from
2691 * processing other work items in a timely manner.
2692 *
2693 * @note Can be called by ISRs.
2694 *
2695 * @param work Address of work item.
2696 *
2697 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002698 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002699 */
2700static inline void k_work_submit(struct k_work *work)
2701{
2702 k_work_submit_to_queue(&k_sys_work_q, work);
2703}
2704
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002705/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002706 * @brief Submit a delayed work item to the system workqueue.
2707 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002708 * This routine schedules work item @a work to be processed by the system
2709 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002710 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002711 * Only when the countdown completes is the work item actually submitted to
2712 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002713 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002714 * Submitting a previously submitted delayed work item that is still
2715 * counting down cancels the existing submission and restarts the countdown
2716 * using the new delay. If the work item is currently pending on the
2717 * workqueue's queue because the countdown has completed it is too late to
2718 * resubmit the item, and resubmission fails without impacting the work item.
2719 * If the work item has already been processed, or is currently being processed,
2720 * its work is considered complete and the work item can be resubmitted.
2721 *
2722 * @warning
2723 * Work items submitted to the system workqueue should avoid using handlers
2724 * that block or yield since this may prevent the system workqueue from
2725 * processing other work items in a timely manner.
2726 *
2727 * @note Can be called by ISRs.
2728 *
2729 * @param work Address of delayed work item.
2730 * @param delay Delay before submitting the work item (in milliseconds).
2731 *
2732 * @retval 0 Work item countdown started.
2733 * @retval -EINPROGRESS Work item is already pending.
2734 * @retval -EINVAL Work item is being processed or has completed its work.
2735 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002736 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002737 */
2738static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002739 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002740{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002741 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002742}
2743
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002744/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002745 * @brief Get time remaining before a delayed work gets scheduled.
2746 *
2747 * This routine computes the (approximate) time remaining before a
2748 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002749 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002750 *
2751 * @param work Delayed work item.
2752 *
2753 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002754 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002755 */
Kumar Galacc334c72017-04-21 10:55:34 -05002756static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002757{
2758 return _timeout_remaining_get(&work->timeout);
2759}
2760
Anas Nashif166f5192018-02-25 08:02:36 -06002761/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002762
Allan Stephensc98da842016-11-11 15:45:03 -05002763/**
2764 * @cond INTERNAL_HIDDEN
2765 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002766
2767struct k_mutex {
2768 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002769 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002770 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002771 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002772
Anas Nashif2f203c22016-12-18 06:57:45 -05002773 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002774};
2775
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002776#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002777 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002778 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002779 .owner = NULL, \
2780 .lock_count = 0, \
2781 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002782 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002783 }
2784
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002785#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2786
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002787/**
Allan Stephensc98da842016-11-11 15:45:03 -05002788 * INTERNAL_HIDDEN @endcond
2789 */
2790
2791/**
2792 * @defgroup mutex_apis Mutex APIs
2793 * @ingroup kernel_apis
2794 * @{
2795 */
2796
2797/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002798 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002799 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002800 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002801 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002802 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002803 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002804 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002805 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002806 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002807#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002808 struct k_mutex name \
2809 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002810 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002811
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002812/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002813 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002814 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002815 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002817 * Upon completion, the mutex is available and does not have an owner.
2818 *
2819 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002820 *
2821 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002822 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002823 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002824__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002825
2826/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002827 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002828 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002829 * This routine locks @a mutex. If the mutex is locked by another thread,
2830 * the calling thread waits until the mutex becomes available or until
2831 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002832 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002833 * A thread is permitted to lock a mutex it has already locked. The operation
2834 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002835 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002836 * @param mutex Address of the mutex.
2837 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002838 * or one of the special values K_NO_WAIT and K_FOREVER.
2839 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002840 * @retval 0 Mutex locked.
2841 * @retval -EBUSY Returned without waiting.
2842 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002843 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002844 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002845__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002846
2847/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002848 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002849 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002850 * This routine unlocks @a mutex. The mutex must already be locked by the
2851 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002852 *
2853 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002854 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002855 * thread.
2856 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002857 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002858 *
2859 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002860 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002861 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002862__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002863
Allan Stephensc98da842016-11-11 15:45:03 -05002864/**
Anas Nashif166f5192018-02-25 08:02:36 -06002865 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002866 */
2867
2868/**
2869 * @cond INTERNAL_HIDDEN
2870 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002871
2872struct k_sem {
2873 _wait_q_t wait_q;
2874 unsigned int count;
2875 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002876 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002877
Anas Nashif2f203c22016-12-18 06:57:45 -05002878 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002879};
2880
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002881#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002882 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002883 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002884 .count = initial_count, \
2885 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002886 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002887 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002888 }
2889
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002890#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2891
Allan Stephensc98da842016-11-11 15:45:03 -05002892/**
2893 * INTERNAL_HIDDEN @endcond
2894 */
2895
2896/**
2897 * @defgroup semaphore_apis Semaphore APIs
2898 * @ingroup kernel_apis
2899 * @{
2900 */
2901
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002902/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002903 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002904 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002905 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002906 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002907 * @param sem Address of the semaphore.
2908 * @param initial_count Initial semaphore count.
2909 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002910 *
2911 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002912 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002913 */
Andrew Boie99280232017-09-29 14:17:47 -07002914__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2915 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002916
2917/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002918 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002919 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002920 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002921 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002922 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2923 *
2924 * @param sem Address of the semaphore.
2925 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002926 * or one of the special values K_NO_WAIT and K_FOREVER.
2927 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002928 * @note When porting code from the nanokernel legacy API to the new API, be
2929 * careful with the return value of this function. The return value is the
2930 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2931 * non-zero means failure, while the nano_sem_take family returns 1 for success
2932 * and 0 for failure.
2933 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002934 * @retval 0 Semaphore taken.
2935 * @retval -EBUSY Returned without waiting.
2936 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002937 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002938 */
Andrew Boie99280232017-09-29 14:17:47 -07002939__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002940
2941/**
2942 * @brief Give a semaphore.
2943 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002944 * This routine gives @a sem, unless the semaphore is already at its maximum
2945 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002946 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002947 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002948 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002949 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002950 *
2951 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002952 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002953 */
Andrew Boie99280232017-09-29 14:17:47 -07002954__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002955
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002956/**
2957 * @brief Reset a semaphore's count to zero.
2958 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002959 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002960 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002961 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002962 *
2963 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002964 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002965 */
Andrew Boie990bf162017-10-03 12:36:49 -07002966__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002967
Anas Nashif954d5502018-02-25 08:37:28 -06002968/**
2969 * @internal
2970 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002971static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002972{
2973 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002974}
2975
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002976/**
2977 * @brief Get a semaphore's count.
2978 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002979 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002980 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002981 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002983 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002984 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002985 */
Andrew Boie990bf162017-10-03 12:36:49 -07002986__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002987
Anas Nashif954d5502018-02-25 08:37:28 -06002988/**
2989 * @internal
2990 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002991static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002992{
2993 return sem->count;
2994}
2995
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002996/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002997 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002999 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003000 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003001 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003002 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003003 * @param name Name of the semaphore.
3004 * @param initial_count Initial semaphore count.
3005 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003006 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003007 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003008#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003009 struct k_sem name \
3010 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07003011 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303012 BUILD_ASSERT(((count_limit) != 0) && \
3013 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003014
Anas Nashif166f5192018-02-25 08:02:36 -06003015/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003016
3017/**
3018 * @defgroup alert_apis Alert APIs
3019 * @ingroup kernel_apis
3020 * @{
3021 */
3022
Allan Stephens5eceb852016-11-16 10:16:30 -05003023/**
3024 * @typedef k_alert_handler_t
3025 * @brief Alert handler function type.
3026 *
3027 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07003028 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05003029 * and is only invoked if the alert has been initialized with one.
3030 *
3031 * @param alert Address of the alert.
3032 *
3033 * @return 0 if alert has been consumed; non-zero if alert should pend.
3034 */
3035typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05003036
Anas Nashif166f5192018-02-25 08:02:36 -06003037/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003038
3039/**
3040 * @cond INTERNAL_HIDDEN
3041 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003042
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003043#define K_ALERT_DEFAULT NULL
3044#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003045
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003046struct k_alert {
3047 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003048 atomic_t send_count;
3049 struct k_work work_item;
3050 struct k_sem sem;
3051
Anas Nashif2f203c22016-12-18 06:57:45 -05003052 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003053};
3054
Anas Nashif954d5502018-02-25 08:37:28 -06003055/**
3056 * @internal
3057 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003058extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003059
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003060#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003061 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003062 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003063 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003064 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
3065 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003066 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003067 }
3068
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003069#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
3070
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003071/**
Allan Stephensc98da842016-11-11 15:45:03 -05003072 * INTERNAL_HIDDEN @endcond
3073 */
3074
3075/**
3076 * @addtogroup alert_apis
3077 * @{
3078 */
3079
3080/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003081 * @def K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts)
3082 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003083 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003084 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003085 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003086 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003087 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003088 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003089 * @param name Name of the alert.
3090 * @param alert_handler Action to take when alert is sent. Specify either
3091 * the address of a function to be invoked by the system workqueue
3092 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
3093 * K_ALERT_DEFAULT (which causes the alert to pend).
3094 * @param max_num_pending_alerts Maximum number of pending alerts.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003095 *
3096 * @req K-ALERT-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003097 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003098#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003099 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003100 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003101 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003102 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003103
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003104/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003105 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003106 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003107 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003108 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003109 * @param alert Address of the alert.
3110 * @param handler Action to take when alert is sent. Specify either the address
3111 * of a function to be invoked by the system workqueue thread,
3112 * K_ALERT_IGNORE (which causes the alert to be ignored), or
3113 * K_ALERT_DEFAULT (which causes the alert to pend).
3114 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003115 *
3116 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003117 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003118 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003119extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
3120 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003121
3122/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003123 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003124 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003125 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003126 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003127 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3128 *
3129 * @param alert Address of the alert.
3130 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003131 * or one of the special values K_NO_WAIT and K_FOREVER.
3132 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003133 * @retval 0 Alert received.
3134 * @retval -EBUSY Returned without waiting.
3135 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003136 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003137 */
Andrew Boie310e9872017-09-29 04:41:15 -07003138__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003139
3140/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003141 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003142 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003143 * This routine signals @a alert. The action specified for @a alert will
3144 * be taken, which may trigger the execution of an alert handler function
3145 * and/or cause the alert to pend (assuming the alert has not reached its
3146 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003147 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003148 * @note Can be called by ISRs.
3149 *
3150 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003151 *
3152 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003153 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003154 */
Andrew Boie310e9872017-09-29 04:41:15 -07003155__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003156
3157/**
Anas Nashif166f5192018-02-25 08:02:36 -06003158 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003159 */
3160
Allan Stephensc98da842016-11-11 15:45:03 -05003161/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003162 * @defgroup msgq_apis Message Queue APIs
3163 * @ingroup kernel_apis
3164 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003165 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003166
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003167/**
3168 * @brief Message Queue Structure
3169 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003170struct k_msgq {
3171 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003172 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003173 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003174 char *buffer_start;
3175 char *buffer_end;
3176 char *read_ptr;
3177 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003178 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003179
Anas Nashif2f203c22016-12-18 06:57:45 -05003180 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Andrew Boie0fe789f2018-04-12 18:35:56 -07003181 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003182};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003183/**
3184 * @cond INTERNAL_HIDDEN
3185 */
3186
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003187
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003188#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003189 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003190 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003191 .max_msgs = q_max_msgs, \
3192 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003193 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003194 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003195 .read_ptr = q_buffer, \
3196 .write_ptr = q_buffer, \
3197 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003198 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003199 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003200#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003201/**
3202 * INTERNAL_HIDDEN @endcond
3203 */
3204
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003205
Andrew Boie0fe789f2018-04-12 18:35:56 -07003206#define K_MSGQ_FLAG_ALLOC BIT(0)
3207
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003208/**
3209 * @brief Message Queue Attributes
3210 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303211struct k_msgq_attrs {
3212 size_t msg_size;
3213 u32_t max_msgs;
3214 u32_t used_msgs;
3215};
3216
Allan Stephensc98da842016-11-11 15:45:03 -05003217
3218/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003219 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003220 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003221 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3222 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003223 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3224 * message is similarly aligned to this boundary, @a q_msg_size must also be
3225 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003226 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003227 * The message queue can be accessed outside the module where it is defined
3228 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003229 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003230 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003231 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003232 * @param q_name Name of the message queue.
3233 * @param q_msg_size Message size (in bytes).
3234 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003235 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003236 *
3237 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003238 */
3239#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003240 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003241 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003242 struct k_msgq q_name \
3243 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003244 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003245 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003246
Peter Mitsisd7a37502016-10-13 11:37:40 -04003247/**
3248 * @brief Initialize a message queue.
3249 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003250 * This routine initializes a message queue object, prior to its first use.
3251 *
Allan Stephensda827222016-11-09 14:23:58 -06003252 * The message queue's ring buffer must contain space for @a max_msgs messages,
3253 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3254 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3255 * that each message is similarly aligned to this boundary, @a q_msg_size
3256 * must also be a multiple of N.
3257 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003258 * @param q Address of the message queue.
3259 * @param buffer Pointer to ring buffer that holds queued messages.
3260 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003261 * @param max_msgs Maximum number of messages that can be queued.
3262 *
3263 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003264 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003265 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003266void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3267 u32_t max_msgs);
3268
3269/**
3270 * @brief Initialize a message queue.
3271 *
3272 * This routine initializes a message queue object, prior to its first use,
3273 * allocating its internal ring buffer from the calling thread's resource
3274 * pool.
3275 *
3276 * Memory allocated for the ring buffer can be released by calling
3277 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3278 * all of its references.
3279 *
3280 * @param q Address of the message queue.
3281 * @param msg_size Message size (in bytes).
3282 * @param max_msgs Maximum number of messages that can be queued.
3283 *
3284 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3285 * thread's resource pool, or -EINVAL if the size parameters cause
3286 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003287 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003288 */
3289__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3290 u32_t max_msgs);
3291
3292
3293void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003294
3295/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003296 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003297 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003298 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003299 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003300 * @note Can be called by ISRs.
3301 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003302 * @param q Address of the message queue.
3303 * @param data Pointer to the message.
3304 * @param timeout Waiting period to add the message (in milliseconds),
3305 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003306 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003307 * @retval 0 Message sent.
3308 * @retval -ENOMSG Returned without waiting or queue purged.
3309 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003310 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003311 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003312__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003313
3314/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003315 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003316 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003317 * This routine receives a message from message queue @a q in a "first in,
3318 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003319 *
Allan Stephensc98da842016-11-11 15:45:03 -05003320 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003321 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003322 * @param q Address of the message queue.
3323 * @param data Address of area to hold the received message.
3324 * @param timeout Waiting period to receive the message (in milliseconds),
3325 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003326 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003327 * @retval 0 Message received.
3328 * @retval -ENOMSG Returned without waiting.
3329 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003330 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003331 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003332__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003333
3334/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003335 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003336 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003337 * This routine discards all unreceived messages in a message queue's ring
3338 * buffer. Any threads that are blocked waiting to send a message to the
3339 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003340 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003341 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003342 *
3343 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003344 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003345 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003346__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003347
Peter Mitsis67be2492016-10-07 11:44:34 -04003348/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003349 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003350 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003351 * This routine returns the number of unused entries in a message queue's
3352 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003353 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003354 * @param q Address of the message queue.
3355 *
3356 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003357 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003358 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003359__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3360
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303361/**
3362 * @brief Get basic attributes of a message queue.
3363 *
3364 * This routine fetches basic attributes of message queue into attr argument.
3365 *
3366 * @param q Address of the message queue.
3367 * @param attrs pointer to message queue attribute structure.
3368 *
3369 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003370 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303371 */
3372__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3373
3374
Andrew Boie82edb6e2017-10-02 10:53:06 -07003375static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003376{
3377 return q->max_msgs - q->used_msgs;
3378}
3379
Peter Mitsisd7a37502016-10-13 11:37:40 -04003380/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003381 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003382 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003383 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003384 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003385 * @param q Address of the message queue.
3386 *
3387 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003388 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003389 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003390__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3391
3392static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003393{
3394 return q->used_msgs;
3395}
3396
Anas Nashif166f5192018-02-25 08:02:36 -06003397/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003398
3399/**
3400 * @defgroup mem_pool_apis Memory Pool APIs
3401 * @ingroup kernel_apis
3402 * @{
3403 */
3404
Andy Ross73cb9582017-05-09 10:42:39 -07003405/* Note on sizing: the use of a 20 bit field for block means that,
3406 * assuming a reasonable minimum block size of 16 bytes, we're limited
3407 * to 16M of memory managed by a single pool. Long term it would be
3408 * good to move to a variable bit size based on configuration.
3409 */
3410struct k_mem_block_id {
3411 u32_t pool : 8;
3412 u32_t level : 4;
3413 u32_t block : 20;
3414};
3415
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003416struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003417 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003418 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003419};
3420
Anas Nashif166f5192018-02-25 08:02:36 -06003421/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003422
3423/**
3424 * @defgroup mailbox_apis Mailbox APIs
3425 * @ingroup kernel_apis
3426 * @{
3427 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003428
3429struct k_mbox_msg {
3430 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003431 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003432 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003433 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003434 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003435 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003436 /** sender's message data buffer */
3437 void *tx_data;
3438 /** internal use only - needed for legacy API support */
3439 void *_rx_data;
3440 /** message data block descriptor */
3441 struct k_mem_block tx_block;
3442 /** source thread id */
3443 k_tid_t rx_source_thread;
3444 /** target thread id */
3445 k_tid_t tx_target_thread;
3446 /** internal use only - thread waiting on send (may be a dummy) */
3447 k_tid_t _syncing_thread;
3448#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3449 /** internal use only - semaphore used during asynchronous send */
3450 struct k_sem *_async_sem;
3451#endif
3452};
3453
3454struct k_mbox {
3455 _wait_q_t tx_msg_queue;
3456 _wait_q_t rx_msg_queue;
3457
Anas Nashif2f203c22016-12-18 06:57:45 -05003458 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003459};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003460/**
3461 * @cond INTERNAL_HIDDEN
3462 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003463
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003464#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003465 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003466 .tx_msg_queue = _WAIT_Q_INIT(&obj.tx_msg_queue), \
3467 .rx_msg_queue = _WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003468 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003469 }
3470
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003471#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3472
Peter Mitsis12092702016-10-14 12:57:23 -04003473/**
Allan Stephensc98da842016-11-11 15:45:03 -05003474 * INTERNAL_HIDDEN @endcond
3475 */
3476
3477/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003478 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003479 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003480 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003481 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003482 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003483 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003484 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003485 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003486 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003487#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003488 struct k_mbox name \
3489 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003490 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003491
Peter Mitsis12092702016-10-14 12:57:23 -04003492/**
3493 * @brief Initialize a mailbox.
3494 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003495 * This routine initializes a mailbox object, prior to its first use.
3496 *
3497 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003498 *
3499 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003500 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003501 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003502extern void k_mbox_init(struct k_mbox *mbox);
3503
Peter Mitsis12092702016-10-14 12:57:23 -04003504/**
3505 * @brief Send a mailbox message in a synchronous manner.
3506 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003507 * This routine sends a message to @a mbox and waits for a receiver to both
3508 * receive and process it. The message data may be in a buffer, in a memory
3509 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003510 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003511 * @param mbox Address of the mailbox.
3512 * @param tx_msg Address of the transmit message descriptor.
3513 * @param timeout Waiting period for the message to be received (in
3514 * milliseconds), or one of the special values K_NO_WAIT
3515 * and K_FOREVER. Once the message has been received,
3516 * this routine waits as long as necessary for the message
3517 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003518 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003519 * @retval 0 Message sent.
3520 * @retval -ENOMSG Returned without waiting.
3521 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003522 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003523 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003524extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003525 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003526
Peter Mitsis12092702016-10-14 12:57:23 -04003527/**
3528 * @brief Send a mailbox message in an asynchronous manner.
3529 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003530 * This routine sends a message to @a mbox without waiting for a receiver
3531 * to process it. The message data may be in a buffer, in a memory pool block,
3532 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3533 * will be given when the message has been both received and completely
3534 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003535 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003536 * @param mbox Address of the mailbox.
3537 * @param tx_msg Address of the transmit message descriptor.
3538 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003539 *
3540 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003541 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003542 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003543extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003544 struct k_sem *sem);
3545
Peter Mitsis12092702016-10-14 12:57:23 -04003546/**
3547 * @brief Receive a mailbox message.
3548 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003549 * This routine receives a message from @a mbox, then optionally retrieves
3550 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003551 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003552 * @param mbox Address of the mailbox.
3553 * @param rx_msg Address of the receive message descriptor.
3554 * @param buffer Address of the buffer to receive data, or NULL to defer data
3555 * retrieval and message disposal until later.
3556 * @param timeout Waiting period for a message to be received (in
3557 * milliseconds), or one of the special values K_NO_WAIT
3558 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003559 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003560 * @retval 0 Message received.
3561 * @retval -ENOMSG Returned without waiting.
3562 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003563 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003564 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003565extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003566 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003567
3568/**
3569 * @brief Retrieve mailbox message data into a buffer.
3570 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003571 * This routine completes the processing of a received message by retrieving
3572 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003573 *
3574 * Alternatively, this routine can be used to dispose of a received message
3575 * without retrieving its data.
3576 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003577 * @param rx_msg Address of the receive message descriptor.
3578 * @param buffer Address of the buffer to receive data, or NULL to discard
3579 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003580 *
3581 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003582 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003583 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003584extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003585
3586/**
3587 * @brief Retrieve mailbox message data into a memory pool block.
3588 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003589 * This routine completes the processing of a received message by retrieving
3590 * its data into a memory pool block, then disposing of the message.
3591 * The memory pool block that results from successful retrieval must be
3592 * returned to the pool once the data has been processed, even in cases
3593 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003594 *
3595 * Alternatively, this routine can be used to dispose of a received message
3596 * without retrieving its data. In this case there is no need to return a
3597 * memory pool block to the pool.
3598 *
3599 * This routine allocates a new memory pool block for the data only if the
3600 * data is not already in one. If a new block cannot be allocated, the routine
3601 * returns a failure code and the received message is left unchanged. This
3602 * permits the caller to reattempt data retrieval at a later time or to dispose
3603 * of the received message without retrieving its data.
3604 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003605 * @param rx_msg Address of a receive message descriptor.
3606 * @param pool Address of memory pool, or NULL to discard data.
3607 * @param block Address of the area to hold memory pool block info.
3608 * @param timeout Waiting period to wait for a memory pool block (in
3609 * milliseconds), or one of the special values K_NO_WAIT
3610 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003611 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003612 * @retval 0 Data retrieved.
3613 * @retval -ENOMEM Returned without waiting.
3614 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003615 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003616 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003617extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003618 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003619 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003620
Anas Nashif166f5192018-02-25 08:02:36 -06003621/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003622
3623/**
3624 * @cond INTERNAL_HIDDEN
3625 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003626
Andrew Boie44fe8122018-04-12 17:38:12 -07003627#define K_PIPE_FLAG_ALLOC BIT(0) /* Buffer was allocated */
3628
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003629struct k_pipe {
3630 unsigned char *buffer; /* Pipe buffer: may be NULL */
3631 size_t size; /* Buffer size */
3632 size_t bytes_used; /* # bytes used in buffer */
3633 size_t read_index; /* Where in buffer to read from */
3634 size_t write_index; /* Where in buffer to write */
3635
3636 struct {
3637 _wait_q_t readers; /* Reader wait queue */
3638 _wait_q_t writers; /* Writer wait queue */
3639 } wait_q;
3640
Anas Nashif2f203c22016-12-18 06:57:45 -05003641 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Andrew Boie44fe8122018-04-12 17:38:12 -07003642 u8_t flags; /* Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003643};
3644
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003645#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003646 { \
3647 .buffer = pipe_buffer, \
3648 .size = pipe_buffer_size, \
3649 .bytes_used = 0, \
3650 .read_index = 0, \
3651 .write_index = 0, \
Andy Rossccf3bf72018-05-10 11:10:34 -07003652 .wait_q.writers = _WAIT_Q_INIT(&obj.wait_q.writers), \
3653 .wait_q.readers = _WAIT_Q_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003654 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003655 }
3656
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003657#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3658
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003659/**
Allan Stephensc98da842016-11-11 15:45:03 -05003660 * INTERNAL_HIDDEN @endcond
3661 */
3662
3663/**
3664 * @defgroup pipe_apis Pipe APIs
3665 * @ingroup kernel_apis
3666 * @{
3667 */
3668
3669/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003670 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003671 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003672 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003673 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003674 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003675 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003676 * @param name Name of the pipe.
3677 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3678 * or zero if no ring buffer is used.
3679 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003680 *
3681 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003682 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003683#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3684 static unsigned char __kernel_noinit __aligned(pipe_align) \
3685 _k_pipe_buf_##name[pipe_buffer_size]; \
3686 struct k_pipe name \
3687 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003688 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003689
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003690/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003691 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003692 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003693 * This routine initializes a pipe object, prior to its first use.
3694 *
3695 * @param pipe Address of the pipe.
3696 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3697 * is used.
3698 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3699 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003700 *
3701 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003702 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003703 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003704void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3705
3706/**
3707 * @brief Release a pipe's allocated buffer
3708 *
3709 * If a pipe object was given a dynamically allocated buffer via
3710 * k_pipe_alloc_init(), this will free it. This function does nothing
3711 * if the buffer wasn't dynamically allocated.
3712 *
3713 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003714 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003715 */
3716void k_pipe_cleanup(struct k_pipe *pipe);
3717
3718/**
3719 * @brief Initialize a pipe and allocate a buffer for it
3720 *
3721 * Storage for the buffer region will be allocated from the calling thread's
3722 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3723 * or userspace is enabled and the pipe object loses all references to it.
3724 *
3725 * This function should only be called on uninitialized pipe objects.
3726 *
3727 * @param pipe Address of the pipe.
3728 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3729 * buffer is used.
3730 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003731 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003732 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003733 */
3734__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003735
3736/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003737 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003738 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003739 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003740 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003741 * @param pipe Address of the pipe.
3742 * @param data Address of data to write.
3743 * @param bytes_to_write Size of data (in bytes).
3744 * @param bytes_written Address of area to hold the number of bytes written.
3745 * @param min_xfer Minimum number of bytes to write.
3746 * @param timeout Waiting period to wait for the data to be written (in
3747 * milliseconds), or one of the special values K_NO_WAIT
3748 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003749 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003750 * @retval 0 At least @a min_xfer bytes of data were written.
3751 * @retval -EIO Returned without waiting; zero data bytes were written.
3752 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003753 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003754 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003755 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003756__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3757 size_t bytes_to_write, size_t *bytes_written,
3758 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003759
3760/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003761 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003762 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003763 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003764 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003765 * @param pipe Address of the pipe.
3766 * @param data Address to place the data read from pipe.
3767 * @param bytes_to_read Maximum number of data bytes to read.
3768 * @param bytes_read Address of area to hold the number of bytes read.
3769 * @param min_xfer Minimum number of data bytes to read.
3770 * @param timeout Waiting period to wait for the data to be read (in
3771 * milliseconds), or one of the special values K_NO_WAIT
3772 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003773 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003774 * @retval 0 At least @a min_xfer bytes of data were read.
3775 * @retval -EIO Returned without waiting; zero data bytes were read.
3776 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003777 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003778 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003779 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003780__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3781 size_t bytes_to_read, size_t *bytes_read,
3782 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003783
3784/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003785 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003786 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003787 * This routine writes the data contained in a memory block to @a pipe.
3788 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003789 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003790 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003791 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003792 * @param block Memory block containing data to send
3793 * @param size Number of data bytes in memory block to send
3794 * @param sem Semaphore to signal upon completion (else NULL)
3795 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003796 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003797 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003798 */
3799extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3800 size_t size, struct k_sem *sem);
3801
Anas Nashif166f5192018-02-25 08:02:36 -06003802/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003803
Allan Stephensc98da842016-11-11 15:45:03 -05003804/**
3805 * @cond INTERNAL_HIDDEN
3806 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003807
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003808struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003809 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003810 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003811 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003812 char *buffer;
3813 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003814 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003815
Anas Nashif2f203c22016-12-18 06:57:45 -05003816 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003817};
3818
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003819#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003820 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003821 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003822 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003823 .num_blocks = slab_num_blocks, \
3824 .block_size = slab_block_size, \
3825 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003826 .free_list = NULL, \
3827 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003828 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003829 }
3830
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003831#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3832
3833
Peter Mitsis578f9112016-10-07 13:50:31 -04003834/**
Allan Stephensc98da842016-11-11 15:45:03 -05003835 * INTERNAL_HIDDEN @endcond
3836 */
3837
3838/**
3839 * @defgroup mem_slab_apis Memory Slab APIs
3840 * @ingroup kernel_apis
3841 * @{
3842 */
3843
3844/**
Allan Stephensda827222016-11-09 14:23:58 -06003845 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003846 *
Allan Stephensda827222016-11-09 14:23:58 -06003847 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003848 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003849 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3850 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003851 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003852 *
Allan Stephensda827222016-11-09 14:23:58 -06003853 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003854 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003855 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003856 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003857 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003858 * @param name Name of the memory slab.
3859 * @param slab_block_size Size of each memory block (in bytes).
3860 * @param slab_num_blocks Number memory blocks.
3861 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003862 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003863 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003864#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3865 char __noinit __aligned(slab_align) \
3866 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3867 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003868 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003869 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003870 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003871
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003872/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003873 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003874 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003875 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003876 *
Allan Stephensda827222016-11-09 14:23:58 -06003877 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3878 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3879 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3880 * To ensure that each memory block is similarly aligned to this boundary,
3881 * @a slab_block_size must also be a multiple of N.
3882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003883 * @param slab Address of the memory slab.
3884 * @param buffer Pointer to buffer used for the memory blocks.
3885 * @param block_size Size of each memory block (in bytes).
3886 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003887 *
3888 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003889 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003890 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003891extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003892 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003893
3894/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003895 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003897 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003898 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003899 * @param slab Address of the memory slab.
3900 * @param mem Pointer to block address area.
3901 * @param timeout Maximum time to wait for operation to complete
3902 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3903 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003904 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003905 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003906 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003907 * @retval -ENOMEM Returned without waiting.
3908 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003909 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003910 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003911extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003912 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003913
3914/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003915 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003917 * This routine releases a previously allocated memory block back to its
3918 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003919 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003920 * @param slab Address of the memory slab.
3921 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003922 *
3923 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003924 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003925 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003926extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003927
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003928/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003929 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003930 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003931 * This routine gets the number of memory blocks that are currently
3932 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003933 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003934 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003935 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003936 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003937 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003938 */
Kumar Galacc334c72017-04-21 10:55:34 -05003939static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003940{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003941 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003942}
3943
Peter Mitsisc001aa82016-10-13 13:53:37 -04003944/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003945 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003946 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003947 * This routine gets the number of memory blocks that are currently
3948 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003949 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003950 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003951 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003952 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003953 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04003954 */
Kumar Galacc334c72017-04-21 10:55:34 -05003955static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003956{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003957 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003958}
3959
Anas Nashif166f5192018-02-25 08:02:36 -06003960/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003961
3962/**
3963 * @cond INTERNAL_HIDDEN
3964 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003965
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003966struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003967 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003968 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003969};
3970
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003971/**
Allan Stephensc98da842016-11-11 15:45:03 -05003972 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003973 */
3974
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003975/**
Allan Stephensc98da842016-11-11 15:45:03 -05003976 * @addtogroup mem_pool_apis
3977 * @{
3978 */
3979
3980/**
3981 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003983 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3984 * long. The memory pool allows blocks to be repeatedly partitioned into
3985 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003986 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003987 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003988 * If the pool is to be accessed outside the module where it is defined, it
3989 * can be declared via
3990 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003991 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003993 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003994 * @param minsz Size of the smallest blocks in the pool (in bytes).
3995 * @param maxsz Size of the largest blocks in the pool (in bytes).
3996 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003997 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003998 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003999 */
Andy Ross73cb9582017-05-09 10:42:39 -07004000#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
4001 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
4002 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08004003 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07004004 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004005 .base = { \
4006 .buf = _mpool_buf_##name, \
4007 .max_sz = maxsz, \
4008 .n_max = nmax, \
4009 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
4010 .levels = _mpool_lvls_##name, \
4011 .flags = SYS_MEM_POOL_KERNEL \
4012 } \
Andy Ross73cb9582017-05-09 10:42:39 -07004013 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004014
Peter Mitsis937042c2016-10-13 13:18:26 -04004015/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004016 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004017 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004018 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004019 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004020 * @param pool Address of the memory pool.
4021 * @param block Pointer to block descriptor for the allocated memory.
4022 * @param size Amount of memory to allocate (in bytes).
4023 * @param timeout Maximum time to wait for operation to complete
4024 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4025 * or K_FOREVER to wait as long as necessary.
4026 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004027 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004028 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004029 * @retval -ENOMEM Returned without waiting.
4030 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004031 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004032 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004033extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004034 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004035
4036/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004037 * @brief Allocate memory from a memory pool with malloc() semantics
4038 *
4039 * Such memory must be released using k_free().
4040 *
4041 * @param pool Address of the memory pool.
4042 * @param size Amount of memory to allocate (in bytes).
4043 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004044 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004045 */
4046extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4047
4048/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004049 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004050 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004051 * This routine releases a previously allocated memory block back to its
4052 * memory pool.
4053 *
4054 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004055 *
4056 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004057 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004058 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004059extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004060
4061/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004062 * @brief Free memory allocated from a memory pool.
4063 *
4064 * This routine releases a previously allocated memory block back to its
4065 * memory pool
4066 *
4067 * @param id Memory block identifier.
4068 *
4069 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004070 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004071 */
4072extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4073
4074/**
Anas Nashif166f5192018-02-25 08:02:36 -06004075 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004076 */
4077
4078/**
4079 * @defgroup heap_apis Heap Memory Pool APIs
4080 * @ingroup kernel_apis
4081 * @{
4082 */
4083
4084/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004085 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004086 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004087 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004088 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004089 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004090 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004091 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004092 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004093 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004094 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004095extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004096
4097/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004098 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004099 *
4100 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004101 * returned must have been allocated from the heap memory pool or
4102 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004103 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004104 * If @a ptr is NULL, no operation is performed.
4105 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004106 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004107 *
4108 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004109 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004110 */
4111extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004112
Allan Stephensc98da842016-11-11 15:45:03 -05004113/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004114 * @brief Allocate memory from heap, array style
4115 *
4116 * This routine provides traditional calloc() semantics. Memory is
4117 * allocated from the heap memory pool and zeroed.
4118 *
4119 * @param nmemb Number of elements in the requested array
4120 * @param size Size of each array element (in bytes).
4121 *
4122 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004123 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004124 */
4125extern void *k_calloc(size_t nmemb, size_t size);
4126
Anas Nashif166f5192018-02-25 08:02:36 -06004127/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004128
Benjamin Walshacc68c12017-01-29 18:57:45 -05004129/* polling API - PRIVATE */
4130
Benjamin Walshb0179862017-02-02 16:39:57 -05004131#ifdef CONFIG_POLL
4132#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
4133#else
4134#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
4135#endif
4136
Benjamin Walshacc68c12017-01-29 18:57:45 -05004137/* private - implementation data created as needed, per-type */
4138struct _poller {
4139 struct k_thread *thread;
4140};
4141
4142/* private - types bit positions */
4143enum _poll_types_bits {
4144 /* can be used to ignore an event */
4145 _POLL_TYPE_IGNORE,
4146
4147 /* to be signaled by k_poll_signal() */
4148 _POLL_TYPE_SIGNAL,
4149
4150 /* semaphore availability */
4151 _POLL_TYPE_SEM_AVAILABLE,
4152
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004153 /* queue/fifo/lifo data availability */
4154 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004155
4156 _POLL_NUM_TYPES
4157};
4158
4159#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4160
4161/* private - states bit positions */
4162enum _poll_states_bits {
4163 /* default state when creating event */
4164 _POLL_STATE_NOT_READY,
4165
Benjamin Walshacc68c12017-01-29 18:57:45 -05004166 /* signaled by k_poll_signal() */
4167 _POLL_STATE_SIGNALED,
4168
4169 /* semaphore is available */
4170 _POLL_STATE_SEM_AVAILABLE,
4171
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004172 /* data is available to read on queue/fifo/lifo */
4173 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004174
4175 _POLL_NUM_STATES
4176};
4177
4178#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4179
4180#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004181 (32 - (0 \
4182 + 8 /* tag */ \
4183 + _POLL_NUM_TYPES \
4184 + _POLL_NUM_STATES \
4185 + 1 /* modes */ \
4186 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004187
Benjamin Walshacc68c12017-01-29 18:57:45 -05004188/* end of polling API - PRIVATE */
4189
4190
4191/**
4192 * @defgroup poll_apis Async polling APIs
4193 * @ingroup kernel_apis
4194 * @{
4195 */
4196
4197/* Public polling API */
4198
4199/* public - values for k_poll_event.type bitfield */
4200#define K_POLL_TYPE_IGNORE 0
4201#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4202#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004203#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4204#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004205
4206/* public - polling modes */
4207enum k_poll_modes {
4208 /* polling thread does not take ownership of objects when available */
4209 K_POLL_MODE_NOTIFY_ONLY = 0,
4210
4211 K_POLL_NUM_MODES
4212};
4213
4214/* public - values for k_poll_event.state bitfield */
4215#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004216#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4217#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004218#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4219#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004220
4221/* public - poll signal object */
4222struct k_poll_signal {
4223 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004224 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004225
4226 /*
4227 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4228 * user resets it to 0.
4229 */
4230 unsigned int signaled;
4231
4232 /* custom result value passed to k_poll_signal() if needed */
4233 int result;
4234};
4235
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004236#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004237 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004238 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004239 .signaled = 0, \
4240 .result = 0, \
4241 }
4242
4243struct k_poll_event {
4244 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004245 sys_dnode_t _node;
4246
4247 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004248 struct _poller *poller;
4249
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004250 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004251 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004252
Benjamin Walshacc68c12017-01-29 18:57:45 -05004253 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004254 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004255
4256 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004257 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004258
4259 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004260 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004261
4262 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004263 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004264
4265 /* per-type data */
4266 union {
4267 void *obj;
4268 struct k_poll_signal *signal;
4269 struct k_sem *sem;
4270 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004271 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004272 };
4273};
4274
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004275#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004276 { \
4277 .poller = NULL, \
4278 .type = event_type, \
4279 .state = K_POLL_STATE_NOT_READY, \
4280 .mode = event_mode, \
4281 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004282 { .obj = event_obj }, \
4283 }
4284
4285#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4286 event_tag) \
4287 { \
4288 .type = event_type, \
4289 .tag = event_tag, \
4290 .state = K_POLL_STATE_NOT_READY, \
4291 .mode = event_mode, \
4292 .unused = 0, \
4293 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004294 }
4295
4296/**
4297 * @brief Initialize one struct k_poll_event instance
4298 *
4299 * After this routine is called on a poll event, the event it ready to be
4300 * placed in an event array to be passed to k_poll().
4301 *
4302 * @param event The event to initialize.
4303 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4304 * values. Only values that apply to the same object being polled
4305 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4306 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004307 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004308 * @param obj Kernel object or poll signal.
4309 *
4310 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004311 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004312 */
4313
Kumar Galacc334c72017-04-21 10:55:34 -05004314extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004315 int mode, void *obj);
4316
4317/**
4318 * @brief Wait for one or many of multiple poll events to occur
4319 *
4320 * This routine allows a thread to wait concurrently for one or many of
4321 * multiple poll events to have occurred. Such events can be a kernel object
4322 * being available, like a semaphore, or a poll signal event.
4323 *
4324 * When an event notifies that a kernel object is available, the kernel object
4325 * is not "given" to the thread calling k_poll(): it merely signals the fact
4326 * that the object was available when the k_poll() call was in effect. Also,
4327 * all threads trying to acquire an object the regular way, i.e. by pending on
4328 * the object, have precedence over the thread polling on the object. This
4329 * means that the polling thread will never get the poll event on an object
4330 * until the object becomes available and its pend queue is empty. For this
4331 * reason, the k_poll() call is more effective when the objects being polled
4332 * only have one thread, the polling thread, trying to acquire them.
4333 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004334 * When k_poll() returns 0, the caller should loop on all the events that were
4335 * passed to k_poll() and check the state field for the values that were
4336 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004337 *
4338 * Before being reused for another call to k_poll(), the user has to reset the
4339 * state field to K_POLL_STATE_NOT_READY.
4340 *
Andrew Boie3772f772018-05-07 16:52:57 -07004341 * When called from user mode, a temporary memory allocation is required from
4342 * the caller's resource pool.
4343 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004344 * @param events An array of pointers to events to be polled for.
4345 * @param num_events The number of events in the array.
4346 * @param timeout Waiting period for an event to be ready (in milliseconds),
4347 * or one of the special values K_NO_WAIT and K_FOREVER.
4348 *
4349 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004350 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02004351 * @retval -EINTR Poller thread has been interrupted.
Andrew Boie3772f772018-05-07 16:52:57 -07004352 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4353 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004354 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004355 */
4356
Andrew Boie3772f772018-05-07 16:52:57 -07004357__syscall int k_poll(struct k_poll_event *events, int num_events,
4358 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004359
4360/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004361 * @brief Initialize a poll signal object.
4362 *
4363 * Ready a poll signal object to be signaled via k_poll_signal().
4364 *
4365 * @param signal A poll signal.
4366 *
4367 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004368 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004369 */
4370
Andrew Boie3772f772018-05-07 16:52:57 -07004371__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4372
4373/*
4374 * @brief Reset a poll signal object's state to unsignaled.
4375 *
4376 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004377 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004378 */
4379__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4380
4381static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4382{
4383 signal->signaled = 0;
4384}
4385
4386/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004387 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004388 *
4389 * @param signal A poll signal object
4390 * @param signaled An integer buffer which will be written nonzero if the
4391 * object was signaled
4392 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004393 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004394 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004395 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004396 */
4397__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4398 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004399
4400/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004401 * @brief Signal a poll signal object.
4402 *
4403 * This routine makes ready a poll signal, which is basically a poll event of
4404 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4405 * made ready to run. A @a result value can be specified.
4406 *
4407 * The poll signal contains a 'signaled' field that, when set by
Andrew Boie3772f772018-05-07 16:52:57 -07004408 * k_poll_signal(), stays set until the user sets it back to 0 with
4409 * k_poll_signal_reset(). It thus has to be reset by the user before being
4410 * passed again to k_poll() or k_poll() will consider it being signaled, and
4411 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004412 *
4413 * @param signal A poll signal.
4414 * @param result The value to store in the result field of the signal.
4415 *
4416 * @retval 0 The signal was delivered successfully.
4417 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004418 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004419 */
4420
Andrew Boie3772f772018-05-07 16:52:57 -07004421__syscall int k_poll_signal(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004422
Anas Nashif954d5502018-02-25 08:37:28 -06004423/**
4424 * @internal
4425 */
Andy Ross8606fab2018-03-26 10:54:40 -07004426extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004427
Anas Nashif166f5192018-02-25 08:02:36 -06004428/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004429
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004430/**
4431 * @brief Make the CPU idle.
4432 *
4433 * This function makes the CPU idle until an event wakes it up.
4434 *
4435 * In a regular system, the idle thread should be the only thread responsible
4436 * for making the CPU idle and triggering any type of power management.
4437 * However, in some more constrained systems, such as a single-threaded system,
4438 * the only thread would be responsible for this if needed.
4439 *
4440 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004441 * @req K-MISC-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004442 */
4443extern void k_cpu_idle(void);
4444
4445/**
4446 * @brief Make the CPU idle in an atomic fashion.
4447 *
4448 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4449 * must be done atomically before making the CPU idle.
4450 *
4451 * @param key Interrupt locking key obtained from irq_lock().
4452 *
4453 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004454 * @req K-MISC-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004455 */
4456extern void k_cpu_atomic_idle(unsigned int key);
4457
Anas Nashif954d5502018-02-25 08:37:28 -06004458
4459/**
4460 * @internal
4461 */
Kumar Galacc334c72017-04-21 10:55:34 -05004462extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004463
Andrew Boiecdb94d62017-04-18 15:22:05 -07004464#ifdef _ARCH_EXCEPT
4465/* This archtecture has direct support for triggering a CPU exception */
4466#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4467#else
4468
Andrew Boiecdb94d62017-04-18 15:22:05 -07004469/* NOTE: This is the implementation for arches that do not implement
4470 * _ARCH_EXCEPT() to generate a real CPU exception.
4471 *
4472 * We won't have a real exception frame to determine the PC value when
4473 * the oops occurred, so print file and line number before we jump into
4474 * the fatal error handler.
4475 */
4476#define _k_except_reason(reason) do { \
4477 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4478 _NanoFatalErrorHandler(reason, &_default_esf); \
4479 CODE_UNREACHABLE; \
4480 } while (0)
4481
4482#endif /* _ARCH__EXCEPT */
4483
4484/**
4485 * @brief Fatally terminate a thread
4486 *
4487 * This should be called when a thread has encountered an unrecoverable
4488 * runtime condition and needs to terminate. What this ultimately
4489 * means is determined by the _fatal_error_handler() implementation, which
4490 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4491 *
4492 * If this is called from ISR context, the default system fatal error handler
4493 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004494 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004495 */
4496#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4497
4498/**
4499 * @brief Fatally terminate the system
4500 *
4501 * This should be called when the Zephyr kernel has encountered an
4502 * unrecoverable runtime condition and needs to terminate. What this ultimately
4503 * means is determined by the _fatal_error_handler() implementation, which
4504 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004505 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004506 */
4507#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4508
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004509/*
4510 * private APIs that are utilized by one or more public APIs
4511 */
4512
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004513#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004514/**
4515 * @internal
4516 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004517extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004518#else
Anas Nashif954d5502018-02-25 08:37:28 -06004519/**
4520 * @internal
4521 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004522#define _init_static_threads() do { } while ((0))
4523#endif
4524
Anas Nashif954d5502018-02-25 08:37:28 -06004525/**
4526 * @internal
4527 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004528extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004529/**
4530 * @internal
4531 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004532extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004533
Andrew Boiedc5d9352017-06-02 12:56:47 -07004534/* arch/cpu.h may declare an architecture or platform-specific macro
4535 * for properly declaring stacks, compatible with MMU/MPU constraints if
4536 * enabled
4537 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004538
4539/**
4540 * @brief Obtain an extern reference to a stack
4541 *
4542 * This macro properly brings the symbol of a thread stack declared
4543 * elsewhere into scope.
4544 *
4545 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004546 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004547 */
4548#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4549
Andrew Boiedc5d9352017-06-02 12:56:47 -07004550#ifdef _ARCH_THREAD_STACK_DEFINE
4551#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4552#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4553 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4554#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4555#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004556static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004557{
4558 return _ARCH_THREAD_STACK_BUFFER(sym);
4559}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004560#else
4561/**
4562 * @brief Declare a toplevel thread stack memory region
4563 *
4564 * This declares a region of memory suitable for use as a thread's stack.
4565 *
4566 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4567 * 'noinit' section so that it isn't zeroed at boot
4568 *
Andrew Boie507852a2017-07-25 18:47:07 -07004569 * The declared symbol will always be a k_thread_stack_t which can be passed to
4570 * k_thread_create, but should otherwise not be manipulated. If the buffer
4571 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004572 *
4573 * It is legal to precede this definition with the 'static' keyword.
4574 *
4575 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4576 * parameter of k_thread_create(), it may not be the same as the
4577 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4578 *
4579 * @param sym Thread stack symbol name
4580 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004581 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004582 */
4583#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004584 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004585
4586/**
4587 * @brief Declare a toplevel array of thread stack memory regions
4588 *
4589 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4590 * definition for additional details and constraints.
4591 *
4592 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4593 * 'noinit' section so that it isn't zeroed at boot
4594 *
4595 * @param sym Thread stack symbol name
4596 * @param nmemb Number of stacks to declare
4597 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004598 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004599 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004600#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004601 struct _k_thread_stack_element __noinit \
4602 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004603
4604/**
4605 * @brief Declare an embedded stack memory region
4606 *
4607 * Used for stacks embedded within other data structures. Use is highly
4608 * discouraged but in some cases necessary. For memory protection scenarios,
4609 * it is very important that any RAM preceding this member not be writable
4610 * by threads else a stack overflow will lead to silent corruption. In other
4611 * words, the containing data structure should live in RAM owned by the kernel.
4612 *
4613 * @param sym Thread stack symbol name
4614 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004615 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004616 */
4617#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004618 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004619
4620/**
4621 * @brief Return the size in bytes of a stack memory region
4622 *
4623 * Convenience macro for passing the desired stack size to k_thread_create()
4624 * since the underlying implementation may actually create something larger
4625 * (for instance a guard area).
4626 *
4627 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004628 * passed to K_THREAD_STACK_DEFINE.
4629 *
4630 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4631 * it is not guaranteed to return the original value since each array
4632 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004633 *
4634 * @param sym Stack memory symbol
4635 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004636 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004637 */
4638#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4639
4640/**
4641 * @brief Get a pointer to the physical stack buffer
4642 *
4643 * Convenience macro to get at the real underlying stack buffer used by
4644 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4645 * This is really only intended for diagnostic tools which want to examine
4646 * stack memory contents.
4647 *
4648 * @param sym Declared stack symbol name
4649 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004650 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004651 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004652static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004653{
4654 return (char *)sym;
4655}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004656
4657#endif /* _ARCH_DECLARE_STACK */
4658
Chunlin Hane9c97022017-07-07 20:29:30 +08004659/**
4660 * @defgroup mem_domain_apis Memory domain APIs
4661 * @ingroup kernel_apis
4662 * @{
4663 */
4664
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004665/**
4666 * @def MEM_PARTITION_ENTRY
4667 * @brief Used to declare a memory partition entry
4668 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004669 */
4670#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4671 {\
4672 .start = _start, \
4673 .size = _size, \
4674 .attr = _attr, \
4675 }
4676
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004677/**
4678 * @def K_MEM_PARTITION_DEFINE
4679 * @brief Used to declare a memory partition
4680 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004681 */
4682#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4683#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4684 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304685 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004686 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4687#else
4688#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304689 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004690 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4691#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4692
Chunlin Hane9c97022017-07-07 20:29:30 +08004693/* memory partition */
4694struct k_mem_partition {
4695 /* start address of memory partition */
4696 u32_t start;
4697 /* size of memory partition */
4698 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304699#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004700 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304701 k_mem_partition_attr_t attr;
4702#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004703};
4704
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304705/* memory domian
4706 * Note: Always declare this structure with __kernel prefix
4707 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004708struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304709#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004710 /* partitions in the domain */
4711 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304712#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004713 /* domain q */
4714 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004715 /* number of partitions in the domain */
4716 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004717};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304718
Chunlin Hane9c97022017-07-07 20:29:30 +08004719
4720/**
4721 * @brief Initialize a memory domain.
4722 *
4723 * Initialize a memory domain with given name and memory partitions.
4724 *
4725 * @param domain The memory domain to be initialized.
4726 * @param num_parts The number of array items of "parts" parameter.
4727 * @param parts An array of pointers to the memory partitions. Can be NULL
4728 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004729 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004730 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004731extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004732 struct k_mem_partition *parts[]);
4733/**
4734 * @brief Destroy a memory domain.
4735 *
4736 * Destroy a memory domain.
4737 *
4738 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004739 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004740 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004741extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4742
4743/**
4744 * @brief Add a memory partition into a memory domain.
4745 *
4746 * Add a memory partition into a memory domain.
4747 *
4748 * @param domain The memory domain to be added a memory partition.
4749 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004750 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004751 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004752extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4753 struct k_mem_partition *part);
4754
4755/**
4756 * @brief Remove a memory partition from a memory domain.
4757 *
4758 * Remove a memory partition from a memory domain.
4759 *
4760 * @param domain The memory domain to be removed a memory partition.
4761 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004762 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004763 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004764extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4765 struct k_mem_partition *part);
4766
4767/**
4768 * @brief Add a thread into a memory domain.
4769 *
4770 * Add a thread into a memory domain.
4771 *
4772 * @param domain The memory domain that the thread is going to be added into.
4773 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004774 *
4775 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004776 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004777extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4778 k_tid_t thread);
4779
4780/**
4781 * @brief Remove a thread from its memory domain.
4782 *
4783 * Remove a thread from its memory domain.
4784 *
4785 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004786 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004787 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004788extern void k_mem_domain_remove_thread(k_tid_t thread);
4789
Anas Nashif166f5192018-02-25 08:02:36 -06004790/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004791
Andrew Boie756f9072017-10-10 16:01:49 -07004792/**
4793 * @brief Emit a character buffer to the console device
4794 *
4795 * @param c String of characters to print
4796 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004797 *
4798 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004799 */
4800__syscall void k_str_out(char *c, size_t n);
4801
Andy Rosse7172672018-01-24 15:48:32 -08004802/**
4803 * @brief Start a numbered CPU on a MP-capable system
4804
4805 * This starts and initializes a specific CPU. The main thread on
4806 * startup is running on CPU zero, other processors are numbered
4807 * sequentially. On return from this function, the CPU is known to
4808 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004809 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004810 * with the provided key will work to enable them.
4811 *
4812 * Normally, in SMP mode this function will be called by the kernel
4813 * initialization and should not be used as a user API. But it is
4814 * defined here for special-purpose apps which want Zephyr running on
4815 * one core and to use others for design-specific processing.
4816 *
4817 * @param cpu_num Integer number of the CPU
4818 * @param stack Stack memory for the CPU
4819 * @param sz Stack buffer size, in bytes
4820 * @param fn Function to begin running on the CPU. First argument is
4821 * an irq_unlock() key.
4822 * @param arg Untyped argument to be passed to "fn"
4823 */
4824extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4825 void (*fn)(int, void *), void *arg);
4826
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004827#ifdef __cplusplus
4828}
4829#endif
4830
Andrew Boiee004dec2016-11-07 09:01:19 -08004831#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4832/*
4833 * Define new and delete operators.
4834 * At this moment, the operators do nothing since objects are supposed
4835 * to be statically allocated.
4836 */
4837inline void operator delete(void *ptr)
4838{
4839 (void)ptr;
4840}
4841
4842inline void operator delete[](void *ptr)
4843{
4844 (void)ptr;
4845}
4846
4847inline void *operator new(size_t size)
4848{
4849 (void)size;
4850 return NULL;
4851}
4852
4853inline void *operator new[](size_t size)
4854{
4855 (void)size;
4856 return NULL;
4857}
4858
4859/* Placement versions of operator new and delete */
4860inline void operator delete(void *ptr1, void *ptr2)
4861{
4862 (void)ptr1;
4863 (void)ptr2;
4864}
4865
4866inline void operator delete[](void *ptr1, void *ptr2)
4867{
4868 (void)ptr1;
4869 (void)ptr2;
4870}
4871
4872inline void *operator new(size_t size, void *ptr)
4873{
4874 (void)size;
4875 return ptr;
4876}
4877
4878inline void *operator new[](size_t size, void *ptr)
4879{
4880 (void)size;
4881 return ptr;
4882}
4883
4884#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4885
Andrew Boiefa94ee72017-09-28 16:54:35 -07004886#include <syscalls/kernel.h>
4887
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004888#endif /* !_ASMLANGUAGE */
4889
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004890#endif /* _kernel__h_ */