blob: 21a0b3a33c065e7f2a23bd2096bef7e32c9088fd [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);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002949};
2950
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002951#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002952 { \
2953 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002954 .max_msgs = q_max_msgs, \
2955 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002956 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002957 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002958 .read_ptr = q_buffer, \
2959 .write_ptr = q_buffer, \
2960 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002961 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002962 }
2963
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002964#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2965
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05302966struct k_msgq_attrs {
2967 size_t msg_size;
2968 u32_t max_msgs;
2969 u32_t used_msgs;
2970};
2971
Peter Mitsis1da807e2016-10-06 11:36:59 -04002972/**
Allan Stephensc98da842016-11-11 15:45:03 -05002973 * INTERNAL_HIDDEN @endcond
2974 */
2975
2976/**
2977 * @defgroup msgq_apis Message Queue APIs
2978 * @ingroup kernel_apis
2979 * @{
2980 */
2981
2982/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002983 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002984 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002985 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2986 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002987 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2988 * message is similarly aligned to this boundary, @a q_msg_size must also be
2989 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002990 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002991 * The message queue can be accessed outside the module where it is defined
2992 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002993 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002994 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002995 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002996 * @param q_name Name of the message queue.
2997 * @param q_msg_size Message size (in bytes).
2998 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002999 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003000 */
3001#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
3002 static char __noinit __aligned(q_align) \
3003 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003004 struct k_msgq q_name \
3005 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003006 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003007 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003008
Peter Mitsisd7a37502016-10-13 11:37:40 -04003009/**
3010 * @brief Initialize a message queue.
3011 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003012 * This routine initializes a message queue object, prior to its first use.
3013 *
Allan Stephensda827222016-11-09 14:23:58 -06003014 * The message queue's ring buffer must contain space for @a max_msgs messages,
3015 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3016 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3017 * that each message is similarly aligned to this boundary, @a q_msg_size
3018 * must also be a multiple of N.
3019 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003020 * @param q Address of the message queue.
3021 * @param buffer Pointer to ring buffer that holds queued messages.
3022 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003023 * @param max_msgs Maximum number of messages that can be queued.
3024 *
3025 * @return N/A
3026 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003027__syscall void k_msgq_init(struct k_msgq *q, char *buffer,
3028 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003029
3030/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003031 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003032 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003033 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003034 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003035 * @note Can be called by ISRs.
3036 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003037 * @param q Address of the message queue.
3038 * @param data Pointer to the message.
3039 * @param timeout Waiting period to add the message (in milliseconds),
3040 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003041 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003042 * @retval 0 Message sent.
3043 * @retval -ENOMSG Returned without waiting or queue purged.
3044 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003045 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003046__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003047
3048/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003049 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003050 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003051 * This routine receives a message from message queue @a q in a "first in,
3052 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003053 *
Allan Stephensc98da842016-11-11 15:45:03 -05003054 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003055 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003056 * @param q Address of the message queue.
3057 * @param data Address of area to hold the received message.
3058 * @param timeout Waiting period to receive the message (in milliseconds),
3059 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003060 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003061 * @retval 0 Message received.
3062 * @retval -ENOMSG Returned without waiting.
3063 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003064 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003065__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003066
3067/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003068 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003069 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003070 * This routine discards all unreceived messages in a message queue's ring
3071 * buffer. Any threads that are blocked waiting to send a message to the
3072 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003073 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003074 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003075 *
3076 * @return N/A
3077 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003078__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003079
Peter Mitsis67be2492016-10-07 11:44:34 -04003080/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003081 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003082 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003083 * This routine returns the number of unused entries in a message queue's
3084 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003085 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003086 * @param q Address of the message queue.
3087 *
3088 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04003089 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003090__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3091
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303092/**
3093 * @brief Get basic attributes of a message queue.
3094 *
3095 * This routine fetches basic attributes of message queue into attr argument.
3096 *
3097 * @param q Address of the message queue.
3098 * @param attrs pointer to message queue attribute structure.
3099 *
3100 * @return N/A
3101 */
3102__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3103
3104
Andrew Boie82edb6e2017-10-02 10:53:06 -07003105static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003106{
3107 return q->max_msgs - q->used_msgs;
3108}
3109
Peter Mitsisd7a37502016-10-13 11:37:40 -04003110/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003111 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003112 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003113 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003114 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003115 * @param q Address of the message queue.
3116 *
3117 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003118 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003119__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3120
3121static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003122{
3123 return q->used_msgs;
3124}
3125
Anas Nashif166f5192018-02-25 08:02:36 -06003126/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003127
3128/**
3129 * @defgroup mem_pool_apis Memory Pool APIs
3130 * @ingroup kernel_apis
3131 * @{
3132 */
3133
Andy Ross73cb9582017-05-09 10:42:39 -07003134/* Note on sizing: the use of a 20 bit field for block means that,
3135 * assuming a reasonable minimum block size of 16 bytes, we're limited
3136 * to 16M of memory managed by a single pool. Long term it would be
3137 * good to move to a variable bit size based on configuration.
3138 */
3139struct k_mem_block_id {
3140 u32_t pool : 8;
3141 u32_t level : 4;
3142 u32_t block : 20;
3143};
3144
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003145struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003146 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003147 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003148};
3149
Anas Nashif166f5192018-02-25 08:02:36 -06003150/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003151
3152/**
3153 * @defgroup mailbox_apis Mailbox APIs
3154 * @ingroup kernel_apis
3155 * @{
3156 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003157
3158struct k_mbox_msg {
3159 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003160 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003161 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003162 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003163 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003164 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003165 /** sender's message data buffer */
3166 void *tx_data;
3167 /** internal use only - needed for legacy API support */
3168 void *_rx_data;
3169 /** message data block descriptor */
3170 struct k_mem_block tx_block;
3171 /** source thread id */
3172 k_tid_t rx_source_thread;
3173 /** target thread id */
3174 k_tid_t tx_target_thread;
3175 /** internal use only - thread waiting on send (may be a dummy) */
3176 k_tid_t _syncing_thread;
3177#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3178 /** internal use only - semaphore used during asynchronous send */
3179 struct k_sem *_async_sem;
3180#endif
3181};
3182
Allan Stephensc98da842016-11-11 15:45:03 -05003183/**
3184 * @cond INTERNAL_HIDDEN
3185 */
3186
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003187struct k_mbox {
3188 _wait_q_t tx_msg_queue;
3189 _wait_q_t rx_msg_queue;
3190
Anas Nashif2f203c22016-12-18 06:57:45 -05003191 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003192};
3193
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003194#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003195 { \
3196 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3197 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003198 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003199 }
3200
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003201#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3202
Peter Mitsis12092702016-10-14 12:57:23 -04003203/**
Allan Stephensc98da842016-11-11 15:45:03 -05003204 * INTERNAL_HIDDEN @endcond
3205 */
3206
3207/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003208 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003209 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003210 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003211 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003212 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003213 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003214 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003215 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003216#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003217 struct k_mbox name \
3218 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003219 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003220
Peter Mitsis12092702016-10-14 12:57:23 -04003221/**
3222 * @brief Initialize a mailbox.
3223 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003224 * This routine initializes a mailbox object, prior to its first use.
3225 *
3226 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003227 *
3228 * @return N/A
3229 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003230extern void k_mbox_init(struct k_mbox *mbox);
3231
Peter Mitsis12092702016-10-14 12:57:23 -04003232/**
3233 * @brief Send a mailbox message in a synchronous manner.
3234 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003235 * This routine sends a message to @a mbox and waits for a receiver to both
3236 * receive and process it. The message data may be in a buffer, in a memory
3237 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003238 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003239 * @param mbox Address of the mailbox.
3240 * @param tx_msg Address of the transmit message descriptor.
3241 * @param timeout Waiting period for the message to be received (in
3242 * milliseconds), or one of the special values K_NO_WAIT
3243 * and K_FOREVER. Once the message has been received,
3244 * this routine waits as long as necessary for the message
3245 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003246 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003247 * @retval 0 Message sent.
3248 * @retval -ENOMSG Returned without waiting.
3249 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003250 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003251extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003252 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003253
Peter Mitsis12092702016-10-14 12:57:23 -04003254/**
3255 * @brief Send a mailbox message in an asynchronous manner.
3256 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003257 * This routine sends a message to @a mbox without waiting for a receiver
3258 * to process it. The message data may be in a buffer, in a memory pool block,
3259 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3260 * will be given when the message has been both received and completely
3261 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003262 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003263 * @param mbox Address of the mailbox.
3264 * @param tx_msg Address of the transmit message descriptor.
3265 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003266 *
3267 * @return N/A
3268 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003269extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003270 struct k_sem *sem);
3271
Peter Mitsis12092702016-10-14 12:57:23 -04003272/**
3273 * @brief Receive a mailbox message.
3274 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003275 * This routine receives a message from @a mbox, then optionally retrieves
3276 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003277 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003278 * @param mbox Address of the mailbox.
3279 * @param rx_msg Address of the receive message descriptor.
3280 * @param buffer Address of the buffer to receive data, or NULL to defer data
3281 * retrieval and message disposal until later.
3282 * @param timeout Waiting period for a message to be received (in
3283 * milliseconds), or one of the special values K_NO_WAIT
3284 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003285 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003286 * @retval 0 Message received.
3287 * @retval -ENOMSG Returned without waiting.
3288 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003289 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003290extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003291 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003292
3293/**
3294 * @brief Retrieve mailbox message data into a buffer.
3295 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003296 * This routine completes the processing of a received message by retrieving
3297 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003298 *
3299 * Alternatively, this routine can be used to dispose of a received message
3300 * without retrieving its data.
3301 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003302 * @param rx_msg Address of the receive message descriptor.
3303 * @param buffer Address of the buffer to receive data, or NULL to discard
3304 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003305 *
3306 * @return N/A
3307 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003308extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003309
3310/**
3311 * @brief Retrieve mailbox message data into a memory pool block.
3312 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003313 * This routine completes the processing of a received message by retrieving
3314 * its data into a memory pool block, then disposing of the message.
3315 * The memory pool block that results from successful retrieval must be
3316 * returned to the pool once the data has been processed, even in cases
3317 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003318 *
3319 * Alternatively, this routine can be used to dispose of a received message
3320 * without retrieving its data. In this case there is no need to return a
3321 * memory pool block to the pool.
3322 *
3323 * This routine allocates a new memory pool block for the data only if the
3324 * data is not already in one. If a new block cannot be allocated, the routine
3325 * returns a failure code and the received message is left unchanged. This
3326 * permits the caller to reattempt data retrieval at a later time or to dispose
3327 * of the received message without retrieving its data.
3328 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003329 * @param rx_msg Address of a receive message descriptor.
3330 * @param pool Address of memory pool, or NULL to discard data.
3331 * @param block Address of the area to hold memory pool block info.
3332 * @param timeout Waiting period to wait for a memory pool block (in
3333 * milliseconds), or one of the special values K_NO_WAIT
3334 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003335 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003336 * @retval 0 Data retrieved.
3337 * @retval -ENOMEM Returned without waiting.
3338 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003339 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003340extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003341 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003342 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003343
Anas Nashif166f5192018-02-25 08:02:36 -06003344/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003345
3346/**
3347 * @cond INTERNAL_HIDDEN
3348 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003349
Andrew Boie44fe8122018-04-12 17:38:12 -07003350#define K_PIPE_FLAG_ALLOC BIT(0) /* Buffer was allocated */
3351
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003352struct k_pipe {
3353 unsigned char *buffer; /* Pipe buffer: may be NULL */
3354 size_t size; /* Buffer size */
3355 size_t bytes_used; /* # bytes used in buffer */
3356 size_t read_index; /* Where in buffer to read from */
3357 size_t write_index; /* Where in buffer to write */
3358
3359 struct {
3360 _wait_q_t readers; /* Reader wait queue */
3361 _wait_q_t writers; /* Writer wait queue */
3362 } wait_q;
3363
Anas Nashif2f203c22016-12-18 06:57:45 -05003364 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Andrew Boie44fe8122018-04-12 17:38:12 -07003365 u8_t flags; /* Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003366};
3367
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003368#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003369 { \
3370 .buffer = pipe_buffer, \
3371 .size = pipe_buffer_size, \
3372 .bytes_used = 0, \
3373 .read_index = 0, \
3374 .write_index = 0, \
3375 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3376 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003377 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003378 }
3379
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003380#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3381
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003382/**
Allan Stephensc98da842016-11-11 15:45:03 -05003383 * INTERNAL_HIDDEN @endcond
3384 */
3385
3386/**
3387 * @defgroup pipe_apis Pipe APIs
3388 * @ingroup kernel_apis
3389 * @{
3390 */
3391
3392/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003393 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003394 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003395 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003396 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003397 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003398 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003399 * @param name Name of the pipe.
3400 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3401 * or zero if no ring buffer is used.
3402 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003403 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003404#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3405 static unsigned char __kernel_noinit __aligned(pipe_align) \
3406 _k_pipe_buf_##name[pipe_buffer_size]; \
3407 struct k_pipe name \
3408 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003409 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003410
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003411/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003412 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003413 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003414 * This routine initializes a pipe object, prior to its first use.
3415 *
3416 * @param pipe Address of the pipe.
3417 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3418 * is used.
3419 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3420 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003421 *
3422 * @return N/A
3423 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003424void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3425
3426/**
3427 * @brief Release a pipe's allocated buffer
3428 *
3429 * If a pipe object was given a dynamically allocated buffer via
3430 * k_pipe_alloc_init(), this will free it. This function does nothing
3431 * if the buffer wasn't dynamically allocated.
3432 *
3433 * @param pipe Address of the pipe.
3434 */
3435void k_pipe_cleanup(struct k_pipe *pipe);
3436
3437/**
3438 * @brief Initialize a pipe and allocate a buffer for it
3439 *
3440 * Storage for the buffer region will be allocated from the calling thread's
3441 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3442 * or userspace is enabled and the pipe object loses all references to it.
3443 *
3444 * This function should only be called on uninitialized pipe objects.
3445 *
3446 * @param pipe Address of the pipe.
3447 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3448 * buffer is used.
3449 * @retval 0 on success
3450 * @retval -ENOMEM if memory couln't be allocated
3451 */
3452__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003453
3454/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003455 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003456 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003457 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003458 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003459 * @param pipe Address of the pipe.
3460 * @param data Address of data to write.
3461 * @param bytes_to_write Size of data (in bytes).
3462 * @param bytes_written Address of area to hold the number of bytes written.
3463 * @param min_xfer Minimum number of bytes to write.
3464 * @param timeout Waiting period to wait for the data to be written (in
3465 * milliseconds), or one of the special values K_NO_WAIT
3466 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003467 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003468 * @retval 0 At least @a min_xfer bytes of data were written.
3469 * @retval -EIO Returned without waiting; zero data bytes were written.
3470 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003471 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003472 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003473__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3474 size_t bytes_to_write, size_t *bytes_written,
3475 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003476
3477/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003478 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003479 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003480 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003481 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003482 * @param pipe Address of the pipe.
3483 * @param data Address to place the data read from pipe.
3484 * @param bytes_to_read Maximum number of data bytes to read.
3485 * @param bytes_read Address of area to hold the number of bytes read.
3486 * @param min_xfer Minimum number of data bytes to read.
3487 * @param timeout Waiting period to wait for the data to be read (in
3488 * milliseconds), or one of the special values K_NO_WAIT
3489 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003490 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003491 * @retval 0 At least @a min_xfer bytes of data were read.
3492 * @retval -EIO Returned without waiting; zero data bytes were read.
3493 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003494 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003495 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003496__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3497 size_t bytes_to_read, size_t *bytes_read,
3498 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003499
3500/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003501 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003502 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003503 * This routine writes the data contained in a memory block to @a pipe.
3504 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003505 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003506 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003507 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003508 * @param block Memory block containing data to send
3509 * @param size Number of data bytes in memory block to send
3510 * @param sem Semaphore to signal upon completion (else NULL)
3511 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003512 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003513 */
3514extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3515 size_t size, struct k_sem *sem);
3516
Anas Nashif166f5192018-02-25 08:02:36 -06003517/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003518
Allan Stephensc98da842016-11-11 15:45:03 -05003519/**
3520 * @cond INTERNAL_HIDDEN
3521 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003522
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003523struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003524 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003525 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003526 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003527 char *buffer;
3528 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003529 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003530
Anas Nashif2f203c22016-12-18 06:57:45 -05003531 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003532};
3533
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003534#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003535 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003536 { \
3537 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003538 .num_blocks = slab_num_blocks, \
3539 .block_size = slab_block_size, \
3540 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003541 .free_list = NULL, \
3542 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003543 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003544 }
3545
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003546#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3547
3548
Peter Mitsis578f9112016-10-07 13:50:31 -04003549/**
Allan Stephensc98da842016-11-11 15:45:03 -05003550 * INTERNAL_HIDDEN @endcond
3551 */
3552
3553/**
3554 * @defgroup mem_slab_apis Memory Slab APIs
3555 * @ingroup kernel_apis
3556 * @{
3557 */
3558
3559/**
Allan Stephensda827222016-11-09 14:23:58 -06003560 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003561 *
Allan Stephensda827222016-11-09 14:23:58 -06003562 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003563 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003564 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3565 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003566 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003567 *
Allan Stephensda827222016-11-09 14:23:58 -06003568 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003569 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003570 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003571 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003572 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003573 * @param name Name of the memory slab.
3574 * @param slab_block_size Size of each memory block (in bytes).
3575 * @param slab_num_blocks Number memory blocks.
3576 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003577 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003578#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3579 char __noinit __aligned(slab_align) \
3580 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3581 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003582 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003583 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003584 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003585
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003586/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003587 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003588 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003589 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003590 *
Allan Stephensda827222016-11-09 14:23:58 -06003591 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3592 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3593 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3594 * To ensure that each memory block is similarly aligned to this boundary,
3595 * @a slab_block_size must also be a multiple of N.
3596 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003597 * @param slab Address of the memory slab.
3598 * @param buffer Pointer to buffer used for the memory blocks.
3599 * @param block_size Size of each memory block (in bytes).
3600 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003601 *
3602 * @return N/A
3603 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003604extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003605 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003606
3607/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003608 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003609 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003610 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003611 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003612 * @param slab Address of the memory slab.
3613 * @param mem Pointer to block address area.
3614 * @param timeout Maximum time to wait for operation to complete
3615 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3616 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003617 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003618 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003619 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003620 * @retval -ENOMEM Returned without waiting.
3621 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003622 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003623extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003624 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003625
3626/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003627 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003628 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003629 * This routine releases a previously allocated memory block back to its
3630 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003631 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003632 * @param slab Address of the memory slab.
3633 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003634 *
3635 * @return N/A
3636 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003637extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003638
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003639/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003640 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003641 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003642 * This routine gets the number of memory blocks that are currently
3643 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003644 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003645 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003646 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003647 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003648 */
Kumar Galacc334c72017-04-21 10:55:34 -05003649static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003650{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003651 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003652}
3653
Peter Mitsisc001aa82016-10-13 13:53:37 -04003654/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003655 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003656 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003657 * This routine gets the number of memory blocks that are currently
3658 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003659 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003660 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003661 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003662 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003663 */
Kumar Galacc334c72017-04-21 10:55:34 -05003664static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003665{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003666 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003667}
3668
Anas Nashif166f5192018-02-25 08:02:36 -06003669/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003670
3671/**
3672 * @cond INTERNAL_HIDDEN
3673 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003674
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003675struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003676 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003677 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003678};
3679
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003680/**
Allan Stephensc98da842016-11-11 15:45:03 -05003681 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003682 */
3683
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003684/**
Allan Stephensc98da842016-11-11 15:45:03 -05003685 * @addtogroup mem_pool_apis
3686 * @{
3687 */
3688
3689/**
3690 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003691 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003692 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3693 * long. The memory pool allows blocks to be repeatedly partitioned into
3694 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003695 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003696 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003697 * If the pool is to be accessed outside the module where it is defined, it
3698 * can be declared via
3699 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003700 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003701 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003702 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003703 * @param minsz Size of the smallest blocks in the pool (in bytes).
3704 * @param maxsz Size of the largest blocks in the pool (in bytes).
3705 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003706 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003707 */
Andy Ross73cb9582017-05-09 10:42:39 -07003708#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3709 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3710 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08003711 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07003712 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08003713 .base = { \
3714 .buf = _mpool_buf_##name, \
3715 .max_sz = maxsz, \
3716 .n_max = nmax, \
3717 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3718 .levels = _mpool_lvls_##name, \
3719 .flags = SYS_MEM_POOL_KERNEL \
3720 } \
Andy Ross73cb9582017-05-09 10:42:39 -07003721 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003722
Peter Mitsis937042c2016-10-13 13:18:26 -04003723/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003724 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003725 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003726 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003727 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003728 * @param pool Address of the memory pool.
3729 * @param block Pointer to block descriptor for the allocated memory.
3730 * @param size Amount of memory to allocate (in bytes).
3731 * @param timeout Maximum time to wait for operation to complete
3732 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3733 * or K_FOREVER to wait as long as necessary.
3734 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003735 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003736 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003737 * @retval -ENOMEM Returned without waiting.
3738 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003739 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003740extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003741 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003742
3743/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07003744 * @brief Allocate memory from a memory pool with malloc() semantics
3745 *
3746 * Such memory must be released using k_free().
3747 *
3748 * @param pool Address of the memory pool.
3749 * @param size Amount of memory to allocate (in bytes).
3750 * @return Address of the allocated memory if successful, otherwise NULL
3751 */
3752extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
3753
3754/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003755 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003756 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003757 * This routine releases a previously allocated memory block back to its
3758 * memory pool.
3759 *
3760 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003761 *
3762 * @return N/A
3763 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003764extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003765
3766/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003767 * @brief Free memory allocated from a memory pool.
3768 *
3769 * This routine releases a previously allocated memory block back to its
3770 * memory pool
3771 *
3772 * @param id Memory block identifier.
3773 *
3774 * @return N/A
3775 */
3776extern void k_mem_pool_free_id(struct k_mem_block_id *id);
3777
3778/**
Anas Nashif166f5192018-02-25 08:02:36 -06003779 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003780 */
3781
3782/**
3783 * @defgroup heap_apis Heap Memory Pool APIs
3784 * @ingroup kernel_apis
3785 * @{
3786 */
3787
3788/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003789 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003790 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003791 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003792 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003793 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003794 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003795 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003796 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003797 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003798extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003799
3800/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003801 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003802 *
3803 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07003804 * returned must have been allocated from the heap memory pool or
3805 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04003806 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003807 * If @a ptr is NULL, no operation is performed.
3808 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003809 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003810 *
3811 * @return N/A
3812 */
3813extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003814
Allan Stephensc98da842016-11-11 15:45:03 -05003815/**
Andrew Boie7f95e832017-11-08 14:40:01 -08003816 * @brief Allocate memory from heap, array style
3817 *
3818 * This routine provides traditional calloc() semantics. Memory is
3819 * allocated from the heap memory pool and zeroed.
3820 *
3821 * @param nmemb Number of elements in the requested array
3822 * @param size Size of each array element (in bytes).
3823 *
3824 * @return Address of the allocated memory if successful; otherwise NULL.
3825 */
3826extern void *k_calloc(size_t nmemb, size_t size);
3827
Anas Nashif166f5192018-02-25 08:02:36 -06003828/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003829
Benjamin Walshacc68c12017-01-29 18:57:45 -05003830/* polling API - PRIVATE */
3831
Benjamin Walshb0179862017-02-02 16:39:57 -05003832#ifdef CONFIG_POLL
3833#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3834#else
3835#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3836#endif
3837
Benjamin Walshacc68c12017-01-29 18:57:45 -05003838/* private - implementation data created as needed, per-type */
3839struct _poller {
3840 struct k_thread *thread;
3841};
3842
3843/* private - types bit positions */
3844enum _poll_types_bits {
3845 /* can be used to ignore an event */
3846 _POLL_TYPE_IGNORE,
3847
3848 /* to be signaled by k_poll_signal() */
3849 _POLL_TYPE_SIGNAL,
3850
3851 /* semaphore availability */
3852 _POLL_TYPE_SEM_AVAILABLE,
3853
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003854 /* queue/fifo/lifo data availability */
3855 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003856
3857 _POLL_NUM_TYPES
3858};
3859
3860#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3861
3862/* private - states bit positions */
3863enum _poll_states_bits {
3864 /* default state when creating event */
3865 _POLL_STATE_NOT_READY,
3866
Benjamin Walshacc68c12017-01-29 18:57:45 -05003867 /* signaled by k_poll_signal() */
3868 _POLL_STATE_SIGNALED,
3869
3870 /* semaphore is available */
3871 _POLL_STATE_SEM_AVAILABLE,
3872
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003873 /* data is available to read on queue/fifo/lifo */
3874 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003875
3876 _POLL_NUM_STATES
3877};
3878
3879#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3880
3881#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003882 (32 - (0 \
3883 + 8 /* tag */ \
3884 + _POLL_NUM_TYPES \
3885 + _POLL_NUM_STATES \
3886 + 1 /* modes */ \
3887 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003888
Benjamin Walshacc68c12017-01-29 18:57:45 -05003889/* end of polling API - PRIVATE */
3890
3891
3892/**
3893 * @defgroup poll_apis Async polling APIs
3894 * @ingroup kernel_apis
3895 * @{
3896 */
3897
3898/* Public polling API */
3899
3900/* public - values for k_poll_event.type bitfield */
3901#define K_POLL_TYPE_IGNORE 0
3902#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3903#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003904#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3905#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003906
3907/* public - polling modes */
3908enum k_poll_modes {
3909 /* polling thread does not take ownership of objects when available */
3910 K_POLL_MODE_NOTIFY_ONLY = 0,
3911
3912 K_POLL_NUM_MODES
3913};
3914
3915/* public - values for k_poll_event.state bitfield */
3916#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003917#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3918#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003919#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3920#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003921
3922/* public - poll signal object */
3923struct k_poll_signal {
3924 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003925 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003926
3927 /*
3928 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3929 * user resets it to 0.
3930 */
3931 unsigned int signaled;
3932
3933 /* custom result value passed to k_poll_signal() if needed */
3934 int result;
3935};
3936
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003937#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003938 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003939 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003940 .signaled = 0, \
3941 .result = 0, \
3942 }
3943
3944struct k_poll_event {
3945 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003946 sys_dnode_t _node;
3947
3948 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003949 struct _poller *poller;
3950
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003951 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003952 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003953
Benjamin Walshacc68c12017-01-29 18:57:45 -05003954 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003955 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003956
3957 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003958 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003959
3960 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003961 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003962
3963 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003964 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003965
3966 /* per-type data */
3967 union {
3968 void *obj;
3969 struct k_poll_signal *signal;
3970 struct k_sem *sem;
3971 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003972 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003973 };
3974};
3975
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003976#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003977 { \
3978 .poller = NULL, \
3979 .type = event_type, \
3980 .state = K_POLL_STATE_NOT_READY, \
3981 .mode = event_mode, \
3982 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003983 { .obj = event_obj }, \
3984 }
3985
3986#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3987 event_tag) \
3988 { \
3989 .type = event_type, \
3990 .tag = event_tag, \
3991 .state = K_POLL_STATE_NOT_READY, \
3992 .mode = event_mode, \
3993 .unused = 0, \
3994 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003995 }
3996
3997/**
3998 * @brief Initialize one struct k_poll_event instance
3999 *
4000 * After this routine is called on a poll event, the event it ready to be
4001 * placed in an event array to be passed to k_poll().
4002 *
4003 * @param event The event to initialize.
4004 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4005 * values. Only values that apply to the same object being polled
4006 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4007 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004008 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004009 * @param obj Kernel object or poll signal.
4010 *
4011 * @return N/A
4012 */
4013
Kumar Galacc334c72017-04-21 10:55:34 -05004014extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004015 int mode, void *obj);
4016
4017/**
4018 * @brief Wait for one or many of multiple poll events to occur
4019 *
4020 * This routine allows a thread to wait concurrently for one or many of
4021 * multiple poll events to have occurred. Such events can be a kernel object
4022 * being available, like a semaphore, or a poll signal event.
4023 *
4024 * When an event notifies that a kernel object is available, the kernel object
4025 * is not "given" to the thread calling k_poll(): it merely signals the fact
4026 * that the object was available when the k_poll() call was in effect. Also,
4027 * all threads trying to acquire an object the regular way, i.e. by pending on
4028 * the object, have precedence over the thread polling on the object. This
4029 * means that the polling thread will never get the poll event on an object
4030 * until the object becomes available and its pend queue is empty. For this
4031 * reason, the k_poll() call is more effective when the objects being polled
4032 * only have one thread, the polling thread, trying to acquire them.
4033 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004034 * When k_poll() returns 0, the caller should loop on all the events that were
4035 * passed to k_poll() and check the state field for the values that were
4036 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004037 *
4038 * Before being reused for another call to k_poll(), the user has to reset the
4039 * state field to K_POLL_STATE_NOT_READY.
4040 *
4041 * @param events An array of pointers to events to be polled for.
4042 * @param num_events The number of events in the array.
4043 * @param timeout Waiting period for an event to be ready (in milliseconds),
4044 * or one of the special values K_NO_WAIT and K_FOREVER.
4045 *
4046 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004047 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02004048 * @retval -EINTR Poller thread has been interrupted.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004049 */
4050
4051extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05004052 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004053
4054/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004055 * @brief Initialize a poll signal object.
4056 *
4057 * Ready a poll signal object to be signaled via k_poll_signal().
4058 *
4059 * @param signal A poll signal.
4060 *
4061 * @return N/A
4062 */
4063
4064extern void k_poll_signal_init(struct k_poll_signal *signal);
4065
4066/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004067 * @brief Signal a poll signal object.
4068 *
4069 * This routine makes ready a poll signal, which is basically a poll event of
4070 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4071 * made ready to run. A @a result value can be specified.
4072 *
4073 * The poll signal contains a 'signaled' field that, when set by
4074 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
4075 * be reset by the user before being passed again to k_poll() or k_poll() will
4076 * consider it being signaled, and will return immediately.
4077 *
4078 * @param signal A poll signal.
4079 * @param result The value to store in the result field of the signal.
4080 *
4081 * @retval 0 The signal was delivered successfully.
4082 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
4083 */
4084
4085extern int k_poll_signal(struct k_poll_signal *signal, int result);
4086
Anas Nashif954d5502018-02-25 08:37:28 -06004087/**
4088 * @internal
4089 */
Andy Ross8606fab2018-03-26 10:54:40 -07004090extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004091
Anas Nashif166f5192018-02-25 08:02:36 -06004092/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004093
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004094/**
4095 * @brief Make the CPU idle.
4096 *
4097 * This function makes the CPU idle until an event wakes it up.
4098 *
4099 * In a regular system, the idle thread should be the only thread responsible
4100 * for making the CPU idle and triggering any type of power management.
4101 * However, in some more constrained systems, such as a single-threaded system,
4102 * the only thread would be responsible for this if needed.
4103 *
4104 * @return N/A
4105 */
4106extern void k_cpu_idle(void);
4107
4108/**
4109 * @brief Make the CPU idle in an atomic fashion.
4110 *
4111 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4112 * must be done atomically before making the CPU idle.
4113 *
4114 * @param key Interrupt locking key obtained from irq_lock().
4115 *
4116 * @return N/A
4117 */
4118extern void k_cpu_atomic_idle(unsigned int key);
4119
Anas Nashif954d5502018-02-25 08:37:28 -06004120
4121/**
4122 * @internal
4123 */
Kumar Galacc334c72017-04-21 10:55:34 -05004124extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004125
Andrew Boiecdb94d62017-04-18 15:22:05 -07004126#ifdef _ARCH_EXCEPT
4127/* This archtecture has direct support for triggering a CPU exception */
4128#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4129#else
4130
Andrew Boiecdb94d62017-04-18 15:22:05 -07004131/* NOTE: This is the implementation for arches that do not implement
4132 * _ARCH_EXCEPT() to generate a real CPU exception.
4133 *
4134 * We won't have a real exception frame to determine the PC value when
4135 * the oops occurred, so print file and line number before we jump into
4136 * the fatal error handler.
4137 */
4138#define _k_except_reason(reason) do { \
4139 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4140 _NanoFatalErrorHandler(reason, &_default_esf); \
4141 CODE_UNREACHABLE; \
4142 } while (0)
4143
4144#endif /* _ARCH__EXCEPT */
4145
4146/**
4147 * @brief Fatally terminate a thread
4148 *
4149 * This should be called when a thread has encountered an unrecoverable
4150 * runtime condition and needs to terminate. What this ultimately
4151 * means is determined by the _fatal_error_handler() implementation, which
4152 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4153 *
4154 * If this is called from ISR context, the default system fatal error handler
4155 * will treat it as an unrecoverable system error, just like k_panic().
4156 */
4157#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4158
4159/**
4160 * @brief Fatally terminate the system
4161 *
4162 * This should be called when the Zephyr kernel has encountered an
4163 * unrecoverable runtime condition and needs to terminate. What this ultimately
4164 * means is determined by the _fatal_error_handler() implementation, which
4165 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4166 */
4167#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4168
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004169/*
4170 * private APIs that are utilized by one or more public APIs
4171 */
4172
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004173#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004174/**
4175 * @internal
4176 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004177extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004178#else
Anas Nashif954d5502018-02-25 08:37:28 -06004179/**
4180 * @internal
4181 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004182#define _init_static_threads() do { } while ((0))
4183#endif
4184
Anas Nashif954d5502018-02-25 08:37:28 -06004185/**
4186 * @internal
4187 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004188extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004189/**
4190 * @internal
4191 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004192extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004193
Andrew Boiedc5d9352017-06-02 12:56:47 -07004194/* arch/cpu.h may declare an architecture or platform-specific macro
4195 * for properly declaring stacks, compatible with MMU/MPU constraints if
4196 * enabled
4197 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004198
4199/**
4200 * @brief Obtain an extern reference to a stack
4201 *
4202 * This macro properly brings the symbol of a thread stack declared
4203 * elsewhere into scope.
4204 *
4205 * @param sym Thread stack symbol name
4206 */
4207#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4208
Andrew Boiedc5d9352017-06-02 12:56:47 -07004209#ifdef _ARCH_THREAD_STACK_DEFINE
4210#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4211#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4212 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4213#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4214#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004215static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004216{
4217 return _ARCH_THREAD_STACK_BUFFER(sym);
4218}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004219#else
4220/**
4221 * @brief Declare a toplevel thread stack memory region
4222 *
4223 * This declares a region of memory suitable for use as a thread's stack.
4224 *
4225 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4226 * 'noinit' section so that it isn't zeroed at boot
4227 *
Andrew Boie507852a2017-07-25 18:47:07 -07004228 * The declared symbol will always be a k_thread_stack_t which can be passed to
4229 * k_thread_create, but should otherwise not be manipulated. If the buffer
4230 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004231 *
4232 * It is legal to precede this definition with the 'static' keyword.
4233 *
4234 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4235 * parameter of k_thread_create(), it may not be the same as the
4236 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4237 *
4238 * @param sym Thread stack symbol name
4239 * @param size Size of the stack memory region
4240 */
4241#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004242 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004243
4244/**
4245 * @brief Declare a toplevel array of thread stack memory regions
4246 *
4247 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4248 * definition for additional details and constraints.
4249 *
4250 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4251 * 'noinit' section so that it isn't zeroed at boot
4252 *
4253 * @param sym Thread stack symbol name
4254 * @param nmemb Number of stacks to declare
4255 * @param size Size of the stack memory region
4256 */
4257
4258#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004259 struct _k_thread_stack_element __noinit \
4260 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004261
4262/**
4263 * @brief Declare an embedded stack memory region
4264 *
4265 * Used for stacks embedded within other data structures. Use is highly
4266 * discouraged but in some cases necessary. For memory protection scenarios,
4267 * it is very important that any RAM preceding this member not be writable
4268 * by threads else a stack overflow will lead to silent corruption. In other
4269 * words, the containing data structure should live in RAM owned by the kernel.
4270 *
4271 * @param sym Thread stack symbol name
4272 * @param size Size of the stack memory region
4273 */
4274#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004275 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004276
4277/**
4278 * @brief Return the size in bytes of a stack memory region
4279 *
4280 * Convenience macro for passing the desired stack size to k_thread_create()
4281 * since the underlying implementation may actually create something larger
4282 * (for instance a guard area).
4283 *
4284 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004285 * passed to K_THREAD_STACK_DEFINE.
4286 *
4287 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4288 * it is not guaranteed to return the original value since each array
4289 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004290 *
4291 * @param sym Stack memory symbol
4292 * @return Size of the stack
4293 */
4294#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4295
4296/**
4297 * @brief Get a pointer to the physical stack buffer
4298 *
4299 * Convenience macro to get at the real underlying stack buffer used by
4300 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4301 * This is really only intended for diagnostic tools which want to examine
4302 * stack memory contents.
4303 *
4304 * @param sym Declared stack symbol name
4305 * @return The buffer itself, a char *
4306 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004307static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004308{
4309 return (char *)sym;
4310}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004311
4312#endif /* _ARCH_DECLARE_STACK */
4313
Chunlin Hane9c97022017-07-07 20:29:30 +08004314/**
4315 * @defgroup mem_domain_apis Memory domain APIs
4316 * @ingroup kernel_apis
4317 * @{
4318 */
4319
4320/** @def MEM_PARTITION_ENTRY
4321 * @brief Used to declare a memory partition entry
4322 */
4323#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4324 {\
4325 .start = _start, \
4326 .size = _size, \
4327 .attr = _attr, \
4328 }
4329
4330/** @def K_MEM_PARTITION_DEFINE
4331 * @brief Used to declare a memory partition
4332 */
4333#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4334#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4335 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304336 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004337 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4338#else
4339#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304340 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004341 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4342#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4343
Chunlin Hane9c97022017-07-07 20:29:30 +08004344/* memory partition */
4345struct k_mem_partition {
4346 /* start address of memory partition */
4347 u32_t start;
4348 /* size of memory partition */
4349 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304350#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004351 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304352 k_mem_partition_attr_t attr;
4353#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004354};
4355
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304356/* memory domian
4357 * Note: Always declare this structure with __kernel prefix
4358 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004359struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304360#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004361 /* partitions in the domain */
4362 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304363#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004364 /* domain q */
4365 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004366 /* number of partitions in the domain */
4367 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004368};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304369
Chunlin Hane9c97022017-07-07 20:29:30 +08004370
4371/**
4372 * @brief Initialize a memory domain.
4373 *
4374 * Initialize a memory domain with given name and memory partitions.
4375 *
4376 * @param domain The memory domain to be initialized.
4377 * @param num_parts The number of array items of "parts" parameter.
4378 * @param parts An array of pointers to the memory partitions. Can be NULL
4379 * if num_parts is zero.
4380 */
4381
Leandro Pereira08de6582018-02-28 14:22:57 -08004382extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004383 struct k_mem_partition *parts[]);
4384/**
4385 * @brief Destroy a memory domain.
4386 *
4387 * Destroy a memory domain.
4388 *
4389 * @param domain The memory domain to be destroyed.
4390 */
4391
4392extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4393
4394/**
4395 * @brief Add a memory partition into a memory domain.
4396 *
4397 * Add a memory partition into a memory domain.
4398 *
4399 * @param domain The memory domain to be added a memory partition.
4400 * @param part The memory partition to be added
4401 */
4402
4403extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4404 struct k_mem_partition *part);
4405
4406/**
4407 * @brief Remove a memory partition from a memory domain.
4408 *
4409 * Remove a memory partition from a memory domain.
4410 *
4411 * @param domain The memory domain to be removed a memory partition.
4412 * @param part The memory partition to be removed
4413 */
4414
4415extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4416 struct k_mem_partition *part);
4417
4418/**
4419 * @brief Add a thread into a memory domain.
4420 *
4421 * Add a thread into a memory domain.
4422 *
4423 * @param domain The memory domain that the thread is going to be added into.
4424 * @param thread ID of thread going to be added into the memory domain.
4425 */
4426
4427extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4428 k_tid_t thread);
4429
4430/**
4431 * @brief Remove a thread from its memory domain.
4432 *
4433 * Remove a thread from its memory domain.
4434 *
4435 * @param thread ID of thread going to be removed from its memory domain.
4436 */
4437
4438extern void k_mem_domain_remove_thread(k_tid_t thread);
4439
Anas Nashif166f5192018-02-25 08:02:36 -06004440/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004441
Andrew Boie756f9072017-10-10 16:01:49 -07004442/**
4443 * @brief Emit a character buffer to the console device
4444 *
4445 * @param c String of characters to print
4446 * @param n The length of the string
4447 */
4448__syscall void k_str_out(char *c, size_t n);
4449
Andy Rosse7172672018-01-24 15:48:32 -08004450/**
4451 * @brief Start a numbered CPU on a MP-capable system
4452
4453 * This starts and initializes a specific CPU. The main thread on
4454 * startup is running on CPU zero, other processors are numbered
4455 * sequentially. On return from this function, the CPU is known to
4456 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004457 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004458 * with the provided key will work to enable them.
4459 *
4460 * Normally, in SMP mode this function will be called by the kernel
4461 * initialization and should not be used as a user API. But it is
4462 * defined here for special-purpose apps which want Zephyr running on
4463 * one core and to use others for design-specific processing.
4464 *
4465 * @param cpu_num Integer number of the CPU
4466 * @param stack Stack memory for the CPU
4467 * @param sz Stack buffer size, in bytes
4468 * @param fn Function to begin running on the CPU. First argument is
4469 * an irq_unlock() key.
4470 * @param arg Untyped argument to be passed to "fn"
4471 */
4472extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4473 void (*fn)(int, void *), void *arg);
4474
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004475#ifdef __cplusplus
4476}
4477#endif
4478
Andrew Boiee004dec2016-11-07 09:01:19 -08004479#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4480/*
4481 * Define new and delete operators.
4482 * At this moment, the operators do nothing since objects are supposed
4483 * to be statically allocated.
4484 */
4485inline void operator delete(void *ptr)
4486{
4487 (void)ptr;
4488}
4489
4490inline void operator delete[](void *ptr)
4491{
4492 (void)ptr;
4493}
4494
4495inline void *operator new(size_t size)
4496{
4497 (void)size;
4498 return NULL;
4499}
4500
4501inline void *operator new[](size_t size)
4502{
4503 (void)size;
4504 return NULL;
4505}
4506
4507/* Placement versions of operator new and delete */
4508inline void operator delete(void *ptr1, void *ptr2)
4509{
4510 (void)ptr1;
4511 (void)ptr2;
4512}
4513
4514inline void operator delete[](void *ptr1, void *ptr2)
4515{
4516 (void)ptr1;
4517 (void)ptr2;
4518}
4519
4520inline void *operator new(size_t size, void *ptr)
4521{
4522 (void)size;
4523 return ptr;
4524}
4525
4526inline void *operator new[](size_t size, void *ptr)
4527{
4528 (void)size;
4529 return ptr;
4530}
4531
4532#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4533
Andrew Boiefa94ee72017-09-28 16:54:35 -07004534#include <syscalls/kernel.h>
4535
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004536#endif /* !_ASMLANGUAGE */
4537
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004538#endif /* _kernel__h_ */