blob: 57f80e01ffb4c1dd34d4dbd4143b6dbe375a690e [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 */
Andrew Boief3bee952018-05-02 17:44:39 -07002149#define K_STACK_FLAG_ALLOC BIT(0) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002150
2151struct k_stack {
2152 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002153 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002154
Anas Nashif2f203c22016-12-18 06:57:45 -05002155 _OBJECT_TRACING_NEXT_PTR(k_stack);
Andrew Boief3bee952018-05-02 17:44:39 -07002156 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002157};
2158
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002159#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002160 { \
2161 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2162 .base = stack_buffer, \
2163 .next = stack_buffer, \
2164 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002165 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002166 }
2167
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002168#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2169
Allan Stephensc98da842016-11-11 15:45:03 -05002170/**
2171 * INTERNAL_HIDDEN @endcond
2172 */
2173
2174/**
2175 * @defgroup stack_apis Stack APIs
2176 * @ingroup kernel_apis
2177 * @{
2178 */
2179
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002180/**
2181 * @brief Initialize a stack.
2182 *
2183 * This routine initializes a stack object, prior to its first use.
2184 *
2185 * @param stack Address of the stack.
2186 * @param buffer Address of array used to hold stacked values.
2187 * @param num_entries Maximum number of values that can be stacked.
2188 *
2189 * @return N/A
2190 */
Andrew Boief3bee952018-05-02 17:44:39 -07002191void k_stack_init(struct k_stack *stack,
2192 u32_t *buffer, unsigned int num_entries);
2193
2194
2195/**
2196 * @brief Initialize a stack.
2197 *
2198 * This routine initializes a stack object, prior to its first use. Internal
2199 * buffers will be allocated from the calling thread's resource pool.
2200 * This memory will be released if k_stack_cleanup() is called, or
2201 * userspace is enabled and the stack object loses all references to it.
2202 *
2203 * @param stack Address of the stack.
2204 * @param num_entries Maximum number of values that can be stacked.
2205 *
2206 * @return -ENOMEM if memory couldn't be allocated
2207 */
2208
2209__syscall int k_stack_alloc_init(struct k_stack *stack,
2210 unsigned int num_entries);
2211
2212/**
2213 * @brief Release a stack's allocated buffer
2214 *
2215 * If a stack object was given a dynamically allocated buffer via
2216 * k_stack_alloc_init(), this will free it. This function does nothing
2217 * if the buffer wasn't dynamically allocated.
2218 *
2219 * @param stack Address of the stack.
2220 */
2221void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002222
2223/**
2224 * @brief Push an element onto a stack.
2225 *
2226 * This routine adds a 32-bit value @a data to @a stack.
2227 *
2228 * @note Can be called by ISRs.
2229 *
2230 * @param stack Address of the stack.
2231 * @param data Value to push onto the stack.
2232 *
2233 * @return N/A
2234 */
Andrew Boiee8734462017-09-29 16:42:07 -07002235__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002236
2237/**
2238 * @brief Pop an element from a stack.
2239 *
2240 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2241 * manner and stores the value in @a data.
2242 *
2243 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2244 *
2245 * @param stack Address of the stack.
2246 * @param data Address of area to hold the value popped from the stack.
2247 * @param timeout Waiting period to obtain a value (in milliseconds),
2248 * or one of the special values K_NO_WAIT and K_FOREVER.
2249 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002250 * @retval 0 Element popped from stack.
2251 * @retval -EBUSY Returned without waiting.
2252 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002253 */
Andrew Boiee8734462017-09-29 16:42:07 -07002254__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002255
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002256/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002257 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002258 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002259 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002260 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002261 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002262 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002263 * @param name Name of the stack.
2264 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002265 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002266#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002267 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002268 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002269 struct k_stack name \
2270 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002271 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002272 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002273
Anas Nashif166f5192018-02-25 08:02:36 -06002274/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002275
Allan Stephens6bba9b02016-11-16 14:56:54 -05002276struct k_work;
2277
Allan Stephensc98da842016-11-11 15:45:03 -05002278/**
2279 * @defgroup workqueue_apis Workqueue Thread APIs
2280 * @ingroup kernel_apis
2281 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002282 */
2283
Allan Stephens6bba9b02016-11-16 14:56:54 -05002284/**
2285 * @typedef k_work_handler_t
2286 * @brief Work item handler function type.
2287 *
2288 * A work item's handler function is executed by a workqueue's thread
2289 * when the work item is processed by the workqueue.
2290 *
2291 * @param work Address of the work item.
2292 *
2293 * @return N/A
2294 */
2295typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002296
2297/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002298 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002299 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002300
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002301struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002302 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002303 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002304};
2305
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002306enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002307 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002308};
2309
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002310struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002311 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002312 k_work_handler_t handler;
2313 atomic_t flags[1];
2314};
2315
Allan Stephens6bba9b02016-11-16 14:56:54 -05002316struct k_delayed_work {
2317 struct k_work work;
2318 struct _timeout timeout;
2319 struct k_work_q *work_q;
2320};
2321
2322extern struct k_work_q k_sys_work_q;
2323
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002324/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002325 * INTERNAL_HIDDEN @endcond
2326 */
2327
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002328#define _K_WORK_INITIALIZER(work_handler) \
2329 { \
2330 ._reserved = NULL, \
2331 .handler = work_handler, \
2332 .flags = { 0 } \
2333 }
2334
2335#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2336
Allan Stephens6bba9b02016-11-16 14:56:54 -05002337/**
2338 * @brief Initialize a statically-defined work item.
2339 *
2340 * This macro can be used to initialize a statically-defined workqueue work
2341 * item, prior to its first use. For example,
2342 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002343 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002344 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002345 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002346 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002347 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002348#define K_WORK_DEFINE(work, work_handler) \
2349 struct k_work work \
2350 __in_section(_k_work, static, work) = \
2351 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002352
2353/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002354 * @brief Initialize a work item.
2355 *
2356 * This routine initializes a workqueue work item, prior to its first use.
2357 *
2358 * @param work Address of work item.
2359 * @param handler Function to invoke each time work item is processed.
2360 *
2361 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002362 */
2363static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2364{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002365 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002366 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002367 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002368}
2369
2370/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002371 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002372 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002373 * This routine submits work item @a work to be processed by workqueue
2374 * @a work_q. If the work item is already pending in the workqueue's queue
2375 * as a result of an earlier submission, this routine has no effect on the
2376 * work item. If the work item has already been processed, or is currently
2377 * being processed, its work is considered complete and the work item can be
2378 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002379 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002380 * @warning
2381 * A submitted work item must not be modified until it has been processed
2382 * by the workqueue.
2383 *
2384 * @note Can be called by ISRs.
2385 *
2386 * @param work_q Address of workqueue.
2387 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002388 *
2389 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002390 */
2391static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2392 struct k_work *work)
2393{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002394 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002395 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002396 }
2397}
2398
2399/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002400 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002401 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002402 * This routine indicates if work item @a work is pending in a workqueue's
2403 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002404 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002405 * @note Can be called by ISRs.
2406 *
2407 * @param work Address of work item.
2408 *
2409 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002410 */
2411static inline int k_work_pending(struct k_work *work)
2412{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002413 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002414}
2415
2416/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002417 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002418 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002419 * This routine starts workqueue @a work_q. The workqueue spawns its work
2420 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002421 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002422 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002423 * @param stack Pointer to work queue thread's stack space, as defined by
2424 * K_THREAD_STACK_DEFINE()
2425 * @param stack_size Size of the work queue thread's stack (in bytes), which
2426 * should either be the same constant passed to
2427 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002428 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002429 *
2430 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002431 */
Andrew Boie507852a2017-07-25 18:47:07 -07002432extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002433 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002434 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002435
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002436/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002437 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002438 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002439 * This routine initializes a workqueue delayed work item, prior to
2440 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002441 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002442 * @param work Address of delayed work item.
2443 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002444 *
2445 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002446 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002447extern void k_delayed_work_init(struct k_delayed_work *work,
2448 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002449
2450/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002451 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002452 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002453 * This routine schedules work item @a work to be processed by workqueue
2454 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002455 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002456 * Only when the countdown completes is the work item actually submitted to
2457 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002458 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002459 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002460 * counting down cancels the existing submission and restarts the
2461 * countdown using the new delay. Note that this behavior is
2462 * inherently subject to race conditions with the pre-existing
2463 * timeouts and work queue, so care must be taken to synchronize such
2464 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002465 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002466 * @warning
2467 * A delayed work item must not be modified until it has been processed
2468 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002469 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002470 * @note Can be called by ISRs.
2471 *
2472 * @param work_q Address of workqueue.
2473 * @param work Address of delayed work item.
2474 * @param delay Delay before submitting the work item (in milliseconds).
2475 *
2476 * @retval 0 Work item countdown started.
2477 * @retval -EINPROGRESS Work item is already pending.
2478 * @retval -EINVAL Work item is being processed or has completed its work.
2479 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002480 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002481extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2482 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002483 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002484
2485/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002486 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002487 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002488 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002489 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002490 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002491 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002492 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002493 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002494 * @param work Address of delayed work item.
2495 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002496 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002497 * @retval -EINPROGRESS Work item is already pending.
2498 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002499 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002500extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002501
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002502/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002503 * @brief Submit a work item to the system workqueue.
2504 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002505 * This routine submits work item @a work to be processed by the system
2506 * workqueue. If the work item is already pending in the workqueue's queue
2507 * as a result of an earlier submission, this routine has no effect on the
2508 * work item. If the work item has already been processed, or is currently
2509 * being processed, its work is considered complete and the work item can be
2510 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002511 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002512 * @warning
2513 * Work items submitted to the system workqueue should avoid using handlers
2514 * that block or yield since this may prevent the system workqueue from
2515 * processing other work items in a timely manner.
2516 *
2517 * @note Can be called by ISRs.
2518 *
2519 * @param work Address of work item.
2520 *
2521 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002522 */
2523static inline void k_work_submit(struct k_work *work)
2524{
2525 k_work_submit_to_queue(&k_sys_work_q, work);
2526}
2527
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002528/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002529 * @brief Submit a delayed work item to the system workqueue.
2530 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002531 * This routine schedules work item @a work to be processed by the system
2532 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002533 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002534 * Only when the countdown completes is the work item actually submitted to
2535 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002536 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002537 * Submitting a previously submitted delayed work item that is still
2538 * counting down cancels the existing submission and restarts the countdown
2539 * using the new delay. If the work item is currently pending on the
2540 * workqueue's queue because the countdown has completed it is too late to
2541 * resubmit the item, and resubmission fails without impacting the work item.
2542 * If the work item has already been processed, or is currently being processed,
2543 * its work is considered complete and the work item can be resubmitted.
2544 *
2545 * @warning
2546 * Work items submitted to the system workqueue should avoid using handlers
2547 * that block or yield since this may prevent the system workqueue from
2548 * processing other work items in a timely manner.
2549 *
2550 * @note Can be called by ISRs.
2551 *
2552 * @param work Address of delayed work item.
2553 * @param delay Delay before submitting the work item (in milliseconds).
2554 *
2555 * @retval 0 Work item countdown started.
2556 * @retval -EINPROGRESS Work item is already pending.
2557 * @retval -EINVAL Work item is being processed or has completed its work.
2558 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002559 */
2560static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002561 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002562{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002563 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002564}
2565
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002566/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002567 * @brief Get time remaining before a delayed work gets scheduled.
2568 *
2569 * This routine computes the (approximate) time remaining before a
2570 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002571 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002572 *
2573 * @param work Delayed work item.
2574 *
2575 * @return Remaining time (in milliseconds).
2576 */
Kumar Galacc334c72017-04-21 10:55:34 -05002577static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002578{
2579 return _timeout_remaining_get(&work->timeout);
2580}
2581
Anas Nashif166f5192018-02-25 08:02:36 -06002582/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002583
Allan Stephensc98da842016-11-11 15:45:03 -05002584/**
2585 * @cond INTERNAL_HIDDEN
2586 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002587
2588struct k_mutex {
2589 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002590 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002591 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002592 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002593
Anas Nashif2f203c22016-12-18 06:57:45 -05002594 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002595};
2596
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002597#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002598 { \
2599 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2600 .owner = NULL, \
2601 .lock_count = 0, \
2602 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002603 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002604 }
2605
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002606#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2607
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002608/**
Allan Stephensc98da842016-11-11 15:45:03 -05002609 * INTERNAL_HIDDEN @endcond
2610 */
2611
2612/**
2613 * @defgroup mutex_apis Mutex APIs
2614 * @ingroup kernel_apis
2615 * @{
2616 */
2617
2618/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002619 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002620 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002621 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002622 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002623 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002624 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002625 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002626 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002627#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002628 struct k_mutex name \
2629 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002630 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002631
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002632/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002633 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002634 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002635 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002636 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002637 * Upon completion, the mutex is available and does not have an owner.
2638 *
2639 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002640 *
2641 * @return N/A
2642 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002643__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002644
2645/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002646 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002647 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002648 * This routine locks @a mutex. If the mutex is locked by another thread,
2649 * the calling thread waits until the mutex becomes available or until
2650 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002651 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002652 * A thread is permitted to lock a mutex it has already locked. The operation
2653 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002654 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002655 * @param mutex Address of the mutex.
2656 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002657 * or one of the special values K_NO_WAIT and K_FOREVER.
2658 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002659 * @retval 0 Mutex locked.
2660 * @retval -EBUSY Returned without waiting.
2661 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002662 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002663__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002664
2665/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002666 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002667 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002668 * This routine unlocks @a mutex. The mutex must already be locked by the
2669 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002670 *
2671 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002672 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002673 * thread.
2674 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002675 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002676 *
2677 * @return N/A
2678 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002679__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002680
Allan Stephensc98da842016-11-11 15:45:03 -05002681/**
Anas Nashif166f5192018-02-25 08:02:36 -06002682 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002683 */
2684
2685/**
2686 * @cond INTERNAL_HIDDEN
2687 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002688
2689struct k_sem {
2690 _wait_q_t wait_q;
2691 unsigned int count;
2692 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002693 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002694
Anas Nashif2f203c22016-12-18 06:57:45 -05002695 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002696};
2697
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002698#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002699 { \
2700 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2701 .count = initial_count, \
2702 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002703 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002704 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002705 }
2706
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002707#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2708
Allan Stephensc98da842016-11-11 15:45:03 -05002709/**
2710 * INTERNAL_HIDDEN @endcond
2711 */
2712
2713/**
2714 * @defgroup semaphore_apis Semaphore APIs
2715 * @ingroup kernel_apis
2716 * @{
2717 */
2718
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002719/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002720 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002722 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002723 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002724 * @param sem Address of the semaphore.
2725 * @param initial_count Initial semaphore count.
2726 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002727 *
2728 * @return N/A
2729 */
Andrew Boie99280232017-09-29 14:17:47 -07002730__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2731 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002732
2733/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002734 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002735 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002736 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002737 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002738 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2739 *
2740 * @param sem Address of the semaphore.
2741 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002742 * or one of the special values K_NO_WAIT and K_FOREVER.
2743 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002744 * @note When porting code from the nanokernel legacy API to the new API, be
2745 * careful with the return value of this function. The return value is the
2746 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2747 * non-zero means failure, while the nano_sem_take family returns 1 for success
2748 * and 0 for failure.
2749 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002750 * @retval 0 Semaphore taken.
2751 * @retval -EBUSY Returned without waiting.
2752 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002753 */
Andrew Boie99280232017-09-29 14:17:47 -07002754__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002755
2756/**
2757 * @brief Give a semaphore.
2758 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002759 * This routine gives @a sem, unless the semaphore is already at its maximum
2760 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002761 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002762 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002763 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002764 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002765 *
2766 * @return N/A
2767 */
Andrew Boie99280232017-09-29 14:17:47 -07002768__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002769
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002770/**
2771 * @brief Reset a semaphore's count to zero.
2772 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002773 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002774 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002775 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002776 *
2777 * @return N/A
2778 */
Andrew Boie990bf162017-10-03 12:36:49 -07002779__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002780
Anas Nashif954d5502018-02-25 08:37:28 -06002781/**
2782 * @internal
2783 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002784static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002785{
2786 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002787}
2788
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002789/**
2790 * @brief Get a semaphore's count.
2791 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002792 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002793 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002794 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002795 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002796 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002797 */
Andrew Boie990bf162017-10-03 12:36:49 -07002798__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002799
Anas Nashif954d5502018-02-25 08:37:28 -06002800/**
2801 * @internal
2802 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002803static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002804{
2805 return sem->count;
2806}
2807
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002808/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002809 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002810 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002811 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002812 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002813 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002814 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002815 * @param name Name of the semaphore.
2816 * @param initial_count Initial semaphore count.
2817 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002818 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002819#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002820 struct k_sem name \
2821 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07002822 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05302823 BUILD_ASSERT(((count_limit) != 0) && \
2824 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002825
Anas Nashif166f5192018-02-25 08:02:36 -06002826/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002827
2828/**
2829 * @defgroup alert_apis Alert APIs
2830 * @ingroup kernel_apis
2831 * @{
2832 */
2833
Allan Stephens5eceb852016-11-16 10:16:30 -05002834/**
2835 * @typedef k_alert_handler_t
2836 * @brief Alert handler function type.
2837 *
2838 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002839 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002840 * and is only invoked if the alert has been initialized with one.
2841 *
2842 * @param alert Address of the alert.
2843 *
2844 * @return 0 if alert has been consumed; non-zero if alert should pend.
2845 */
2846typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002847
Anas Nashif166f5192018-02-25 08:02:36 -06002848/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002849
2850/**
2851 * @cond INTERNAL_HIDDEN
2852 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002853
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002854#define K_ALERT_DEFAULT NULL
2855#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002856
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002857struct k_alert {
2858 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002859 atomic_t send_count;
2860 struct k_work work_item;
2861 struct k_sem sem;
2862
Anas Nashif2f203c22016-12-18 06:57:45 -05002863 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002864};
2865
Anas Nashif954d5502018-02-25 08:37:28 -06002866/**
2867 * @internal
2868 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002869extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002870
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002871#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002872 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002873 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002874 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002875 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2876 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002877 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002878 }
2879
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002880#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2881
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002882/**
Allan Stephensc98da842016-11-11 15:45:03 -05002883 * INTERNAL_HIDDEN @endcond
2884 */
2885
2886/**
2887 * @addtogroup alert_apis
2888 * @{
2889 */
2890
2891/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002892 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002893 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002894 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002895 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002896 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002897 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002898 * @param name Name of the alert.
2899 * @param alert_handler Action to take when alert is sent. Specify either
2900 * the address of a function to be invoked by the system workqueue
2901 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2902 * K_ALERT_DEFAULT (which causes the alert to pend).
2903 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002904 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002905#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002906 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002907 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002908 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002909 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002910
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002911/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002912 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002913 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002914 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002915 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002916 * @param alert Address of the alert.
2917 * @param handler Action to take when alert is sent. Specify either the address
2918 * of a function to be invoked by the system workqueue thread,
2919 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2920 * K_ALERT_DEFAULT (which causes the alert to pend).
2921 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002922 *
2923 * @return N/A
2924 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002925extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2926 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002927
2928/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002929 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002930 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002931 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002932 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002933 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2934 *
2935 * @param alert Address of the alert.
2936 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002937 * or one of the special values K_NO_WAIT and K_FOREVER.
2938 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002939 * @retval 0 Alert received.
2940 * @retval -EBUSY Returned without waiting.
2941 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002942 */
Andrew Boie310e9872017-09-29 04:41:15 -07002943__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002944
2945/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002946 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002947 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002948 * This routine signals @a alert. The action specified for @a alert will
2949 * be taken, which may trigger the execution of an alert handler function
2950 * and/or cause the alert to pend (assuming the alert has not reached its
2951 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002952 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002953 * @note Can be called by ISRs.
2954 *
2955 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002956 *
2957 * @return N/A
2958 */
Andrew Boie310e9872017-09-29 04:41:15 -07002959__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002960
2961/**
Anas Nashif166f5192018-02-25 08:02:36 -06002962 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002963 */
2964
Allan Stephensc98da842016-11-11 15:45:03 -05002965/**
2966 * @cond INTERNAL_HIDDEN
2967 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002968
2969struct k_msgq {
2970 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002971 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002972 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002973 char *buffer_start;
2974 char *buffer_end;
2975 char *read_ptr;
2976 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002977 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002978
Anas Nashif2f203c22016-12-18 06:57:45 -05002979 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Andrew Boie0fe789f2018-04-12 18:35:56 -07002980 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002981};
2982
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002983#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002984 { \
2985 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002986 .max_msgs = q_max_msgs, \
2987 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002988 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002989 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002990 .read_ptr = q_buffer, \
2991 .write_ptr = q_buffer, \
2992 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002993 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002994 }
2995
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002996#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2997
Andrew Boie0fe789f2018-04-12 18:35:56 -07002998#define K_MSGQ_FLAG_ALLOC BIT(0)
2999
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303000struct k_msgq_attrs {
3001 size_t msg_size;
3002 u32_t max_msgs;
3003 u32_t used_msgs;
3004};
3005
Peter Mitsis1da807e2016-10-06 11:36:59 -04003006/**
Allan Stephensc98da842016-11-11 15:45:03 -05003007 * INTERNAL_HIDDEN @endcond
3008 */
3009
3010/**
3011 * @defgroup msgq_apis Message Queue APIs
3012 * @ingroup kernel_apis
3013 * @{
3014 */
3015
3016/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003017 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003018 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003019 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3020 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003021 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3022 * message is similarly aligned to this boundary, @a q_msg_size must also be
3023 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003024 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003025 * The message queue can be accessed outside the module where it is defined
3026 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003027 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003028 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003029 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003030 * @param q_name Name of the message queue.
3031 * @param q_msg_size Message size (in bytes).
3032 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003033 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003034 */
3035#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003036 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003037 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003038 struct k_msgq q_name \
3039 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003040 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003041 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003042
Peter Mitsisd7a37502016-10-13 11:37:40 -04003043/**
3044 * @brief Initialize a message queue.
3045 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003046 * This routine initializes a message queue object, prior to its first use.
3047 *
Allan Stephensda827222016-11-09 14:23:58 -06003048 * The message queue's ring buffer must contain space for @a max_msgs messages,
3049 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3050 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3051 * that each message is similarly aligned to this boundary, @a q_msg_size
3052 * must also be a multiple of N.
3053 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003054 * @param q Address of the message queue.
3055 * @param buffer Pointer to ring buffer that holds queued messages.
3056 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003057 * @param max_msgs Maximum number of messages that can be queued.
3058 *
3059 * @return N/A
3060 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003061void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3062 u32_t max_msgs);
3063
3064/**
3065 * @brief Initialize a message queue.
3066 *
3067 * This routine initializes a message queue object, prior to its first use,
3068 * allocating its internal ring buffer from the calling thread's resource
3069 * pool.
3070 *
3071 * Memory allocated for the ring buffer can be released by calling
3072 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3073 * all of its references.
3074 *
3075 * @param q Address of the message queue.
3076 * @param msg_size Message size (in bytes).
3077 * @param max_msgs Maximum number of messages that can be queued.
3078 *
3079 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3080 * thread's resource pool, or -EINVAL if the size parameters cause
3081 * an integer overflow.
3082 */
3083__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3084 u32_t max_msgs);
3085
3086
3087void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003088
3089/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003090 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003091 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003092 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003093 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003094 * @note Can be called by ISRs.
3095 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003096 * @param q Address of the message queue.
3097 * @param data Pointer to the message.
3098 * @param timeout Waiting period to add the message (in milliseconds),
3099 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003100 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003101 * @retval 0 Message sent.
3102 * @retval -ENOMSG Returned without waiting or queue purged.
3103 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003104 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003105__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003106
3107/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003108 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003109 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003110 * This routine receives a message from message queue @a q in a "first in,
3111 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003112 *
Allan Stephensc98da842016-11-11 15:45:03 -05003113 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003114 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003115 * @param q Address of the message queue.
3116 * @param data Address of area to hold the received message.
3117 * @param timeout Waiting period to receive the message (in milliseconds),
3118 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003119 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003120 * @retval 0 Message received.
3121 * @retval -ENOMSG Returned without waiting.
3122 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003123 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003124__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003125
3126/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003127 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003128 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003129 * This routine discards all unreceived messages in a message queue's ring
3130 * buffer. Any threads that are blocked waiting to send a message to the
3131 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003132 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003133 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003134 *
3135 * @return N/A
3136 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003137__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003138
Peter Mitsis67be2492016-10-07 11:44:34 -04003139/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003140 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003141 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003142 * This routine returns the number of unused entries in a message queue's
3143 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003144 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003145 * @param q Address of the message queue.
3146 *
3147 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04003148 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003149__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3150
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303151/**
3152 * @brief Get basic attributes of a message queue.
3153 *
3154 * This routine fetches basic attributes of message queue into attr argument.
3155 *
3156 * @param q Address of the message queue.
3157 * @param attrs pointer to message queue attribute structure.
3158 *
3159 * @return N/A
3160 */
3161__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3162
3163
Andrew Boie82edb6e2017-10-02 10:53:06 -07003164static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003165{
3166 return q->max_msgs - q->used_msgs;
3167}
3168
Peter Mitsisd7a37502016-10-13 11:37:40 -04003169/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003170 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003171 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003172 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003173 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003174 * @param q Address of the message queue.
3175 *
3176 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003177 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003178__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3179
3180static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003181{
3182 return q->used_msgs;
3183}
3184
Anas Nashif166f5192018-02-25 08:02:36 -06003185/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003186
3187/**
3188 * @defgroup mem_pool_apis Memory Pool APIs
3189 * @ingroup kernel_apis
3190 * @{
3191 */
3192
Andy Ross73cb9582017-05-09 10:42:39 -07003193/* Note on sizing: the use of a 20 bit field for block means that,
3194 * assuming a reasonable minimum block size of 16 bytes, we're limited
3195 * to 16M of memory managed by a single pool. Long term it would be
3196 * good to move to a variable bit size based on configuration.
3197 */
3198struct k_mem_block_id {
3199 u32_t pool : 8;
3200 u32_t level : 4;
3201 u32_t block : 20;
3202};
3203
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003204struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003205 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003206 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003207};
3208
Anas Nashif166f5192018-02-25 08:02:36 -06003209/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003210
3211/**
3212 * @defgroup mailbox_apis Mailbox APIs
3213 * @ingroup kernel_apis
3214 * @{
3215 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003216
3217struct k_mbox_msg {
3218 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003219 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003220 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003221 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003222 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003223 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003224 /** sender's message data buffer */
3225 void *tx_data;
3226 /** internal use only - needed for legacy API support */
3227 void *_rx_data;
3228 /** message data block descriptor */
3229 struct k_mem_block tx_block;
3230 /** source thread id */
3231 k_tid_t rx_source_thread;
3232 /** target thread id */
3233 k_tid_t tx_target_thread;
3234 /** internal use only - thread waiting on send (may be a dummy) */
3235 k_tid_t _syncing_thread;
3236#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3237 /** internal use only - semaphore used during asynchronous send */
3238 struct k_sem *_async_sem;
3239#endif
3240};
3241
Allan Stephensc98da842016-11-11 15:45:03 -05003242/**
3243 * @cond INTERNAL_HIDDEN
3244 */
3245
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003246struct k_mbox {
3247 _wait_q_t tx_msg_queue;
3248 _wait_q_t rx_msg_queue;
3249
Anas Nashif2f203c22016-12-18 06:57:45 -05003250 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003251};
3252
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003253#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003254 { \
3255 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3256 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003257 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003258 }
3259
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003260#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3261
Peter Mitsis12092702016-10-14 12:57:23 -04003262/**
Allan Stephensc98da842016-11-11 15:45:03 -05003263 * INTERNAL_HIDDEN @endcond
3264 */
3265
3266/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003267 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003268 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003269 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003270 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003271 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003272 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003273 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003274 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003275#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003276 struct k_mbox name \
3277 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003278 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003279
Peter Mitsis12092702016-10-14 12:57:23 -04003280/**
3281 * @brief Initialize a mailbox.
3282 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003283 * This routine initializes a mailbox object, prior to its first use.
3284 *
3285 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003286 *
3287 * @return N/A
3288 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003289extern void k_mbox_init(struct k_mbox *mbox);
3290
Peter Mitsis12092702016-10-14 12:57:23 -04003291/**
3292 * @brief Send a mailbox message in a synchronous manner.
3293 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003294 * This routine sends a message to @a mbox and waits for a receiver to both
3295 * receive and process it. The message data may be in a buffer, in a memory
3296 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003297 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003298 * @param mbox Address of the mailbox.
3299 * @param tx_msg Address of the transmit message descriptor.
3300 * @param timeout Waiting period for the message to be received (in
3301 * milliseconds), or one of the special values K_NO_WAIT
3302 * and K_FOREVER. Once the message has been received,
3303 * this routine waits as long as necessary for the message
3304 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003305 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003306 * @retval 0 Message sent.
3307 * @retval -ENOMSG Returned without waiting.
3308 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003309 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003310extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003311 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003312
Peter Mitsis12092702016-10-14 12:57:23 -04003313/**
3314 * @brief Send a mailbox message in an asynchronous manner.
3315 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003316 * This routine sends a message to @a mbox without waiting for a receiver
3317 * to process it. The message data may be in a buffer, in a memory pool block,
3318 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3319 * will be given when the message has been both received and completely
3320 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003321 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003322 * @param mbox Address of the mailbox.
3323 * @param tx_msg Address of the transmit message descriptor.
3324 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003325 *
3326 * @return N/A
3327 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003328extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003329 struct k_sem *sem);
3330
Peter Mitsis12092702016-10-14 12:57:23 -04003331/**
3332 * @brief Receive a mailbox message.
3333 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003334 * This routine receives a message from @a mbox, then optionally retrieves
3335 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003336 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003337 * @param mbox Address of the mailbox.
3338 * @param rx_msg Address of the receive message descriptor.
3339 * @param buffer Address of the buffer to receive data, or NULL to defer data
3340 * retrieval and message disposal until later.
3341 * @param timeout Waiting period for a message to be received (in
3342 * milliseconds), or one of the special values K_NO_WAIT
3343 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003344 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003345 * @retval 0 Message received.
3346 * @retval -ENOMSG Returned without waiting.
3347 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003348 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003349extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003350 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003351
3352/**
3353 * @brief Retrieve mailbox message data into a buffer.
3354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003355 * This routine completes the processing of a received message by retrieving
3356 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003357 *
3358 * Alternatively, this routine can be used to dispose of a received message
3359 * without retrieving its data.
3360 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003361 * @param rx_msg Address of the receive message descriptor.
3362 * @param buffer Address of the buffer to receive data, or NULL to discard
3363 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003364 *
3365 * @return N/A
3366 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003367extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003368
3369/**
3370 * @brief Retrieve mailbox message data into a memory pool block.
3371 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003372 * This routine completes the processing of a received message by retrieving
3373 * its data into a memory pool block, then disposing of the message.
3374 * The memory pool block that results from successful retrieval must be
3375 * returned to the pool once the data has been processed, even in cases
3376 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003377 *
3378 * Alternatively, this routine can be used to dispose of a received message
3379 * without retrieving its data. In this case there is no need to return a
3380 * memory pool block to the pool.
3381 *
3382 * This routine allocates a new memory pool block for the data only if the
3383 * data is not already in one. If a new block cannot be allocated, the routine
3384 * returns a failure code and the received message is left unchanged. This
3385 * permits the caller to reattempt data retrieval at a later time or to dispose
3386 * of the received message without retrieving its data.
3387 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003388 * @param rx_msg Address of a receive message descriptor.
3389 * @param pool Address of memory pool, or NULL to discard data.
3390 * @param block Address of the area to hold memory pool block info.
3391 * @param timeout Waiting period to wait for a memory pool block (in
3392 * milliseconds), or one of the special values K_NO_WAIT
3393 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003394 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003395 * @retval 0 Data retrieved.
3396 * @retval -ENOMEM Returned without waiting.
3397 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003398 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003399extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003400 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003401 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003402
Anas Nashif166f5192018-02-25 08:02:36 -06003403/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003404
3405/**
3406 * @cond INTERNAL_HIDDEN
3407 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003408
Andrew Boie44fe8122018-04-12 17:38:12 -07003409#define K_PIPE_FLAG_ALLOC BIT(0) /* Buffer was allocated */
3410
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003411struct k_pipe {
3412 unsigned char *buffer; /* Pipe buffer: may be NULL */
3413 size_t size; /* Buffer size */
3414 size_t bytes_used; /* # bytes used in buffer */
3415 size_t read_index; /* Where in buffer to read from */
3416 size_t write_index; /* Where in buffer to write */
3417
3418 struct {
3419 _wait_q_t readers; /* Reader wait queue */
3420 _wait_q_t writers; /* Writer wait queue */
3421 } wait_q;
3422
Anas Nashif2f203c22016-12-18 06:57:45 -05003423 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Andrew Boie44fe8122018-04-12 17:38:12 -07003424 u8_t flags; /* Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003425};
3426
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003427#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003428 { \
3429 .buffer = pipe_buffer, \
3430 .size = pipe_buffer_size, \
3431 .bytes_used = 0, \
3432 .read_index = 0, \
3433 .write_index = 0, \
3434 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3435 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003436 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003437 }
3438
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003439#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3440
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003441/**
Allan Stephensc98da842016-11-11 15:45:03 -05003442 * INTERNAL_HIDDEN @endcond
3443 */
3444
3445/**
3446 * @defgroup pipe_apis Pipe APIs
3447 * @ingroup kernel_apis
3448 * @{
3449 */
3450
3451/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003452 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003453 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003454 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003455 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003456 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003457 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003458 * @param name Name of the pipe.
3459 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3460 * or zero if no ring buffer is used.
3461 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003462 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003463#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3464 static unsigned char __kernel_noinit __aligned(pipe_align) \
3465 _k_pipe_buf_##name[pipe_buffer_size]; \
3466 struct k_pipe name \
3467 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003468 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003469
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003470/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003471 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003472 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003473 * This routine initializes a pipe object, prior to its first use.
3474 *
3475 * @param pipe Address of the pipe.
3476 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3477 * is used.
3478 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3479 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003480 *
3481 * @return N/A
3482 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003483void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3484
3485/**
3486 * @brief Release a pipe's allocated buffer
3487 *
3488 * If a pipe object was given a dynamically allocated buffer via
3489 * k_pipe_alloc_init(), this will free it. This function does nothing
3490 * if the buffer wasn't dynamically allocated.
3491 *
3492 * @param pipe Address of the pipe.
3493 */
3494void k_pipe_cleanup(struct k_pipe *pipe);
3495
3496/**
3497 * @brief Initialize a pipe and allocate a buffer for it
3498 *
3499 * Storage for the buffer region will be allocated from the calling thread's
3500 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3501 * or userspace is enabled and the pipe object loses all references to it.
3502 *
3503 * This function should only be called on uninitialized pipe objects.
3504 *
3505 * @param pipe Address of the pipe.
3506 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3507 * buffer is used.
3508 * @retval 0 on success
3509 * @retval -ENOMEM if memory couln't be allocated
3510 */
3511__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003512
3513/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003514 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003515 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003516 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003517 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003518 * @param pipe Address of the pipe.
3519 * @param data Address of data to write.
3520 * @param bytes_to_write Size of data (in bytes).
3521 * @param bytes_written Address of area to hold the number of bytes written.
3522 * @param min_xfer Minimum number of bytes to write.
3523 * @param timeout Waiting period to wait for the data to be written (in
3524 * milliseconds), or one of the special values K_NO_WAIT
3525 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003526 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003527 * @retval 0 At least @a min_xfer bytes of data were written.
3528 * @retval -EIO Returned without waiting; zero data bytes were written.
3529 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003530 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003531 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003532__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3533 size_t bytes_to_write, size_t *bytes_written,
3534 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003535
3536/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003537 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003538 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003539 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003540 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003541 * @param pipe Address of the pipe.
3542 * @param data Address to place the data read from pipe.
3543 * @param bytes_to_read Maximum number of data bytes to read.
3544 * @param bytes_read Address of area to hold the number of bytes read.
3545 * @param min_xfer Minimum number of data bytes to read.
3546 * @param timeout Waiting period to wait for the data to be read (in
3547 * milliseconds), or one of the special values K_NO_WAIT
3548 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003549 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003550 * @retval 0 At least @a min_xfer bytes of data were read.
3551 * @retval -EIO Returned without waiting; zero data bytes were read.
3552 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003553 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003554 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003555__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3556 size_t bytes_to_read, size_t *bytes_read,
3557 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003558
3559/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003560 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003561 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003562 * This routine writes the data contained in a memory block to @a pipe.
3563 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003564 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003565 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003566 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003567 * @param block Memory block containing data to send
3568 * @param size Number of data bytes in memory block to send
3569 * @param sem Semaphore to signal upon completion (else NULL)
3570 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003571 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003572 */
3573extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3574 size_t size, struct k_sem *sem);
3575
Anas Nashif166f5192018-02-25 08:02:36 -06003576/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003577
Allan Stephensc98da842016-11-11 15:45:03 -05003578/**
3579 * @cond INTERNAL_HIDDEN
3580 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003581
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003582struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003583 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003584 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003585 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003586 char *buffer;
3587 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003588 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003589
Anas Nashif2f203c22016-12-18 06:57:45 -05003590 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003591};
3592
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003593#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003594 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003595 { \
3596 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003597 .num_blocks = slab_num_blocks, \
3598 .block_size = slab_block_size, \
3599 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003600 .free_list = NULL, \
3601 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003602 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003603 }
3604
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003605#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3606
3607
Peter Mitsis578f9112016-10-07 13:50:31 -04003608/**
Allan Stephensc98da842016-11-11 15:45:03 -05003609 * INTERNAL_HIDDEN @endcond
3610 */
3611
3612/**
3613 * @defgroup mem_slab_apis Memory Slab APIs
3614 * @ingroup kernel_apis
3615 * @{
3616 */
3617
3618/**
Allan Stephensda827222016-11-09 14:23:58 -06003619 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003620 *
Allan Stephensda827222016-11-09 14:23:58 -06003621 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003622 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003623 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3624 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003625 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003626 *
Allan Stephensda827222016-11-09 14:23:58 -06003627 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003628 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003629 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003630 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003631 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003632 * @param name Name of the memory slab.
3633 * @param slab_block_size Size of each memory block (in bytes).
3634 * @param slab_num_blocks Number memory blocks.
3635 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003636 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003637#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3638 char __noinit __aligned(slab_align) \
3639 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3640 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003641 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003642 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003643 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003644
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003645/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003646 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003647 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003648 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003649 *
Allan Stephensda827222016-11-09 14:23:58 -06003650 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3651 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3652 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3653 * To ensure that each memory block is similarly aligned to this boundary,
3654 * @a slab_block_size must also be a multiple of N.
3655 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003656 * @param slab Address of the memory slab.
3657 * @param buffer Pointer to buffer used for the memory blocks.
3658 * @param block_size Size of each memory block (in bytes).
3659 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003660 *
3661 * @return N/A
3662 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003663extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003664 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003665
3666/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003667 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003668 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003669 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003670 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003671 * @param slab Address of the memory slab.
3672 * @param mem Pointer to block address area.
3673 * @param timeout Maximum time to wait for operation to complete
3674 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3675 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003676 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003677 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003678 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003679 * @retval -ENOMEM Returned without waiting.
3680 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003681 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003682extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003683 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003684
3685/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003686 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003687 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003688 * This routine releases a previously allocated memory block back to its
3689 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003690 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003691 * @param slab Address of the memory slab.
3692 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003693 *
3694 * @return N/A
3695 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003696extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003697
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003698/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003699 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003700 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003701 * This routine gets the number of memory blocks that are currently
3702 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003703 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003704 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003705 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003706 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003707 */
Kumar Galacc334c72017-04-21 10:55:34 -05003708static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003709{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003710 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003711}
3712
Peter Mitsisc001aa82016-10-13 13:53:37 -04003713/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003714 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003716 * This routine gets the number of memory blocks that are currently
3717 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003718 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003719 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003720 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003721 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003722 */
Kumar Galacc334c72017-04-21 10:55:34 -05003723static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003724{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003725 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003726}
3727
Anas Nashif166f5192018-02-25 08:02:36 -06003728/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003729
3730/**
3731 * @cond INTERNAL_HIDDEN
3732 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003733
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003734struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003735 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003736 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003737};
3738
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003739/**
Allan Stephensc98da842016-11-11 15:45:03 -05003740 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003741 */
3742
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003743/**
Allan Stephensc98da842016-11-11 15:45:03 -05003744 * @addtogroup mem_pool_apis
3745 * @{
3746 */
3747
3748/**
3749 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003750 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003751 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3752 * long. The memory pool allows blocks to be repeatedly partitioned into
3753 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003754 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003755 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003756 * If the pool is to be accessed outside the module where it is defined, it
3757 * can be declared via
3758 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003759 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003760 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003761 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003762 * @param minsz Size of the smallest blocks in the pool (in bytes).
3763 * @param maxsz Size of the largest blocks in the pool (in bytes).
3764 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003765 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003766 */
Andy Ross73cb9582017-05-09 10:42:39 -07003767#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3768 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3769 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08003770 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07003771 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08003772 .base = { \
3773 .buf = _mpool_buf_##name, \
3774 .max_sz = maxsz, \
3775 .n_max = nmax, \
3776 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3777 .levels = _mpool_lvls_##name, \
3778 .flags = SYS_MEM_POOL_KERNEL \
3779 } \
Andy Ross73cb9582017-05-09 10:42:39 -07003780 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003781
Peter Mitsis937042c2016-10-13 13:18:26 -04003782/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003783 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003784 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003785 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003786 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003787 * @param pool Address of the memory pool.
3788 * @param block Pointer to block descriptor for the allocated memory.
3789 * @param size Amount of memory to allocate (in bytes).
3790 * @param timeout Maximum time to wait for operation to complete
3791 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3792 * or K_FOREVER to wait as long as necessary.
3793 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003794 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003795 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003796 * @retval -ENOMEM Returned without waiting.
3797 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003798 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003799extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003800 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003801
3802/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07003803 * @brief Allocate memory from a memory pool with malloc() semantics
3804 *
3805 * Such memory must be released using k_free().
3806 *
3807 * @param pool Address of the memory pool.
3808 * @param size Amount of memory to allocate (in bytes).
3809 * @return Address of the allocated memory if successful, otherwise NULL
3810 */
3811extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
3812
3813/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003814 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003815 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003816 * This routine releases a previously allocated memory block back to its
3817 * memory pool.
3818 *
3819 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003820 *
3821 * @return N/A
3822 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003823extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003824
3825/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003826 * @brief Free memory allocated from a memory pool.
3827 *
3828 * This routine releases a previously allocated memory block back to its
3829 * memory pool
3830 *
3831 * @param id Memory block identifier.
3832 *
3833 * @return N/A
3834 */
3835extern void k_mem_pool_free_id(struct k_mem_block_id *id);
3836
3837/**
Anas Nashif166f5192018-02-25 08:02:36 -06003838 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003839 */
3840
3841/**
3842 * @defgroup heap_apis Heap Memory Pool APIs
3843 * @ingroup kernel_apis
3844 * @{
3845 */
3846
3847/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003848 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003849 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003850 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003851 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003852 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003853 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003854 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003855 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003856 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003857extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003858
3859/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003860 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003861 *
3862 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07003863 * returned must have been allocated from the heap memory pool or
3864 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04003865 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003866 * If @a ptr is NULL, no operation is performed.
3867 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003868 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003869 *
3870 * @return N/A
3871 */
3872extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003873
Allan Stephensc98da842016-11-11 15:45:03 -05003874/**
Andrew Boie7f95e832017-11-08 14:40:01 -08003875 * @brief Allocate memory from heap, array style
3876 *
3877 * This routine provides traditional calloc() semantics. Memory is
3878 * allocated from the heap memory pool and zeroed.
3879 *
3880 * @param nmemb Number of elements in the requested array
3881 * @param size Size of each array element (in bytes).
3882 *
3883 * @return Address of the allocated memory if successful; otherwise NULL.
3884 */
3885extern void *k_calloc(size_t nmemb, size_t size);
3886
Anas Nashif166f5192018-02-25 08:02:36 -06003887/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003888
Benjamin Walshacc68c12017-01-29 18:57:45 -05003889/* polling API - PRIVATE */
3890
Benjamin Walshb0179862017-02-02 16:39:57 -05003891#ifdef CONFIG_POLL
3892#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3893#else
3894#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3895#endif
3896
Benjamin Walshacc68c12017-01-29 18:57:45 -05003897/* private - implementation data created as needed, per-type */
3898struct _poller {
3899 struct k_thread *thread;
3900};
3901
3902/* private - types bit positions */
3903enum _poll_types_bits {
3904 /* can be used to ignore an event */
3905 _POLL_TYPE_IGNORE,
3906
3907 /* to be signaled by k_poll_signal() */
3908 _POLL_TYPE_SIGNAL,
3909
3910 /* semaphore availability */
3911 _POLL_TYPE_SEM_AVAILABLE,
3912
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003913 /* queue/fifo/lifo data availability */
3914 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003915
3916 _POLL_NUM_TYPES
3917};
3918
3919#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3920
3921/* private - states bit positions */
3922enum _poll_states_bits {
3923 /* default state when creating event */
3924 _POLL_STATE_NOT_READY,
3925
Benjamin Walshacc68c12017-01-29 18:57:45 -05003926 /* signaled by k_poll_signal() */
3927 _POLL_STATE_SIGNALED,
3928
3929 /* semaphore is available */
3930 _POLL_STATE_SEM_AVAILABLE,
3931
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003932 /* data is available to read on queue/fifo/lifo */
3933 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003934
3935 _POLL_NUM_STATES
3936};
3937
3938#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3939
3940#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003941 (32 - (0 \
3942 + 8 /* tag */ \
3943 + _POLL_NUM_TYPES \
3944 + _POLL_NUM_STATES \
3945 + 1 /* modes */ \
3946 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003947
Benjamin Walshacc68c12017-01-29 18:57:45 -05003948/* end of polling API - PRIVATE */
3949
3950
3951/**
3952 * @defgroup poll_apis Async polling APIs
3953 * @ingroup kernel_apis
3954 * @{
3955 */
3956
3957/* Public polling API */
3958
3959/* public - values for k_poll_event.type bitfield */
3960#define K_POLL_TYPE_IGNORE 0
3961#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3962#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003963#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3964#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003965
3966/* public - polling modes */
3967enum k_poll_modes {
3968 /* polling thread does not take ownership of objects when available */
3969 K_POLL_MODE_NOTIFY_ONLY = 0,
3970
3971 K_POLL_NUM_MODES
3972};
3973
3974/* public - values for k_poll_event.state bitfield */
3975#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003976#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3977#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003978#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3979#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003980
3981/* public - poll signal object */
3982struct k_poll_signal {
3983 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003984 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003985
3986 /*
3987 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3988 * user resets it to 0.
3989 */
3990 unsigned int signaled;
3991
3992 /* custom result value passed to k_poll_signal() if needed */
3993 int result;
3994};
3995
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003996#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003997 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003998 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003999 .signaled = 0, \
4000 .result = 0, \
4001 }
4002
4003struct k_poll_event {
4004 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004005 sys_dnode_t _node;
4006
4007 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004008 struct _poller *poller;
4009
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004010 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004011 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004012
Benjamin Walshacc68c12017-01-29 18:57:45 -05004013 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004014 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004015
4016 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004017 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004018
4019 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004020 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004021
4022 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004023 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004024
4025 /* per-type data */
4026 union {
4027 void *obj;
4028 struct k_poll_signal *signal;
4029 struct k_sem *sem;
4030 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004031 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004032 };
4033};
4034
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004035#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004036 { \
4037 .poller = NULL, \
4038 .type = event_type, \
4039 .state = K_POLL_STATE_NOT_READY, \
4040 .mode = event_mode, \
4041 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004042 { .obj = event_obj }, \
4043 }
4044
4045#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4046 event_tag) \
4047 { \
4048 .type = event_type, \
4049 .tag = event_tag, \
4050 .state = K_POLL_STATE_NOT_READY, \
4051 .mode = event_mode, \
4052 .unused = 0, \
4053 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004054 }
4055
4056/**
4057 * @brief Initialize one struct k_poll_event instance
4058 *
4059 * After this routine is called on a poll event, the event it ready to be
4060 * placed in an event array to be passed to k_poll().
4061 *
4062 * @param event The event to initialize.
4063 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4064 * values. Only values that apply to the same object being polled
4065 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4066 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004067 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004068 * @param obj Kernel object or poll signal.
4069 *
4070 * @return N/A
4071 */
4072
Kumar Galacc334c72017-04-21 10:55:34 -05004073extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004074 int mode, void *obj);
4075
4076/**
4077 * @brief Wait for one or many of multiple poll events to occur
4078 *
4079 * This routine allows a thread to wait concurrently for one or many of
4080 * multiple poll events to have occurred. Such events can be a kernel object
4081 * being available, like a semaphore, or a poll signal event.
4082 *
4083 * When an event notifies that a kernel object is available, the kernel object
4084 * is not "given" to the thread calling k_poll(): it merely signals the fact
4085 * that the object was available when the k_poll() call was in effect. Also,
4086 * all threads trying to acquire an object the regular way, i.e. by pending on
4087 * the object, have precedence over the thread polling on the object. This
4088 * means that the polling thread will never get the poll event on an object
4089 * until the object becomes available and its pend queue is empty. For this
4090 * reason, the k_poll() call is more effective when the objects being polled
4091 * only have one thread, the polling thread, trying to acquire them.
4092 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004093 * When k_poll() returns 0, the caller should loop on all the events that were
4094 * passed to k_poll() and check the state field for the values that were
4095 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004096 *
4097 * Before being reused for another call to k_poll(), the user has to reset the
4098 * state field to K_POLL_STATE_NOT_READY.
4099 *
4100 * @param events An array of pointers to events to be polled for.
4101 * @param num_events The number of events in the array.
4102 * @param timeout Waiting period for an event to be ready (in milliseconds),
4103 * or one of the special values K_NO_WAIT and K_FOREVER.
4104 *
4105 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004106 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02004107 * @retval -EINTR Poller thread has been interrupted.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004108 */
4109
4110extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05004111 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004112
4113/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004114 * @brief Initialize a poll signal object.
4115 *
4116 * Ready a poll signal object to be signaled via k_poll_signal().
4117 *
4118 * @param signal A poll signal.
4119 *
4120 * @return N/A
4121 */
4122
4123extern void k_poll_signal_init(struct k_poll_signal *signal);
4124
4125/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004126 * @brief Signal a poll signal object.
4127 *
4128 * This routine makes ready a poll signal, which is basically a poll event of
4129 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4130 * made ready to run. A @a result value can be specified.
4131 *
4132 * The poll signal contains a 'signaled' field that, when set by
4133 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
4134 * be reset by the user before being passed again to k_poll() or k_poll() will
4135 * consider it being signaled, and will return immediately.
4136 *
4137 * @param signal A poll signal.
4138 * @param result The value to store in the result field of the signal.
4139 *
4140 * @retval 0 The signal was delivered successfully.
4141 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
4142 */
4143
4144extern int k_poll_signal(struct k_poll_signal *signal, int result);
4145
Anas Nashif954d5502018-02-25 08:37:28 -06004146/**
4147 * @internal
4148 */
Andy Ross8606fab2018-03-26 10:54:40 -07004149extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004150
Anas Nashif166f5192018-02-25 08:02:36 -06004151/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004152
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004153/**
4154 * @brief Make the CPU idle.
4155 *
4156 * This function makes the CPU idle until an event wakes it up.
4157 *
4158 * In a regular system, the idle thread should be the only thread responsible
4159 * for making the CPU idle and triggering any type of power management.
4160 * However, in some more constrained systems, such as a single-threaded system,
4161 * the only thread would be responsible for this if needed.
4162 *
4163 * @return N/A
4164 */
4165extern void k_cpu_idle(void);
4166
4167/**
4168 * @brief Make the CPU idle in an atomic fashion.
4169 *
4170 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4171 * must be done atomically before making the CPU idle.
4172 *
4173 * @param key Interrupt locking key obtained from irq_lock().
4174 *
4175 * @return N/A
4176 */
4177extern void k_cpu_atomic_idle(unsigned int key);
4178
Anas Nashif954d5502018-02-25 08:37:28 -06004179
4180/**
4181 * @internal
4182 */
Kumar Galacc334c72017-04-21 10:55:34 -05004183extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004184
Andrew Boiecdb94d62017-04-18 15:22:05 -07004185#ifdef _ARCH_EXCEPT
4186/* This archtecture has direct support for triggering a CPU exception */
4187#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4188#else
4189
Andrew Boiecdb94d62017-04-18 15:22:05 -07004190/* NOTE: This is the implementation for arches that do not implement
4191 * _ARCH_EXCEPT() to generate a real CPU exception.
4192 *
4193 * We won't have a real exception frame to determine the PC value when
4194 * the oops occurred, so print file and line number before we jump into
4195 * the fatal error handler.
4196 */
4197#define _k_except_reason(reason) do { \
4198 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4199 _NanoFatalErrorHandler(reason, &_default_esf); \
4200 CODE_UNREACHABLE; \
4201 } while (0)
4202
4203#endif /* _ARCH__EXCEPT */
4204
4205/**
4206 * @brief Fatally terminate a thread
4207 *
4208 * This should be called when a thread has encountered an unrecoverable
4209 * runtime condition and needs to terminate. What this ultimately
4210 * means is determined by the _fatal_error_handler() implementation, which
4211 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4212 *
4213 * If this is called from ISR context, the default system fatal error handler
4214 * will treat it as an unrecoverable system error, just like k_panic().
4215 */
4216#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4217
4218/**
4219 * @brief Fatally terminate the system
4220 *
4221 * This should be called when the Zephyr kernel has encountered an
4222 * unrecoverable runtime condition and needs to terminate. What this ultimately
4223 * means is determined by the _fatal_error_handler() implementation, which
4224 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4225 */
4226#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4227
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004228/*
4229 * private APIs that are utilized by one or more public APIs
4230 */
4231
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004232#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004233/**
4234 * @internal
4235 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004236extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004237#else
Anas Nashif954d5502018-02-25 08:37:28 -06004238/**
4239 * @internal
4240 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004241#define _init_static_threads() do { } while ((0))
4242#endif
4243
Anas Nashif954d5502018-02-25 08:37:28 -06004244/**
4245 * @internal
4246 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004247extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004248/**
4249 * @internal
4250 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004251extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004252
Andrew Boiedc5d9352017-06-02 12:56:47 -07004253/* arch/cpu.h may declare an architecture or platform-specific macro
4254 * for properly declaring stacks, compatible with MMU/MPU constraints if
4255 * enabled
4256 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004257
4258/**
4259 * @brief Obtain an extern reference to a stack
4260 *
4261 * This macro properly brings the symbol of a thread stack declared
4262 * elsewhere into scope.
4263 *
4264 * @param sym Thread stack symbol name
4265 */
4266#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4267
Andrew Boiedc5d9352017-06-02 12:56:47 -07004268#ifdef _ARCH_THREAD_STACK_DEFINE
4269#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4270#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4271 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4272#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4273#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004274static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004275{
4276 return _ARCH_THREAD_STACK_BUFFER(sym);
4277}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004278#else
4279/**
4280 * @brief Declare a toplevel thread stack memory region
4281 *
4282 * This declares a region of memory suitable for use as a thread's stack.
4283 *
4284 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4285 * 'noinit' section so that it isn't zeroed at boot
4286 *
Andrew Boie507852a2017-07-25 18:47:07 -07004287 * The declared symbol will always be a k_thread_stack_t which can be passed to
4288 * k_thread_create, but should otherwise not be manipulated. If the buffer
4289 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004290 *
4291 * It is legal to precede this definition with the 'static' keyword.
4292 *
4293 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4294 * parameter of k_thread_create(), it may not be the same as the
4295 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4296 *
4297 * @param sym Thread stack symbol name
4298 * @param size Size of the stack memory region
4299 */
4300#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004301 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004302
4303/**
4304 * @brief Declare a toplevel array of thread stack memory regions
4305 *
4306 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4307 * definition for additional details and constraints.
4308 *
4309 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4310 * 'noinit' section so that it isn't zeroed at boot
4311 *
4312 * @param sym Thread stack symbol name
4313 * @param nmemb Number of stacks to declare
4314 * @param size Size of the stack memory region
4315 */
4316
4317#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004318 struct _k_thread_stack_element __noinit \
4319 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004320
4321/**
4322 * @brief Declare an embedded stack memory region
4323 *
4324 * Used for stacks embedded within other data structures. Use is highly
4325 * discouraged but in some cases necessary. For memory protection scenarios,
4326 * it is very important that any RAM preceding this member not be writable
4327 * by threads else a stack overflow will lead to silent corruption. In other
4328 * words, the containing data structure should live in RAM owned by the kernel.
4329 *
4330 * @param sym Thread stack symbol name
4331 * @param size Size of the stack memory region
4332 */
4333#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004334 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004335
4336/**
4337 * @brief Return the size in bytes of a stack memory region
4338 *
4339 * Convenience macro for passing the desired stack size to k_thread_create()
4340 * since the underlying implementation may actually create something larger
4341 * (for instance a guard area).
4342 *
4343 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004344 * passed to K_THREAD_STACK_DEFINE.
4345 *
4346 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4347 * it is not guaranteed to return the original value since each array
4348 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004349 *
4350 * @param sym Stack memory symbol
4351 * @return Size of the stack
4352 */
4353#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4354
4355/**
4356 * @brief Get a pointer to the physical stack buffer
4357 *
4358 * Convenience macro to get at the real underlying stack buffer used by
4359 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4360 * This is really only intended for diagnostic tools which want to examine
4361 * stack memory contents.
4362 *
4363 * @param sym Declared stack symbol name
4364 * @return The buffer itself, a char *
4365 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004366static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004367{
4368 return (char *)sym;
4369}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004370
4371#endif /* _ARCH_DECLARE_STACK */
4372
Chunlin Hane9c97022017-07-07 20:29:30 +08004373/**
4374 * @defgroup mem_domain_apis Memory domain APIs
4375 * @ingroup kernel_apis
4376 * @{
4377 */
4378
4379/** @def MEM_PARTITION_ENTRY
4380 * @brief Used to declare a memory partition entry
4381 */
4382#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4383 {\
4384 .start = _start, \
4385 .size = _size, \
4386 .attr = _attr, \
4387 }
4388
4389/** @def K_MEM_PARTITION_DEFINE
4390 * @brief Used to declare a memory partition
4391 */
4392#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4393#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4394 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304395 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004396 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4397#else
4398#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304399 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004400 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4401#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4402
Chunlin Hane9c97022017-07-07 20:29:30 +08004403/* memory partition */
4404struct k_mem_partition {
4405 /* start address of memory partition */
4406 u32_t start;
4407 /* size of memory partition */
4408 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304409#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004410 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304411 k_mem_partition_attr_t attr;
4412#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004413};
4414
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304415/* memory domian
4416 * Note: Always declare this structure with __kernel prefix
4417 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004418struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304419#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004420 /* partitions in the domain */
4421 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304422#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004423 /* domain q */
4424 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004425 /* number of partitions in the domain */
4426 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004427};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304428
Chunlin Hane9c97022017-07-07 20:29:30 +08004429
4430/**
4431 * @brief Initialize a memory domain.
4432 *
4433 * Initialize a memory domain with given name and memory partitions.
4434 *
4435 * @param domain The memory domain to be initialized.
4436 * @param num_parts The number of array items of "parts" parameter.
4437 * @param parts An array of pointers to the memory partitions. Can be NULL
4438 * if num_parts is zero.
4439 */
4440
Leandro Pereira08de6582018-02-28 14:22:57 -08004441extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004442 struct k_mem_partition *parts[]);
4443/**
4444 * @brief Destroy a memory domain.
4445 *
4446 * Destroy a memory domain.
4447 *
4448 * @param domain The memory domain to be destroyed.
4449 */
4450
4451extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4452
4453/**
4454 * @brief Add a memory partition into a memory domain.
4455 *
4456 * Add a memory partition into a memory domain.
4457 *
4458 * @param domain The memory domain to be added a memory partition.
4459 * @param part The memory partition to be added
4460 */
4461
4462extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4463 struct k_mem_partition *part);
4464
4465/**
4466 * @brief Remove a memory partition from a memory domain.
4467 *
4468 * Remove a memory partition from a memory domain.
4469 *
4470 * @param domain The memory domain to be removed a memory partition.
4471 * @param part The memory partition to be removed
4472 */
4473
4474extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4475 struct k_mem_partition *part);
4476
4477/**
4478 * @brief Add a thread into a memory domain.
4479 *
4480 * Add a thread into a memory domain.
4481 *
4482 * @param domain The memory domain that the thread is going to be added into.
4483 * @param thread ID of thread going to be added into the memory domain.
4484 */
4485
4486extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4487 k_tid_t thread);
4488
4489/**
4490 * @brief Remove a thread from its memory domain.
4491 *
4492 * Remove a thread from its memory domain.
4493 *
4494 * @param thread ID of thread going to be removed from its memory domain.
4495 */
4496
4497extern void k_mem_domain_remove_thread(k_tid_t thread);
4498
Anas Nashif166f5192018-02-25 08:02:36 -06004499/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004500
Andrew Boie756f9072017-10-10 16:01:49 -07004501/**
4502 * @brief Emit a character buffer to the console device
4503 *
4504 * @param c String of characters to print
4505 * @param n The length of the string
4506 */
4507__syscall void k_str_out(char *c, size_t n);
4508
Andy Rosse7172672018-01-24 15:48:32 -08004509/**
4510 * @brief Start a numbered CPU on a MP-capable system
4511
4512 * This starts and initializes a specific CPU. The main thread on
4513 * startup is running on CPU zero, other processors are numbered
4514 * sequentially. On return from this function, the CPU is known to
4515 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004516 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004517 * with the provided key will work to enable them.
4518 *
4519 * Normally, in SMP mode this function will be called by the kernel
4520 * initialization and should not be used as a user API. But it is
4521 * defined here for special-purpose apps which want Zephyr running on
4522 * one core and to use others for design-specific processing.
4523 *
4524 * @param cpu_num Integer number of the CPU
4525 * @param stack Stack memory for the CPU
4526 * @param sz Stack buffer size, in bytes
4527 * @param fn Function to begin running on the CPU. First argument is
4528 * an irq_unlock() key.
4529 * @param arg Untyped argument to be passed to "fn"
4530 */
4531extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4532 void (*fn)(int, void *), void *arg);
4533
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004534#ifdef __cplusplus
4535}
4536#endif
4537
Andrew Boiee004dec2016-11-07 09:01:19 -08004538#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4539/*
4540 * Define new and delete operators.
4541 * At this moment, the operators do nothing since objects are supposed
4542 * to be statically allocated.
4543 */
4544inline void operator delete(void *ptr)
4545{
4546 (void)ptr;
4547}
4548
4549inline void operator delete[](void *ptr)
4550{
4551 (void)ptr;
4552}
4553
4554inline void *operator new(size_t size)
4555{
4556 (void)size;
4557 return NULL;
4558}
4559
4560inline void *operator new[](size_t size)
4561{
4562 (void)size;
4563 return NULL;
4564}
4565
4566/* Placement versions of operator new and delete */
4567inline void operator delete(void *ptr1, void *ptr2)
4568{
4569 (void)ptr1;
4570 (void)ptr2;
4571}
4572
4573inline void operator delete[](void *ptr1, void *ptr2)
4574{
4575 (void)ptr1;
4576 (void)ptr2;
4577}
4578
4579inline void *operator new(size_t size, void *ptr)
4580{
4581 (void)size;
4582 return ptr;
4583}
4584
4585inline void *operator new[](size_t size, void *ptr)
4586{
4587 (void)size;
4588 return ptr;
4589}
4590
4591#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4592
Andrew Boiefa94ee72017-09-28 16:54:35 -07004593#include <syscalls/kernel.h>
4594
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004595#endif /* !_ASMLANGUAGE */
4596
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004597#endif /* _kernel__h_ */