blob: 098bdd7faddf8e065076d455675008ca1312cc2a [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @file
9 *
10 * @brief Public kernel APIs.
11 */
12
13#ifndef _kernel__h_
14#define _kernel__h_
15
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050016#if !defined(_ASMLANGUAGE)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040017#include <stddef.h>
Kumar Gala78908162017-04-19 10:32:08 -050018#include <zephyr/types.h>
Anas Nashif173902f2017-01-17 07:08:56 -050019#include <limits.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020#include <toolchain.h>
Anas Nashif397d29d2017-06-17 11:30:47 -040021#include <linker/sections.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040022#include <atomic.h>
23#include <errno.h>
24#include <misc/__assert.h>
25#include <misc/dlist.h>
26#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050027#include <misc/util.h>
Andrew Boieaa6de292018-03-06 17:12:37 -080028#include <misc/mempool_base.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050029#include <kernel_version.h>
Leandro Pereiraadce1d12017-10-13 15:45:02 -070030#include <random/rand32.h>
Andrew Boie73abd322017-04-04 13:19:13 -070031#include <kernel_arch_thread.h>
Andrew Boie13ca6fe2017-09-23 12:05:49 -070032#include <syscall.h>
Andrew Boie43263fc2017-11-02 11:07:31 -070033#include <misc/printk.h>
34#include <arch/cpu.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040035
36#ifdef __cplusplus
37extern "C" {
38#endif
39
Anas Nashifbbb157d2017-01-15 08:46:31 -050040/**
41 * @brief Kernel APIs
42 * @defgroup kernel_apis Kernel APIs
43 * @{
44 * @}
45 */
46
Anas Nashif61f4b242016-11-18 10:53:59 -050047#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040048#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
49#else
50#define K_DEBUG(fmt, ...)
51#endif
52
Benjamin Walsh2f280412017-01-14 19:23:46 -050053#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
54#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
55#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
56#elif defined(CONFIG_COOP_ENABLED)
57#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
58#define _NUM_PREEMPT_PRIO (0)
59#elif defined(CONFIG_PREEMPT_ENABLED)
60#define _NUM_COOP_PRIO (0)
61#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
62#else
63#error "invalid configuration"
64#endif
65
66#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040067#define K_PRIO_PREEMPT(x) (x)
68
Benjamin Walsh456c6da2016-09-02 18:55:39 -040069#define K_ANY NULL
70#define K_END NULL
71
Benjamin Walshedb35702017-01-14 18:47:22 -050072#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040073#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050074#elif defined(CONFIG_COOP_ENABLED)
75#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
76#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040077#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050078#else
79#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040080#endif
81
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050082#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040083#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
84#else
85#define K_LOWEST_THREAD_PRIO -1
86#endif
87
Benjamin Walshfab8d922016-11-08 15:36:36 -050088#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
89
Benjamin Walsh456c6da2016-09-02 18:55:39 -040090#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
91#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
92
93typedef sys_dlist_t _wait_q_t;
94
Anas Nashif2f203c22016-12-18 06:57:45 -050095#ifdef CONFIG_OBJECT_TRACING
96#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
97#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040098#else
Anas Nashif2f203c22016-12-18 06:57:45 -050099#define _OBJECT_TRACING_INIT
100#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400101#endif
102
Benjamin Walshacc68c12017-01-29 18:57:45 -0500103#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300104#define _POLL_EVENT_OBJ_INIT(obj) \
105 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
106#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500107#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300108#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500109#define _POLL_EVENT
110#endif
111
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500112struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400113struct k_mutex;
114struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400115struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400116struct k_msgq;
117struct k_mbox;
118struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200119struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400120struct k_fifo;
121struct k_lifo;
122struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400123struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400124struct k_mem_pool;
125struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500126struct k_poll_event;
127struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800128struct k_mem_domain;
129struct k_mem_partition;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400130
Andrew Boie5bd891d2017-09-27 12:59:28 -0700131/* This enumeration needs to be kept in sync with the lists of kernel objects
132 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
133 * function in kernel/userspace.c
134 */
Andrew Boie945af952017-08-22 13:15:23 -0700135enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700136 K_OBJ_ANY,
137
Leandro Pereirac2003672018-04-04 13:50:32 -0700138 /** @cond
139 * Doxygen should ignore this build-time generated include file
140 * when genrating API documentation. Enumeration values are
141 * generated during build by gen_kobject_list.py. It includes
142 * basic kernel objects (e.g. pipes and mutexes) and driver types.
143 */
144#include <kobj-types-enum.h>
145 /** @endcond
146 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700147
Andrew Boie945af952017-08-22 13:15:23 -0700148 K_OBJ_LAST
149};
150
151#ifdef CONFIG_USERSPACE
152/* Table generated by gperf, these objects are retrieved via
153 * _k_object_find() */
154struct _k_object {
155 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700156 u8_t perms[CONFIG_MAX_THREAD_BYTES];
157 u8_t type;
158 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700159 u32_t data;
Andrew Boie945af952017-08-22 13:15:23 -0700160} __packed;
161
Andrew Boie877f82e2017-10-17 11:20:22 -0700162struct _k_object_assignment {
163 struct k_thread *thread;
164 void * const *objects;
165};
166
167/**
168 * @brief Grant a static thread access to a list of kernel objects
169 *
170 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
171 * a set of kernel objects. These objects do not need to be in an initialized
172 * state. The permissions will be granted when the threads are initialized
173 * in the early boot sequence.
174 *
175 * All arguments beyond the first must be pointers to kernel objects.
176 *
177 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
178 */
179#define K_THREAD_ACCESS_GRANT(name_, ...) \
180 static void * const _CONCAT(_object_list_, name_)[] = \
181 { __VA_ARGS__, NULL }; \
182 static __used __in_section_unique(object_access) \
183 const struct _k_object_assignment \
184 _CONCAT(_object_access_, name_) = \
185 { (&_k_thread_obj_ ## name_), \
186 (_CONCAT(_object_list_, name_)) }
187
Andrew Boie945af952017-08-22 13:15:23 -0700188#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700189#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie97bf0012018-04-24 17:01:37 -0700190#define K_OBJ_FLAG_ALLOC BIT(2)
Andrew Boie945af952017-08-22 13:15:23 -0700191
192/**
193 * Lookup a kernel object and init its metadata if it exists
194 *
195 * Calling this on an object will make it usable from userspace.
196 * Intended to be called as the last statement in kernel object init
197 * functions.
198 *
199 * @param object Address of the kernel object
200 */
201void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700202#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700203
204#define K_THREAD_ACCESS_GRANT(thread, ...)
205
Anas Nashif954d5502018-02-25 08:37:28 -0600206/**
207 * @internal
208 */
Andrew Boie743e4682017-10-04 12:25:50 -0700209static inline void _k_object_init(void *obj)
210{
211 ARG_UNUSED(obj);
212}
213
Anas Nashif954d5502018-02-25 08:37:28 -0600214/**
215 * @internal
216 */
Andrew Boie743e4682017-10-04 12:25:50 -0700217static inline void _impl_k_object_access_grant(void *object,
218 struct k_thread *thread)
219{
220 ARG_UNUSED(object);
221 ARG_UNUSED(thread);
222}
223
Anas Nashif954d5502018-02-25 08:37:28 -0600224/**
225 * @internal
226 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700227static inline void k_object_access_revoke(void *object,
228 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700229{
230 ARG_UNUSED(object);
231 ARG_UNUSED(thread);
232}
233
Andrew Boiee9cfc542018-04-13 13:15:28 -0700234/**
235 * @internal
236 */
237static inline void _impl_k_object_release(void *object)
238{
239 ARG_UNUSED(object);
240}
241
Andrew Boie41bab6e2017-10-14 14:42:23 -0700242static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700243{
244 ARG_UNUSED(object);
245}
246#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700247
248/**
249 * grant a thread access to a kernel object
250 *
251 * The thread will be granted access to the object if the caller is from
252 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700253 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700254 *
255 * @param object Address of kernel object
256 * @param thread Thread to grant access to the object
257 */
Andrew Boie743e4682017-10-04 12:25:50 -0700258__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700259
Andrew Boiea89bf012017-10-09 14:47:55 -0700260/**
261 * grant a thread access to a kernel object
262 *
263 * The thread will lose access to the object if the caller is from
264 * supervisor mode, or the caller is from user mode AND has permissions
265 * on both the object and the thread whose access is being revoked.
266 *
267 * @param object Address of kernel object
268 * @param thread Thread to remove access to the object
269 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700270void k_object_access_revoke(void *object, struct k_thread *thread);
271
272
273__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700274
275/**
276 * grant all present and future threads access to an object
277 *
278 * If the caller is from supervisor mode, or the caller is from user mode and
279 * have sufficient permissions on the object, then that object will have
280 * permissions granted to it for *all* current and future threads running in
281 * the system, effectively becoming a public kernel object.
282 *
283 * Use of this API should be avoided on systems that are running untrusted code
284 * as it is possible for such code to derive the addresses of kernel objects
285 * and perform unwanted operations on them.
286 *
Andrew Boie04caa672017-10-13 13:57:07 -0700287 * It is not possible to revoke permissions on public objects; once public,
288 * any thread may use it.
289 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700290 * @param object Address of kernel object
291 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700292void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700293
Andrew Boie31bdfc02017-11-08 16:38:03 -0800294/**
295 * Allocate a kernel object of a designated type
296 *
297 * This will instantiate at runtime a kernel object of the specified type,
298 * returning a pointer to it. The object will be returned in an uninitialized
299 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700300 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800301 *
302 * Currently, allocation of thread stacks is not supported.
303 *
304 * @param otype Requested kernel object type
305 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
306 * available
307 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700308__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800309
Andrew Boie97bf0012018-04-24 17:01:37 -0700310#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800311/**
312 * Free a kernel object previously allocated with k_object_alloc()
313 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700314 * This will return memory for a kernel object back to resource pool it was
315 * allocated from. Care must be exercised that the object will not be used
316 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800317 *
318 * @param obj Pointer to the kernel object memory address.
319 */
320void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700321#else
322static inline void *_impl_k_object_alloc(enum k_objects otype)
323{
324 return NULL;
325}
326
327static inline void k_obj_free(void *obj)
328{
329 ARG_UNUSED(obj);
330}
Andrew Boie31bdfc02017-11-08 16:38:03 -0800331#endif /* CONFIG_DYNAMIC_OBJECTS */
332
Andrew Boiebca15da2017-10-15 14:17:48 -0700333/* Using typedef deliberately here, this is quite intended to be an opaque
334 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
335 *
336 * The purpose of this data type is to clearly distinguish between the
337 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
338 * buffer which composes the stack data actually used by the underlying
339 * thread; they cannot be used interchangably as some arches precede the
340 * stack buffer region with guard areas that trigger a MPU or MMU fault
341 * if written to.
342 *
343 * APIs that want to work with the buffer inside should continue to use
344 * char *.
345 *
346 * Stacks should always be created with K_THREAD_STACK_DEFINE().
347 */
348struct __packed _k_thread_stack_element {
349 char data;
350};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700351typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700352
Andrew Boie73abd322017-04-04 13:19:13 -0700353/* timeouts */
354
355struct _timeout;
356typedef void (*_timeout_func_t)(struct _timeout *t);
357
358struct _timeout {
359 sys_dnode_t node;
360 struct k_thread *thread;
361 sys_dlist_t *wait_q;
362 s32_t delta_ticks_from_prev;
363 _timeout_func_t func;
364};
365
366extern s32_t _timeout_remaining_get(struct _timeout *timeout);
367
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700368/**
369 * @typedef k_thread_entry_t
370 * @brief Thread entry point function type.
371 *
372 * A thread's entry point function is invoked when the thread starts executing.
373 * Up to 3 argument values can be passed to the function.
374 *
375 * The thread terminates execution permanently if the entry point function
376 * returns. The thread is responsible for releasing any shared resources
377 * it may own (such as mutexes and dynamically allocated memory), prior to
378 * returning.
379 *
380 * @param p1 First argument.
381 * @param p2 Second argument.
382 * @param p3 Third argument.
383 *
384 * @return N/A
385 */
386typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700387
388#ifdef CONFIG_THREAD_MONITOR
389struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700390 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700391 void *parameter1;
392 void *parameter2;
393 void *parameter3;
394};
395#endif
396
397/* can be used for creating 'dummy' threads, e.g. for pending on objects */
398struct _thread_base {
399
400 /* this thread's entry in a ready/wait queue */
401 sys_dnode_t k_q_node;
402
403 /* user facing 'thread options'; values defined in include/kernel.h */
404 u8_t user_options;
405
406 /* thread state */
407 u8_t thread_state;
408
409 /*
410 * scheduler lock count and thread priority
411 *
412 * These two fields control the preemptibility of a thread.
413 *
414 * When the scheduler is locked, sched_locked is decremented, which
415 * means that the scheduler is locked for values from 0xff to 0x01. A
416 * thread is coop if its prio is negative, thus 0x80 to 0xff when
417 * looked at the value as unsigned.
418 *
419 * By putting them end-to-end, this means that a thread is
420 * non-preemptible if the bundled value is greater than or equal to
421 * 0x0080.
422 */
423 union {
424 struct {
425#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
426 u8_t sched_locked;
427 s8_t prio;
428#else /* LITTLE and PDP */
429 s8_t prio;
430 u8_t sched_locked;
431#endif
432 };
433 u16_t preempt;
434 };
435
Andy Ross2724fd12018-01-29 14:55:20 -0800436#ifdef CONFIG_SMP
437 /* True for the per-CPU idle threads */
438 u8_t is_idle;
439
440 /* Non-zero when actively running on a CPU */
441 u8_t active;
442
443 /* CPU index on which thread was last run */
444 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700445
446 /* Recursive count of irq_lock() calls */
447 u8_t global_lock_count;
Andy Ross2724fd12018-01-29 14:55:20 -0800448#endif
449
Andrew Boie73abd322017-04-04 13:19:13 -0700450 /* data returned by APIs */
451 void *swap_data;
452
453#ifdef CONFIG_SYS_CLOCK_EXISTS
454 /* this thread's entry in a timeout queue */
455 struct _timeout timeout;
456#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700457};
458
459typedef struct _thread_base _thread_base_t;
460
461#if defined(CONFIG_THREAD_STACK_INFO)
462/* Contains the stack information of a thread */
463struct _thread_stack_info {
464 /* Stack Start */
465 u32_t start;
466 /* Stack Size */
467 u32_t size;
468};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700469
470typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700471#endif /* CONFIG_THREAD_STACK_INFO */
472
Chunlin Hane9c97022017-07-07 20:29:30 +0800473#if defined(CONFIG_USERSPACE)
474struct _mem_domain_info {
475 /* memory domain queue node */
476 sys_dnode_t mem_domain_q_node;
477 /* memory domain of the thread */
478 struct k_mem_domain *mem_domain;
479};
480
481#endif /* CONFIG_USERSPACE */
482
Andrew Boie73abd322017-04-04 13:19:13 -0700483struct k_thread {
484
485 struct _thread_base base;
486
487 /* defined by the architecture, but all archs need these */
488 struct _caller_saved caller_saved;
489 struct _callee_saved callee_saved;
490
491 /* static thread init data */
492 void *init_data;
493
494 /* abort function */
495 void (*fn_abort)(void);
496
497#if defined(CONFIG_THREAD_MONITOR)
498 /* thread entry and parameters description */
499 struct __thread_entry *entry;
500
501 /* next item in list of all threads */
502 struct k_thread *next_thread;
503#endif
504
505#ifdef CONFIG_THREAD_CUSTOM_DATA
506 /* crude thread-local storage */
507 void *custom_data;
508#endif
509
510#ifdef CONFIG_ERRNO
511 /* per-thread errno variable */
512 int errno_var;
513#endif
514
515#if defined(CONFIG_THREAD_STACK_INFO)
516 /* Stack Info */
517 struct _thread_stack_info stack_info;
518#endif /* CONFIG_THREAD_STACK_INFO */
519
Chunlin Hane9c97022017-07-07 20:29:30 +0800520#if defined(CONFIG_USERSPACE)
521 /* memory domain info of the thread */
522 struct _mem_domain_info mem_domain_info;
Andrew Boiebca15da2017-10-15 14:17:48 -0700523 /* Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700524 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800525#endif /* CONFIG_USERSPACE */
526
Andy Ross042d8ec2017-12-09 08:37:20 -0800527#if defined(CONFIG_USE_SWITCH)
528 /* When using __switch() a few previously arch-specific items
529 * become part of the core OS
530 */
531
532 /* _Swap() return value */
533 int swap_retval;
534
535 /* Context handle returned via _arch_switch() */
536 void *switch_handle;
537#endif
Andrew Boie92e5bd72018-04-12 17:12:15 -0700538 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800539
Andrew Boie73abd322017-04-04 13:19:13 -0700540 /* arch-specifics: must always be at the end */
541 struct _thread_arch arch;
542};
543
544typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400545typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700546#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400547
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400548enum execution_context_types {
549 K_ISR = 0,
550 K_COOP_THREAD,
551 K_PREEMPT_THREAD,
552};
553
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400554/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100555 * @defgroup profiling_apis Profiling APIs
556 * @ingroup kernel_apis
557 * @{
558 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530559typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
560 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100561
562/**
563 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
564 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700565 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100566 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
567 *
568 * CONFIG_MAIN_STACK_SIZE
569 * CONFIG_IDLE_STACK_SIZE
570 * CONFIG_ISR_STACK_SIZE
571 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
572 *
573 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
574 * produce output.
575 *
576 * @return N/A
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530577 *
578 * @deprecated This API is deprecated. Use k_thread_foreach().
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100579 */
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530580__deprecated extern void k_call_stacks_analyze(void);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100581
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530582/**
583 * @brief Iterate over all the threads in the system.
584 *
585 * This routine iterates over all the threads in the system and
586 * calls the user_cb function for each thread.
587 *
588 * @param user_cb Pointer to the user callback function.
589 * @param user_data Pointer to user data.
590 *
591 * @note CONFIG_THREAD_MONITOR must be set for this function
592 * to be effective. Also this API uses irq_lock to protect the
593 * _kernel.threads list which means creation of new threads and
594 * terminations of existing threads are blocked until this
595 * API returns.
596 *
597 * @return N/A
598 */
599extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
600
Anas Nashif166f5192018-02-25 08:02:36 -0600601/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100602
603/**
Allan Stephensc98da842016-11-11 15:45:03 -0500604 * @defgroup thread_apis Thread APIs
605 * @ingroup kernel_apis
606 * @{
607 */
608
Benjamin Walshed240f22017-01-22 13:05:08 -0500609#endif /* !_ASMLANGUAGE */
610
611
612/*
613 * Thread user options. May be needed by assembly code. Common part uses low
614 * bits, arch-specific use high bits.
615 */
616
617/* system thread that must not abort */
618#define K_ESSENTIAL (1 << 0)
619
620#if defined(CONFIG_FP_SHARING)
621/* thread uses floating point registers */
622#define K_FP_REGS (1 << 1)
623#endif
624
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700625/* This thread has dropped from supervisor mode to user mode and consequently
626 * has additional restrictions
627 */
628#define K_USER (1 << 2)
629
Andrew Boie47f8fd12017-10-05 11:11:02 -0700630/* Indicates that the thread being created should inherit all kernel object
631 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
632 * is not enabled.
633 */
634#define K_INHERIT_PERMS (1 << 3)
635
Benjamin Walshed240f22017-01-22 13:05:08 -0500636#ifdef CONFIG_X86
637/* x86 Bitmask definitions for threads user options */
638
639#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
640/* thread uses SSEx (and also FP) registers */
641#define K_SSE_REGS (1 << 7)
642#endif
643#endif
644
645/* end - thread options */
646
647#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400648/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700649 * @brief Create a thread.
650 *
651 * This routine initializes a thread, then schedules it for execution.
652 *
653 * The new thread may be scheduled for immediate execution or a delayed start.
654 * If the newly spawned thread does not have a delayed start the kernel
655 * scheduler may preempt the current thread to allow the new thread to
656 * execute.
657 *
658 * Thread options are architecture-specific, and can include K_ESSENTIAL,
659 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
660 * them using "|" (the logical OR operator).
661 *
662 * Historically, users often would use the beginning of the stack memory region
663 * to store the struct k_thread data, although corruption will occur if the
664 * stack overflows this region and stack protection features may not detect this
665 * situation.
666 *
667 * @param new_thread Pointer to uninitialized struct k_thread
668 * @param stack Pointer to the stack space.
669 * @param stack_size Stack size in bytes.
670 * @param entry Thread entry function.
671 * @param p1 1st entry point parameter.
672 * @param p2 2nd entry point parameter.
673 * @param p3 3rd entry point parameter.
674 * @param prio Thread priority.
675 * @param options Thread options.
676 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
677 *
678 * @return ID of new thread.
679 */
Andrew Boie662c3452017-10-02 10:51:18 -0700680__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700681 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700682 size_t stack_size,
683 k_thread_entry_t entry,
684 void *p1, void *p2, void *p3,
685 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700686
Andrew Boie3f091b52017-08-30 14:34:14 -0700687/**
688 * @brief Drop a thread's privileges permanently to user mode
689 *
690 * @param entry Function to start executing from
691 * @param p1 1st entry point parameter
692 * @param p2 2nd entry point parameter
693 * @param p3 3rd entry point parameter
694 */
695extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
696 void *p1, void *p2,
697 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700698
Andrew Boied26cf2d2017-03-30 13:07:02 -0700699/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700700 * @brief Grant a thread access to a NULL-terminated set of kernel objects
701 *
702 * This is a convenience function. For the provided thread, grant access to
703 * the remaining arguments, which must be pointers to kernel objects.
704 * The final argument must be a NULL.
705 *
706 * The thread object must be initialized (i.e. running). The objects don't
707 * need to be.
708 *
709 * @param thread Thread to grant access to objects
710 * @param ... NULL-terminated list of kernel object pointers
711 */
712extern void __attribute__((sentinel))
713 k_thread_access_grant(struct k_thread *thread, ...);
714
715/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700716 * @brief Assign a resource memory pool to a thread
717 *
718 * By default, threads have no resource pool assigned unless their parent
719 * thread has a resource pool, in which case it is inherited. Multiple
720 * threads may be assigned to the same memory pool.
721 *
722 * Changing a thread's resource pool will not migrate allocations from the
723 * previous pool.
724 *
725 * @param thread Target thread to assign a memory pool for resource requests,
726 * or NULL if the thread should no longer have a memory pool.
727 * @param pool Memory pool to use for resources.
728 */
729static inline void k_thread_resource_pool_assign(struct k_thread *thread,
730 struct k_mem_pool *pool)
731{
732 thread->resource_pool = pool;
733}
734
735#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
736/**
737 * @brief Assign the system heap as a thread's resource pool
738 *
739 * Similar to k_thread_resource_pool_assign(), but the thread will use
740 * the kernel heap to draw memory.
741 *
742 * Use with caution, as a malicious thread could perform DoS attacks on the
743 * kernel heap.
744 *
745 * @param thread Target thread to assign the system heap for resource requests
746 */
747void k_thread_system_pool_assign(struct k_thread *thread);
748#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
749
750/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500751 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400752 *
Allan Stephensc98da842016-11-11 15:45:03 -0500753 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500754 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400755 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500756 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400757 *
758 * @return N/A
759 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700760__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400761
762/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500763 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400764 *
765 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500766 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400767 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400768 * @return N/A
769 */
Kumar Galacc334c72017-04-21 10:55:34 -0500770extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400771
772/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500773 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400774 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500775 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400776 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500777 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400778 *
779 * @return N/A
780 */
Andrew Boie468190a2017-09-29 14:00:48 -0700781__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400782
783/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500784 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400785 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500786 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400787 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500788 * If @a thread is not currently sleeping, the routine has no effect.
789 *
790 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400791 *
792 * @return N/A
793 */
Andrew Boie468190a2017-09-29 14:00:48 -0700794__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400795
796/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500797 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500799 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400800 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700801__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400802
803/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500804 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400805 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500806 * This routine prevents @a thread from executing if it has not yet started
807 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400808 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500809 * @param thread ID of thread to cancel.
810 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700811 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500812 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700813 *
814 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400815 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700816__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400817
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400818/**
Allan Stephensc98da842016-11-11 15:45:03 -0500819 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400820 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500821 * This routine permanently stops execution of @a thread. The thread is taken
822 * off all kernel queues it is part of (i.e. the ready queue, the timeout
823 * queue, or a kernel object wait queue). However, any kernel resources the
824 * thread might currently own (such as mutexes or memory blocks) are not
825 * released. It is the responsibility of the caller of this routine to ensure
826 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400827 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500828 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400829 *
830 * @return N/A
831 */
Andrew Boie468190a2017-09-29 14:00:48 -0700832__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400833
Andrew Boie7d627c52017-08-30 11:01:56 -0700834
835/**
836 * @brief Start an inactive thread
837 *
838 * If a thread was created with K_FOREVER in the delay parameter, it will
839 * not be added to the scheduling queue until this function is called
840 * on it.
841 *
842 * @param thread thread to start
843 */
Andrew Boie468190a2017-09-29 14:00:48 -0700844__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700845
Allan Stephensc98da842016-11-11 15:45:03 -0500846/**
847 * @cond INTERNAL_HIDDEN
848 */
849
Benjamin Walshd211a522016-12-06 11:44:01 -0500850/* timeout has timed out and is not on _timeout_q anymore */
851#define _EXPIRED (-2)
852
853/* timeout is not in use */
854#define _INACTIVE (-1)
855
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400856struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700857 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700858 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400859 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700860 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500861 void *init_p1;
862 void *init_p2;
863 void *init_p3;
864 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500865 u32_t init_options;
866 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500867 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400868};
869
Andrew Boied26cf2d2017-03-30 13:07:02 -0700870#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400871 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500872 prio, options, delay, abort, groups) \
873 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700874 .init_thread = (thread), \
875 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500876 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700877 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400878 .init_p1 = (void *)p1, \
879 .init_p2 = (void *)p2, \
880 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500881 .init_prio = (prio), \
882 .init_options = (options), \
883 .init_delay = (delay), \
884 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400885 }
886
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400887/**
Allan Stephensc98da842016-11-11 15:45:03 -0500888 * INTERNAL_HIDDEN @endcond
889 */
890
891/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500892 * @brief Statically define and initialize a thread.
893 *
894 * The thread may be scheduled for immediate execution or a delayed start.
895 *
896 * Thread options are architecture-specific, and can include K_ESSENTIAL,
897 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
898 * them using "|" (the logical OR operator).
899 *
900 * The ID of the thread can be accessed using:
901 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500902 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500903 *
904 * @param name Name of the thread.
905 * @param stack_size Stack size in bytes.
906 * @param entry Thread entry function.
907 * @param p1 1st entry point parameter.
908 * @param p2 2nd entry point parameter.
909 * @param p3 3rd entry point parameter.
910 * @param prio Thread priority.
911 * @param options Thread options.
912 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400913 *
914 * @internal It has been observed that the x86 compiler by default aligns
915 * these _static_thread_data structures to 32-byte boundaries, thereby
916 * wasting space. To work around this, force a 4-byte alignment.
917 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500918#define K_THREAD_DEFINE(name, stack_size, \
919 entry, p1, p2, p3, \
920 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700921 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700922 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500923 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500924 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700925 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
926 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500927 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700928 NULL, 0); \
929 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400930
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400931/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500932 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400933 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500934 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400935 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500936 * @param thread ID of thread whose priority is needed.
937 *
938 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400939 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700940__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400941
942/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500943 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400944 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500945 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400946 *
947 * Rescheduling can occur immediately depending on the priority @a thread is
948 * set to:
949 *
950 * - If its priority is raised above the priority of the caller of this
951 * function, and the caller is preemptible, @a thread will be scheduled in.
952 *
953 * - If the caller operates on itself, it lowers its priority below that of
954 * other threads in the system, and the caller is preemptible, the thread of
955 * highest priority will be scheduled in.
956 *
957 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
958 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
959 * highest priority.
960 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500961 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400962 * @param prio New priority.
963 *
964 * @warning Changing the priority of a thread currently involved in mutex
965 * priority inheritance may result in undefined behavior.
966 *
967 * @return N/A
968 */
Andrew Boie468190a2017-09-29 14:00:48 -0700969__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400970
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400971/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500972 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400973 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500974 * This routine prevents the kernel scheduler from making @a thread the
975 * current thread. All other internal operations on @a thread are still
976 * performed; for example, any timeout it is waiting on keeps ticking,
977 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400978 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500979 * If @a thread is already suspended, the routine has no effect.
980 *
981 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400982 *
983 * @return N/A
984 */
Andrew Boie468190a2017-09-29 14:00:48 -0700985__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400986
987/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500988 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400989 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500990 * This routine allows the kernel scheduler to make @a thread the current
991 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500993 * If @a thread is not currently suspended, the routine has no effect.
994 *
995 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400996 *
997 * @return N/A
998 */
Andrew Boie468190a2017-09-29 14:00:48 -0700999__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001000
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001001/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001002 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001003 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001004 * This routine specifies how the scheduler will perform time slicing of
1005 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001006 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001007 * To enable time slicing, @a slice must be non-zero. The scheduler
1008 * ensures that no thread runs for more than the specified time limit
1009 * before other threads of that priority are given a chance to execute.
1010 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001011 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001013 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001014 * execute. Once the scheduler selects a thread for execution, there is no
1015 * minimum guaranteed time the thread will execute before threads of greater or
1016 * equal priority are scheduled.
1017 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001018 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001019 * for execution, this routine has no effect; the thread is immediately
1020 * rescheduled after the slice period expires.
1021 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001022 * To disable timeslicing, set both @a slice and @a prio to zero.
1023 *
1024 * @param slice Maximum time slice length (in milliseconds).
1025 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001026 *
1027 * @return N/A
1028 */
Kumar Galacc334c72017-04-21 10:55:34 -05001029extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001030
Anas Nashif166f5192018-02-25 08:02:36 -06001031/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001032
1033/**
1034 * @addtogroup isr_apis
1035 * @{
1036 */
1037
1038/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001039 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001040 *
Allan Stephensc98da842016-11-11 15:45:03 -05001041 * This routine allows the caller to customize its actions, depending on
1042 * whether it is a thread or an ISR.
1043 *
1044 * @note Can be called by ISRs.
1045 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001046 * @return 0 if invoked by a thread.
1047 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001048 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -05001049extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001050
Benjamin Walsh445830d2016-11-10 15:54:27 -05001051/**
1052 * @brief Determine if code is running in a preemptible thread.
1053 *
Allan Stephensc98da842016-11-11 15:45:03 -05001054 * This routine allows the caller to customize its actions, depending on
1055 * whether it can be preempted by another thread. The routine returns a 'true'
1056 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001057 *
Allan Stephensc98da842016-11-11 15:45:03 -05001058 * - The code is running in a thread, not at ISR.
1059 * - The thread's priority is in the preemptible range.
1060 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001061 *
Allan Stephensc98da842016-11-11 15:45:03 -05001062 * @note Can be called by ISRs.
1063 *
1064 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001065 * @return Non-zero if invoked by a preemptible thread.
1066 */
Andrew Boie468190a2017-09-29 14:00:48 -07001067__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001068
Allan Stephensc98da842016-11-11 15:45:03 -05001069/**
Anas Nashif166f5192018-02-25 08:02:36 -06001070 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001071 */
1072
1073/**
1074 * @addtogroup thread_apis
1075 * @{
1076 */
1077
1078/**
1079 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001080 *
Allan Stephensc98da842016-11-11 15:45:03 -05001081 * This routine prevents the current thread from being preempted by another
1082 * thread by instructing the scheduler to treat it as a cooperative thread.
1083 * If the thread subsequently performs an operation that makes it unready,
1084 * it will be context switched out in the normal manner. When the thread
1085 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001086 *
Allan Stephensc98da842016-11-11 15:45:03 -05001087 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001088 *
Allan Stephensc98da842016-11-11 15:45:03 -05001089 * @note k_sched_lock() and k_sched_unlock() should normally be used
1090 * when the operation being performed can be safely interrupted by ISRs.
1091 * However, if the amount of processing involved is very small, better
1092 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001093 *
1094 * @return N/A
1095 */
1096extern void k_sched_lock(void);
1097
Allan Stephensc98da842016-11-11 15:45:03 -05001098/**
1099 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001100 *
Allan Stephensc98da842016-11-11 15:45:03 -05001101 * This routine reverses the effect of a previous call to k_sched_lock().
1102 * A thread must call the routine once for each time it called k_sched_lock()
1103 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001104 *
1105 * @return N/A
1106 */
1107extern void k_sched_unlock(void);
1108
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001109/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001110 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001111 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001112 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001113 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001114 * Custom data is not used by the kernel itself, and is freely available
1115 * for a thread to use as it sees fit. It can be used as a framework
1116 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001117 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001118 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001119 *
1120 * @return N/A
1121 */
Andrew Boie468190a2017-09-29 14:00:48 -07001122__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001123
1124/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001125 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001126 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001127 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001128 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001129 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001130 */
Andrew Boie468190a2017-09-29 14:00:48 -07001131__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001132
1133/**
Anas Nashif166f5192018-02-25 08:02:36 -06001134 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001135 */
1136
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001137#include <sys_clock.h>
1138
Allan Stephensc2f15a42016-11-17 12:24:22 -05001139/**
1140 * @addtogroup clock_apis
1141 * @{
1142 */
1143
1144/**
1145 * @brief Generate null timeout delay.
1146 *
1147 * This macro generates a timeout delay that that instructs a kernel API
1148 * not to wait if the requested operation cannot be performed immediately.
1149 *
1150 * @return Timeout delay value.
1151 */
1152#define K_NO_WAIT 0
1153
1154/**
1155 * @brief Generate timeout delay from milliseconds.
1156 *
1157 * This macro generates a timeout delay that that instructs a kernel API
1158 * to wait up to @a ms milliseconds to perform the requested operation.
1159 *
1160 * @param ms Duration in milliseconds.
1161 *
1162 * @return Timeout delay value.
1163 */
Johan Hedberg14471692016-11-13 10:52:15 +02001164#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001165
1166/**
1167 * @brief Generate timeout delay from seconds.
1168 *
1169 * This macro generates a timeout delay that that instructs a kernel API
1170 * to wait up to @a s seconds to perform the requested operation.
1171 *
1172 * @param s Duration in seconds.
1173 *
1174 * @return Timeout delay value.
1175 */
Johan Hedberg14471692016-11-13 10:52:15 +02001176#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001177
1178/**
1179 * @brief Generate timeout delay from minutes.
1180 *
1181 * This macro generates a timeout delay that that instructs a kernel API
1182 * to wait up to @a m minutes to perform the requested operation.
1183 *
1184 * @param m Duration in minutes.
1185 *
1186 * @return Timeout delay value.
1187 */
Johan Hedberg14471692016-11-13 10:52:15 +02001188#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001189
1190/**
1191 * @brief Generate timeout delay from hours.
1192 *
1193 * This macro generates a timeout delay that that instructs a kernel API
1194 * to wait up to @a h hours to perform the requested operation.
1195 *
1196 * @param h Duration in hours.
1197 *
1198 * @return Timeout delay value.
1199 */
Johan Hedberg14471692016-11-13 10:52:15 +02001200#define K_HOURS(h) K_MINUTES((h) * 60)
1201
Allan Stephensc98da842016-11-11 15:45:03 -05001202/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001203 * @brief Generate infinite timeout delay.
1204 *
1205 * This macro generates a timeout delay that that instructs a kernel API
1206 * to wait as long as necessary to perform the requested operation.
1207 *
1208 * @return Timeout delay value.
1209 */
1210#define K_FOREVER (-1)
1211
1212/**
Anas Nashif166f5192018-02-25 08:02:36 -06001213 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001214 */
1215
1216/**
Allan Stephensc98da842016-11-11 15:45:03 -05001217 * @cond INTERNAL_HIDDEN
1218 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001219
Benjamin Walsh62092182016-12-20 14:39:08 -05001220/* kernel clocks */
1221
1222#if (sys_clock_ticks_per_sec == 1000) || \
1223 (sys_clock_ticks_per_sec == 500) || \
1224 (sys_clock_ticks_per_sec == 250) || \
1225 (sys_clock_ticks_per_sec == 125) || \
1226 (sys_clock_ticks_per_sec == 100) || \
1227 (sys_clock_ticks_per_sec == 50) || \
1228 (sys_clock_ticks_per_sec == 25) || \
1229 (sys_clock_ticks_per_sec == 20) || \
1230 (sys_clock_ticks_per_sec == 10) || \
1231 (sys_clock_ticks_per_sec == 1)
1232
1233 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1234#else
1235 /* yields horrible 64-bit math on many architectures: try to avoid */
1236 #define _NON_OPTIMIZED_TICKS_PER_SEC
1237#endif
1238
1239#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001240extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001241#else
Kumar Galacc334c72017-04-21 10:55:34 -05001242static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001243{
Kumar Galacc334c72017-04-21 10:55:34 -05001244 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001245}
1246#endif
1247
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001248/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001249#ifdef CONFIG_TICKLESS_KERNEL
1250#define _TICK_ALIGN 0
1251#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001252#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001253#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001254
Kumar Galacc334c72017-04-21 10:55:34 -05001255static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001256{
Benjamin Walsh62092182016-12-20 14:39:08 -05001257#ifdef CONFIG_SYS_CLOCK_EXISTS
1258
1259#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001260 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001261#else
Kumar Galacc334c72017-04-21 10:55:34 -05001262 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001263#endif
1264
1265#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001266 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001267 return 0;
1268#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001269}
1270
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001271struct k_timer {
1272 /*
1273 * _timeout structure must be first here if we want to use
1274 * dynamic timer allocation. timeout.node is used in the double-linked
1275 * list of free timers
1276 */
1277 struct _timeout timeout;
1278
Allan Stephens45bfa372016-10-12 12:39:42 -05001279 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001280 _wait_q_t wait_q;
1281
1282 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001283 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001284
1285 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001286 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001287
1288 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001289 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001290
Allan Stephens45bfa372016-10-12 12:39:42 -05001291 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001292 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001293
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001294 /* user-specific data, also used to support legacy features */
1295 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001296
Anas Nashif2f203c22016-12-18 06:57:45 -05001297 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001298};
1299
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001300#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001301 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001302 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001303 .timeout.wait_q = NULL, \
1304 .timeout.thread = NULL, \
1305 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001306 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001307 .expiry_fn = expiry, \
1308 .stop_fn = stop, \
1309 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001310 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001311 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001312 }
1313
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001314#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1315
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001316/**
Allan Stephensc98da842016-11-11 15:45:03 -05001317 * INTERNAL_HIDDEN @endcond
1318 */
1319
1320/**
1321 * @defgroup timer_apis Timer APIs
1322 * @ingroup kernel_apis
1323 * @{
1324 */
1325
1326/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001327 * @typedef k_timer_expiry_t
1328 * @brief Timer expiry function type.
1329 *
1330 * A timer's expiry function is executed by the system clock interrupt handler
1331 * each time the timer expires. The expiry function is optional, and is only
1332 * invoked if the timer has been initialized with one.
1333 *
1334 * @param timer Address of timer.
1335 *
1336 * @return N/A
1337 */
1338typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1339
1340/**
1341 * @typedef k_timer_stop_t
1342 * @brief Timer stop function type.
1343 *
1344 * A timer's stop function is executed if the timer is stopped prematurely.
1345 * The function runs in the context of the thread that stops the timer.
1346 * The stop function is optional, and is only invoked if the timer has been
1347 * initialized with one.
1348 *
1349 * @param timer Address of timer.
1350 *
1351 * @return N/A
1352 */
1353typedef void (*k_timer_stop_t)(struct k_timer *timer);
1354
1355/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001356 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001357 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001358 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001359 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001360 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001361 *
1362 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001363 * @param expiry_fn Function to invoke each time the timer expires.
1364 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001365 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001366#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001367 struct k_timer name \
1368 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001369 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001370
Allan Stephens45bfa372016-10-12 12:39:42 -05001371/**
1372 * @brief Initialize a timer.
1373 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001374 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001375 *
1376 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001377 * @param expiry_fn Function to invoke each time the timer expires.
1378 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001379 *
1380 * @return N/A
1381 */
1382extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001383 k_timer_expiry_t expiry_fn,
1384 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001385
Allan Stephens45bfa372016-10-12 12:39:42 -05001386/**
1387 * @brief Start a timer.
1388 *
1389 * This routine starts a timer, and resets its status to zero. The timer
1390 * begins counting down using the specified duration and period values.
1391 *
1392 * Attempting to start a timer that is already running is permitted.
1393 * The timer's status is reset to zero and the timer begins counting down
1394 * using the new duration and period values.
1395 *
1396 * @param timer Address of timer.
1397 * @param duration Initial timer duration (in milliseconds).
1398 * @param period Timer period (in milliseconds).
1399 *
1400 * @return N/A
1401 */
Andrew Boiea354d492017-09-29 16:22:28 -07001402__syscall void k_timer_start(struct k_timer *timer,
1403 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001404
1405/**
1406 * @brief Stop a timer.
1407 *
1408 * This routine stops a running timer prematurely. The timer's stop function,
1409 * if one exists, is invoked by the caller.
1410 *
1411 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001412 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001413 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001414 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1415 * if @a k_timer_stop is to be called from ISRs.
1416 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001417 * @param timer Address of timer.
1418 *
1419 * @return N/A
1420 */
Andrew Boiea354d492017-09-29 16:22:28 -07001421__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001422
1423/**
1424 * @brief Read timer status.
1425 *
1426 * This routine reads the timer's status, which indicates the number of times
1427 * it has expired since its status was last read.
1428 *
1429 * Calling this routine resets the timer's status to zero.
1430 *
1431 * @param timer Address of timer.
1432 *
1433 * @return Timer status.
1434 */
Andrew Boiea354d492017-09-29 16:22:28 -07001435__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001436
1437/**
1438 * @brief Synchronize thread to timer expiration.
1439 *
1440 * This routine blocks the calling thread until the timer's status is non-zero
1441 * (indicating that it has expired at least once since it was last examined)
1442 * or the timer is stopped. If the timer status is already non-zero,
1443 * or the timer is already stopped, the caller continues without waiting.
1444 *
1445 * Calling this routine resets the timer's status to zero.
1446 *
1447 * This routine must not be used by interrupt handlers, since they are not
1448 * allowed to block.
1449 *
1450 * @param timer Address of timer.
1451 *
1452 * @return Timer status.
1453 */
Andrew Boiea354d492017-09-29 16:22:28 -07001454__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001455
1456/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001457 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001458 *
1459 * This routine computes the (approximate) time remaining before a running
1460 * timer next expires. If the timer is not running, it returns zero.
1461 *
1462 * @param timer Address of timer.
1463 *
1464 * @return Remaining time (in milliseconds).
1465 */
Andrew Boiea354d492017-09-29 16:22:28 -07001466__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1467
1468static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001469{
1470 return _timeout_remaining_get(&timer->timeout);
1471}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001472
Allan Stephensc98da842016-11-11 15:45:03 -05001473/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001474 * @brief Associate user-specific data with a timer.
1475 *
1476 * This routine records the @a user_data with the @a timer, to be retrieved
1477 * later.
1478 *
1479 * It can be used e.g. in a timer handler shared across multiple subsystems to
1480 * retrieve data specific to the subsystem this timer is associated with.
1481 *
1482 * @param timer Address of timer.
1483 * @param user_data User data to associate with the timer.
1484 *
1485 * @return N/A
1486 */
Andrew Boiea354d492017-09-29 16:22:28 -07001487__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1488
Anas Nashif954d5502018-02-25 08:37:28 -06001489/**
1490 * @internal
1491 */
Andrew Boiea354d492017-09-29 16:22:28 -07001492static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1493 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001494{
1495 timer->user_data = user_data;
1496}
1497
1498/**
1499 * @brief Retrieve the user-specific data from a timer.
1500 *
1501 * @param timer Address of timer.
1502 *
1503 * @return The user data.
1504 */
Andrew Boiea354d492017-09-29 16:22:28 -07001505__syscall void *k_timer_user_data_get(struct k_timer *timer);
1506
1507static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001508{
1509 return timer->user_data;
1510}
1511
Anas Nashif166f5192018-02-25 08:02:36 -06001512/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001513
Allan Stephensc98da842016-11-11 15:45:03 -05001514/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001515 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001516 * @{
1517 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001518
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001519/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001520 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001521 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001522 * This routine returns the elapsed time since the system booted,
1523 * in milliseconds.
1524 *
1525 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001526 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001527__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001528
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001529/**
1530 * @brief Enable clock always on in tickless kernel
1531 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001532 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001533 * there are no timer events programmed in tickless kernel
1534 * scheduling. This is necessary if the clock is used to track
1535 * passage of time.
1536 *
1537 * @retval prev_status Previous status of always on flag
1538 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301539#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001540static inline int k_enable_sys_clock_always_on(void)
1541{
1542 int prev_status = _sys_clock_always_on;
1543
1544 _sys_clock_always_on = 1;
1545 _enable_sys_clock();
1546
1547 return prev_status;
1548}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301549#else
1550#define k_enable_sys_clock_always_on() do { } while ((0))
1551#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001552
1553/**
1554 * @brief Disable clock always on in tickless kernel
1555 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001556 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001557 * there are no timer events programmed in tickless kernel
1558 * scheduling. To save power, this routine should be called
1559 * immediately when clock is not used to track time.
1560 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301561#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001562static inline void k_disable_sys_clock_always_on(void)
1563{
1564 _sys_clock_always_on = 0;
1565}
1566#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001567#define k_disable_sys_clock_always_on() do { } while ((0))
1568#endif
1569
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001570/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001571 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001572 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001573 * This routine returns the lower 32-bits of the elapsed time since the system
1574 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001575 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001576 * This routine can be more efficient than k_uptime_get(), as it reduces the
1577 * need for interrupt locking and 64-bit math. However, the 32-bit result
1578 * cannot hold a system uptime time larger than approximately 50 days, so the
1579 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001580 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001581 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001582 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001583__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001584
1585/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001586 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001587 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001588 * This routine computes the elapsed time between the current system uptime
1589 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001590 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001591 * @param reftime Pointer to a reference time, which is updated to the current
1592 * uptime upon return.
1593 *
1594 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001595 */
Kumar Galacc334c72017-04-21 10:55:34 -05001596extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001597
1598/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001599 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001600 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001601 * This routine computes the elapsed time between the current system uptime
1602 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001603 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001604 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1605 * need for interrupt locking and 64-bit math. However, the 32-bit result
1606 * cannot hold an elapsed time larger than approximately 50 days, so the
1607 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001608 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001609 * @param reftime Pointer to a reference time, which is updated to the current
1610 * uptime upon return.
1611 *
1612 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001613 */
Kumar Galacc334c72017-04-21 10:55:34 -05001614extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001615
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001616/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001617 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001618 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001619 * This routine returns the current time, as measured by the system's hardware
1620 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001621 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001622 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001623 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001624#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001625
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001626/**
Anas Nashif166f5192018-02-25 08:02:36 -06001627 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001628 */
1629
Allan Stephensc98da842016-11-11 15:45:03 -05001630/**
1631 * @cond INTERNAL_HIDDEN
1632 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001633
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001634struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001635 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001636 union {
1637 _wait_q_t wait_q;
1638
1639 _POLL_EVENT;
1640 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001641
1642 _OBJECT_TRACING_NEXT_PTR(k_queue);
1643};
1644
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001645#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001646 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001647 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Michael Hope5f67a612018-03-17 12:44:40 +01001648 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001649 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001650 _OBJECT_TRACING_INIT \
1651 }
1652
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001653#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1654
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001655/**
1656 * INTERNAL_HIDDEN @endcond
1657 */
1658
1659/**
1660 * @defgroup queue_apis Queue APIs
1661 * @ingroup kernel_apis
1662 * @{
1663 */
1664
1665/**
1666 * @brief Initialize a queue.
1667 *
1668 * This routine initializes a queue object, prior to its first use.
1669 *
1670 * @param queue Address of the queue.
1671 *
1672 * @return N/A
1673 */
1674extern void k_queue_init(struct k_queue *queue);
1675
1676/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001677 * @brief Cancel waiting on a queue.
1678 *
1679 * This routine causes first thread pending on @a queue, if any, to
1680 * return from k_queue_get() call with NULL value (as if timeout expired).
1681 *
1682 * @note Can be called by ISRs.
1683 *
1684 * @param queue Address of the queue.
1685 *
1686 * @return N/A
1687 */
1688extern void k_queue_cancel_wait(struct k_queue *queue);
1689
1690/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001691 * @brief Append an element to the end of a queue.
1692 *
1693 * This routine appends a data item to @a queue. A queue data item must be
1694 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1695 * reserved for the kernel's use.
1696 *
1697 * @note Can be called by ISRs.
1698 *
1699 * @param queue Address of the queue.
1700 * @param data Address of the data item.
1701 *
1702 * @return N/A
1703 */
1704extern void k_queue_append(struct k_queue *queue, void *data);
1705
1706/**
1707 * @brief Prepend an element to a queue.
1708 *
1709 * This routine prepends a data item to @a queue. A queue data item must be
1710 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1711 * reserved for the kernel's use.
1712 *
1713 * @note Can be called by ISRs.
1714 *
1715 * @param queue Address of the queue.
1716 * @param data Address of the data item.
1717 *
1718 * @return N/A
1719 */
1720extern void k_queue_prepend(struct k_queue *queue, void *data);
1721
1722/**
1723 * @brief Inserts an element to a queue.
1724 *
1725 * This routine inserts a data item to @a queue after previous item. A queue
1726 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1727 * item are reserved for the kernel's use.
1728 *
1729 * @note Can be called by ISRs.
1730 *
1731 * @param queue Address of the queue.
1732 * @param prev Address of the previous data item.
1733 * @param data Address of the data item.
1734 *
1735 * @return N/A
1736 */
1737extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1738
1739/**
1740 * @brief Atomically append a list of elements to a queue.
1741 *
1742 * This routine adds a list of data items to @a queue in one operation.
1743 * The data items must be in a singly-linked list, with the first 32 bits
1744 * in each data item pointing to the next data item; the list must be
1745 * NULL-terminated.
1746 *
1747 * @note Can be called by ISRs.
1748 *
1749 * @param queue Address of the queue.
1750 * @param head Pointer to first node in singly-linked list.
1751 * @param tail Pointer to last node in singly-linked list.
1752 *
1753 * @return N/A
1754 */
1755extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1756
1757/**
1758 * @brief Atomically add a list of elements to a queue.
1759 *
1760 * This routine adds a list of data items to @a queue in one operation.
1761 * The data items must be in a singly-linked list implemented using a
1762 * sys_slist_t object. Upon completion, the original list is empty.
1763 *
1764 * @note Can be called by ISRs.
1765 *
1766 * @param queue Address of the queue.
1767 * @param list Pointer to sys_slist_t object.
1768 *
1769 * @return N/A
1770 */
1771extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1772
1773/**
1774 * @brief Get an element from a queue.
1775 *
1776 * This routine removes first data item from @a queue. The first 32 bits of the
1777 * data item are reserved for the kernel's use.
1778 *
1779 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1780 *
1781 * @param queue Address of the queue.
1782 * @param timeout Waiting period to obtain a data item (in milliseconds),
1783 * or one of the special values K_NO_WAIT and K_FOREVER.
1784 *
1785 * @return Address of the data item if successful; NULL if returned
1786 * without waiting, or waiting period timed out.
1787 */
Kumar Galacc334c72017-04-21 10:55:34 -05001788extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001789
1790/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001791 * @brief Remove an element from a queue.
1792 *
1793 * This routine removes data item from @a queue. The first 32 bits of the
1794 * data item are reserved for the kernel's use. Removing elements from k_queue
1795 * rely on sys_slist_find_and_remove which is not a constant time operation.
1796 *
1797 * @note Can be called by ISRs
1798 *
1799 * @param queue Address of the queue.
1800 * @param data Address of the data item.
1801 *
1802 * @return true if data item was removed
1803 */
1804static inline bool k_queue_remove(struct k_queue *queue, void *data)
1805{
1806 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1807}
1808
1809/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001810 * @brief Query a queue to see if it has data available.
1811 *
1812 * Note that the data might be already gone by the time this function returns
1813 * if other threads are also trying to read from the queue.
1814 *
1815 * @note Can be called by ISRs.
1816 *
1817 * @param queue Address of the queue.
1818 *
1819 * @return Non-zero if the queue is empty.
1820 * @return 0 if data is available.
1821 */
1822static inline int k_queue_is_empty(struct k_queue *queue)
1823{
1824 return (int)sys_slist_is_empty(&queue->data_q);
1825}
1826
1827/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001828 * @brief Peek element at the head of queue.
1829 *
1830 * Return element from the head of queue without removing it.
1831 *
1832 * @param queue Address of the queue.
1833 *
1834 * @return Head element, or NULL if queue is empty.
1835 */
1836static inline void *k_queue_peek_head(struct k_queue *queue)
1837{
1838 return sys_slist_peek_head(&queue->data_q);
1839}
1840
1841/**
1842 * @brief Peek element at the tail of queue.
1843 *
1844 * Return element from the tail of queue without removing it.
1845 *
1846 * @param queue Address of the queue.
1847 *
1848 * @return Tail element, or NULL if queue is empty.
1849 */
1850static inline void *k_queue_peek_tail(struct k_queue *queue)
1851{
1852 return sys_slist_peek_tail(&queue->data_q);
1853}
1854
1855/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001856 * @brief Statically define and initialize a queue.
1857 *
1858 * The queue can be accessed outside the module where it is defined using:
1859 *
1860 * @code extern struct k_queue <name>; @endcode
1861 *
1862 * @param name Name of the queue.
1863 */
1864#define K_QUEUE_DEFINE(name) \
1865 struct k_queue name \
1866 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001867 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001868
Anas Nashif166f5192018-02-25 08:02:36 -06001869/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001870
1871/**
1872 * @cond INTERNAL_HIDDEN
1873 */
1874
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001875struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001876 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001877};
1878
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001879#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001880 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001881 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001882 }
1883
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001884#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1885
Allan Stephensc98da842016-11-11 15:45:03 -05001886/**
1887 * INTERNAL_HIDDEN @endcond
1888 */
1889
1890/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001891 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05001892 * @ingroup kernel_apis
1893 * @{
1894 */
1895
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001896/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001897 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001898 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001899 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001900 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001901 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001902 *
1903 * @return N/A
1904 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001905#define k_fifo_init(fifo) \
1906 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001907
1908/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001909 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001910 *
1911 * This routine causes first thread pending on @a fifo, if any, to
1912 * return from k_fifo_get() call with NULL value (as if timeout
1913 * expired).
1914 *
1915 * @note Can be called by ISRs.
1916 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001917 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001918 *
1919 * @return N/A
1920 */
1921#define k_fifo_cancel_wait(fifo) \
1922 k_queue_cancel_wait((struct k_queue *) fifo)
1923
1924/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001925 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001926 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001927 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001928 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1929 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001930 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001931 * @note Can be called by ISRs.
1932 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001933 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001934 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001935 *
1936 * @return N/A
1937 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001938#define k_fifo_put(fifo, data) \
1939 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001940
1941/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001942 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001943 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001944 * This routine adds a list of data items to @a fifo in one operation.
1945 * The data items must be in a singly-linked list, with the first 32 bits
1946 * each data item pointing to the next data item; the list must be
1947 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001948 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001949 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001950 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001951 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001952 * @param head Pointer to first node in singly-linked list.
1953 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001954 *
1955 * @return N/A
1956 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001957#define k_fifo_put_list(fifo, head, tail) \
1958 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001959
1960/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001961 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001962 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001963 * This routine adds a list of data items to @a fifo in one operation.
1964 * The data items must be in a singly-linked list implemented using a
1965 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001966 * and must be re-initialized via sys_slist_init().
1967 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001968 * @note Can be called by ISRs.
1969 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001970 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001971 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001972 *
1973 * @return N/A
1974 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001975#define k_fifo_put_slist(fifo, list) \
1976 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001977
1978/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001979 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001980 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001981 * This routine removes a data item from @a fifo in a "first in, first out"
1982 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001983 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001984 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1985 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001986 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001987 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001988 * or one of the special values K_NO_WAIT and K_FOREVER.
1989 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001990 * @return Address of the data item if successful; NULL if returned
1991 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001992 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001993#define k_fifo_get(fifo, timeout) \
1994 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001995
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001996/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001997 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001998 *
1999 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002000 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002001 *
2002 * @note Can be called by ISRs.
2003 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002004 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002005 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002006 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002007 * @return 0 if data is available.
2008 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002009#define k_fifo_is_empty(fifo) \
2010 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002011
2012/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002013 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002014 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002015 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302016 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002017 * on each iteration of processing, a head container will be peeked,
2018 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002019 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002020 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002021 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002022 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002023 * @return Head element, or NULL if the FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002024 */
2025#define k_fifo_peek_head(fifo) \
2026 k_queue_peek_head((struct k_queue *) fifo)
2027
2028/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002029 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002030 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002031 * Return element from the tail of FIFO queue (without removing it). A usecase
2032 * for this is if elements of the FIFO queue are themselves containers. Then
2033 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002034 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002035 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002036 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002037 * @return Tail element, or NULL if a FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002038 */
2039#define k_fifo_peek_tail(fifo) \
2040 k_queue_peek_tail((struct k_queue *) fifo)
2041
2042/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002043 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002044 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002045 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002046 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002047 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002048 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002049 * @param name Name of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002050 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002051#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002052 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002053 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002054 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002055
Anas Nashif166f5192018-02-25 08:02:36 -06002056/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002057
2058/**
2059 * @cond INTERNAL_HIDDEN
2060 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002061
2062struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002063 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002064};
2065
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002066#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002067 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002068 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002069 }
2070
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002071#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2072
Allan Stephensc98da842016-11-11 15:45:03 -05002073/**
2074 * INTERNAL_HIDDEN @endcond
2075 */
2076
2077/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002078 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002079 * @ingroup kernel_apis
2080 * @{
2081 */
2082
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002083/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002084 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002085 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002086 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002087 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002088 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002089 *
2090 * @return N/A
2091 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002092#define k_lifo_init(lifo) \
2093 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002094
2095/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002096 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002097 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002098 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002099 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2100 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002101 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002102 * @note Can be called by ISRs.
2103 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002104 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002105 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002106 *
2107 * @return N/A
2108 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002109#define k_lifo_put(lifo, data) \
2110 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002111
2112/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002113 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002114 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002115 * This routine removes a data item from @a lifo in a "last in, first out"
2116 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002117 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002118 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2119 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002120 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002121 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002122 * or one of the special values K_NO_WAIT and K_FOREVER.
2123 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002124 * @return Address of the data item if successful; NULL if returned
2125 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002126 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002127#define k_lifo_get(lifo, timeout) \
2128 k_queue_get((struct k_queue *) lifo, 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 Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002132 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002133 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002134 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002135 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002136 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002137 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002138 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002139#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002140 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002141 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002142 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002143
Anas Nashif166f5192018-02-25 08:02:36 -06002144/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002145
2146/**
2147 * @cond INTERNAL_HIDDEN
2148 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002149
2150struct k_stack {
2151 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002152 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002153
Anas Nashif2f203c22016-12-18 06:57:45 -05002154 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002155};
2156
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002157#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002158 { \
2159 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2160 .base = stack_buffer, \
2161 .next = stack_buffer, \
2162 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002163 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002164 }
2165
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002166#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2167
Allan Stephensc98da842016-11-11 15:45:03 -05002168/**
2169 * INTERNAL_HIDDEN @endcond
2170 */
2171
2172/**
2173 * @defgroup stack_apis Stack APIs
2174 * @ingroup kernel_apis
2175 * @{
2176 */
2177
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002178/**
2179 * @brief Initialize a stack.
2180 *
2181 * This routine initializes a stack object, prior to its first use.
2182 *
2183 * @param stack Address of the stack.
2184 * @param buffer Address of array used to hold stacked values.
2185 * @param num_entries Maximum number of values that can be stacked.
2186 *
2187 * @return N/A
2188 */
Andrew Boiee8734462017-09-29 16:42:07 -07002189__syscall void k_stack_init(struct k_stack *stack,
Andrew Boie8e3e6d02017-10-12 14:01:25 -07002190 u32_t *buffer, unsigned int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002191
2192/**
2193 * @brief Push an element onto a stack.
2194 *
2195 * This routine adds a 32-bit value @a data to @a stack.
2196 *
2197 * @note Can be called by ISRs.
2198 *
2199 * @param stack Address of the stack.
2200 * @param data Value to push onto the stack.
2201 *
2202 * @return N/A
2203 */
Andrew Boiee8734462017-09-29 16:42:07 -07002204__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002205
2206/**
2207 * @brief Pop an element from a stack.
2208 *
2209 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2210 * manner and stores the value in @a data.
2211 *
2212 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2213 *
2214 * @param stack Address of the stack.
2215 * @param data Address of area to hold the value popped from the stack.
2216 * @param timeout Waiting period to obtain a value (in milliseconds),
2217 * or one of the special values K_NO_WAIT and K_FOREVER.
2218 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002219 * @retval 0 Element popped from stack.
2220 * @retval -EBUSY Returned without waiting.
2221 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002222 */
Andrew Boiee8734462017-09-29 16:42:07 -07002223__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002224
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002225/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002226 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002227 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002228 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002229 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002230 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002231 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002232 * @param name Name of the stack.
2233 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002234 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002235#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002236 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002237 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002238 struct k_stack name \
2239 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002240 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002241 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002242
Anas Nashif166f5192018-02-25 08:02:36 -06002243/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002244
Allan Stephens6bba9b02016-11-16 14:56:54 -05002245struct k_work;
2246
Allan Stephensc98da842016-11-11 15:45:03 -05002247/**
2248 * @defgroup workqueue_apis Workqueue Thread APIs
2249 * @ingroup kernel_apis
2250 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002251 */
2252
Allan Stephens6bba9b02016-11-16 14:56:54 -05002253/**
2254 * @typedef k_work_handler_t
2255 * @brief Work item handler function type.
2256 *
2257 * A work item's handler function is executed by a workqueue's thread
2258 * when the work item is processed by the workqueue.
2259 *
2260 * @param work Address of the work item.
2261 *
2262 * @return N/A
2263 */
2264typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002265
2266/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002267 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002268 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002269
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002270struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002271 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002272 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002273};
2274
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002275enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002276 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002277};
2278
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002279struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002280 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002281 k_work_handler_t handler;
2282 atomic_t flags[1];
2283};
2284
Allan Stephens6bba9b02016-11-16 14:56:54 -05002285struct k_delayed_work {
2286 struct k_work work;
2287 struct _timeout timeout;
2288 struct k_work_q *work_q;
2289};
2290
2291extern struct k_work_q k_sys_work_q;
2292
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002293/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002294 * INTERNAL_HIDDEN @endcond
2295 */
2296
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002297#define _K_WORK_INITIALIZER(work_handler) \
2298 { \
2299 ._reserved = NULL, \
2300 .handler = work_handler, \
2301 .flags = { 0 } \
2302 }
2303
2304#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2305
Allan Stephens6bba9b02016-11-16 14:56:54 -05002306/**
2307 * @brief Initialize a statically-defined work item.
2308 *
2309 * This macro can be used to initialize a statically-defined workqueue work
2310 * item, prior to its first use. For example,
2311 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002312 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002313 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002314 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002315 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002316 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002317#define K_WORK_DEFINE(work, work_handler) \
2318 struct k_work work \
2319 __in_section(_k_work, static, work) = \
2320 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002321
2322/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002323 * @brief Initialize a work item.
2324 *
2325 * This routine initializes a workqueue work item, prior to its first use.
2326 *
2327 * @param work Address of work item.
2328 * @param handler Function to invoke each time work item is processed.
2329 *
2330 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002331 */
2332static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2333{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002334 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002335 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002336 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002337}
2338
2339/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002340 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002341 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002342 * This routine submits work item @a work to be processed by workqueue
2343 * @a work_q. If the work item is already pending in the workqueue's queue
2344 * as a result of an earlier submission, this routine has no effect on the
2345 * work item. If the work item has already been processed, or is currently
2346 * being processed, its work is considered complete and the work item can be
2347 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002348 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002349 * @warning
2350 * A submitted work item must not be modified until it has been processed
2351 * by the workqueue.
2352 *
2353 * @note Can be called by ISRs.
2354 *
2355 * @param work_q Address of workqueue.
2356 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002357 *
2358 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002359 */
2360static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2361 struct k_work *work)
2362{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002363 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002364 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002365 }
2366}
2367
2368/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002369 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002370 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002371 * This routine indicates if work item @a work is pending in a workqueue's
2372 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002373 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002374 * @note Can be called by ISRs.
2375 *
2376 * @param work Address of work item.
2377 *
2378 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002379 */
2380static inline int k_work_pending(struct k_work *work)
2381{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002382 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002383}
2384
2385/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002386 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002387 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002388 * This routine starts workqueue @a work_q. The workqueue spawns its work
2389 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002390 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002391 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002392 * @param stack Pointer to work queue thread's stack space, as defined by
2393 * K_THREAD_STACK_DEFINE()
2394 * @param stack_size Size of the work queue thread's stack (in bytes), which
2395 * should either be the same constant passed to
2396 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002397 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002398 *
2399 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002400 */
Andrew Boie507852a2017-07-25 18:47:07 -07002401extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002402 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002403 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002404
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002405/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002406 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002407 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002408 * This routine initializes a workqueue delayed work item, prior to
2409 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002410 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002411 * @param work Address of delayed work item.
2412 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002413 *
2414 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002415 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002416extern void k_delayed_work_init(struct k_delayed_work *work,
2417 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002418
2419/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002420 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002421 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002422 * This routine schedules work item @a work to be processed by workqueue
2423 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002424 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002425 * Only when the countdown completes is the work item actually submitted to
2426 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002427 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002428 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002429 * counting down cancels the existing submission and restarts the
2430 * countdown using the new delay. Note that this behavior is
2431 * inherently subject to race conditions with the pre-existing
2432 * timeouts and work queue, so care must be taken to synchronize such
2433 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002434 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002435 * @warning
2436 * A delayed work item must not be modified until it has been processed
2437 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002438 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002439 * @note Can be called by ISRs.
2440 *
2441 * @param work_q Address of workqueue.
2442 * @param work Address of delayed work item.
2443 * @param delay Delay before submitting the work item (in milliseconds).
2444 *
2445 * @retval 0 Work item countdown started.
2446 * @retval -EINPROGRESS Work item is already pending.
2447 * @retval -EINVAL Work item is being processed or has completed its work.
2448 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002449 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002450extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2451 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002452 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002453
2454/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002455 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002456 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002457 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002458 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002459 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002460 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002461 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002462 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002463 * @param work Address of delayed work item.
2464 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002465 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002466 * @retval -EINPROGRESS Work item is already pending.
2467 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002468 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002469extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002470
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002471/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002472 * @brief Submit a work item to the system workqueue.
2473 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002474 * This routine submits work item @a work to be processed by the system
2475 * workqueue. If the work item is already pending in the workqueue's queue
2476 * as a result of an earlier submission, this routine has no effect on the
2477 * work item. If the work item has already been processed, or is currently
2478 * being processed, its work is considered complete and the work item can be
2479 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002480 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002481 * @warning
2482 * Work items submitted to the system workqueue should avoid using handlers
2483 * that block or yield since this may prevent the system workqueue from
2484 * processing other work items in a timely manner.
2485 *
2486 * @note Can be called by ISRs.
2487 *
2488 * @param work Address of work item.
2489 *
2490 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002491 */
2492static inline void k_work_submit(struct k_work *work)
2493{
2494 k_work_submit_to_queue(&k_sys_work_q, work);
2495}
2496
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002497/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002498 * @brief Submit a delayed work item to the system workqueue.
2499 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002500 * This routine schedules work item @a work to be processed by the system
2501 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002502 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002503 * Only when the countdown completes is the work item actually submitted to
2504 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002505 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002506 * Submitting a previously submitted delayed work item that is still
2507 * counting down cancels the existing submission and restarts the countdown
2508 * using the new delay. If the work item is currently pending on the
2509 * workqueue's queue because the countdown has completed it is too late to
2510 * resubmit the item, and resubmission fails without impacting the work item.
2511 * If the work item has already been processed, or is currently being processed,
2512 * its work is considered complete and the work item can be resubmitted.
2513 *
2514 * @warning
2515 * Work items submitted to the system workqueue should avoid using handlers
2516 * that block or yield since this may prevent the system workqueue from
2517 * processing other work items in a timely manner.
2518 *
2519 * @note Can be called by ISRs.
2520 *
2521 * @param work Address of delayed work item.
2522 * @param delay Delay before submitting the work item (in milliseconds).
2523 *
2524 * @retval 0 Work item countdown started.
2525 * @retval -EINPROGRESS Work item is already pending.
2526 * @retval -EINVAL Work item is being processed or has completed its work.
2527 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002528 */
2529static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002530 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002531{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002532 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002533}
2534
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002535/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002536 * @brief Get time remaining before a delayed work gets scheduled.
2537 *
2538 * This routine computes the (approximate) time remaining before a
2539 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002540 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002541 *
2542 * @param work Delayed work item.
2543 *
2544 * @return Remaining time (in milliseconds).
2545 */
Kumar Galacc334c72017-04-21 10:55:34 -05002546static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002547{
2548 return _timeout_remaining_get(&work->timeout);
2549}
2550
Anas Nashif166f5192018-02-25 08:02:36 -06002551/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002552
Allan Stephensc98da842016-11-11 15:45:03 -05002553/**
2554 * @cond INTERNAL_HIDDEN
2555 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002556
2557struct k_mutex {
2558 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002559 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002560 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002561 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002562
Anas Nashif2f203c22016-12-18 06:57:45 -05002563 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002564};
2565
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002566#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002567 { \
2568 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2569 .owner = NULL, \
2570 .lock_count = 0, \
2571 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002572 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002573 }
2574
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002575#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2576
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002577/**
Allan Stephensc98da842016-11-11 15:45:03 -05002578 * INTERNAL_HIDDEN @endcond
2579 */
2580
2581/**
2582 * @defgroup mutex_apis Mutex APIs
2583 * @ingroup kernel_apis
2584 * @{
2585 */
2586
2587/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002588 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002589 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002590 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002591 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002592 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002593 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002594 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002595 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002596#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002597 struct k_mutex name \
2598 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002599 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002600
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002601/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002602 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002603 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002604 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002605 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002606 * Upon completion, the mutex is available and does not have an owner.
2607 *
2608 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002609 *
2610 * @return N/A
2611 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002612__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002613
2614/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002615 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002616 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002617 * This routine locks @a mutex. If the mutex is locked by another thread,
2618 * the calling thread waits until the mutex becomes available or until
2619 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002620 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002621 * A thread is permitted to lock a mutex it has already locked. The operation
2622 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002623 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002624 * @param mutex Address of the mutex.
2625 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002626 * or one of the special values K_NO_WAIT and K_FOREVER.
2627 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002628 * @retval 0 Mutex locked.
2629 * @retval -EBUSY Returned without waiting.
2630 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002631 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002632__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002633
2634/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002635 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002636 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002637 * This routine unlocks @a mutex. The mutex must already be locked by the
2638 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002639 *
2640 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002641 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002642 * thread.
2643 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002644 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002645 *
2646 * @return N/A
2647 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002648__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002649
Allan Stephensc98da842016-11-11 15:45:03 -05002650/**
Anas Nashif166f5192018-02-25 08:02:36 -06002651 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002652 */
2653
2654/**
2655 * @cond INTERNAL_HIDDEN
2656 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002657
2658struct k_sem {
2659 _wait_q_t wait_q;
2660 unsigned int count;
2661 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002662 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002663
Anas Nashif2f203c22016-12-18 06:57:45 -05002664 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002665};
2666
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002667#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002668 { \
2669 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2670 .count = initial_count, \
2671 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002672 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002673 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002674 }
2675
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002676#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2677
Allan Stephensc98da842016-11-11 15:45:03 -05002678/**
2679 * INTERNAL_HIDDEN @endcond
2680 */
2681
2682/**
2683 * @defgroup semaphore_apis Semaphore APIs
2684 * @ingroup kernel_apis
2685 * @{
2686 */
2687
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002688/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002689 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002690 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002691 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002692 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002693 * @param sem Address of the semaphore.
2694 * @param initial_count Initial semaphore count.
2695 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002696 *
2697 * @return N/A
2698 */
Andrew Boie99280232017-09-29 14:17:47 -07002699__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2700 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002701
2702/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002703 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002704 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002705 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002706 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002707 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2708 *
2709 * @param sem Address of the semaphore.
2710 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002711 * or one of the special values K_NO_WAIT and K_FOREVER.
2712 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002713 * @note When porting code from the nanokernel legacy API to the new API, be
2714 * careful with the return value of this function. The return value is the
2715 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2716 * non-zero means failure, while the nano_sem_take family returns 1 for success
2717 * and 0 for failure.
2718 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002719 * @retval 0 Semaphore taken.
2720 * @retval -EBUSY Returned without waiting.
2721 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002722 */
Andrew Boie99280232017-09-29 14:17:47 -07002723__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002724
2725/**
2726 * @brief Give a semaphore.
2727 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002728 * This routine gives @a sem, unless the semaphore is already at its maximum
2729 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002730 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002731 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002732 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002733 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002734 *
2735 * @return N/A
2736 */
Andrew Boie99280232017-09-29 14:17:47 -07002737__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002738
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002739/**
2740 * @brief Reset a semaphore's count to zero.
2741 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002742 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002743 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002744 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002745 *
2746 * @return N/A
2747 */
Andrew Boie990bf162017-10-03 12:36:49 -07002748__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002749
Anas Nashif954d5502018-02-25 08:37:28 -06002750/**
2751 * @internal
2752 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002753static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002754{
2755 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002756}
2757
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002758/**
2759 * @brief Get a semaphore's count.
2760 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002761 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002762 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002763 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002764 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002765 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002766 */
Andrew Boie990bf162017-10-03 12:36:49 -07002767__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002768
Anas Nashif954d5502018-02-25 08:37:28 -06002769/**
2770 * @internal
2771 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002772static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002773{
2774 return sem->count;
2775}
2776
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002777/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002778 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002779 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002780 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002781 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002782 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002783 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002784 * @param name Name of the semaphore.
2785 * @param initial_count Initial semaphore count.
2786 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002787 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002788#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002789 struct k_sem name \
2790 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07002791 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05302792 BUILD_ASSERT(((count_limit) != 0) && \
2793 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002794
Anas Nashif166f5192018-02-25 08:02:36 -06002795/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002796
2797/**
2798 * @defgroup alert_apis Alert APIs
2799 * @ingroup kernel_apis
2800 * @{
2801 */
2802
Allan Stephens5eceb852016-11-16 10:16:30 -05002803/**
2804 * @typedef k_alert_handler_t
2805 * @brief Alert handler function type.
2806 *
2807 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002808 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002809 * and is only invoked if the alert has been initialized with one.
2810 *
2811 * @param alert Address of the alert.
2812 *
2813 * @return 0 if alert has been consumed; non-zero if alert should pend.
2814 */
2815typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002816
Anas Nashif166f5192018-02-25 08:02:36 -06002817/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002818
2819/**
2820 * @cond INTERNAL_HIDDEN
2821 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002822
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002823#define K_ALERT_DEFAULT NULL
2824#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002825
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002826struct k_alert {
2827 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002828 atomic_t send_count;
2829 struct k_work work_item;
2830 struct k_sem sem;
2831
Anas Nashif2f203c22016-12-18 06:57:45 -05002832 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002833};
2834
Anas Nashif954d5502018-02-25 08:37:28 -06002835/**
2836 * @internal
2837 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002838extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002839
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002840#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002841 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002842 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002843 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002844 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2845 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002846 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002847 }
2848
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002849#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2850
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002851/**
Allan Stephensc98da842016-11-11 15:45:03 -05002852 * INTERNAL_HIDDEN @endcond
2853 */
2854
2855/**
2856 * @addtogroup alert_apis
2857 * @{
2858 */
2859
2860/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002861 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002862 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002863 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002864 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002865 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002867 * @param name Name of the alert.
2868 * @param alert_handler Action to take when alert is sent. Specify either
2869 * the address of a function to be invoked by the system workqueue
2870 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2871 * K_ALERT_DEFAULT (which causes the alert to pend).
2872 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002873 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002874#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002875 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002876 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002877 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002878 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002879
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002880/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002881 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002883 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002884 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002885 * @param alert Address of the alert.
2886 * @param handler Action to take when alert is sent. Specify either the address
2887 * of a function to be invoked by the system workqueue thread,
2888 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2889 * K_ALERT_DEFAULT (which causes the alert to pend).
2890 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002891 *
2892 * @return N/A
2893 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002894extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2895 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002896
2897/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002898 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002899 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002900 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002901 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002902 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2903 *
2904 * @param alert Address of the alert.
2905 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002906 * or one of the special values K_NO_WAIT and K_FOREVER.
2907 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002908 * @retval 0 Alert received.
2909 * @retval -EBUSY Returned without waiting.
2910 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002911 */
Andrew Boie310e9872017-09-29 04:41:15 -07002912__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002913
2914/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002915 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002917 * This routine signals @a alert. The action specified for @a alert will
2918 * be taken, which may trigger the execution of an alert handler function
2919 * and/or cause the alert to pend (assuming the alert has not reached its
2920 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002921 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002922 * @note Can be called by ISRs.
2923 *
2924 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002925 *
2926 * @return N/A
2927 */
Andrew Boie310e9872017-09-29 04:41:15 -07002928__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002929
2930/**
Anas Nashif166f5192018-02-25 08:02:36 -06002931 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002932 */
2933
Allan Stephensc98da842016-11-11 15:45:03 -05002934/**
2935 * @cond INTERNAL_HIDDEN
2936 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002937
2938struct k_msgq {
2939 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002940 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002941 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002942 char *buffer_start;
2943 char *buffer_end;
2944 char *read_ptr;
2945 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002946 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002947
Anas Nashif2f203c22016-12-18 06:57:45 -05002948 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Andrew Boie0fe789f2018-04-12 18:35:56 -07002949 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002950};
2951
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002952#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002953 { \
2954 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002955 .max_msgs = q_max_msgs, \
2956 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002957 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002958 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002959 .read_ptr = q_buffer, \
2960 .write_ptr = q_buffer, \
2961 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002962 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002963 }
2964
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002965#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2966
Andrew Boie0fe789f2018-04-12 18:35:56 -07002967#define K_MSGQ_FLAG_ALLOC BIT(0)
2968
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05302969struct k_msgq_attrs {
2970 size_t msg_size;
2971 u32_t max_msgs;
2972 u32_t used_msgs;
2973};
2974
Peter Mitsis1da807e2016-10-06 11:36:59 -04002975/**
Allan Stephensc98da842016-11-11 15:45:03 -05002976 * INTERNAL_HIDDEN @endcond
2977 */
2978
2979/**
2980 * @defgroup msgq_apis Message Queue APIs
2981 * @ingroup kernel_apis
2982 * @{
2983 */
2984
2985/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002986 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002987 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002988 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2989 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002990 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2991 * message is similarly aligned to this boundary, @a q_msg_size must also be
2992 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002994 * The message queue can be accessed outside the module where it is defined
2995 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002996 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002997 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002999 * @param q_name Name of the message queue.
3000 * @param q_msg_size Message size (in bytes).
3001 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003002 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003003 */
3004#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003005 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003006 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003007 struct k_msgq q_name \
3008 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003009 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003010 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003011
Peter Mitsisd7a37502016-10-13 11:37:40 -04003012/**
3013 * @brief Initialize a message queue.
3014 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003015 * This routine initializes a message queue object, prior to its first use.
3016 *
Allan Stephensda827222016-11-09 14:23:58 -06003017 * The message queue's ring buffer must contain space for @a max_msgs messages,
3018 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3019 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3020 * that each message is similarly aligned to this boundary, @a q_msg_size
3021 * must also be a multiple of N.
3022 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003023 * @param q Address of the message queue.
3024 * @param buffer Pointer to ring buffer that holds queued messages.
3025 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003026 * @param max_msgs Maximum number of messages that can be queued.
3027 *
3028 * @return N/A
3029 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003030void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3031 u32_t max_msgs);
3032
3033/**
3034 * @brief Initialize a message queue.
3035 *
3036 * This routine initializes a message queue object, prior to its first use,
3037 * allocating its internal ring buffer from the calling thread's resource
3038 * pool.
3039 *
3040 * Memory allocated for the ring buffer can be released by calling
3041 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3042 * all of its references.
3043 *
3044 * @param q Address of the message queue.
3045 * @param msg_size Message size (in bytes).
3046 * @param max_msgs Maximum number of messages that can be queued.
3047 *
3048 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3049 * thread's resource pool, or -EINVAL if the size parameters cause
3050 * an integer overflow.
3051 */
3052__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3053 u32_t max_msgs);
3054
3055
3056void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003057
3058/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003059 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003060 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003061 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003062 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003063 * @note Can be called by ISRs.
3064 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003065 * @param q Address of the message queue.
3066 * @param data Pointer to the message.
3067 * @param timeout Waiting period to add the message (in milliseconds),
3068 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003069 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003070 * @retval 0 Message sent.
3071 * @retval -ENOMSG Returned without waiting or queue purged.
3072 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003073 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003074__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003075
3076/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003077 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003078 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003079 * This routine receives a message from message queue @a q in a "first in,
3080 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003081 *
Allan Stephensc98da842016-11-11 15:45:03 -05003082 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003083 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003084 * @param q Address of the message queue.
3085 * @param data Address of area to hold the received message.
3086 * @param timeout Waiting period to receive the message (in milliseconds),
3087 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003088 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003089 * @retval 0 Message received.
3090 * @retval -ENOMSG Returned without waiting.
3091 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003092 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003093__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003094
3095/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003096 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003097 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003098 * This routine discards all unreceived messages in a message queue's ring
3099 * buffer. Any threads that are blocked waiting to send a message to the
3100 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003101 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003102 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003103 *
3104 * @return N/A
3105 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003106__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003107
Peter Mitsis67be2492016-10-07 11:44:34 -04003108/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003109 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003110 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003111 * This routine returns the number of unused entries in a message queue's
3112 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003113 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003114 * @param q Address of the message queue.
3115 *
3116 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04003117 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003118__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3119
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303120/**
3121 * @brief Get basic attributes of a message queue.
3122 *
3123 * This routine fetches basic attributes of message queue into attr argument.
3124 *
3125 * @param q Address of the message queue.
3126 * @param attrs pointer to message queue attribute structure.
3127 *
3128 * @return N/A
3129 */
3130__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3131
3132
Andrew Boie82edb6e2017-10-02 10:53:06 -07003133static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003134{
3135 return q->max_msgs - q->used_msgs;
3136}
3137
Peter Mitsisd7a37502016-10-13 11:37:40 -04003138/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003139 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003140 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003141 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003142 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003143 * @param q Address of the message queue.
3144 *
3145 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003146 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003147__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3148
3149static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003150{
3151 return q->used_msgs;
3152}
3153
Anas Nashif166f5192018-02-25 08:02:36 -06003154/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003155
3156/**
3157 * @defgroup mem_pool_apis Memory Pool APIs
3158 * @ingroup kernel_apis
3159 * @{
3160 */
3161
Andy Ross73cb9582017-05-09 10:42:39 -07003162/* Note on sizing: the use of a 20 bit field for block means that,
3163 * assuming a reasonable minimum block size of 16 bytes, we're limited
3164 * to 16M of memory managed by a single pool. Long term it would be
3165 * good to move to a variable bit size based on configuration.
3166 */
3167struct k_mem_block_id {
3168 u32_t pool : 8;
3169 u32_t level : 4;
3170 u32_t block : 20;
3171};
3172
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003173struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003174 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003175 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003176};
3177
Anas Nashif166f5192018-02-25 08:02:36 -06003178/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003179
3180/**
3181 * @defgroup mailbox_apis Mailbox APIs
3182 * @ingroup kernel_apis
3183 * @{
3184 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003185
3186struct k_mbox_msg {
3187 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003188 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003189 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003190 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003191 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003192 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003193 /** sender's message data buffer */
3194 void *tx_data;
3195 /** internal use only - needed for legacy API support */
3196 void *_rx_data;
3197 /** message data block descriptor */
3198 struct k_mem_block tx_block;
3199 /** source thread id */
3200 k_tid_t rx_source_thread;
3201 /** target thread id */
3202 k_tid_t tx_target_thread;
3203 /** internal use only - thread waiting on send (may be a dummy) */
3204 k_tid_t _syncing_thread;
3205#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3206 /** internal use only - semaphore used during asynchronous send */
3207 struct k_sem *_async_sem;
3208#endif
3209};
3210
Allan Stephensc98da842016-11-11 15:45:03 -05003211/**
3212 * @cond INTERNAL_HIDDEN
3213 */
3214
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003215struct k_mbox {
3216 _wait_q_t tx_msg_queue;
3217 _wait_q_t rx_msg_queue;
3218
Anas Nashif2f203c22016-12-18 06:57:45 -05003219 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003220};
3221
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003222#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003223 { \
3224 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3225 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003226 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003227 }
3228
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003229#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3230
Peter Mitsis12092702016-10-14 12:57:23 -04003231/**
Allan Stephensc98da842016-11-11 15:45:03 -05003232 * INTERNAL_HIDDEN @endcond
3233 */
3234
3235/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003236 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003237 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003238 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003239 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003240 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003241 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003242 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003243 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003244#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003245 struct k_mbox name \
3246 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003247 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003248
Peter Mitsis12092702016-10-14 12:57:23 -04003249/**
3250 * @brief Initialize a mailbox.
3251 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003252 * This routine initializes a mailbox object, prior to its first use.
3253 *
3254 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003255 *
3256 * @return N/A
3257 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003258extern void k_mbox_init(struct k_mbox *mbox);
3259
Peter Mitsis12092702016-10-14 12:57:23 -04003260/**
3261 * @brief Send a mailbox message in a synchronous manner.
3262 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003263 * This routine sends a message to @a mbox and waits for a receiver to both
3264 * receive and process it. The message data may be in a buffer, in a memory
3265 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003266 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003267 * @param mbox Address of the mailbox.
3268 * @param tx_msg Address of the transmit message descriptor.
3269 * @param timeout Waiting period for the message to be received (in
3270 * milliseconds), or one of the special values K_NO_WAIT
3271 * and K_FOREVER. Once the message has been received,
3272 * this routine waits as long as necessary for the message
3273 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003274 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003275 * @retval 0 Message sent.
3276 * @retval -ENOMSG Returned without waiting.
3277 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003278 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003279extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003280 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003281
Peter Mitsis12092702016-10-14 12:57:23 -04003282/**
3283 * @brief Send a mailbox message in an asynchronous manner.
3284 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003285 * This routine sends a message to @a mbox without waiting for a receiver
3286 * to process it. The message data may be in a buffer, in a memory pool block,
3287 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3288 * will be given when the message has been both received and completely
3289 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003290 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003291 * @param mbox Address of the mailbox.
3292 * @param tx_msg Address of the transmit message descriptor.
3293 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003294 *
3295 * @return N/A
3296 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003297extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003298 struct k_sem *sem);
3299
Peter Mitsis12092702016-10-14 12:57:23 -04003300/**
3301 * @brief Receive a mailbox message.
3302 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003303 * This routine receives a message from @a mbox, then optionally retrieves
3304 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003305 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003306 * @param mbox Address of the mailbox.
3307 * @param rx_msg Address of the receive message descriptor.
3308 * @param buffer Address of the buffer to receive data, or NULL to defer data
3309 * retrieval and message disposal until later.
3310 * @param timeout Waiting period for a message to be received (in
3311 * milliseconds), or one of the special values K_NO_WAIT
3312 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003313 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003314 * @retval 0 Message received.
3315 * @retval -ENOMSG Returned without waiting.
3316 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003317 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003318extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003319 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003320
3321/**
3322 * @brief Retrieve mailbox message data into a buffer.
3323 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003324 * This routine completes the processing of a received message by retrieving
3325 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003326 *
3327 * Alternatively, this routine can be used to dispose of a received message
3328 * without retrieving its data.
3329 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003330 * @param rx_msg Address of the receive message descriptor.
3331 * @param buffer Address of the buffer to receive data, or NULL to discard
3332 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003333 *
3334 * @return N/A
3335 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003336extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003337
3338/**
3339 * @brief Retrieve mailbox message data into a memory pool block.
3340 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003341 * This routine completes the processing of a received message by retrieving
3342 * its data into a memory pool block, then disposing of the message.
3343 * The memory pool block that results from successful retrieval must be
3344 * returned to the pool once the data has been processed, even in cases
3345 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003346 *
3347 * Alternatively, this routine can be used to dispose of a received message
3348 * without retrieving its data. In this case there is no need to return a
3349 * memory pool block to the pool.
3350 *
3351 * This routine allocates a new memory pool block for the data only if the
3352 * data is not already in one. If a new block cannot be allocated, the routine
3353 * returns a failure code and the received message is left unchanged. This
3354 * permits the caller to reattempt data retrieval at a later time or to dispose
3355 * of the received message without retrieving its data.
3356 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003357 * @param rx_msg Address of a receive message descriptor.
3358 * @param pool Address of memory pool, or NULL to discard data.
3359 * @param block Address of the area to hold memory pool block info.
3360 * @param timeout Waiting period to wait for a memory pool block (in
3361 * milliseconds), or one of the special values K_NO_WAIT
3362 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003363 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003364 * @retval 0 Data retrieved.
3365 * @retval -ENOMEM Returned without waiting.
3366 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003367 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003368extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003369 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003370 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003371
Anas Nashif166f5192018-02-25 08:02:36 -06003372/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003373
3374/**
3375 * @cond INTERNAL_HIDDEN
3376 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003377
Andrew Boie44fe8122018-04-12 17:38:12 -07003378#define K_PIPE_FLAG_ALLOC BIT(0) /* Buffer was allocated */
3379
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003380struct k_pipe {
3381 unsigned char *buffer; /* Pipe buffer: may be NULL */
3382 size_t size; /* Buffer size */
3383 size_t bytes_used; /* # bytes used in buffer */
3384 size_t read_index; /* Where in buffer to read from */
3385 size_t write_index; /* Where in buffer to write */
3386
3387 struct {
3388 _wait_q_t readers; /* Reader wait queue */
3389 _wait_q_t writers; /* Writer wait queue */
3390 } wait_q;
3391
Anas Nashif2f203c22016-12-18 06:57:45 -05003392 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Andrew Boie44fe8122018-04-12 17:38:12 -07003393 u8_t flags; /* Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003394};
3395
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003396#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003397 { \
3398 .buffer = pipe_buffer, \
3399 .size = pipe_buffer_size, \
3400 .bytes_used = 0, \
3401 .read_index = 0, \
3402 .write_index = 0, \
3403 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3404 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003405 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003406 }
3407
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003408#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3409
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003410/**
Allan Stephensc98da842016-11-11 15:45:03 -05003411 * INTERNAL_HIDDEN @endcond
3412 */
3413
3414/**
3415 * @defgroup pipe_apis Pipe APIs
3416 * @ingroup kernel_apis
3417 * @{
3418 */
3419
3420/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003421 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003422 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003423 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003424 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003425 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003426 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003427 * @param name Name of the pipe.
3428 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3429 * or zero if no ring buffer is used.
3430 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003431 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003432#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3433 static unsigned char __kernel_noinit __aligned(pipe_align) \
3434 _k_pipe_buf_##name[pipe_buffer_size]; \
3435 struct k_pipe name \
3436 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003437 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003438
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003439/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003440 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003441 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003442 * This routine initializes a pipe object, prior to its first use.
3443 *
3444 * @param pipe Address of the pipe.
3445 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3446 * is used.
3447 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3448 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003449 *
3450 * @return N/A
3451 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003452void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3453
3454/**
3455 * @brief Release a pipe's allocated buffer
3456 *
3457 * If a pipe object was given a dynamically allocated buffer via
3458 * k_pipe_alloc_init(), this will free it. This function does nothing
3459 * if the buffer wasn't dynamically allocated.
3460 *
3461 * @param pipe Address of the pipe.
3462 */
3463void k_pipe_cleanup(struct k_pipe *pipe);
3464
3465/**
3466 * @brief Initialize a pipe and allocate a buffer for it
3467 *
3468 * Storage for the buffer region will be allocated from the calling thread's
3469 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3470 * or userspace is enabled and the pipe object loses all references to it.
3471 *
3472 * This function should only be called on uninitialized pipe objects.
3473 *
3474 * @param pipe Address of the pipe.
3475 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3476 * buffer is used.
3477 * @retval 0 on success
3478 * @retval -ENOMEM if memory couln't be allocated
3479 */
3480__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003481
3482/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003483 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003484 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003485 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003486 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003487 * @param pipe Address of the pipe.
3488 * @param data Address of data to write.
3489 * @param bytes_to_write Size of data (in bytes).
3490 * @param bytes_written Address of area to hold the number of bytes written.
3491 * @param min_xfer Minimum number of bytes to write.
3492 * @param timeout Waiting period to wait for the data to be written (in
3493 * milliseconds), or one of the special values K_NO_WAIT
3494 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003495 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003496 * @retval 0 At least @a min_xfer bytes of data were written.
3497 * @retval -EIO Returned without waiting; zero data bytes were written.
3498 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003499 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003500 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003501__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3502 size_t bytes_to_write, size_t *bytes_written,
3503 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003504
3505/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003506 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003507 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003508 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003509 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003510 * @param pipe Address of the pipe.
3511 * @param data Address to place the data read from pipe.
3512 * @param bytes_to_read Maximum number of data bytes to read.
3513 * @param bytes_read Address of area to hold the number of bytes read.
3514 * @param min_xfer Minimum number of data bytes to read.
3515 * @param timeout Waiting period to wait for the data to be read (in
3516 * milliseconds), or one of the special values K_NO_WAIT
3517 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003518 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003519 * @retval 0 At least @a min_xfer bytes of data were read.
3520 * @retval -EIO Returned without waiting; zero data bytes were read.
3521 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003522 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003523 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003524__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3525 size_t bytes_to_read, size_t *bytes_read,
3526 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003527
3528/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003529 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003530 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003531 * This routine writes the data contained in a memory block to @a pipe.
3532 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003533 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003534 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003535 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003536 * @param block Memory block containing data to send
3537 * @param size Number of data bytes in memory block to send
3538 * @param sem Semaphore to signal upon completion (else NULL)
3539 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003540 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003541 */
3542extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3543 size_t size, struct k_sem *sem);
3544
Anas Nashif166f5192018-02-25 08:02:36 -06003545/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003546
Allan Stephensc98da842016-11-11 15:45:03 -05003547/**
3548 * @cond INTERNAL_HIDDEN
3549 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003550
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003551struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003552 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003553 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003554 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003555 char *buffer;
3556 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003557 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003558
Anas Nashif2f203c22016-12-18 06:57:45 -05003559 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003560};
3561
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003562#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003563 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003564 { \
3565 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003566 .num_blocks = slab_num_blocks, \
3567 .block_size = slab_block_size, \
3568 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003569 .free_list = NULL, \
3570 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003571 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003572 }
3573
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003574#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3575
3576
Peter Mitsis578f9112016-10-07 13:50:31 -04003577/**
Allan Stephensc98da842016-11-11 15:45:03 -05003578 * INTERNAL_HIDDEN @endcond
3579 */
3580
3581/**
3582 * @defgroup mem_slab_apis Memory Slab APIs
3583 * @ingroup kernel_apis
3584 * @{
3585 */
3586
3587/**
Allan Stephensda827222016-11-09 14:23:58 -06003588 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003589 *
Allan Stephensda827222016-11-09 14:23:58 -06003590 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003591 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003592 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3593 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003594 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003595 *
Allan Stephensda827222016-11-09 14:23:58 -06003596 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003597 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003598 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003599 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003600 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003601 * @param name Name of the memory slab.
3602 * @param slab_block_size Size of each memory block (in bytes).
3603 * @param slab_num_blocks Number memory blocks.
3604 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003605 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003606#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3607 char __noinit __aligned(slab_align) \
3608 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3609 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003610 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003611 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003612 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003613
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003614/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003615 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003616 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003617 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003618 *
Allan Stephensda827222016-11-09 14:23:58 -06003619 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3620 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3621 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3622 * To ensure that each memory block is similarly aligned to this boundary,
3623 * @a slab_block_size must also be a multiple of N.
3624 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003625 * @param slab Address of the memory slab.
3626 * @param buffer Pointer to buffer used for the memory blocks.
3627 * @param block_size Size of each memory block (in bytes).
3628 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003629 *
3630 * @return N/A
3631 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003632extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003633 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003634
3635/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003636 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003637 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003638 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003639 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003640 * @param slab Address of the memory slab.
3641 * @param mem Pointer to block address area.
3642 * @param timeout Maximum time to wait for operation to complete
3643 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3644 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003645 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003646 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003647 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003648 * @retval -ENOMEM Returned without waiting.
3649 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003650 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003651extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003652 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003653
3654/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003655 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003656 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003657 * This routine releases a previously allocated memory block back to its
3658 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003659 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003660 * @param slab Address of the memory slab.
3661 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003662 *
3663 * @return N/A
3664 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003665extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003666
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003667/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003668 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003669 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003670 * This routine gets the number of memory blocks that are currently
3671 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003672 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003673 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003674 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003675 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003676 */
Kumar Galacc334c72017-04-21 10:55:34 -05003677static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003678{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003679 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003680}
3681
Peter Mitsisc001aa82016-10-13 13:53:37 -04003682/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003683 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003684 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003685 * This routine gets the number of memory blocks that are currently
3686 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003687 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003688 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003689 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003690 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003691 */
Kumar Galacc334c72017-04-21 10:55:34 -05003692static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003693{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003694 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003695}
3696
Anas Nashif166f5192018-02-25 08:02:36 -06003697/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003698
3699/**
3700 * @cond INTERNAL_HIDDEN
3701 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003702
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003703struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003704 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003705 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003706};
3707
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003708/**
Allan Stephensc98da842016-11-11 15:45:03 -05003709 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003710 */
3711
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003712/**
Allan Stephensc98da842016-11-11 15:45:03 -05003713 * @addtogroup mem_pool_apis
3714 * @{
3715 */
3716
3717/**
3718 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003719 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003720 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3721 * long. The memory pool allows blocks to be repeatedly partitioned into
3722 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003723 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003724 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003725 * If the pool is to be accessed outside the module where it is defined, it
3726 * can be declared via
3727 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003728 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003729 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003730 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003731 * @param minsz Size of the smallest blocks in the pool (in bytes).
3732 * @param maxsz Size of the largest blocks in the pool (in bytes).
3733 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003734 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003735 */
Andy Ross73cb9582017-05-09 10:42:39 -07003736#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3737 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3738 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08003739 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07003740 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08003741 .base = { \
3742 .buf = _mpool_buf_##name, \
3743 .max_sz = maxsz, \
3744 .n_max = nmax, \
3745 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3746 .levels = _mpool_lvls_##name, \
3747 .flags = SYS_MEM_POOL_KERNEL \
3748 } \
Andy Ross73cb9582017-05-09 10:42:39 -07003749 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003750
Peter Mitsis937042c2016-10-13 13:18:26 -04003751/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003752 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003753 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003754 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003755 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003756 * @param pool Address of the memory pool.
3757 * @param block Pointer to block descriptor for the allocated memory.
3758 * @param size Amount of memory to allocate (in bytes).
3759 * @param timeout Maximum time to wait for operation to complete
3760 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3761 * or K_FOREVER to wait as long as necessary.
3762 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003763 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003764 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003765 * @retval -ENOMEM Returned without waiting.
3766 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003767 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003768extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003769 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003770
3771/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07003772 * @brief Allocate memory from a memory pool with malloc() semantics
3773 *
3774 * Such memory must be released using k_free().
3775 *
3776 * @param pool Address of the memory pool.
3777 * @param size Amount of memory to allocate (in bytes).
3778 * @return Address of the allocated memory if successful, otherwise NULL
3779 */
3780extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
3781
3782/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003783 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003784 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003785 * This routine releases a previously allocated memory block back to its
3786 * memory pool.
3787 *
3788 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003789 *
3790 * @return N/A
3791 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003792extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003793
3794/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003795 * @brief Free memory allocated from a memory pool.
3796 *
3797 * This routine releases a previously allocated memory block back to its
3798 * memory pool
3799 *
3800 * @param id Memory block identifier.
3801 *
3802 * @return N/A
3803 */
3804extern void k_mem_pool_free_id(struct k_mem_block_id *id);
3805
3806/**
Anas Nashif166f5192018-02-25 08:02:36 -06003807 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003808 */
3809
3810/**
3811 * @defgroup heap_apis Heap Memory Pool APIs
3812 * @ingroup kernel_apis
3813 * @{
3814 */
3815
3816/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003817 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003818 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003819 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003820 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003821 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003822 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003823 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003824 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003825 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003826extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003827
3828/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003829 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003830 *
3831 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07003832 * returned must have been allocated from the heap memory pool or
3833 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04003834 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003835 * If @a ptr is NULL, no operation is performed.
3836 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003837 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003838 *
3839 * @return N/A
3840 */
3841extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003842
Allan Stephensc98da842016-11-11 15:45:03 -05003843/**
Andrew Boie7f95e832017-11-08 14:40:01 -08003844 * @brief Allocate memory from heap, array style
3845 *
3846 * This routine provides traditional calloc() semantics. Memory is
3847 * allocated from the heap memory pool and zeroed.
3848 *
3849 * @param nmemb Number of elements in the requested array
3850 * @param size Size of each array element (in bytes).
3851 *
3852 * @return Address of the allocated memory if successful; otherwise NULL.
3853 */
3854extern void *k_calloc(size_t nmemb, size_t size);
3855
Anas Nashif166f5192018-02-25 08:02:36 -06003856/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003857
Benjamin Walshacc68c12017-01-29 18:57:45 -05003858/* polling API - PRIVATE */
3859
Benjamin Walshb0179862017-02-02 16:39:57 -05003860#ifdef CONFIG_POLL
3861#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3862#else
3863#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3864#endif
3865
Benjamin Walshacc68c12017-01-29 18:57:45 -05003866/* private - implementation data created as needed, per-type */
3867struct _poller {
3868 struct k_thread *thread;
3869};
3870
3871/* private - types bit positions */
3872enum _poll_types_bits {
3873 /* can be used to ignore an event */
3874 _POLL_TYPE_IGNORE,
3875
3876 /* to be signaled by k_poll_signal() */
3877 _POLL_TYPE_SIGNAL,
3878
3879 /* semaphore availability */
3880 _POLL_TYPE_SEM_AVAILABLE,
3881
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003882 /* queue/fifo/lifo data availability */
3883 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003884
3885 _POLL_NUM_TYPES
3886};
3887
3888#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3889
3890/* private - states bit positions */
3891enum _poll_states_bits {
3892 /* default state when creating event */
3893 _POLL_STATE_NOT_READY,
3894
Benjamin Walshacc68c12017-01-29 18:57:45 -05003895 /* signaled by k_poll_signal() */
3896 _POLL_STATE_SIGNALED,
3897
3898 /* semaphore is available */
3899 _POLL_STATE_SEM_AVAILABLE,
3900
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003901 /* data is available to read on queue/fifo/lifo */
3902 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003903
3904 _POLL_NUM_STATES
3905};
3906
3907#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3908
3909#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003910 (32 - (0 \
3911 + 8 /* tag */ \
3912 + _POLL_NUM_TYPES \
3913 + _POLL_NUM_STATES \
3914 + 1 /* modes */ \
3915 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003916
Benjamin Walshacc68c12017-01-29 18:57:45 -05003917/* end of polling API - PRIVATE */
3918
3919
3920/**
3921 * @defgroup poll_apis Async polling APIs
3922 * @ingroup kernel_apis
3923 * @{
3924 */
3925
3926/* Public polling API */
3927
3928/* public - values for k_poll_event.type bitfield */
3929#define K_POLL_TYPE_IGNORE 0
3930#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3931#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003932#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3933#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003934
3935/* public - polling modes */
3936enum k_poll_modes {
3937 /* polling thread does not take ownership of objects when available */
3938 K_POLL_MODE_NOTIFY_ONLY = 0,
3939
3940 K_POLL_NUM_MODES
3941};
3942
3943/* public - values for k_poll_event.state bitfield */
3944#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003945#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3946#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003947#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3948#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003949
3950/* public - poll signal object */
3951struct k_poll_signal {
3952 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003953 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003954
3955 /*
3956 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3957 * user resets it to 0.
3958 */
3959 unsigned int signaled;
3960
3961 /* custom result value passed to k_poll_signal() if needed */
3962 int result;
3963};
3964
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003965#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003966 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003967 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003968 .signaled = 0, \
3969 .result = 0, \
3970 }
3971
3972struct k_poll_event {
3973 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003974 sys_dnode_t _node;
3975
3976 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003977 struct _poller *poller;
3978
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003979 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003980 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003981
Benjamin Walshacc68c12017-01-29 18:57:45 -05003982 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003983 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003984
3985 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003986 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003987
3988 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003989 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003990
3991 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003992 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003993
3994 /* per-type data */
3995 union {
3996 void *obj;
3997 struct k_poll_signal *signal;
3998 struct k_sem *sem;
3999 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004000 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004001 };
4002};
4003
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004004#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004005 { \
4006 .poller = NULL, \
4007 .type = event_type, \
4008 .state = K_POLL_STATE_NOT_READY, \
4009 .mode = event_mode, \
4010 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004011 { .obj = event_obj }, \
4012 }
4013
4014#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4015 event_tag) \
4016 { \
4017 .type = event_type, \
4018 .tag = event_tag, \
4019 .state = K_POLL_STATE_NOT_READY, \
4020 .mode = event_mode, \
4021 .unused = 0, \
4022 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004023 }
4024
4025/**
4026 * @brief Initialize one struct k_poll_event instance
4027 *
4028 * After this routine is called on a poll event, the event it ready to be
4029 * placed in an event array to be passed to k_poll().
4030 *
4031 * @param event The event to initialize.
4032 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4033 * values. Only values that apply to the same object being polled
4034 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4035 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004036 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004037 * @param obj Kernel object or poll signal.
4038 *
4039 * @return N/A
4040 */
4041
Kumar Galacc334c72017-04-21 10:55:34 -05004042extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004043 int mode, void *obj);
4044
4045/**
4046 * @brief Wait for one or many of multiple poll events to occur
4047 *
4048 * This routine allows a thread to wait concurrently for one or many of
4049 * multiple poll events to have occurred. Such events can be a kernel object
4050 * being available, like a semaphore, or a poll signal event.
4051 *
4052 * When an event notifies that a kernel object is available, the kernel object
4053 * is not "given" to the thread calling k_poll(): it merely signals the fact
4054 * that the object was available when the k_poll() call was in effect. Also,
4055 * all threads trying to acquire an object the regular way, i.e. by pending on
4056 * the object, have precedence over the thread polling on the object. This
4057 * means that the polling thread will never get the poll event on an object
4058 * until the object becomes available and its pend queue is empty. For this
4059 * reason, the k_poll() call is more effective when the objects being polled
4060 * only have one thread, the polling thread, trying to acquire them.
4061 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004062 * When k_poll() returns 0, the caller should loop on all the events that were
4063 * passed to k_poll() and check the state field for the values that were
4064 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004065 *
4066 * Before being reused for another call to k_poll(), the user has to reset the
4067 * state field to K_POLL_STATE_NOT_READY.
4068 *
4069 * @param events An array of pointers to events to be polled for.
4070 * @param num_events The number of events in the array.
4071 * @param timeout Waiting period for an event to be ready (in milliseconds),
4072 * or one of the special values K_NO_WAIT and K_FOREVER.
4073 *
4074 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004075 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02004076 * @retval -EINTR Poller thread has been interrupted.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004077 */
4078
4079extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05004080 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004081
4082/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004083 * @brief Initialize a poll signal object.
4084 *
4085 * Ready a poll signal object to be signaled via k_poll_signal().
4086 *
4087 * @param signal A poll signal.
4088 *
4089 * @return N/A
4090 */
4091
4092extern void k_poll_signal_init(struct k_poll_signal *signal);
4093
4094/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004095 * @brief Signal a poll signal object.
4096 *
4097 * This routine makes ready a poll signal, which is basically a poll event of
4098 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4099 * made ready to run. A @a result value can be specified.
4100 *
4101 * The poll signal contains a 'signaled' field that, when set by
4102 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
4103 * be reset by the user before being passed again to k_poll() or k_poll() will
4104 * consider it being signaled, and will return immediately.
4105 *
4106 * @param signal A poll signal.
4107 * @param result The value to store in the result field of the signal.
4108 *
4109 * @retval 0 The signal was delivered successfully.
4110 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
4111 */
4112
4113extern int k_poll_signal(struct k_poll_signal *signal, int result);
4114
Anas Nashif954d5502018-02-25 08:37:28 -06004115/**
4116 * @internal
4117 */
Andy Ross8606fab2018-03-26 10:54:40 -07004118extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004119
Anas Nashif166f5192018-02-25 08:02:36 -06004120/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004121
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004122/**
4123 * @brief Make the CPU idle.
4124 *
4125 * This function makes the CPU idle until an event wakes it up.
4126 *
4127 * In a regular system, the idle thread should be the only thread responsible
4128 * for making the CPU idle and triggering any type of power management.
4129 * However, in some more constrained systems, such as a single-threaded system,
4130 * the only thread would be responsible for this if needed.
4131 *
4132 * @return N/A
4133 */
4134extern void k_cpu_idle(void);
4135
4136/**
4137 * @brief Make the CPU idle in an atomic fashion.
4138 *
4139 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4140 * must be done atomically before making the CPU idle.
4141 *
4142 * @param key Interrupt locking key obtained from irq_lock().
4143 *
4144 * @return N/A
4145 */
4146extern void k_cpu_atomic_idle(unsigned int key);
4147
Anas Nashif954d5502018-02-25 08:37:28 -06004148
4149/**
4150 * @internal
4151 */
Kumar Galacc334c72017-04-21 10:55:34 -05004152extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004153
Andrew Boiecdb94d62017-04-18 15:22:05 -07004154#ifdef _ARCH_EXCEPT
4155/* This archtecture has direct support for triggering a CPU exception */
4156#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4157#else
4158
Andrew Boiecdb94d62017-04-18 15:22:05 -07004159/* NOTE: This is the implementation for arches that do not implement
4160 * _ARCH_EXCEPT() to generate a real CPU exception.
4161 *
4162 * We won't have a real exception frame to determine the PC value when
4163 * the oops occurred, so print file and line number before we jump into
4164 * the fatal error handler.
4165 */
4166#define _k_except_reason(reason) do { \
4167 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4168 _NanoFatalErrorHandler(reason, &_default_esf); \
4169 CODE_UNREACHABLE; \
4170 } while (0)
4171
4172#endif /* _ARCH__EXCEPT */
4173
4174/**
4175 * @brief Fatally terminate a thread
4176 *
4177 * This should be called when a thread has encountered an unrecoverable
4178 * runtime condition and needs to terminate. What this ultimately
4179 * means is determined by the _fatal_error_handler() implementation, which
4180 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4181 *
4182 * If this is called from ISR context, the default system fatal error handler
4183 * will treat it as an unrecoverable system error, just like k_panic().
4184 */
4185#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4186
4187/**
4188 * @brief Fatally terminate the system
4189 *
4190 * This should be called when the Zephyr kernel has encountered an
4191 * unrecoverable runtime condition and needs to terminate. What this ultimately
4192 * means is determined by the _fatal_error_handler() implementation, which
4193 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4194 */
4195#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4196
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004197/*
4198 * private APIs that are utilized by one or more public APIs
4199 */
4200
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004201#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004202/**
4203 * @internal
4204 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004205extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004206#else
Anas Nashif954d5502018-02-25 08:37:28 -06004207/**
4208 * @internal
4209 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004210#define _init_static_threads() do { } while ((0))
4211#endif
4212
Anas Nashif954d5502018-02-25 08:37:28 -06004213/**
4214 * @internal
4215 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004216extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004217/**
4218 * @internal
4219 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004220extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004221
Andrew Boiedc5d9352017-06-02 12:56:47 -07004222/* arch/cpu.h may declare an architecture or platform-specific macro
4223 * for properly declaring stacks, compatible with MMU/MPU constraints if
4224 * enabled
4225 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004226
4227/**
4228 * @brief Obtain an extern reference to a stack
4229 *
4230 * This macro properly brings the symbol of a thread stack declared
4231 * elsewhere into scope.
4232 *
4233 * @param sym Thread stack symbol name
4234 */
4235#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4236
Andrew Boiedc5d9352017-06-02 12:56:47 -07004237#ifdef _ARCH_THREAD_STACK_DEFINE
4238#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4239#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4240 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4241#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4242#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004243static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004244{
4245 return _ARCH_THREAD_STACK_BUFFER(sym);
4246}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004247#else
4248/**
4249 * @brief Declare a toplevel thread stack memory region
4250 *
4251 * This declares a region of memory suitable for use as a thread's stack.
4252 *
4253 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4254 * 'noinit' section so that it isn't zeroed at boot
4255 *
Andrew Boie507852a2017-07-25 18:47:07 -07004256 * The declared symbol will always be a k_thread_stack_t which can be passed to
4257 * k_thread_create, but should otherwise not be manipulated. If the buffer
4258 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004259 *
4260 * It is legal to precede this definition with the 'static' keyword.
4261 *
4262 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4263 * parameter of k_thread_create(), it may not be the same as the
4264 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4265 *
4266 * @param sym Thread stack symbol name
4267 * @param size Size of the stack memory region
4268 */
4269#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004270 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004271
4272/**
4273 * @brief Declare a toplevel array of thread stack memory regions
4274 *
4275 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4276 * definition for additional details and constraints.
4277 *
4278 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4279 * 'noinit' section so that it isn't zeroed at boot
4280 *
4281 * @param sym Thread stack symbol name
4282 * @param nmemb Number of stacks to declare
4283 * @param size Size of the stack memory region
4284 */
4285
4286#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004287 struct _k_thread_stack_element __noinit \
4288 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004289
4290/**
4291 * @brief Declare an embedded stack memory region
4292 *
4293 * Used for stacks embedded within other data structures. Use is highly
4294 * discouraged but in some cases necessary. For memory protection scenarios,
4295 * it is very important that any RAM preceding this member not be writable
4296 * by threads else a stack overflow will lead to silent corruption. In other
4297 * words, the containing data structure should live in RAM owned by the kernel.
4298 *
4299 * @param sym Thread stack symbol name
4300 * @param size Size of the stack memory region
4301 */
4302#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004303 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004304
4305/**
4306 * @brief Return the size in bytes of a stack memory region
4307 *
4308 * Convenience macro for passing the desired stack size to k_thread_create()
4309 * since the underlying implementation may actually create something larger
4310 * (for instance a guard area).
4311 *
4312 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004313 * passed to K_THREAD_STACK_DEFINE.
4314 *
4315 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4316 * it is not guaranteed to return the original value since each array
4317 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004318 *
4319 * @param sym Stack memory symbol
4320 * @return Size of the stack
4321 */
4322#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4323
4324/**
4325 * @brief Get a pointer to the physical stack buffer
4326 *
4327 * Convenience macro to get at the real underlying stack buffer used by
4328 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4329 * This is really only intended for diagnostic tools which want to examine
4330 * stack memory contents.
4331 *
4332 * @param sym Declared stack symbol name
4333 * @return The buffer itself, a char *
4334 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004335static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004336{
4337 return (char *)sym;
4338}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004339
4340#endif /* _ARCH_DECLARE_STACK */
4341
Chunlin Hane9c97022017-07-07 20:29:30 +08004342/**
4343 * @defgroup mem_domain_apis Memory domain APIs
4344 * @ingroup kernel_apis
4345 * @{
4346 */
4347
4348/** @def MEM_PARTITION_ENTRY
4349 * @brief Used to declare a memory partition entry
4350 */
4351#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4352 {\
4353 .start = _start, \
4354 .size = _size, \
4355 .attr = _attr, \
4356 }
4357
4358/** @def K_MEM_PARTITION_DEFINE
4359 * @brief Used to declare a memory partition
4360 */
4361#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4362#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4363 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304364 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004365 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4366#else
4367#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304368 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004369 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4370#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4371
Chunlin Hane9c97022017-07-07 20:29:30 +08004372/* memory partition */
4373struct k_mem_partition {
4374 /* start address of memory partition */
4375 u32_t start;
4376 /* size of memory partition */
4377 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304378#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004379 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304380 k_mem_partition_attr_t attr;
4381#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004382};
4383
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304384/* memory domian
4385 * Note: Always declare this structure with __kernel prefix
4386 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004387struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304388#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004389 /* partitions in the domain */
4390 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304391#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004392 /* domain q */
4393 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004394 /* number of partitions in the domain */
4395 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004396};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304397
Chunlin Hane9c97022017-07-07 20:29:30 +08004398
4399/**
4400 * @brief Initialize a memory domain.
4401 *
4402 * Initialize a memory domain with given name and memory partitions.
4403 *
4404 * @param domain The memory domain to be initialized.
4405 * @param num_parts The number of array items of "parts" parameter.
4406 * @param parts An array of pointers to the memory partitions. Can be NULL
4407 * if num_parts is zero.
4408 */
4409
Leandro Pereira08de6582018-02-28 14:22:57 -08004410extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004411 struct k_mem_partition *parts[]);
4412/**
4413 * @brief Destroy a memory domain.
4414 *
4415 * Destroy a memory domain.
4416 *
4417 * @param domain The memory domain to be destroyed.
4418 */
4419
4420extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4421
4422/**
4423 * @brief Add a memory partition into a memory domain.
4424 *
4425 * Add a memory partition into a memory domain.
4426 *
4427 * @param domain The memory domain to be added a memory partition.
4428 * @param part The memory partition to be added
4429 */
4430
4431extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4432 struct k_mem_partition *part);
4433
4434/**
4435 * @brief Remove a memory partition from a memory domain.
4436 *
4437 * Remove a memory partition from a memory domain.
4438 *
4439 * @param domain The memory domain to be removed a memory partition.
4440 * @param part The memory partition to be removed
4441 */
4442
4443extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4444 struct k_mem_partition *part);
4445
4446/**
4447 * @brief Add a thread into a memory domain.
4448 *
4449 * Add a thread into a memory domain.
4450 *
4451 * @param domain The memory domain that the thread is going to be added into.
4452 * @param thread ID of thread going to be added into the memory domain.
4453 */
4454
4455extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4456 k_tid_t thread);
4457
4458/**
4459 * @brief Remove a thread from its memory domain.
4460 *
4461 * Remove a thread from its memory domain.
4462 *
4463 * @param thread ID of thread going to be removed from its memory domain.
4464 */
4465
4466extern void k_mem_domain_remove_thread(k_tid_t thread);
4467
Anas Nashif166f5192018-02-25 08:02:36 -06004468/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004469
Andrew Boie756f9072017-10-10 16:01:49 -07004470/**
4471 * @brief Emit a character buffer to the console device
4472 *
4473 * @param c String of characters to print
4474 * @param n The length of the string
4475 */
4476__syscall void k_str_out(char *c, size_t n);
4477
Andy Rosse7172672018-01-24 15:48:32 -08004478/**
4479 * @brief Start a numbered CPU on a MP-capable system
4480
4481 * This starts and initializes a specific CPU. The main thread on
4482 * startup is running on CPU zero, other processors are numbered
4483 * sequentially. On return from this function, the CPU is known to
4484 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004485 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004486 * with the provided key will work to enable them.
4487 *
4488 * Normally, in SMP mode this function will be called by the kernel
4489 * initialization and should not be used as a user API. But it is
4490 * defined here for special-purpose apps which want Zephyr running on
4491 * one core and to use others for design-specific processing.
4492 *
4493 * @param cpu_num Integer number of the CPU
4494 * @param stack Stack memory for the CPU
4495 * @param sz Stack buffer size, in bytes
4496 * @param fn Function to begin running on the CPU. First argument is
4497 * an irq_unlock() key.
4498 * @param arg Untyped argument to be passed to "fn"
4499 */
4500extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4501 void (*fn)(int, void *), void *arg);
4502
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004503#ifdef __cplusplus
4504}
4505#endif
4506
Andrew Boiee004dec2016-11-07 09:01:19 -08004507#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4508/*
4509 * Define new and delete operators.
4510 * At this moment, the operators do nothing since objects are supposed
4511 * to be statically allocated.
4512 */
4513inline void operator delete(void *ptr)
4514{
4515 (void)ptr;
4516}
4517
4518inline void operator delete[](void *ptr)
4519{
4520 (void)ptr;
4521}
4522
4523inline void *operator new(size_t size)
4524{
4525 (void)size;
4526 return NULL;
4527}
4528
4529inline void *operator new[](size_t size)
4530{
4531 (void)size;
4532 return NULL;
4533}
4534
4535/* Placement versions of operator new and delete */
4536inline void operator delete(void *ptr1, void *ptr2)
4537{
4538 (void)ptr1;
4539 (void)ptr2;
4540}
4541
4542inline void operator delete[](void *ptr1, void *ptr2)
4543{
4544 (void)ptr1;
4545 (void)ptr2;
4546}
4547
4548inline void *operator new(size_t size, void *ptr)
4549{
4550 (void)size;
4551 return ptr;
4552}
4553
4554inline void *operator new[](size_t size, void *ptr)
4555{
4556 (void)size;
4557 return ptr;
4558}
4559
4560#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4561
Andrew Boiefa94ee72017-09-28 16:54:35 -07004562#include <syscalls/kernel.h>
4563
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004564#endif /* !_ASMLANGUAGE */
4565
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004566#endif /* _kernel__h_ */