blob: c272efeed0ca89f0d860dbac91eb77ac2cefe17a [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 Boie945af952017-08-22 13:15:23 -0700190
191/**
192 * Lookup a kernel object and init its metadata if it exists
193 *
194 * Calling this on an object will make it usable from userspace.
195 * Intended to be called as the last statement in kernel object init
196 * functions.
197 *
198 * @param object Address of the kernel object
199 */
200void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700201#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700202
203#define K_THREAD_ACCESS_GRANT(thread, ...)
204
Anas Nashif954d5502018-02-25 08:37:28 -0600205/**
206 * @internal
207 */
Andrew Boie743e4682017-10-04 12:25:50 -0700208static inline void _k_object_init(void *obj)
209{
210 ARG_UNUSED(obj);
211}
212
Anas Nashif954d5502018-02-25 08:37:28 -0600213/**
214 * @internal
215 */
Andrew Boie743e4682017-10-04 12:25:50 -0700216static inline void _impl_k_object_access_grant(void *object,
217 struct k_thread *thread)
218{
219 ARG_UNUSED(object);
220 ARG_UNUSED(thread);
221}
222
Anas Nashif954d5502018-02-25 08:37:28 -0600223/**
224 * @internal
225 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700226static inline void k_object_access_revoke(void *object,
227 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700228{
229 ARG_UNUSED(object);
230 ARG_UNUSED(thread);
231}
232
Andrew Boiee9cfc542018-04-13 13:15:28 -0700233/**
234 * @internal
235 */
236static inline void _impl_k_object_release(void *object)
237{
238 ARG_UNUSED(object);
239}
240
Andrew Boie41bab6e2017-10-14 14:42:23 -0700241static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700242{
243 ARG_UNUSED(object);
244}
245#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700246
247/**
248 * grant a thread access to a kernel object
249 *
250 * The thread will be granted access to the object if the caller is from
251 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700252 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700253 *
254 * @param object Address of kernel object
255 * @param thread Thread to grant access to the object
256 */
Andrew Boie743e4682017-10-04 12:25:50 -0700257__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700258
Andrew Boiea89bf012017-10-09 14:47:55 -0700259/**
260 * grant a thread access to a kernel object
261 *
262 * The thread will lose access to the object if the caller is from
263 * supervisor mode, or the caller is from user mode AND has permissions
264 * on both the object and the thread whose access is being revoked.
265 *
266 * @param object Address of kernel object
267 * @param thread Thread to remove access to the object
268 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700269void k_object_access_revoke(void *object, struct k_thread *thread);
270
271
272__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700273
274/**
275 * grant all present and future threads access to an object
276 *
277 * If the caller is from supervisor mode, or the caller is from user mode and
278 * have sufficient permissions on the object, then that object will have
279 * permissions granted to it for *all* current and future threads running in
280 * the system, effectively becoming a public kernel object.
281 *
282 * Use of this API should be avoided on systems that are running untrusted code
283 * as it is possible for such code to derive the addresses of kernel objects
284 * and perform unwanted operations on them.
285 *
Andrew Boie04caa672017-10-13 13:57:07 -0700286 * It is not possible to revoke permissions on public objects; once public,
287 * any thread may use it.
288 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700289 * @param object Address of kernel object
290 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700291void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700292
Andrew Boie31bdfc02017-11-08 16:38:03 -0800293#ifdef CONFIG_DYNAMIC_OBJECTS
294/**
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
300 * for the object will be allocated out of the kernel's heap.
301 *
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 */
308void *k_object_alloc(enum k_objects otype);
309
310/**
311 * Free a kernel object previously allocated with k_object_alloc()
312 *
313 * This will return memory for a kernel object back to the system heap.
314 * Care must be exercised that the object will not be used during or after
315 * when this call is made.
316 *
317 * @param obj Pointer to the kernel object memory address.
318 */
319void k_object_free(void *obj);
320#endif /* CONFIG_DYNAMIC_OBJECTS */
321
Andrew Boiebca15da2017-10-15 14:17:48 -0700322/* Using typedef deliberately here, this is quite intended to be an opaque
323 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
324 *
325 * The purpose of this data type is to clearly distinguish between the
326 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
327 * buffer which composes the stack data actually used by the underlying
328 * thread; they cannot be used interchangably as some arches precede the
329 * stack buffer region with guard areas that trigger a MPU or MMU fault
330 * if written to.
331 *
332 * APIs that want to work with the buffer inside should continue to use
333 * char *.
334 *
335 * Stacks should always be created with K_THREAD_STACK_DEFINE().
336 */
337struct __packed _k_thread_stack_element {
338 char data;
339};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700340typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700341
Andrew Boie73abd322017-04-04 13:19:13 -0700342/* timeouts */
343
344struct _timeout;
345typedef void (*_timeout_func_t)(struct _timeout *t);
346
347struct _timeout {
348 sys_dnode_t node;
349 struct k_thread *thread;
350 sys_dlist_t *wait_q;
351 s32_t delta_ticks_from_prev;
352 _timeout_func_t func;
353};
354
355extern s32_t _timeout_remaining_get(struct _timeout *timeout);
356
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700357/**
358 * @typedef k_thread_entry_t
359 * @brief Thread entry point function type.
360 *
361 * A thread's entry point function is invoked when the thread starts executing.
362 * Up to 3 argument values can be passed to the function.
363 *
364 * The thread terminates execution permanently if the entry point function
365 * returns. The thread is responsible for releasing any shared resources
366 * it may own (such as mutexes and dynamically allocated memory), prior to
367 * returning.
368 *
369 * @param p1 First argument.
370 * @param p2 Second argument.
371 * @param p3 Third argument.
372 *
373 * @return N/A
374 */
375typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700376
377#ifdef CONFIG_THREAD_MONITOR
378struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700379 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700380 void *parameter1;
381 void *parameter2;
382 void *parameter3;
383};
384#endif
385
386/* can be used for creating 'dummy' threads, e.g. for pending on objects */
387struct _thread_base {
388
389 /* this thread's entry in a ready/wait queue */
390 sys_dnode_t k_q_node;
391
392 /* user facing 'thread options'; values defined in include/kernel.h */
393 u8_t user_options;
394
395 /* thread state */
396 u8_t thread_state;
397
398 /*
399 * scheduler lock count and thread priority
400 *
401 * These two fields control the preemptibility of a thread.
402 *
403 * When the scheduler is locked, sched_locked is decremented, which
404 * means that the scheduler is locked for values from 0xff to 0x01. A
405 * thread is coop if its prio is negative, thus 0x80 to 0xff when
406 * looked at the value as unsigned.
407 *
408 * By putting them end-to-end, this means that a thread is
409 * non-preemptible if the bundled value is greater than or equal to
410 * 0x0080.
411 */
412 union {
413 struct {
414#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
415 u8_t sched_locked;
416 s8_t prio;
417#else /* LITTLE and PDP */
418 s8_t prio;
419 u8_t sched_locked;
420#endif
421 };
422 u16_t preempt;
423 };
424
Andy Ross2724fd12018-01-29 14:55:20 -0800425#ifdef CONFIG_SMP
426 /* True for the per-CPU idle threads */
427 u8_t is_idle;
428
429 /* Non-zero when actively running on a CPU */
430 u8_t active;
431
432 /* CPU index on which thread was last run */
433 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700434
435 /* Recursive count of irq_lock() calls */
436 u8_t global_lock_count;
Andy Ross2724fd12018-01-29 14:55:20 -0800437#endif
438
Andrew Boie73abd322017-04-04 13:19:13 -0700439 /* data returned by APIs */
440 void *swap_data;
441
442#ifdef CONFIG_SYS_CLOCK_EXISTS
443 /* this thread's entry in a timeout queue */
444 struct _timeout timeout;
445#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700446};
447
448typedef struct _thread_base _thread_base_t;
449
450#if defined(CONFIG_THREAD_STACK_INFO)
451/* Contains the stack information of a thread */
452struct _thread_stack_info {
453 /* Stack Start */
454 u32_t start;
455 /* Stack Size */
456 u32_t size;
457};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700458
459typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700460#endif /* CONFIG_THREAD_STACK_INFO */
461
Chunlin Hane9c97022017-07-07 20:29:30 +0800462#if defined(CONFIG_USERSPACE)
463struct _mem_domain_info {
464 /* memory domain queue node */
465 sys_dnode_t mem_domain_q_node;
466 /* memory domain of the thread */
467 struct k_mem_domain *mem_domain;
468};
469
470#endif /* CONFIG_USERSPACE */
471
Andrew Boie73abd322017-04-04 13:19:13 -0700472struct k_thread {
473
474 struct _thread_base base;
475
476 /* defined by the architecture, but all archs need these */
477 struct _caller_saved caller_saved;
478 struct _callee_saved callee_saved;
479
480 /* static thread init data */
481 void *init_data;
482
483 /* abort function */
484 void (*fn_abort)(void);
485
486#if defined(CONFIG_THREAD_MONITOR)
487 /* thread entry and parameters description */
488 struct __thread_entry *entry;
489
490 /* next item in list of all threads */
491 struct k_thread *next_thread;
492#endif
493
494#ifdef CONFIG_THREAD_CUSTOM_DATA
495 /* crude thread-local storage */
496 void *custom_data;
497#endif
498
499#ifdef CONFIG_ERRNO
500 /* per-thread errno variable */
501 int errno_var;
502#endif
503
504#if defined(CONFIG_THREAD_STACK_INFO)
505 /* Stack Info */
506 struct _thread_stack_info stack_info;
507#endif /* CONFIG_THREAD_STACK_INFO */
508
Chunlin Hane9c97022017-07-07 20:29:30 +0800509#if defined(CONFIG_USERSPACE)
510 /* memory domain info of the thread */
511 struct _mem_domain_info mem_domain_info;
Andrew Boiebca15da2017-10-15 14:17:48 -0700512 /* Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700513 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800514#endif /* CONFIG_USERSPACE */
515
Andy Ross042d8ec2017-12-09 08:37:20 -0800516#if defined(CONFIG_USE_SWITCH)
517 /* When using __switch() a few previously arch-specific items
518 * become part of the core OS
519 */
520
521 /* _Swap() return value */
522 int swap_retval;
523
524 /* Context handle returned via _arch_switch() */
525 void *switch_handle;
526#endif
Andrew Boie92e5bd72018-04-12 17:12:15 -0700527 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800528
Andrew Boie73abd322017-04-04 13:19:13 -0700529 /* arch-specifics: must always be at the end */
530 struct _thread_arch arch;
531};
532
533typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400534typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700535#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400536
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400537enum execution_context_types {
538 K_ISR = 0,
539 K_COOP_THREAD,
540 K_PREEMPT_THREAD,
541};
542
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400543/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100544 * @defgroup profiling_apis Profiling APIs
545 * @ingroup kernel_apis
546 * @{
547 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530548typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
549 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100550
551/**
552 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
553 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700554 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100555 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
556 *
557 * CONFIG_MAIN_STACK_SIZE
558 * CONFIG_IDLE_STACK_SIZE
559 * CONFIG_ISR_STACK_SIZE
560 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
561 *
562 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
563 * produce output.
564 *
565 * @return N/A
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530566 *
567 * @deprecated This API is deprecated. Use k_thread_foreach().
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100568 */
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530569__deprecated extern void k_call_stacks_analyze(void);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100570
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530571/**
572 * @brief Iterate over all the threads in the system.
573 *
574 * This routine iterates over all the threads in the system and
575 * calls the user_cb function for each thread.
576 *
577 * @param user_cb Pointer to the user callback function.
578 * @param user_data Pointer to user data.
579 *
580 * @note CONFIG_THREAD_MONITOR must be set for this function
581 * to be effective. Also this API uses irq_lock to protect the
582 * _kernel.threads list which means creation of new threads and
583 * terminations of existing threads are blocked until this
584 * API returns.
585 *
586 * @return N/A
587 */
588extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
589
Anas Nashif166f5192018-02-25 08:02:36 -0600590/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100591
592/**
Allan Stephensc98da842016-11-11 15:45:03 -0500593 * @defgroup thread_apis Thread APIs
594 * @ingroup kernel_apis
595 * @{
596 */
597
Benjamin Walshed240f22017-01-22 13:05:08 -0500598#endif /* !_ASMLANGUAGE */
599
600
601/*
602 * Thread user options. May be needed by assembly code. Common part uses low
603 * bits, arch-specific use high bits.
604 */
605
606/* system thread that must not abort */
607#define K_ESSENTIAL (1 << 0)
608
609#if defined(CONFIG_FP_SHARING)
610/* thread uses floating point registers */
611#define K_FP_REGS (1 << 1)
612#endif
613
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700614/* This thread has dropped from supervisor mode to user mode and consequently
615 * has additional restrictions
616 */
617#define K_USER (1 << 2)
618
Andrew Boie47f8fd12017-10-05 11:11:02 -0700619/* Indicates that the thread being created should inherit all kernel object
620 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
621 * is not enabled.
622 */
623#define K_INHERIT_PERMS (1 << 3)
624
Benjamin Walshed240f22017-01-22 13:05:08 -0500625#ifdef CONFIG_X86
626/* x86 Bitmask definitions for threads user options */
627
628#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
629/* thread uses SSEx (and also FP) registers */
630#define K_SSE_REGS (1 << 7)
631#endif
632#endif
633
634/* end - thread options */
635
636#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400637/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700638 * @brief Create a thread.
639 *
640 * This routine initializes a thread, then schedules it for execution.
641 *
642 * The new thread may be scheduled for immediate execution or a delayed start.
643 * If the newly spawned thread does not have a delayed start the kernel
644 * scheduler may preempt the current thread to allow the new thread to
645 * execute.
646 *
647 * Thread options are architecture-specific, and can include K_ESSENTIAL,
648 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
649 * them using "|" (the logical OR operator).
650 *
651 * Historically, users often would use the beginning of the stack memory region
652 * to store the struct k_thread data, although corruption will occur if the
653 * stack overflows this region and stack protection features may not detect this
654 * situation.
655 *
656 * @param new_thread Pointer to uninitialized struct k_thread
657 * @param stack Pointer to the stack space.
658 * @param stack_size Stack size in bytes.
659 * @param entry Thread entry function.
660 * @param p1 1st entry point parameter.
661 * @param p2 2nd entry point parameter.
662 * @param p3 3rd entry point parameter.
663 * @param prio Thread priority.
664 * @param options Thread options.
665 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
666 *
667 * @return ID of new thread.
668 */
Andrew Boie662c3452017-10-02 10:51:18 -0700669__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700670 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700671 size_t stack_size,
672 k_thread_entry_t entry,
673 void *p1, void *p2, void *p3,
674 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700675
Andrew Boie3f091b52017-08-30 14:34:14 -0700676/**
677 * @brief Drop a thread's privileges permanently to user mode
678 *
679 * @param entry Function to start executing from
680 * @param p1 1st entry point parameter
681 * @param p2 2nd entry point parameter
682 * @param p3 3rd entry point parameter
683 */
684extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
685 void *p1, void *p2,
686 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700687
Andrew Boied26cf2d2017-03-30 13:07:02 -0700688/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700689 * @brief Grant a thread access to a NULL-terminated set of kernel objects
690 *
691 * This is a convenience function. For the provided thread, grant access to
692 * the remaining arguments, which must be pointers to kernel objects.
693 * The final argument must be a NULL.
694 *
695 * The thread object must be initialized (i.e. running). The objects don't
696 * need to be.
697 *
698 * @param thread Thread to grant access to objects
699 * @param ... NULL-terminated list of kernel object pointers
700 */
701extern void __attribute__((sentinel))
702 k_thread_access_grant(struct k_thread *thread, ...);
703
704/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700705 * @brief Assign a resource memory pool to a thread
706 *
707 * By default, threads have no resource pool assigned unless their parent
708 * thread has a resource pool, in which case it is inherited. Multiple
709 * threads may be assigned to the same memory pool.
710 *
711 * Changing a thread's resource pool will not migrate allocations from the
712 * previous pool.
713 *
714 * @param thread Target thread to assign a memory pool for resource requests,
715 * or NULL if the thread should no longer have a memory pool.
716 * @param pool Memory pool to use for resources.
717 */
718static inline void k_thread_resource_pool_assign(struct k_thread *thread,
719 struct k_mem_pool *pool)
720{
721 thread->resource_pool = pool;
722}
723
724#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
725/**
726 * @brief Assign the system heap as a thread's resource pool
727 *
728 * Similar to k_thread_resource_pool_assign(), but the thread will use
729 * the kernel heap to draw memory.
730 *
731 * Use with caution, as a malicious thread could perform DoS attacks on the
732 * kernel heap.
733 *
734 * @param thread Target thread to assign the system heap for resource requests
735 */
736void k_thread_system_pool_assign(struct k_thread *thread);
737#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
738
739/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500740 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400741 *
Allan Stephensc98da842016-11-11 15:45:03 -0500742 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500743 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400744 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500745 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400746 *
747 * @return N/A
748 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700749__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400750
751/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500752 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400753 *
754 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500755 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400756 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400757 * @return N/A
758 */
Kumar Galacc334c72017-04-21 10:55:34 -0500759extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400760
761/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500762 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400763 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500764 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400765 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500766 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400767 *
768 * @return N/A
769 */
Andrew Boie468190a2017-09-29 14:00:48 -0700770__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400771
772/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500773 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400774 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500775 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400776 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500777 * If @a thread is not currently sleeping, the routine has no effect.
778 *
779 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400780 *
781 * @return N/A
782 */
Andrew Boie468190a2017-09-29 14:00:48 -0700783__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400784
785/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500786 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400787 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500788 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400789 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700790__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400791
792/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500793 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400794 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500795 * This routine prevents @a thread from executing if it has not yet started
796 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400797 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500798 * @param thread ID of thread to cancel.
799 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700800 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500801 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700802 *
803 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400804 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700805__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400806
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400807/**
Allan Stephensc98da842016-11-11 15:45:03 -0500808 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400809 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500810 * This routine permanently stops execution of @a thread. The thread is taken
811 * off all kernel queues it is part of (i.e. the ready queue, the timeout
812 * queue, or a kernel object wait queue). However, any kernel resources the
813 * thread might currently own (such as mutexes or memory blocks) are not
814 * released. It is the responsibility of the caller of this routine to ensure
815 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500817 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400818 *
819 * @return N/A
820 */
Andrew Boie468190a2017-09-29 14:00:48 -0700821__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400822
Andrew Boie7d627c52017-08-30 11:01:56 -0700823
824/**
825 * @brief Start an inactive thread
826 *
827 * If a thread was created with K_FOREVER in the delay parameter, it will
828 * not be added to the scheduling queue until this function is called
829 * on it.
830 *
831 * @param thread thread to start
832 */
Andrew Boie468190a2017-09-29 14:00:48 -0700833__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700834
Allan Stephensc98da842016-11-11 15:45:03 -0500835/**
836 * @cond INTERNAL_HIDDEN
837 */
838
Benjamin Walshd211a522016-12-06 11:44:01 -0500839/* timeout has timed out and is not on _timeout_q anymore */
840#define _EXPIRED (-2)
841
842/* timeout is not in use */
843#define _INACTIVE (-1)
844
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400845struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700846 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700847 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400848 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700849 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500850 void *init_p1;
851 void *init_p2;
852 void *init_p3;
853 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500854 u32_t init_options;
855 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500856 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400857};
858
Andrew Boied26cf2d2017-03-30 13:07:02 -0700859#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400860 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500861 prio, options, delay, abort, groups) \
862 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700863 .init_thread = (thread), \
864 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500865 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700866 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400867 .init_p1 = (void *)p1, \
868 .init_p2 = (void *)p2, \
869 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500870 .init_prio = (prio), \
871 .init_options = (options), \
872 .init_delay = (delay), \
873 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400874 }
875
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400876/**
Allan Stephensc98da842016-11-11 15:45:03 -0500877 * INTERNAL_HIDDEN @endcond
878 */
879
880/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500881 * @brief Statically define and initialize a thread.
882 *
883 * The thread may be scheduled for immediate execution or a delayed start.
884 *
885 * Thread options are architecture-specific, and can include K_ESSENTIAL,
886 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
887 * them using "|" (the logical OR operator).
888 *
889 * The ID of the thread can be accessed using:
890 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500891 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500892 *
893 * @param name Name of the thread.
894 * @param stack_size Stack size in bytes.
895 * @param entry Thread entry function.
896 * @param p1 1st entry point parameter.
897 * @param p2 2nd entry point parameter.
898 * @param p3 3rd entry point parameter.
899 * @param prio Thread priority.
900 * @param options Thread options.
901 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400902 *
903 * @internal It has been observed that the x86 compiler by default aligns
904 * these _static_thread_data structures to 32-byte boundaries, thereby
905 * wasting space. To work around this, force a 4-byte alignment.
906 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500907#define K_THREAD_DEFINE(name, stack_size, \
908 entry, p1, p2, p3, \
909 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700910 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700911 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500912 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500913 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700914 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
915 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500916 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700917 NULL, 0); \
918 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400919
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400920/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500921 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400922 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500923 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400924 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500925 * @param thread ID of thread whose priority is needed.
926 *
927 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400928 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700929__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400930
931/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500932 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400933 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500934 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400935 *
936 * Rescheduling can occur immediately depending on the priority @a thread is
937 * set to:
938 *
939 * - If its priority is raised above the priority of the caller of this
940 * function, and the caller is preemptible, @a thread will be scheduled in.
941 *
942 * - If the caller operates on itself, it lowers its priority below that of
943 * other threads in the system, and the caller is preemptible, the thread of
944 * highest priority will be scheduled in.
945 *
946 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
947 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
948 * highest priority.
949 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500950 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400951 * @param prio New priority.
952 *
953 * @warning Changing the priority of a thread currently involved in mutex
954 * priority inheritance may result in undefined behavior.
955 *
956 * @return N/A
957 */
Andrew Boie468190a2017-09-29 14:00:48 -0700958__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400959
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400960/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500961 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400962 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500963 * This routine prevents the kernel scheduler from making @a thread the
964 * current thread. All other internal operations on @a thread are still
965 * performed; for example, any timeout it is waiting on keeps ticking,
966 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400967 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500968 * If @a thread is already suspended, the routine has no effect.
969 *
970 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400971 *
972 * @return N/A
973 */
Andrew Boie468190a2017-09-29 14:00:48 -0700974__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400975
976/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500977 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400978 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500979 * This routine allows the kernel scheduler to make @a thread the current
980 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400981 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500982 * If @a thread is not currently suspended, the routine has no effect.
983 *
984 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400985 *
986 * @return N/A
987 */
Andrew Boie468190a2017-09-29 14:00:48 -0700988__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400989
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400990/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500991 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500993 * This routine specifies how the scheduler will perform time slicing of
994 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400995 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500996 * To enable time slicing, @a slice must be non-zero. The scheduler
997 * ensures that no thread runs for more than the specified time limit
998 * before other threads of that priority are given a chance to execute.
999 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001000 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001001 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001002 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001003 * execute. Once the scheduler selects a thread for execution, there is no
1004 * minimum guaranteed time the thread will execute before threads of greater or
1005 * equal priority are scheduled.
1006 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001007 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001008 * for execution, this routine has no effect; the thread is immediately
1009 * rescheduled after the slice period expires.
1010 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001011 * To disable timeslicing, set both @a slice and @a prio to zero.
1012 *
1013 * @param slice Maximum time slice length (in milliseconds).
1014 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001015 *
1016 * @return N/A
1017 */
Kumar Galacc334c72017-04-21 10:55:34 -05001018extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001019
Anas Nashif166f5192018-02-25 08:02:36 -06001020/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001021
1022/**
1023 * @addtogroup isr_apis
1024 * @{
1025 */
1026
1027/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001028 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001029 *
Allan Stephensc98da842016-11-11 15:45:03 -05001030 * This routine allows the caller to customize its actions, depending on
1031 * whether it is a thread or an ISR.
1032 *
1033 * @note Can be called by ISRs.
1034 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001035 * @return 0 if invoked by a thread.
1036 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001037 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -05001038extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001039
Benjamin Walsh445830d2016-11-10 15:54:27 -05001040/**
1041 * @brief Determine if code is running in a preemptible thread.
1042 *
Allan Stephensc98da842016-11-11 15:45:03 -05001043 * This routine allows the caller to customize its actions, depending on
1044 * whether it can be preempted by another thread. The routine returns a 'true'
1045 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001046 *
Allan Stephensc98da842016-11-11 15:45:03 -05001047 * - The code is running in a thread, not at ISR.
1048 * - The thread's priority is in the preemptible range.
1049 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001050 *
Allan Stephensc98da842016-11-11 15:45:03 -05001051 * @note Can be called by ISRs.
1052 *
1053 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001054 * @return Non-zero if invoked by a preemptible thread.
1055 */
Andrew Boie468190a2017-09-29 14:00:48 -07001056__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001057
Allan Stephensc98da842016-11-11 15:45:03 -05001058/**
Anas Nashif166f5192018-02-25 08:02:36 -06001059 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001060 */
1061
1062/**
1063 * @addtogroup thread_apis
1064 * @{
1065 */
1066
1067/**
1068 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001069 *
Allan Stephensc98da842016-11-11 15:45:03 -05001070 * This routine prevents the current thread from being preempted by another
1071 * thread by instructing the scheduler to treat it as a cooperative thread.
1072 * If the thread subsequently performs an operation that makes it unready,
1073 * it will be context switched out in the normal manner. When the thread
1074 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001075 *
Allan Stephensc98da842016-11-11 15:45:03 -05001076 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001077 *
Allan Stephensc98da842016-11-11 15:45:03 -05001078 * @note k_sched_lock() and k_sched_unlock() should normally be used
1079 * when the operation being performed can be safely interrupted by ISRs.
1080 * However, if the amount of processing involved is very small, better
1081 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001082 *
1083 * @return N/A
1084 */
1085extern void k_sched_lock(void);
1086
Allan Stephensc98da842016-11-11 15:45:03 -05001087/**
1088 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001089 *
Allan Stephensc98da842016-11-11 15:45:03 -05001090 * This routine reverses the effect of a previous call to k_sched_lock().
1091 * A thread must call the routine once for each time it called k_sched_lock()
1092 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001093 *
1094 * @return N/A
1095 */
1096extern void k_sched_unlock(void);
1097
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001098/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001099 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001100 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001101 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001102 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001103 * Custom data is not used by the kernel itself, and is freely available
1104 * for a thread to use as it sees fit. It can be used as a framework
1105 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001106 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001107 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001108 *
1109 * @return N/A
1110 */
Andrew Boie468190a2017-09-29 14:00:48 -07001111__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001112
1113/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001114 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001115 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001116 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001117 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001118 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001119 */
Andrew Boie468190a2017-09-29 14:00:48 -07001120__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001121
1122/**
Anas Nashif166f5192018-02-25 08:02:36 -06001123 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001124 */
1125
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001126#include <sys_clock.h>
1127
Allan Stephensc2f15a42016-11-17 12:24:22 -05001128/**
1129 * @addtogroup clock_apis
1130 * @{
1131 */
1132
1133/**
1134 * @brief Generate null timeout delay.
1135 *
1136 * This macro generates a timeout delay that that instructs a kernel API
1137 * not to wait if the requested operation cannot be performed immediately.
1138 *
1139 * @return Timeout delay value.
1140 */
1141#define K_NO_WAIT 0
1142
1143/**
1144 * @brief Generate timeout delay from milliseconds.
1145 *
1146 * This macro generates a timeout delay that that instructs a kernel API
1147 * to wait up to @a ms milliseconds to perform the requested operation.
1148 *
1149 * @param ms Duration in milliseconds.
1150 *
1151 * @return Timeout delay value.
1152 */
Johan Hedberg14471692016-11-13 10:52:15 +02001153#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001154
1155/**
1156 * @brief Generate timeout delay from seconds.
1157 *
1158 * This macro generates a timeout delay that that instructs a kernel API
1159 * to wait up to @a s seconds to perform the requested operation.
1160 *
1161 * @param s Duration in seconds.
1162 *
1163 * @return Timeout delay value.
1164 */
Johan Hedberg14471692016-11-13 10:52:15 +02001165#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001166
1167/**
1168 * @brief Generate timeout delay from minutes.
1169 *
1170 * This macro generates a timeout delay that that instructs a kernel API
1171 * to wait up to @a m minutes to perform the requested operation.
1172 *
1173 * @param m Duration in minutes.
1174 *
1175 * @return Timeout delay value.
1176 */
Johan Hedberg14471692016-11-13 10:52:15 +02001177#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001178
1179/**
1180 * @brief Generate timeout delay from hours.
1181 *
1182 * This macro generates a timeout delay that that instructs a kernel API
1183 * to wait up to @a h hours to perform the requested operation.
1184 *
1185 * @param h Duration in hours.
1186 *
1187 * @return Timeout delay value.
1188 */
Johan Hedberg14471692016-11-13 10:52:15 +02001189#define K_HOURS(h) K_MINUTES((h) * 60)
1190
Allan Stephensc98da842016-11-11 15:45:03 -05001191/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001192 * @brief Generate infinite timeout delay.
1193 *
1194 * This macro generates a timeout delay that that instructs a kernel API
1195 * to wait as long as necessary to perform the requested operation.
1196 *
1197 * @return Timeout delay value.
1198 */
1199#define K_FOREVER (-1)
1200
1201/**
Anas Nashif166f5192018-02-25 08:02:36 -06001202 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001203 */
1204
1205/**
Allan Stephensc98da842016-11-11 15:45:03 -05001206 * @cond INTERNAL_HIDDEN
1207 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001208
Benjamin Walsh62092182016-12-20 14:39:08 -05001209/* kernel clocks */
1210
1211#if (sys_clock_ticks_per_sec == 1000) || \
1212 (sys_clock_ticks_per_sec == 500) || \
1213 (sys_clock_ticks_per_sec == 250) || \
1214 (sys_clock_ticks_per_sec == 125) || \
1215 (sys_clock_ticks_per_sec == 100) || \
1216 (sys_clock_ticks_per_sec == 50) || \
1217 (sys_clock_ticks_per_sec == 25) || \
1218 (sys_clock_ticks_per_sec == 20) || \
1219 (sys_clock_ticks_per_sec == 10) || \
1220 (sys_clock_ticks_per_sec == 1)
1221
1222 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1223#else
1224 /* yields horrible 64-bit math on many architectures: try to avoid */
1225 #define _NON_OPTIMIZED_TICKS_PER_SEC
1226#endif
1227
1228#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001229extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001230#else
Kumar Galacc334c72017-04-21 10:55:34 -05001231static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001232{
Kumar Galacc334c72017-04-21 10:55:34 -05001233 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001234}
1235#endif
1236
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001237/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001238#ifdef CONFIG_TICKLESS_KERNEL
1239#define _TICK_ALIGN 0
1240#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001241#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001242#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001243
Kumar Galacc334c72017-04-21 10:55:34 -05001244static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001245{
Benjamin Walsh62092182016-12-20 14:39:08 -05001246#ifdef CONFIG_SYS_CLOCK_EXISTS
1247
1248#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001249 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001250#else
Kumar Galacc334c72017-04-21 10:55:34 -05001251 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001252#endif
1253
1254#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001255 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001256 return 0;
1257#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001258}
1259
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001260struct k_timer {
1261 /*
1262 * _timeout structure must be first here if we want to use
1263 * dynamic timer allocation. timeout.node is used in the double-linked
1264 * list of free timers
1265 */
1266 struct _timeout timeout;
1267
Allan Stephens45bfa372016-10-12 12:39:42 -05001268 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001269 _wait_q_t wait_q;
1270
1271 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001272 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001273
1274 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001275 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001276
1277 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001278 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001279
Allan Stephens45bfa372016-10-12 12:39:42 -05001280 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001281 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001282
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001283 /* user-specific data, also used to support legacy features */
1284 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001285
Anas Nashif2f203c22016-12-18 06:57:45 -05001286 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001287};
1288
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001289#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001290 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001291 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001292 .timeout.wait_q = NULL, \
1293 .timeout.thread = NULL, \
1294 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001295 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001296 .expiry_fn = expiry, \
1297 .stop_fn = stop, \
1298 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001299 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001300 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001301 }
1302
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001303#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1304
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001305/**
Allan Stephensc98da842016-11-11 15:45:03 -05001306 * INTERNAL_HIDDEN @endcond
1307 */
1308
1309/**
1310 * @defgroup timer_apis Timer APIs
1311 * @ingroup kernel_apis
1312 * @{
1313 */
1314
1315/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001316 * @typedef k_timer_expiry_t
1317 * @brief Timer expiry function type.
1318 *
1319 * A timer's expiry function is executed by the system clock interrupt handler
1320 * each time the timer expires. The expiry function is optional, and is only
1321 * invoked if the timer has been initialized with one.
1322 *
1323 * @param timer Address of timer.
1324 *
1325 * @return N/A
1326 */
1327typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1328
1329/**
1330 * @typedef k_timer_stop_t
1331 * @brief Timer stop function type.
1332 *
1333 * A timer's stop function is executed if the timer is stopped prematurely.
1334 * The function runs in the context of the thread that stops the timer.
1335 * The stop function is optional, and is only invoked if the timer has been
1336 * initialized with one.
1337 *
1338 * @param timer Address of timer.
1339 *
1340 * @return N/A
1341 */
1342typedef void (*k_timer_stop_t)(struct k_timer *timer);
1343
1344/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001345 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001346 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001347 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001348 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001349 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001350 *
1351 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001352 * @param expiry_fn Function to invoke each time the timer expires.
1353 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001354 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001355#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001356 struct k_timer name \
1357 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001358 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001359
Allan Stephens45bfa372016-10-12 12:39:42 -05001360/**
1361 * @brief Initialize a timer.
1362 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001363 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001364 *
1365 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001366 * @param expiry_fn Function to invoke each time the timer expires.
1367 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001368 *
1369 * @return N/A
1370 */
1371extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001372 k_timer_expiry_t expiry_fn,
1373 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001374
Allan Stephens45bfa372016-10-12 12:39:42 -05001375/**
1376 * @brief Start a timer.
1377 *
1378 * This routine starts a timer, and resets its status to zero. The timer
1379 * begins counting down using the specified duration and period values.
1380 *
1381 * Attempting to start a timer that is already running is permitted.
1382 * The timer's status is reset to zero and the timer begins counting down
1383 * using the new duration and period values.
1384 *
1385 * @param timer Address of timer.
1386 * @param duration Initial timer duration (in milliseconds).
1387 * @param period Timer period (in milliseconds).
1388 *
1389 * @return N/A
1390 */
Andrew Boiea354d492017-09-29 16:22:28 -07001391__syscall void k_timer_start(struct k_timer *timer,
1392 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001393
1394/**
1395 * @brief Stop a timer.
1396 *
1397 * This routine stops a running timer prematurely. The timer's stop function,
1398 * if one exists, is invoked by the caller.
1399 *
1400 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001401 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001402 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001403 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1404 * if @a k_timer_stop is to be called from ISRs.
1405 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001406 * @param timer Address of timer.
1407 *
1408 * @return N/A
1409 */
Andrew Boiea354d492017-09-29 16:22:28 -07001410__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001411
1412/**
1413 * @brief Read timer status.
1414 *
1415 * This routine reads the timer's status, which indicates the number of times
1416 * it has expired since its status was last read.
1417 *
1418 * Calling this routine resets the timer's status to zero.
1419 *
1420 * @param timer Address of timer.
1421 *
1422 * @return Timer status.
1423 */
Andrew Boiea354d492017-09-29 16:22:28 -07001424__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001425
1426/**
1427 * @brief Synchronize thread to timer expiration.
1428 *
1429 * This routine blocks the calling thread until the timer's status is non-zero
1430 * (indicating that it has expired at least once since it was last examined)
1431 * or the timer is stopped. If the timer status is already non-zero,
1432 * or the timer is already stopped, the caller continues without waiting.
1433 *
1434 * Calling this routine resets the timer's status to zero.
1435 *
1436 * This routine must not be used by interrupt handlers, since they are not
1437 * allowed to block.
1438 *
1439 * @param timer Address of timer.
1440 *
1441 * @return Timer status.
1442 */
Andrew Boiea354d492017-09-29 16:22:28 -07001443__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001444
1445/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001446 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001447 *
1448 * This routine computes the (approximate) time remaining before a running
1449 * timer next expires. If the timer is not running, it returns zero.
1450 *
1451 * @param timer Address of timer.
1452 *
1453 * @return Remaining time (in milliseconds).
1454 */
Andrew Boiea354d492017-09-29 16:22:28 -07001455__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1456
1457static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001458{
1459 return _timeout_remaining_get(&timer->timeout);
1460}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001461
Allan Stephensc98da842016-11-11 15:45:03 -05001462/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001463 * @brief Associate user-specific data with a timer.
1464 *
1465 * This routine records the @a user_data with the @a timer, to be retrieved
1466 * later.
1467 *
1468 * It can be used e.g. in a timer handler shared across multiple subsystems to
1469 * retrieve data specific to the subsystem this timer is associated with.
1470 *
1471 * @param timer Address of timer.
1472 * @param user_data User data to associate with the timer.
1473 *
1474 * @return N/A
1475 */
Andrew Boiea354d492017-09-29 16:22:28 -07001476__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1477
Anas Nashif954d5502018-02-25 08:37:28 -06001478/**
1479 * @internal
1480 */
Andrew Boiea354d492017-09-29 16:22:28 -07001481static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1482 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001483{
1484 timer->user_data = user_data;
1485}
1486
1487/**
1488 * @brief Retrieve the user-specific data from a timer.
1489 *
1490 * @param timer Address of timer.
1491 *
1492 * @return The user data.
1493 */
Andrew Boiea354d492017-09-29 16:22:28 -07001494__syscall void *k_timer_user_data_get(struct k_timer *timer);
1495
1496static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001497{
1498 return timer->user_data;
1499}
1500
Anas Nashif166f5192018-02-25 08:02:36 -06001501/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001502
Allan Stephensc98da842016-11-11 15:45:03 -05001503/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001504 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001505 * @{
1506 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001507
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001508/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001509 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001510 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001511 * This routine returns the elapsed time since the system booted,
1512 * in milliseconds.
1513 *
1514 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001515 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001516__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001517
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001518/**
1519 * @brief Enable clock always on in tickless kernel
1520 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001521 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001522 * there are no timer events programmed in tickless kernel
1523 * scheduling. This is necessary if the clock is used to track
1524 * passage of time.
1525 *
1526 * @retval prev_status Previous status of always on flag
1527 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301528#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001529static inline int k_enable_sys_clock_always_on(void)
1530{
1531 int prev_status = _sys_clock_always_on;
1532
1533 _sys_clock_always_on = 1;
1534 _enable_sys_clock();
1535
1536 return prev_status;
1537}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301538#else
1539#define k_enable_sys_clock_always_on() do { } while ((0))
1540#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001541
1542/**
1543 * @brief Disable clock always on in tickless kernel
1544 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001545 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001546 * there are no timer events programmed in tickless kernel
1547 * scheduling. To save power, this routine should be called
1548 * immediately when clock is not used to track time.
1549 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301550#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001551static inline void k_disable_sys_clock_always_on(void)
1552{
1553 _sys_clock_always_on = 0;
1554}
1555#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001556#define k_disable_sys_clock_always_on() do { } while ((0))
1557#endif
1558
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001559/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001560 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001561 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001562 * This routine returns the lower 32-bits of the elapsed time since the system
1563 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001564 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001565 * This routine can be more efficient than k_uptime_get(), as it reduces the
1566 * need for interrupt locking and 64-bit math. However, the 32-bit result
1567 * cannot hold a system uptime time larger than approximately 50 days, so the
1568 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001569 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001570 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001571 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001572__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001573
1574/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001575 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001576 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001577 * This routine computes the elapsed time between the current system uptime
1578 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001579 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001580 * @param reftime Pointer to a reference time, which is updated to the current
1581 * uptime upon return.
1582 *
1583 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001584 */
Kumar Galacc334c72017-04-21 10:55:34 -05001585extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001586
1587/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001588 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001589 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001590 * This routine computes the elapsed time between the current system uptime
1591 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001592 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001593 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1594 * need for interrupt locking and 64-bit math. However, the 32-bit result
1595 * cannot hold an elapsed time larger than approximately 50 days, so the
1596 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001597 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001598 * @param reftime Pointer to a reference time, which is updated to the current
1599 * uptime upon return.
1600 *
1601 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001602 */
Kumar Galacc334c72017-04-21 10:55:34 -05001603extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001604
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001605/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001606 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001607 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001608 * This routine returns the current time, as measured by the system's hardware
1609 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001610 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001611 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001612 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001613#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001614
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001615/**
Anas Nashif166f5192018-02-25 08:02:36 -06001616 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001617 */
1618
Allan Stephensc98da842016-11-11 15:45:03 -05001619/**
1620 * @cond INTERNAL_HIDDEN
1621 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001622
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001623struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001624 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001625 union {
1626 _wait_q_t wait_q;
1627
1628 _POLL_EVENT;
1629 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001630
1631 _OBJECT_TRACING_NEXT_PTR(k_queue);
1632};
1633
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001634#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001635 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001636 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Michael Hope5f67a612018-03-17 12:44:40 +01001637 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001638 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001639 _OBJECT_TRACING_INIT \
1640 }
1641
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001642#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1643
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001644/**
1645 * INTERNAL_HIDDEN @endcond
1646 */
1647
1648/**
1649 * @defgroup queue_apis Queue APIs
1650 * @ingroup kernel_apis
1651 * @{
1652 */
1653
1654/**
1655 * @brief Initialize a queue.
1656 *
1657 * This routine initializes a queue object, prior to its first use.
1658 *
1659 * @param queue Address of the queue.
1660 *
1661 * @return N/A
1662 */
1663extern void k_queue_init(struct k_queue *queue);
1664
1665/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001666 * @brief Cancel waiting on a queue.
1667 *
1668 * This routine causes first thread pending on @a queue, if any, to
1669 * return from k_queue_get() call with NULL value (as if timeout expired).
1670 *
1671 * @note Can be called by ISRs.
1672 *
1673 * @param queue Address of the queue.
1674 *
1675 * @return N/A
1676 */
1677extern void k_queue_cancel_wait(struct k_queue *queue);
1678
1679/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001680 * @brief Append an element to the end of a queue.
1681 *
1682 * This routine appends a data item to @a queue. A queue data item must be
1683 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1684 * reserved for the kernel's use.
1685 *
1686 * @note Can be called by ISRs.
1687 *
1688 * @param queue Address of the queue.
1689 * @param data Address of the data item.
1690 *
1691 * @return N/A
1692 */
1693extern void k_queue_append(struct k_queue *queue, void *data);
1694
1695/**
1696 * @brief Prepend an element to a queue.
1697 *
1698 * This routine prepends a data item to @a queue. A queue data item must be
1699 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1700 * reserved for the kernel's use.
1701 *
1702 * @note Can be called by ISRs.
1703 *
1704 * @param queue Address of the queue.
1705 * @param data Address of the data item.
1706 *
1707 * @return N/A
1708 */
1709extern void k_queue_prepend(struct k_queue *queue, void *data);
1710
1711/**
1712 * @brief Inserts an element to a queue.
1713 *
1714 * This routine inserts a data item to @a queue after previous item. A queue
1715 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1716 * item are reserved for the kernel's use.
1717 *
1718 * @note Can be called by ISRs.
1719 *
1720 * @param queue Address of the queue.
1721 * @param prev Address of the previous data item.
1722 * @param data Address of the data item.
1723 *
1724 * @return N/A
1725 */
1726extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1727
1728/**
1729 * @brief Atomically append a list of elements to a queue.
1730 *
1731 * This routine adds a list of data items to @a queue in one operation.
1732 * The data items must be in a singly-linked list, with the first 32 bits
1733 * in each data item pointing to the next data item; the list must be
1734 * NULL-terminated.
1735 *
1736 * @note Can be called by ISRs.
1737 *
1738 * @param queue Address of the queue.
1739 * @param head Pointer to first node in singly-linked list.
1740 * @param tail Pointer to last node in singly-linked list.
1741 *
1742 * @return N/A
1743 */
1744extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1745
1746/**
1747 * @brief Atomically add a list of elements to a queue.
1748 *
1749 * This routine adds a list of data items to @a queue in one operation.
1750 * The data items must be in a singly-linked list implemented using a
1751 * sys_slist_t object. Upon completion, the original list is empty.
1752 *
1753 * @note Can be called by ISRs.
1754 *
1755 * @param queue Address of the queue.
1756 * @param list Pointer to sys_slist_t object.
1757 *
1758 * @return N/A
1759 */
1760extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1761
1762/**
1763 * @brief Get an element from a queue.
1764 *
1765 * This routine removes first data item from @a queue. The first 32 bits of the
1766 * data item are reserved for the kernel's use.
1767 *
1768 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1769 *
1770 * @param queue Address of the queue.
1771 * @param timeout Waiting period to obtain a data item (in milliseconds),
1772 * or one of the special values K_NO_WAIT and K_FOREVER.
1773 *
1774 * @return Address of the data item if successful; NULL if returned
1775 * without waiting, or waiting period timed out.
1776 */
Kumar Galacc334c72017-04-21 10:55:34 -05001777extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001778
1779/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001780 * @brief Remove an element from a queue.
1781 *
1782 * This routine removes data item from @a queue. The first 32 bits of the
1783 * data item are reserved for the kernel's use. Removing elements from k_queue
1784 * rely on sys_slist_find_and_remove which is not a constant time operation.
1785 *
1786 * @note Can be called by ISRs
1787 *
1788 * @param queue Address of the queue.
1789 * @param data Address of the data item.
1790 *
1791 * @return true if data item was removed
1792 */
1793static inline bool k_queue_remove(struct k_queue *queue, void *data)
1794{
1795 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1796}
1797
1798/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001799 * @brief Query a queue to see if it has data available.
1800 *
1801 * Note that the data might be already gone by the time this function returns
1802 * if other threads are also trying to read from the queue.
1803 *
1804 * @note Can be called by ISRs.
1805 *
1806 * @param queue Address of the queue.
1807 *
1808 * @return Non-zero if the queue is empty.
1809 * @return 0 if data is available.
1810 */
1811static inline int k_queue_is_empty(struct k_queue *queue)
1812{
1813 return (int)sys_slist_is_empty(&queue->data_q);
1814}
1815
1816/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001817 * @brief Peek element at the head of queue.
1818 *
1819 * Return element from the head of queue without removing it.
1820 *
1821 * @param queue Address of the queue.
1822 *
1823 * @return Head element, or NULL if queue is empty.
1824 */
1825static inline void *k_queue_peek_head(struct k_queue *queue)
1826{
1827 return sys_slist_peek_head(&queue->data_q);
1828}
1829
1830/**
1831 * @brief Peek element at the tail of queue.
1832 *
1833 * Return element from the tail of queue without removing it.
1834 *
1835 * @param queue Address of the queue.
1836 *
1837 * @return Tail element, or NULL if queue is empty.
1838 */
1839static inline void *k_queue_peek_tail(struct k_queue *queue)
1840{
1841 return sys_slist_peek_tail(&queue->data_q);
1842}
1843
1844/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001845 * @brief Statically define and initialize a queue.
1846 *
1847 * The queue can be accessed outside the module where it is defined using:
1848 *
1849 * @code extern struct k_queue <name>; @endcode
1850 *
1851 * @param name Name of the queue.
1852 */
1853#define K_QUEUE_DEFINE(name) \
1854 struct k_queue name \
1855 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001856 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001857
Anas Nashif166f5192018-02-25 08:02:36 -06001858/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001859
1860/**
1861 * @cond INTERNAL_HIDDEN
1862 */
1863
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001864struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001865 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001866};
1867
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001868#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001869 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001870 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001871 }
1872
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001873#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1874
Allan Stephensc98da842016-11-11 15:45:03 -05001875/**
1876 * INTERNAL_HIDDEN @endcond
1877 */
1878
1879/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001880 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05001881 * @ingroup kernel_apis
1882 * @{
1883 */
1884
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001885/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001886 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001887 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001888 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001889 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001890 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001891 *
1892 * @return N/A
1893 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001894#define k_fifo_init(fifo) \
1895 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001896
1897/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001898 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001899 *
1900 * This routine causes first thread pending on @a fifo, if any, to
1901 * return from k_fifo_get() call with NULL value (as if timeout
1902 * expired).
1903 *
1904 * @note Can be called by ISRs.
1905 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001906 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001907 *
1908 * @return N/A
1909 */
1910#define k_fifo_cancel_wait(fifo) \
1911 k_queue_cancel_wait((struct k_queue *) fifo)
1912
1913/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001914 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001915 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001916 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001917 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1918 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001919 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001920 * @note Can be called by ISRs.
1921 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001922 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001923 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001924 *
1925 * @return N/A
1926 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001927#define k_fifo_put(fifo, data) \
1928 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001929
1930/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001931 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001932 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001933 * This routine adds a list of data items to @a fifo in one operation.
1934 * The data items must be in a singly-linked list, with the first 32 bits
1935 * each data item pointing to the next data item; the list must be
1936 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001937 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001938 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001939 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001940 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001941 * @param head Pointer to first node in singly-linked list.
1942 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001943 *
1944 * @return N/A
1945 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001946#define k_fifo_put_list(fifo, head, tail) \
1947 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001948
1949/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001950 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001951 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001952 * This routine adds a list of data items to @a fifo in one operation.
1953 * The data items must be in a singly-linked list implemented using a
1954 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001955 * and must be re-initialized via sys_slist_init().
1956 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001957 * @note Can be called by ISRs.
1958 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001959 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001960 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001961 *
1962 * @return N/A
1963 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001964#define k_fifo_put_slist(fifo, list) \
1965 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001966
1967/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001968 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001969 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001970 * This routine removes a data item from @a fifo in a "first in, first out"
1971 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001972 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001973 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1974 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001975 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001976 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001977 * or one of the special values K_NO_WAIT and K_FOREVER.
1978 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001979 * @return Address of the data item if successful; NULL if returned
1980 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001981 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001982#define k_fifo_get(fifo, timeout) \
1983 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001984
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001985/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001986 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001987 *
1988 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06001989 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001990 *
1991 * @note Can be called by ISRs.
1992 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001993 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001994 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001995 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001996 * @return 0 if data is available.
1997 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001998#define k_fifo_is_empty(fifo) \
1999 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002000
2001/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002002 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002003 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002004 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302005 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002006 * on each iteration of processing, a head container will be peeked,
2007 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002008 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002009 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002010 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002011 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002012 * @return Head element, or NULL if the FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002013 */
2014#define k_fifo_peek_head(fifo) \
2015 k_queue_peek_head((struct k_queue *) fifo)
2016
2017/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002018 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002019 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002020 * Return element from the tail of FIFO queue (without removing it). A usecase
2021 * for this is if elements of the FIFO queue are themselves containers. Then
2022 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002023 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002024 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002025 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002026 * @return Tail element, or NULL if a FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002027 */
2028#define k_fifo_peek_tail(fifo) \
2029 k_queue_peek_tail((struct k_queue *) fifo)
2030
2031/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002032 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002033 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002034 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002035 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002036 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002037 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002038 * @param name Name of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002039 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002040#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002041 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002042 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002043 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002044
Anas Nashif166f5192018-02-25 08:02:36 -06002045/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002046
2047/**
2048 * @cond INTERNAL_HIDDEN
2049 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002050
2051struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002052 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002053};
2054
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002055#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002056 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002057 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002058 }
2059
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002060#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2061
Allan Stephensc98da842016-11-11 15:45:03 -05002062/**
2063 * INTERNAL_HIDDEN @endcond
2064 */
2065
2066/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002067 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002068 * @ingroup kernel_apis
2069 * @{
2070 */
2071
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002072/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002073 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002074 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002075 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002076 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002077 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002078 *
2079 * @return N/A
2080 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002081#define k_lifo_init(lifo) \
2082 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002083
2084/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002085 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002086 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002087 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002088 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2089 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002090 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002091 * @note Can be called by ISRs.
2092 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002093 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002094 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002095 *
2096 * @return N/A
2097 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002098#define k_lifo_put(lifo, data) \
2099 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002100
2101/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002102 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002103 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002104 * This routine removes a data item from @a lifo in a "last in, first out"
2105 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002106 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002107 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2108 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002109 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002110 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002111 * or one of the special values K_NO_WAIT and K_FOREVER.
2112 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002113 * @return Address of the data item if successful; NULL if returned
2114 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002115 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002116#define k_lifo_get(lifo, timeout) \
2117 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002118
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002119/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002120 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002121 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002122 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002123 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002124 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002125 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002126 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002127 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002128#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002129 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002130 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002131 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002132
Anas Nashif166f5192018-02-25 08:02:36 -06002133/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002134
2135/**
2136 * @cond INTERNAL_HIDDEN
2137 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002138
2139struct k_stack {
2140 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002141 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002142
Anas Nashif2f203c22016-12-18 06:57:45 -05002143 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002144};
2145
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002146#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002147 { \
2148 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2149 .base = stack_buffer, \
2150 .next = stack_buffer, \
2151 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002152 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002153 }
2154
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002155#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2156
Allan Stephensc98da842016-11-11 15:45:03 -05002157/**
2158 * INTERNAL_HIDDEN @endcond
2159 */
2160
2161/**
2162 * @defgroup stack_apis Stack APIs
2163 * @ingroup kernel_apis
2164 * @{
2165 */
2166
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002167/**
2168 * @brief Initialize a stack.
2169 *
2170 * This routine initializes a stack object, prior to its first use.
2171 *
2172 * @param stack Address of the stack.
2173 * @param buffer Address of array used to hold stacked values.
2174 * @param num_entries Maximum number of values that can be stacked.
2175 *
2176 * @return N/A
2177 */
Andrew Boiee8734462017-09-29 16:42:07 -07002178__syscall void k_stack_init(struct k_stack *stack,
Andrew Boie8e3e6d02017-10-12 14:01:25 -07002179 u32_t *buffer, unsigned int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002180
2181/**
2182 * @brief Push an element onto a stack.
2183 *
2184 * This routine adds a 32-bit value @a data to @a stack.
2185 *
2186 * @note Can be called by ISRs.
2187 *
2188 * @param stack Address of the stack.
2189 * @param data Value to push onto the stack.
2190 *
2191 * @return N/A
2192 */
Andrew Boiee8734462017-09-29 16:42:07 -07002193__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002194
2195/**
2196 * @brief Pop an element from a stack.
2197 *
2198 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2199 * manner and stores the value in @a data.
2200 *
2201 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2202 *
2203 * @param stack Address of the stack.
2204 * @param data Address of area to hold the value popped from the stack.
2205 * @param timeout Waiting period to obtain a value (in milliseconds),
2206 * or one of the special values K_NO_WAIT and K_FOREVER.
2207 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002208 * @retval 0 Element popped from stack.
2209 * @retval -EBUSY Returned without waiting.
2210 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002211 */
Andrew Boiee8734462017-09-29 16:42:07 -07002212__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002213
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002214/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002215 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002216 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002217 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002218 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002219 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002220 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002221 * @param name Name of the stack.
2222 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002223 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002224#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002225 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002226 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002227 struct k_stack name \
2228 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002229 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002230 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002231
Anas Nashif166f5192018-02-25 08:02:36 -06002232/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002233
Allan Stephens6bba9b02016-11-16 14:56:54 -05002234struct k_work;
2235
Allan Stephensc98da842016-11-11 15:45:03 -05002236/**
2237 * @defgroup workqueue_apis Workqueue Thread APIs
2238 * @ingroup kernel_apis
2239 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002240 */
2241
Allan Stephens6bba9b02016-11-16 14:56:54 -05002242/**
2243 * @typedef k_work_handler_t
2244 * @brief Work item handler function type.
2245 *
2246 * A work item's handler function is executed by a workqueue's thread
2247 * when the work item is processed by the workqueue.
2248 *
2249 * @param work Address of the work item.
2250 *
2251 * @return N/A
2252 */
2253typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002254
2255/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002256 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002257 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002258
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002259struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002260 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002261 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002262};
2263
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002264enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002265 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002266};
2267
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002268struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002269 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002270 k_work_handler_t handler;
2271 atomic_t flags[1];
2272};
2273
Allan Stephens6bba9b02016-11-16 14:56:54 -05002274struct k_delayed_work {
2275 struct k_work work;
2276 struct _timeout timeout;
2277 struct k_work_q *work_q;
2278};
2279
2280extern struct k_work_q k_sys_work_q;
2281
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002282/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002283 * INTERNAL_HIDDEN @endcond
2284 */
2285
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002286#define _K_WORK_INITIALIZER(work_handler) \
2287 { \
2288 ._reserved = NULL, \
2289 .handler = work_handler, \
2290 .flags = { 0 } \
2291 }
2292
2293#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2294
Allan Stephens6bba9b02016-11-16 14:56:54 -05002295/**
2296 * @brief Initialize a statically-defined work item.
2297 *
2298 * This macro can be used to initialize a statically-defined workqueue work
2299 * item, prior to its first use. For example,
2300 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002301 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002302 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002303 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002304 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002305 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002306#define K_WORK_DEFINE(work, work_handler) \
2307 struct k_work work \
2308 __in_section(_k_work, static, work) = \
2309 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002310
2311/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002312 * @brief Initialize a work item.
2313 *
2314 * This routine initializes a workqueue work item, prior to its first use.
2315 *
2316 * @param work Address of work item.
2317 * @param handler Function to invoke each time work item is processed.
2318 *
2319 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002320 */
2321static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2322{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002323 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002324 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002325 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002326}
2327
2328/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002329 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002330 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002331 * This routine submits work item @a work to be processed by workqueue
2332 * @a work_q. If the work item is already pending in the workqueue's queue
2333 * as a result of an earlier submission, this routine has no effect on the
2334 * work item. If the work item has already been processed, or is currently
2335 * being processed, its work is considered complete and the work item can be
2336 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002337 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002338 * @warning
2339 * A submitted work item must not be modified until it has been processed
2340 * by the workqueue.
2341 *
2342 * @note Can be called by ISRs.
2343 *
2344 * @param work_q Address of workqueue.
2345 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002346 *
2347 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002348 */
2349static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2350 struct k_work *work)
2351{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002352 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002353 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002354 }
2355}
2356
2357/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002358 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002359 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002360 * This routine indicates if work item @a work is pending in a workqueue's
2361 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002362 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002363 * @note Can be called by ISRs.
2364 *
2365 * @param work Address of work item.
2366 *
2367 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002368 */
2369static inline int k_work_pending(struct k_work *work)
2370{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002371 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002372}
2373
2374/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002375 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002376 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002377 * This routine starts workqueue @a work_q. The workqueue spawns its work
2378 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002379 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002380 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002381 * @param stack Pointer to work queue thread's stack space, as defined by
2382 * K_THREAD_STACK_DEFINE()
2383 * @param stack_size Size of the work queue thread's stack (in bytes), which
2384 * should either be the same constant passed to
2385 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002386 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002387 *
2388 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002389 */
Andrew Boie507852a2017-07-25 18:47:07 -07002390extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002391 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002392 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002393
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002394/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002395 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002396 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002397 * This routine initializes a workqueue delayed work item, prior to
2398 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002399 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002400 * @param work Address of delayed work item.
2401 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002402 *
2403 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002404 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002405extern void k_delayed_work_init(struct k_delayed_work *work,
2406 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002407
2408/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002409 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002410 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002411 * This routine schedules work item @a work to be processed by workqueue
2412 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002413 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002414 * Only when the countdown completes is the work item actually submitted to
2415 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002416 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002417 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002418 * counting down cancels the existing submission and restarts the
2419 * countdown using the new delay. Note that this behavior is
2420 * inherently subject to race conditions with the pre-existing
2421 * timeouts and work queue, so care must be taken to synchronize such
2422 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002423 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002424 * @warning
2425 * A delayed work item must not be modified until it has been processed
2426 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002427 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002428 * @note Can be called by ISRs.
2429 *
2430 * @param work_q Address of workqueue.
2431 * @param work Address of delayed work item.
2432 * @param delay Delay before submitting the work item (in milliseconds).
2433 *
2434 * @retval 0 Work item countdown started.
2435 * @retval -EINPROGRESS Work item is already pending.
2436 * @retval -EINVAL Work item is being processed or has completed its work.
2437 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002438 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002439extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2440 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002441 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002442
2443/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002444 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002445 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002446 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002447 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002448 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002449 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002450 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002451 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002452 * @param work Address of delayed work item.
2453 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002454 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002455 * @retval -EINPROGRESS Work item is already pending.
2456 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002457 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002458extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002459
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002460/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002461 * @brief Submit a work item to the system workqueue.
2462 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002463 * This routine submits work item @a work to be processed by the system
2464 * workqueue. If the work item is already pending in the workqueue's queue
2465 * as a result of an earlier submission, this routine has no effect on the
2466 * work item. If the work item has already been processed, or is currently
2467 * being processed, its work is considered complete and the work item can be
2468 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002469 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002470 * @warning
2471 * Work items submitted to the system workqueue should avoid using handlers
2472 * that block or yield since this may prevent the system workqueue from
2473 * processing other work items in a timely manner.
2474 *
2475 * @note Can be called by ISRs.
2476 *
2477 * @param work Address of work item.
2478 *
2479 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002480 */
2481static inline void k_work_submit(struct k_work *work)
2482{
2483 k_work_submit_to_queue(&k_sys_work_q, work);
2484}
2485
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002486/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002487 * @brief Submit a delayed work item to the system workqueue.
2488 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002489 * This routine schedules work item @a work to be processed by the system
2490 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002491 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002492 * Only when the countdown completes is the work item actually submitted to
2493 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002494 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002495 * Submitting a previously submitted delayed work item that is still
2496 * counting down cancels the existing submission and restarts the countdown
2497 * using the new delay. If the work item is currently pending on the
2498 * workqueue's queue because the countdown has completed it is too late to
2499 * resubmit the item, and resubmission fails without impacting the work item.
2500 * If the work item has already been processed, or is currently being processed,
2501 * its work is considered complete and the work item can be resubmitted.
2502 *
2503 * @warning
2504 * Work items submitted to the system workqueue should avoid using handlers
2505 * that block or yield since this may prevent the system workqueue from
2506 * processing other work items in a timely manner.
2507 *
2508 * @note Can be called by ISRs.
2509 *
2510 * @param work Address of delayed work item.
2511 * @param delay Delay before submitting the work item (in milliseconds).
2512 *
2513 * @retval 0 Work item countdown started.
2514 * @retval -EINPROGRESS Work item is already pending.
2515 * @retval -EINVAL Work item is being processed or has completed its work.
2516 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002517 */
2518static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002519 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002520{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002521 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002522}
2523
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002524/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002525 * @brief Get time remaining before a delayed work gets scheduled.
2526 *
2527 * This routine computes the (approximate) time remaining before a
2528 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002529 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002530 *
2531 * @param work Delayed work item.
2532 *
2533 * @return Remaining time (in milliseconds).
2534 */
Kumar Galacc334c72017-04-21 10:55:34 -05002535static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002536{
2537 return _timeout_remaining_get(&work->timeout);
2538}
2539
Anas Nashif166f5192018-02-25 08:02:36 -06002540/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002541
Allan Stephensc98da842016-11-11 15:45:03 -05002542/**
2543 * @cond INTERNAL_HIDDEN
2544 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002545
2546struct k_mutex {
2547 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002548 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002549 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002550 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002551
Anas Nashif2f203c22016-12-18 06:57:45 -05002552 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002553};
2554
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002555#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002556 { \
2557 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2558 .owner = NULL, \
2559 .lock_count = 0, \
2560 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002561 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002562 }
2563
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002564#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2565
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002566/**
Allan Stephensc98da842016-11-11 15:45:03 -05002567 * INTERNAL_HIDDEN @endcond
2568 */
2569
2570/**
2571 * @defgroup mutex_apis Mutex APIs
2572 * @ingroup kernel_apis
2573 * @{
2574 */
2575
2576/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002577 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002578 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002579 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002580 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002581 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002582 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002583 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002584 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002585#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002586 struct k_mutex name \
2587 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002588 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002589
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002590/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002591 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002592 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002593 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002594 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002595 * Upon completion, the mutex is available and does not have an owner.
2596 *
2597 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002598 *
2599 * @return N/A
2600 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002601__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002602
2603/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002604 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002605 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002606 * This routine locks @a mutex. If the mutex is locked by another thread,
2607 * the calling thread waits until the mutex becomes available or until
2608 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002609 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002610 * A thread is permitted to lock a mutex it has already locked. The operation
2611 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002612 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002613 * @param mutex Address of the mutex.
2614 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002615 * or one of the special values K_NO_WAIT and K_FOREVER.
2616 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002617 * @retval 0 Mutex locked.
2618 * @retval -EBUSY Returned without waiting.
2619 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002620 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002621__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002622
2623/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002624 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002626 * This routine unlocks @a mutex. The mutex must already be locked by the
2627 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002628 *
2629 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002630 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002631 * thread.
2632 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002633 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002634 *
2635 * @return N/A
2636 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002637__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002638
Allan Stephensc98da842016-11-11 15:45:03 -05002639/**
Anas Nashif166f5192018-02-25 08:02:36 -06002640 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002641 */
2642
2643/**
2644 * @cond INTERNAL_HIDDEN
2645 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002646
2647struct k_sem {
2648 _wait_q_t wait_q;
2649 unsigned int count;
2650 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002651 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002652
Anas Nashif2f203c22016-12-18 06:57:45 -05002653 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002654};
2655
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002656#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002657 { \
2658 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2659 .count = initial_count, \
2660 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002661 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002662 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002663 }
2664
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002665#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2666
Allan Stephensc98da842016-11-11 15:45:03 -05002667/**
2668 * INTERNAL_HIDDEN @endcond
2669 */
2670
2671/**
2672 * @defgroup semaphore_apis Semaphore APIs
2673 * @ingroup kernel_apis
2674 * @{
2675 */
2676
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002677/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002678 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002679 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002680 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002681 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002682 * @param sem Address of the semaphore.
2683 * @param initial_count Initial semaphore count.
2684 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002685 *
2686 * @return N/A
2687 */
Andrew Boie99280232017-09-29 14:17:47 -07002688__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2689 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002690
2691/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002692 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002694 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002695 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002696 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2697 *
2698 * @param sem Address of the semaphore.
2699 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002700 * or one of the special values K_NO_WAIT and K_FOREVER.
2701 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002702 * @note When porting code from the nanokernel legacy API to the new API, be
2703 * careful with the return value of this function. The return value is the
2704 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2705 * non-zero means failure, while the nano_sem_take family returns 1 for success
2706 * and 0 for failure.
2707 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002708 * @retval 0 Semaphore taken.
2709 * @retval -EBUSY Returned without waiting.
2710 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002711 */
Andrew Boie99280232017-09-29 14:17:47 -07002712__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002713
2714/**
2715 * @brief Give a semaphore.
2716 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002717 * This routine gives @a sem, unless the semaphore is already at its maximum
2718 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002719 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002720 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002722 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002723 *
2724 * @return N/A
2725 */
Andrew Boie99280232017-09-29 14:17:47 -07002726__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002727
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002728/**
2729 * @brief Reset a semaphore's count to zero.
2730 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002731 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002732 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002733 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002734 *
2735 * @return N/A
2736 */
Andrew Boie990bf162017-10-03 12:36:49 -07002737__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002738
Anas Nashif954d5502018-02-25 08:37:28 -06002739/**
2740 * @internal
2741 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002742static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002743{
2744 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002745}
2746
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002747/**
2748 * @brief Get a semaphore's count.
2749 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002750 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002751 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002752 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002753 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002754 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002755 */
Andrew Boie990bf162017-10-03 12:36:49 -07002756__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002757
Anas Nashif954d5502018-02-25 08:37:28 -06002758/**
2759 * @internal
2760 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002761static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002762{
2763 return sem->count;
2764}
2765
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002766/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002767 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002768 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002769 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002770 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002771 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002772 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002773 * @param name Name of the semaphore.
2774 * @param initial_count Initial semaphore count.
2775 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002776 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002777#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002778 struct k_sem name \
2779 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07002780 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05302781 BUILD_ASSERT(((count_limit) != 0) && \
2782 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002783
Anas Nashif166f5192018-02-25 08:02:36 -06002784/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002785
2786/**
2787 * @defgroup alert_apis Alert APIs
2788 * @ingroup kernel_apis
2789 * @{
2790 */
2791
Allan Stephens5eceb852016-11-16 10:16:30 -05002792/**
2793 * @typedef k_alert_handler_t
2794 * @brief Alert handler function type.
2795 *
2796 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002797 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002798 * and is only invoked if the alert has been initialized with one.
2799 *
2800 * @param alert Address of the alert.
2801 *
2802 * @return 0 if alert has been consumed; non-zero if alert should pend.
2803 */
2804typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002805
Anas Nashif166f5192018-02-25 08:02:36 -06002806/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002807
2808/**
2809 * @cond INTERNAL_HIDDEN
2810 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002811
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002812#define K_ALERT_DEFAULT NULL
2813#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002814
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002815struct k_alert {
2816 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002817 atomic_t send_count;
2818 struct k_work work_item;
2819 struct k_sem sem;
2820
Anas Nashif2f203c22016-12-18 06:57:45 -05002821 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002822};
2823
Anas Nashif954d5502018-02-25 08:37:28 -06002824/**
2825 * @internal
2826 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002827extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002828
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002829#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002830 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002831 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002832 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002833 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2834 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002835 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002836 }
2837
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002838#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2839
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002840/**
Allan Stephensc98da842016-11-11 15:45:03 -05002841 * INTERNAL_HIDDEN @endcond
2842 */
2843
2844/**
2845 * @addtogroup alert_apis
2846 * @{
2847 */
2848
2849/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002850 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002851 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002852 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002853 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002854 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002856 * @param name Name of the alert.
2857 * @param alert_handler Action to take when alert is sent. Specify either
2858 * the address of a function to be invoked by the system workqueue
2859 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2860 * K_ALERT_DEFAULT (which causes the alert to pend).
2861 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002862 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002863#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002864 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002865 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002866 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002867 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002868
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002869/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002870 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002871 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002872 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002873 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002874 * @param alert Address of the alert.
2875 * @param handler Action to take when alert is sent. Specify either the address
2876 * of a function to be invoked by the system workqueue thread,
2877 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2878 * K_ALERT_DEFAULT (which causes the alert to pend).
2879 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002880 *
2881 * @return N/A
2882 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002883extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2884 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002885
2886/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002887 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002888 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002889 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002890 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002891 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2892 *
2893 * @param alert Address of the alert.
2894 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002895 * or one of the special values K_NO_WAIT and K_FOREVER.
2896 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002897 * @retval 0 Alert received.
2898 * @retval -EBUSY Returned without waiting.
2899 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002900 */
Andrew Boie310e9872017-09-29 04:41:15 -07002901__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002902
2903/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002904 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002905 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002906 * This routine signals @a alert. The action specified for @a alert will
2907 * be taken, which may trigger the execution of an alert handler function
2908 * and/or cause the alert to pend (assuming the alert has not reached its
2909 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002910 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002911 * @note Can be called by ISRs.
2912 *
2913 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002914 *
2915 * @return N/A
2916 */
Andrew Boie310e9872017-09-29 04:41:15 -07002917__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002918
2919/**
Anas Nashif166f5192018-02-25 08:02:36 -06002920 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002921 */
2922
Allan Stephensc98da842016-11-11 15:45:03 -05002923/**
2924 * @cond INTERNAL_HIDDEN
2925 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002926
2927struct k_msgq {
2928 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002929 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002930 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002931 char *buffer_start;
2932 char *buffer_end;
2933 char *read_ptr;
2934 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002935 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002936
Anas Nashif2f203c22016-12-18 06:57:45 -05002937 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002938};
2939
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002940#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002941 { \
2942 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002943 .max_msgs = q_max_msgs, \
2944 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002945 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002946 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002947 .read_ptr = q_buffer, \
2948 .write_ptr = q_buffer, \
2949 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002950 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002951 }
2952
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002953#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2954
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05302955struct k_msgq_attrs {
2956 size_t msg_size;
2957 u32_t max_msgs;
2958 u32_t used_msgs;
2959};
2960
Peter Mitsis1da807e2016-10-06 11:36:59 -04002961/**
Allan Stephensc98da842016-11-11 15:45:03 -05002962 * INTERNAL_HIDDEN @endcond
2963 */
2964
2965/**
2966 * @defgroup msgq_apis Message Queue APIs
2967 * @ingroup kernel_apis
2968 * @{
2969 */
2970
2971/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002972 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002973 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002974 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2975 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002976 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2977 * message is similarly aligned to this boundary, @a q_msg_size must also be
2978 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002979 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002980 * The message queue can be accessed outside the module where it is defined
2981 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002982 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002983 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002984 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002985 * @param q_name Name of the message queue.
2986 * @param q_msg_size Message size (in bytes).
2987 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002988 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002989 */
2990#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2991 static char __noinit __aligned(q_align) \
2992 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002993 struct k_msgq q_name \
2994 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002995 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002996 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002997
Peter Mitsisd7a37502016-10-13 11:37:40 -04002998/**
2999 * @brief Initialize a message queue.
3000 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003001 * This routine initializes a message queue object, prior to its first use.
3002 *
Allan Stephensda827222016-11-09 14:23:58 -06003003 * The message queue's ring buffer must contain space for @a max_msgs messages,
3004 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3005 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3006 * that each message is similarly aligned to this boundary, @a q_msg_size
3007 * must also be a multiple of N.
3008 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003009 * @param q Address of the message queue.
3010 * @param buffer Pointer to ring buffer that holds queued messages.
3011 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003012 * @param max_msgs Maximum number of messages that can be queued.
3013 *
3014 * @return N/A
3015 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003016__syscall void k_msgq_init(struct k_msgq *q, char *buffer,
3017 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003018
3019/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003020 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003021 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003022 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003023 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003024 * @note Can be called by ISRs.
3025 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003026 * @param q Address of the message queue.
3027 * @param data Pointer to the message.
3028 * @param timeout Waiting period to add the message (in milliseconds),
3029 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003030 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003031 * @retval 0 Message sent.
3032 * @retval -ENOMSG Returned without waiting or queue purged.
3033 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003034 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003035__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003036
3037/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003038 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003039 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003040 * This routine receives a message from message queue @a q in a "first in,
3041 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003042 *
Allan Stephensc98da842016-11-11 15:45:03 -05003043 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003044 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003045 * @param q Address of the message queue.
3046 * @param data Address of area to hold the received message.
3047 * @param timeout Waiting period to receive the message (in milliseconds),
3048 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003049 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003050 * @retval 0 Message received.
3051 * @retval -ENOMSG Returned without waiting.
3052 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003053 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003054__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003055
3056/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003057 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003058 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003059 * This routine discards all unreceived messages in a message queue's ring
3060 * buffer. Any threads that are blocked waiting to send a message to the
3061 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003062 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003063 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003064 *
3065 * @return N/A
3066 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003067__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003068
Peter Mitsis67be2492016-10-07 11:44:34 -04003069/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003070 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003071 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003072 * This routine returns the number of unused entries in a message queue's
3073 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003074 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003075 * @param q Address of the message queue.
3076 *
3077 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04003078 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003079__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3080
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303081/**
3082 * @brief Get basic attributes of a message queue.
3083 *
3084 * This routine fetches basic attributes of message queue into attr argument.
3085 *
3086 * @param q Address of the message queue.
3087 * @param attrs pointer to message queue attribute structure.
3088 *
3089 * @return N/A
3090 */
3091__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3092
3093
Andrew Boie82edb6e2017-10-02 10:53:06 -07003094static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003095{
3096 return q->max_msgs - q->used_msgs;
3097}
3098
Peter Mitsisd7a37502016-10-13 11:37:40 -04003099/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003100 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003101 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003102 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003103 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003104 * @param q Address of the message queue.
3105 *
3106 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003107 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003108__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3109
3110static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003111{
3112 return q->used_msgs;
3113}
3114
Anas Nashif166f5192018-02-25 08:02:36 -06003115/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003116
3117/**
3118 * @defgroup mem_pool_apis Memory Pool APIs
3119 * @ingroup kernel_apis
3120 * @{
3121 */
3122
Andy Ross73cb9582017-05-09 10:42:39 -07003123/* Note on sizing: the use of a 20 bit field for block means that,
3124 * assuming a reasonable minimum block size of 16 bytes, we're limited
3125 * to 16M of memory managed by a single pool. Long term it would be
3126 * good to move to a variable bit size based on configuration.
3127 */
3128struct k_mem_block_id {
3129 u32_t pool : 8;
3130 u32_t level : 4;
3131 u32_t block : 20;
3132};
3133
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003134struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003135 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003136 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003137};
3138
Anas Nashif166f5192018-02-25 08:02:36 -06003139/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003140
3141/**
3142 * @defgroup mailbox_apis Mailbox APIs
3143 * @ingroup kernel_apis
3144 * @{
3145 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003146
3147struct k_mbox_msg {
3148 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003149 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003150 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003151 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003152 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003153 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003154 /** sender's message data buffer */
3155 void *tx_data;
3156 /** internal use only - needed for legacy API support */
3157 void *_rx_data;
3158 /** message data block descriptor */
3159 struct k_mem_block tx_block;
3160 /** source thread id */
3161 k_tid_t rx_source_thread;
3162 /** target thread id */
3163 k_tid_t tx_target_thread;
3164 /** internal use only - thread waiting on send (may be a dummy) */
3165 k_tid_t _syncing_thread;
3166#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3167 /** internal use only - semaphore used during asynchronous send */
3168 struct k_sem *_async_sem;
3169#endif
3170};
3171
Allan Stephensc98da842016-11-11 15:45:03 -05003172/**
3173 * @cond INTERNAL_HIDDEN
3174 */
3175
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003176struct k_mbox {
3177 _wait_q_t tx_msg_queue;
3178 _wait_q_t rx_msg_queue;
3179
Anas Nashif2f203c22016-12-18 06:57:45 -05003180 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003181};
3182
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003183#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003184 { \
3185 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3186 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003187 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003188 }
3189
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003190#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3191
Peter Mitsis12092702016-10-14 12:57:23 -04003192/**
Allan Stephensc98da842016-11-11 15:45:03 -05003193 * INTERNAL_HIDDEN @endcond
3194 */
3195
3196/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003197 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003198 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003199 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003200 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003201 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003202 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003203 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003204 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003205#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003206 struct k_mbox name \
3207 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003208 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003209
Peter Mitsis12092702016-10-14 12:57:23 -04003210/**
3211 * @brief Initialize a mailbox.
3212 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003213 * This routine initializes a mailbox object, prior to its first use.
3214 *
3215 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003216 *
3217 * @return N/A
3218 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003219extern void k_mbox_init(struct k_mbox *mbox);
3220
Peter Mitsis12092702016-10-14 12:57:23 -04003221/**
3222 * @brief Send a mailbox message in a synchronous manner.
3223 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003224 * This routine sends a message to @a mbox and waits for a receiver to both
3225 * receive and process it. The message data may be in a buffer, in a memory
3226 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003227 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003228 * @param mbox Address of the mailbox.
3229 * @param tx_msg Address of the transmit message descriptor.
3230 * @param timeout Waiting period for the message to be received (in
3231 * milliseconds), or one of the special values K_NO_WAIT
3232 * and K_FOREVER. Once the message has been received,
3233 * this routine waits as long as necessary for the message
3234 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003235 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003236 * @retval 0 Message sent.
3237 * @retval -ENOMSG Returned without waiting.
3238 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003239 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003240extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003241 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003242
Peter Mitsis12092702016-10-14 12:57:23 -04003243/**
3244 * @brief Send a mailbox message in an asynchronous manner.
3245 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003246 * This routine sends a message to @a mbox without waiting for a receiver
3247 * to process it. The message data may be in a buffer, in a memory pool block,
3248 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3249 * will be given when the message has been both received and completely
3250 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003251 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003252 * @param mbox Address of the mailbox.
3253 * @param tx_msg Address of the transmit message descriptor.
3254 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003255 *
3256 * @return N/A
3257 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003258extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003259 struct k_sem *sem);
3260
Peter Mitsis12092702016-10-14 12:57:23 -04003261/**
3262 * @brief Receive a mailbox message.
3263 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003264 * This routine receives a message from @a mbox, then optionally retrieves
3265 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003266 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003267 * @param mbox Address of the mailbox.
3268 * @param rx_msg Address of the receive message descriptor.
3269 * @param buffer Address of the buffer to receive data, or NULL to defer data
3270 * retrieval and message disposal until later.
3271 * @param timeout Waiting period for a message to be received (in
3272 * milliseconds), or one of the special values K_NO_WAIT
3273 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003274 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003275 * @retval 0 Message received.
3276 * @retval -ENOMSG Returned without waiting.
3277 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003278 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003279extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003280 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003281
3282/**
3283 * @brief Retrieve mailbox message data into a buffer.
3284 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003285 * This routine completes the processing of a received message by retrieving
3286 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003287 *
3288 * Alternatively, this routine can be used to dispose of a received message
3289 * without retrieving its data.
3290 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003291 * @param rx_msg Address of the receive message descriptor.
3292 * @param buffer Address of the buffer to receive data, or NULL to discard
3293 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003294 *
3295 * @return N/A
3296 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003297extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003298
3299/**
3300 * @brief Retrieve mailbox message data into a memory pool block.
3301 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003302 * This routine completes the processing of a received message by retrieving
3303 * its data into a memory pool block, then disposing of the message.
3304 * The memory pool block that results from successful retrieval must be
3305 * returned to the pool once the data has been processed, even in cases
3306 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003307 *
3308 * Alternatively, this routine can be used to dispose of a received message
3309 * without retrieving its data. In this case there is no need to return a
3310 * memory pool block to the pool.
3311 *
3312 * This routine allocates a new memory pool block for the data only if the
3313 * data is not already in one. If a new block cannot be allocated, the routine
3314 * returns a failure code and the received message is left unchanged. This
3315 * permits the caller to reattempt data retrieval at a later time or to dispose
3316 * of the received message without retrieving its data.
3317 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003318 * @param rx_msg Address of a receive message descriptor.
3319 * @param pool Address of memory pool, or NULL to discard data.
3320 * @param block Address of the area to hold memory pool block info.
3321 * @param timeout Waiting period to wait for a memory pool block (in
3322 * milliseconds), or one of the special values K_NO_WAIT
3323 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003324 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003325 * @retval 0 Data retrieved.
3326 * @retval -ENOMEM Returned without waiting.
3327 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003328 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003329extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003330 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003331 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003332
Anas Nashif166f5192018-02-25 08:02:36 -06003333/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003334
3335/**
3336 * @cond INTERNAL_HIDDEN
3337 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003338
3339struct k_pipe {
3340 unsigned char *buffer; /* Pipe buffer: may be NULL */
3341 size_t size; /* Buffer size */
3342 size_t bytes_used; /* # bytes used in buffer */
3343 size_t read_index; /* Where in buffer to read from */
3344 size_t write_index; /* Where in buffer to write */
3345
3346 struct {
3347 _wait_q_t readers; /* Reader wait queue */
3348 _wait_q_t writers; /* Writer wait queue */
3349 } wait_q;
3350
Anas Nashif2f203c22016-12-18 06:57:45 -05003351 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003352};
3353
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003354#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003355 { \
3356 .buffer = pipe_buffer, \
3357 .size = pipe_buffer_size, \
3358 .bytes_used = 0, \
3359 .read_index = 0, \
3360 .write_index = 0, \
3361 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3362 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003363 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003364 }
3365
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003366#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3367
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003368/**
Allan Stephensc98da842016-11-11 15:45:03 -05003369 * INTERNAL_HIDDEN @endcond
3370 */
3371
3372/**
3373 * @defgroup pipe_apis Pipe APIs
3374 * @ingroup kernel_apis
3375 * @{
3376 */
3377
3378/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003379 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003380 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003381 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003382 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003383 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003384 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003385 * @param name Name of the pipe.
3386 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3387 * or zero if no ring buffer is used.
3388 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003389 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003390#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3391 static unsigned char __noinit __aligned(pipe_align) \
3392 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003393 struct k_pipe name \
3394 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003395 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003396
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003397/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003398 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003399 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003400 * This routine initializes a pipe object, prior to its first use.
3401 *
3402 * @param pipe Address of the pipe.
3403 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3404 * is used.
3405 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3406 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003407 *
3408 * @return N/A
3409 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003410__syscall void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3411 size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003412
3413/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003414 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003415 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003416 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003418 * @param pipe Address of the pipe.
3419 * @param data Address of data to write.
3420 * @param bytes_to_write Size of data (in bytes).
3421 * @param bytes_written Address of area to hold the number of bytes written.
3422 * @param min_xfer Minimum number of bytes to write.
3423 * @param timeout Waiting period to wait for the data to be written (in
3424 * milliseconds), or one of the special values K_NO_WAIT
3425 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003426 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003427 * @retval 0 At least @a min_xfer bytes of data were written.
3428 * @retval -EIO Returned without waiting; zero data bytes were written.
3429 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003430 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003431 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003432__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3433 size_t bytes_to_write, size_t *bytes_written,
3434 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003435
3436/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003437 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003438 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003439 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003440 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003441 * @param pipe Address of the pipe.
3442 * @param data Address to place the data read from pipe.
3443 * @param bytes_to_read Maximum number of data bytes to read.
3444 * @param bytes_read Address of area to hold the number of bytes read.
3445 * @param min_xfer Minimum number of data bytes to read.
3446 * @param timeout Waiting period to wait for the data to be read (in
3447 * milliseconds), or one of the special values K_NO_WAIT
3448 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003449 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003450 * @retval 0 At least @a min_xfer bytes of data were read.
3451 * @retval -EIO Returned without waiting; zero data bytes were read.
3452 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003453 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003454 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003455__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3456 size_t bytes_to_read, size_t *bytes_read,
3457 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003458
3459/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003460 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003461 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003462 * This routine writes the data contained in a memory block to @a pipe.
3463 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003464 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003465 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003466 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003467 * @param block Memory block containing data to send
3468 * @param size Number of data bytes in memory block to send
3469 * @param sem Semaphore to signal upon completion (else NULL)
3470 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003471 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003472 */
3473extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3474 size_t size, struct k_sem *sem);
3475
Anas Nashif166f5192018-02-25 08:02:36 -06003476/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003477
Allan Stephensc98da842016-11-11 15:45:03 -05003478/**
3479 * @cond INTERNAL_HIDDEN
3480 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003481
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003482struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003483 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003484 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003485 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003486 char *buffer;
3487 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003488 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003489
Anas Nashif2f203c22016-12-18 06:57:45 -05003490 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003491};
3492
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003493#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003494 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003495 { \
3496 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003497 .num_blocks = slab_num_blocks, \
3498 .block_size = slab_block_size, \
3499 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003500 .free_list = NULL, \
3501 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003502 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003503 }
3504
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003505#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3506
3507
Peter Mitsis578f9112016-10-07 13:50:31 -04003508/**
Allan Stephensc98da842016-11-11 15:45:03 -05003509 * INTERNAL_HIDDEN @endcond
3510 */
3511
3512/**
3513 * @defgroup mem_slab_apis Memory Slab APIs
3514 * @ingroup kernel_apis
3515 * @{
3516 */
3517
3518/**
Allan Stephensda827222016-11-09 14:23:58 -06003519 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003520 *
Allan Stephensda827222016-11-09 14:23:58 -06003521 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003522 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003523 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3524 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003525 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003526 *
Allan Stephensda827222016-11-09 14:23:58 -06003527 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003528 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003529 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003530 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003531 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003532 * @param name Name of the memory slab.
3533 * @param slab_block_size Size of each memory block (in bytes).
3534 * @param slab_num_blocks Number memory blocks.
3535 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003536 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003537#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3538 char __noinit __aligned(slab_align) \
3539 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3540 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003541 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003542 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003543 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003544
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003545/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003546 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003547 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003548 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003549 *
Allan Stephensda827222016-11-09 14:23:58 -06003550 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3551 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3552 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3553 * To ensure that each memory block is similarly aligned to this boundary,
3554 * @a slab_block_size must also be a multiple of N.
3555 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003556 * @param slab Address of the memory slab.
3557 * @param buffer Pointer to buffer used for the memory blocks.
3558 * @param block_size Size of each memory block (in bytes).
3559 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003560 *
3561 * @return N/A
3562 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003563extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003564 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003565
3566/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003567 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003568 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003569 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003570 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003571 * @param slab Address of the memory slab.
3572 * @param mem Pointer to block address area.
3573 * @param timeout Maximum time to wait for operation to complete
3574 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3575 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003576 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003577 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003578 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003579 * @retval -ENOMEM Returned without waiting.
3580 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003581 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003582extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003583 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003584
3585/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003586 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003587 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003588 * This routine releases a previously allocated memory block back to its
3589 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003590 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003591 * @param slab Address of the memory slab.
3592 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003593 *
3594 * @return N/A
3595 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003596extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003597
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003598/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003599 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003600 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003601 * This routine gets the number of memory blocks that are currently
3602 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003603 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003604 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003605 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003606 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003607 */
Kumar Galacc334c72017-04-21 10:55:34 -05003608static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003609{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003610 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003611}
3612
Peter Mitsisc001aa82016-10-13 13:53:37 -04003613/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003614 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003615 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003616 * This routine gets the number of memory blocks that are currently
3617 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003618 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003619 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003620 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003621 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003622 */
Kumar Galacc334c72017-04-21 10:55:34 -05003623static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003624{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003625 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003626}
3627
Anas Nashif166f5192018-02-25 08:02:36 -06003628/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003629
3630/**
3631 * @cond INTERNAL_HIDDEN
3632 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003633
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003634struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003635 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003636 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003637};
3638
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003639/**
Allan Stephensc98da842016-11-11 15:45:03 -05003640 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003641 */
3642
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003643/**
Allan Stephensc98da842016-11-11 15:45:03 -05003644 * @addtogroup mem_pool_apis
3645 * @{
3646 */
3647
3648/**
3649 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003650 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003651 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3652 * long. The memory pool allows blocks to be repeatedly partitioned into
3653 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003654 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003655 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003656 * If the pool is to be accessed outside the module where it is defined, it
3657 * can be declared via
3658 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003659 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003660 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003661 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003662 * @param minsz Size of the smallest blocks in the pool (in bytes).
3663 * @param maxsz Size of the largest blocks in the pool (in bytes).
3664 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003665 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003666 */
Andy Ross73cb9582017-05-09 10:42:39 -07003667#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3668 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3669 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08003670 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07003671 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08003672 .base = { \
3673 .buf = _mpool_buf_##name, \
3674 .max_sz = maxsz, \
3675 .n_max = nmax, \
3676 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3677 .levels = _mpool_lvls_##name, \
3678 .flags = SYS_MEM_POOL_KERNEL \
3679 } \
Andy Ross73cb9582017-05-09 10:42:39 -07003680 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003681
Peter Mitsis937042c2016-10-13 13:18:26 -04003682/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003683 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003684 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003685 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003686 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003687 * @param pool Address of the memory pool.
3688 * @param block Pointer to block descriptor for the allocated memory.
3689 * @param size Amount of memory to allocate (in bytes).
3690 * @param timeout Maximum time to wait for operation to complete
3691 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3692 * or K_FOREVER to wait as long as necessary.
3693 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003694 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003695 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003696 * @retval -ENOMEM Returned without waiting.
3697 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003698 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003699extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003700 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003701
3702/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07003703 * @brief Allocate memory from a memory pool with malloc() semantics
3704 *
3705 * Such memory must be released using k_free().
3706 *
3707 * @param pool Address of the memory pool.
3708 * @param size Amount of memory to allocate (in bytes).
3709 * @return Address of the allocated memory if successful, otherwise NULL
3710 */
3711extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
3712
3713/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003714 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003716 * This routine releases a previously allocated memory block back to its
3717 * memory pool.
3718 *
3719 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003720 *
3721 * @return N/A
3722 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003723extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003724
3725/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003726 * @brief Free memory allocated from a memory pool.
3727 *
3728 * This routine releases a previously allocated memory block back to its
3729 * memory pool
3730 *
3731 * @param id Memory block identifier.
3732 *
3733 * @return N/A
3734 */
3735extern void k_mem_pool_free_id(struct k_mem_block_id *id);
3736
3737/**
Anas Nashif166f5192018-02-25 08:02:36 -06003738 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003739 */
3740
3741/**
3742 * @defgroup heap_apis Heap Memory Pool APIs
3743 * @ingroup kernel_apis
3744 * @{
3745 */
3746
3747/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003748 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003749 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003750 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003751 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003752 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003753 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003754 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003755 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003756 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003757extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003758
3759/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003760 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003761 *
3762 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07003763 * returned must have been allocated from the heap memory pool or
3764 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04003765 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003766 * If @a ptr is NULL, no operation is performed.
3767 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003768 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003769 *
3770 * @return N/A
3771 */
3772extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003773
Allan Stephensc98da842016-11-11 15:45:03 -05003774/**
Andrew Boie7f95e832017-11-08 14:40:01 -08003775 * @brief Allocate memory from heap, array style
3776 *
3777 * This routine provides traditional calloc() semantics. Memory is
3778 * allocated from the heap memory pool and zeroed.
3779 *
3780 * @param nmemb Number of elements in the requested array
3781 * @param size Size of each array element (in bytes).
3782 *
3783 * @return Address of the allocated memory if successful; otherwise NULL.
3784 */
3785extern void *k_calloc(size_t nmemb, size_t size);
3786
Anas Nashif166f5192018-02-25 08:02:36 -06003787/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003788
Benjamin Walshacc68c12017-01-29 18:57:45 -05003789/* polling API - PRIVATE */
3790
Benjamin Walshb0179862017-02-02 16:39:57 -05003791#ifdef CONFIG_POLL
3792#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3793#else
3794#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3795#endif
3796
Benjamin Walshacc68c12017-01-29 18:57:45 -05003797/* private - implementation data created as needed, per-type */
3798struct _poller {
3799 struct k_thread *thread;
3800};
3801
3802/* private - types bit positions */
3803enum _poll_types_bits {
3804 /* can be used to ignore an event */
3805 _POLL_TYPE_IGNORE,
3806
3807 /* to be signaled by k_poll_signal() */
3808 _POLL_TYPE_SIGNAL,
3809
3810 /* semaphore availability */
3811 _POLL_TYPE_SEM_AVAILABLE,
3812
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003813 /* queue/fifo/lifo data availability */
3814 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003815
3816 _POLL_NUM_TYPES
3817};
3818
3819#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3820
3821/* private - states bit positions */
3822enum _poll_states_bits {
3823 /* default state when creating event */
3824 _POLL_STATE_NOT_READY,
3825
Benjamin Walshacc68c12017-01-29 18:57:45 -05003826 /* signaled by k_poll_signal() */
3827 _POLL_STATE_SIGNALED,
3828
3829 /* semaphore is available */
3830 _POLL_STATE_SEM_AVAILABLE,
3831
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003832 /* data is available to read on queue/fifo/lifo */
3833 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003834
3835 _POLL_NUM_STATES
3836};
3837
3838#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3839
3840#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003841 (32 - (0 \
3842 + 8 /* tag */ \
3843 + _POLL_NUM_TYPES \
3844 + _POLL_NUM_STATES \
3845 + 1 /* modes */ \
3846 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003847
Benjamin Walshacc68c12017-01-29 18:57:45 -05003848/* end of polling API - PRIVATE */
3849
3850
3851/**
3852 * @defgroup poll_apis Async polling APIs
3853 * @ingroup kernel_apis
3854 * @{
3855 */
3856
3857/* Public polling API */
3858
3859/* public - values for k_poll_event.type bitfield */
3860#define K_POLL_TYPE_IGNORE 0
3861#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3862#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003863#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3864#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003865
3866/* public - polling modes */
3867enum k_poll_modes {
3868 /* polling thread does not take ownership of objects when available */
3869 K_POLL_MODE_NOTIFY_ONLY = 0,
3870
3871 K_POLL_NUM_MODES
3872};
3873
3874/* public - values for k_poll_event.state bitfield */
3875#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003876#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3877#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003878#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3879#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003880
3881/* public - poll signal object */
3882struct k_poll_signal {
3883 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003884 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003885
3886 /*
3887 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3888 * user resets it to 0.
3889 */
3890 unsigned int signaled;
3891
3892 /* custom result value passed to k_poll_signal() if needed */
3893 int result;
3894};
3895
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003896#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003897 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003898 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003899 .signaled = 0, \
3900 .result = 0, \
3901 }
3902
3903struct k_poll_event {
3904 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003905 sys_dnode_t _node;
3906
3907 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003908 struct _poller *poller;
3909
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003910 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003911 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003912
Benjamin Walshacc68c12017-01-29 18:57:45 -05003913 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003914 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003915
3916 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003917 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003918
3919 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003920 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003921
3922 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003923 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003924
3925 /* per-type data */
3926 union {
3927 void *obj;
3928 struct k_poll_signal *signal;
3929 struct k_sem *sem;
3930 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003931 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003932 };
3933};
3934
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003935#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003936 { \
3937 .poller = NULL, \
3938 .type = event_type, \
3939 .state = K_POLL_STATE_NOT_READY, \
3940 .mode = event_mode, \
3941 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003942 { .obj = event_obj }, \
3943 }
3944
3945#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3946 event_tag) \
3947 { \
3948 .type = event_type, \
3949 .tag = event_tag, \
3950 .state = K_POLL_STATE_NOT_READY, \
3951 .mode = event_mode, \
3952 .unused = 0, \
3953 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003954 }
3955
3956/**
3957 * @brief Initialize one struct k_poll_event instance
3958 *
3959 * After this routine is called on a poll event, the event it ready to be
3960 * placed in an event array to be passed to k_poll().
3961 *
3962 * @param event The event to initialize.
3963 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3964 * values. Only values that apply to the same object being polled
3965 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3966 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003967 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003968 * @param obj Kernel object or poll signal.
3969 *
3970 * @return N/A
3971 */
3972
Kumar Galacc334c72017-04-21 10:55:34 -05003973extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003974 int mode, void *obj);
3975
3976/**
3977 * @brief Wait for one or many of multiple poll events to occur
3978 *
3979 * This routine allows a thread to wait concurrently for one or many of
3980 * multiple poll events to have occurred. Such events can be a kernel object
3981 * being available, like a semaphore, or a poll signal event.
3982 *
3983 * When an event notifies that a kernel object is available, the kernel object
3984 * is not "given" to the thread calling k_poll(): it merely signals the fact
3985 * that the object was available when the k_poll() call was in effect. Also,
3986 * all threads trying to acquire an object the regular way, i.e. by pending on
3987 * the object, have precedence over the thread polling on the object. This
3988 * means that the polling thread will never get the poll event on an object
3989 * until the object becomes available and its pend queue is empty. For this
3990 * reason, the k_poll() call is more effective when the objects being polled
3991 * only have one thread, the polling thread, trying to acquire them.
3992 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003993 * When k_poll() returns 0, the caller should loop on all the events that were
3994 * passed to k_poll() and check the state field for the values that were
3995 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003996 *
3997 * Before being reused for another call to k_poll(), the user has to reset the
3998 * state field to K_POLL_STATE_NOT_READY.
3999 *
4000 * @param events An array of pointers to events to be polled for.
4001 * @param num_events The number of events in the array.
4002 * @param timeout Waiting period for an event to be ready (in milliseconds),
4003 * or one of the special values K_NO_WAIT and K_FOREVER.
4004 *
4005 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004006 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02004007 * @retval -EINTR Poller thread has been interrupted.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004008 */
4009
4010extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05004011 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004012
4013/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004014 * @brief Initialize a poll signal object.
4015 *
4016 * Ready a poll signal object to be signaled via k_poll_signal().
4017 *
4018 * @param signal A poll signal.
4019 *
4020 * @return N/A
4021 */
4022
4023extern void k_poll_signal_init(struct k_poll_signal *signal);
4024
4025/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004026 * @brief Signal a poll signal object.
4027 *
4028 * This routine makes ready a poll signal, which is basically a poll event of
4029 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4030 * made ready to run. A @a result value can be specified.
4031 *
4032 * The poll signal contains a 'signaled' field that, when set by
4033 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
4034 * be reset by the user before being passed again to k_poll() or k_poll() will
4035 * consider it being signaled, and will return immediately.
4036 *
4037 * @param signal A poll signal.
4038 * @param result The value to store in the result field of the signal.
4039 *
4040 * @retval 0 The signal was delivered successfully.
4041 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
4042 */
4043
4044extern int k_poll_signal(struct k_poll_signal *signal, int result);
4045
Anas Nashif954d5502018-02-25 08:37:28 -06004046/**
4047 * @internal
4048 */
Andy Ross8606fab2018-03-26 10:54:40 -07004049extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004050
Anas Nashif166f5192018-02-25 08:02:36 -06004051/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004052
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004053/**
4054 * @brief Make the CPU idle.
4055 *
4056 * This function makes the CPU idle until an event wakes it up.
4057 *
4058 * In a regular system, the idle thread should be the only thread responsible
4059 * for making the CPU idle and triggering any type of power management.
4060 * However, in some more constrained systems, such as a single-threaded system,
4061 * the only thread would be responsible for this if needed.
4062 *
4063 * @return N/A
4064 */
4065extern void k_cpu_idle(void);
4066
4067/**
4068 * @brief Make the CPU idle in an atomic fashion.
4069 *
4070 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4071 * must be done atomically before making the CPU idle.
4072 *
4073 * @param key Interrupt locking key obtained from irq_lock().
4074 *
4075 * @return N/A
4076 */
4077extern void k_cpu_atomic_idle(unsigned int key);
4078
Anas Nashif954d5502018-02-25 08:37:28 -06004079
4080/**
4081 * @internal
4082 */
Kumar Galacc334c72017-04-21 10:55:34 -05004083extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004084
Andrew Boiecdb94d62017-04-18 15:22:05 -07004085#ifdef _ARCH_EXCEPT
4086/* This archtecture has direct support for triggering a CPU exception */
4087#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4088#else
4089
Andrew Boiecdb94d62017-04-18 15:22:05 -07004090/* NOTE: This is the implementation for arches that do not implement
4091 * _ARCH_EXCEPT() to generate a real CPU exception.
4092 *
4093 * We won't have a real exception frame to determine the PC value when
4094 * the oops occurred, so print file and line number before we jump into
4095 * the fatal error handler.
4096 */
4097#define _k_except_reason(reason) do { \
4098 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4099 _NanoFatalErrorHandler(reason, &_default_esf); \
4100 CODE_UNREACHABLE; \
4101 } while (0)
4102
4103#endif /* _ARCH__EXCEPT */
4104
4105/**
4106 * @brief Fatally terminate a thread
4107 *
4108 * This should be called when a thread has encountered an unrecoverable
4109 * runtime condition and needs to terminate. What this ultimately
4110 * means is determined by the _fatal_error_handler() implementation, which
4111 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4112 *
4113 * If this is called from ISR context, the default system fatal error handler
4114 * will treat it as an unrecoverable system error, just like k_panic().
4115 */
4116#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4117
4118/**
4119 * @brief Fatally terminate the system
4120 *
4121 * This should be called when the Zephyr kernel has encountered an
4122 * unrecoverable runtime condition and needs to terminate. What this ultimately
4123 * means is determined by the _fatal_error_handler() implementation, which
4124 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4125 */
4126#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4127
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004128/*
4129 * private APIs that are utilized by one or more public APIs
4130 */
4131
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004132#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004133/**
4134 * @internal
4135 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004136extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004137#else
Anas Nashif954d5502018-02-25 08:37:28 -06004138/**
4139 * @internal
4140 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004141#define _init_static_threads() do { } while ((0))
4142#endif
4143
Anas Nashif954d5502018-02-25 08:37:28 -06004144/**
4145 * @internal
4146 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004147extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004148/**
4149 * @internal
4150 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004151extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004152
Andrew Boiedc5d9352017-06-02 12:56:47 -07004153/* arch/cpu.h may declare an architecture or platform-specific macro
4154 * for properly declaring stacks, compatible with MMU/MPU constraints if
4155 * enabled
4156 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004157
4158/**
4159 * @brief Obtain an extern reference to a stack
4160 *
4161 * This macro properly brings the symbol of a thread stack declared
4162 * elsewhere into scope.
4163 *
4164 * @param sym Thread stack symbol name
4165 */
4166#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4167
Andrew Boiedc5d9352017-06-02 12:56:47 -07004168#ifdef _ARCH_THREAD_STACK_DEFINE
4169#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4170#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4171 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4172#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4173#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004174static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004175{
4176 return _ARCH_THREAD_STACK_BUFFER(sym);
4177}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004178#else
4179/**
4180 * @brief Declare a toplevel thread stack memory region
4181 *
4182 * This declares a region of memory suitable for use as a thread's stack.
4183 *
4184 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4185 * 'noinit' section so that it isn't zeroed at boot
4186 *
Andrew Boie507852a2017-07-25 18:47:07 -07004187 * The declared symbol will always be a k_thread_stack_t which can be passed to
4188 * k_thread_create, but should otherwise not be manipulated. If the buffer
4189 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004190 *
4191 * It is legal to precede this definition with the 'static' keyword.
4192 *
4193 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4194 * parameter of k_thread_create(), it may not be the same as the
4195 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4196 *
4197 * @param sym Thread stack symbol name
4198 * @param size Size of the stack memory region
4199 */
4200#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004201 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004202
4203/**
4204 * @brief Declare a toplevel array of thread stack memory regions
4205 *
4206 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4207 * definition for additional details and constraints.
4208 *
4209 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4210 * 'noinit' section so that it isn't zeroed at boot
4211 *
4212 * @param sym Thread stack symbol name
4213 * @param nmemb Number of stacks to declare
4214 * @param size Size of the stack memory region
4215 */
4216
4217#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004218 struct _k_thread_stack_element __noinit \
4219 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004220
4221/**
4222 * @brief Declare an embedded stack memory region
4223 *
4224 * Used for stacks embedded within other data structures. Use is highly
4225 * discouraged but in some cases necessary. For memory protection scenarios,
4226 * it is very important that any RAM preceding this member not be writable
4227 * by threads else a stack overflow will lead to silent corruption. In other
4228 * words, the containing data structure should live in RAM owned by the kernel.
4229 *
4230 * @param sym Thread stack symbol name
4231 * @param size Size of the stack memory region
4232 */
4233#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004234 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004235
4236/**
4237 * @brief Return the size in bytes of a stack memory region
4238 *
4239 * Convenience macro for passing the desired stack size to k_thread_create()
4240 * since the underlying implementation may actually create something larger
4241 * (for instance a guard area).
4242 *
4243 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004244 * passed to K_THREAD_STACK_DEFINE.
4245 *
4246 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4247 * it is not guaranteed to return the original value since each array
4248 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004249 *
4250 * @param sym Stack memory symbol
4251 * @return Size of the stack
4252 */
4253#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4254
4255/**
4256 * @brief Get a pointer to the physical stack buffer
4257 *
4258 * Convenience macro to get at the real underlying stack buffer used by
4259 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4260 * This is really only intended for diagnostic tools which want to examine
4261 * stack memory contents.
4262 *
4263 * @param sym Declared stack symbol name
4264 * @return The buffer itself, a char *
4265 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004266static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004267{
4268 return (char *)sym;
4269}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004270
4271#endif /* _ARCH_DECLARE_STACK */
4272
Chunlin Hane9c97022017-07-07 20:29:30 +08004273/**
4274 * @defgroup mem_domain_apis Memory domain APIs
4275 * @ingroup kernel_apis
4276 * @{
4277 */
4278
4279/** @def MEM_PARTITION_ENTRY
4280 * @brief Used to declare a memory partition entry
4281 */
4282#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4283 {\
4284 .start = _start, \
4285 .size = _size, \
4286 .attr = _attr, \
4287 }
4288
4289/** @def K_MEM_PARTITION_DEFINE
4290 * @brief Used to declare a memory partition
4291 */
4292#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4293#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4294 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304295 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004296 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4297#else
4298#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304299 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004300 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4301#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4302
Chunlin Hane9c97022017-07-07 20:29:30 +08004303/* memory partition */
4304struct k_mem_partition {
4305 /* start address of memory partition */
4306 u32_t start;
4307 /* size of memory partition */
4308 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304309#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004310 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304311 k_mem_partition_attr_t attr;
4312#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004313};
4314
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304315/* memory domian
4316 * Note: Always declare this structure with __kernel prefix
4317 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004318struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304319#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004320 /* partitions in the domain */
4321 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304322#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004323 /* domain q */
4324 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004325 /* number of partitions in the domain */
4326 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004327};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304328
Chunlin Hane9c97022017-07-07 20:29:30 +08004329
4330/**
4331 * @brief Initialize a memory domain.
4332 *
4333 * Initialize a memory domain with given name and memory partitions.
4334 *
4335 * @param domain The memory domain to be initialized.
4336 * @param num_parts The number of array items of "parts" parameter.
4337 * @param parts An array of pointers to the memory partitions. Can be NULL
4338 * if num_parts is zero.
4339 */
4340
Leandro Pereira08de6582018-02-28 14:22:57 -08004341extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004342 struct k_mem_partition *parts[]);
4343/**
4344 * @brief Destroy a memory domain.
4345 *
4346 * Destroy a memory domain.
4347 *
4348 * @param domain The memory domain to be destroyed.
4349 */
4350
4351extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4352
4353/**
4354 * @brief Add a memory partition into a memory domain.
4355 *
4356 * Add a memory partition into a memory domain.
4357 *
4358 * @param domain The memory domain to be added a memory partition.
4359 * @param part The memory partition to be added
4360 */
4361
4362extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4363 struct k_mem_partition *part);
4364
4365/**
4366 * @brief Remove a memory partition from a memory domain.
4367 *
4368 * Remove a memory partition from a memory domain.
4369 *
4370 * @param domain The memory domain to be removed a memory partition.
4371 * @param part The memory partition to be removed
4372 */
4373
4374extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4375 struct k_mem_partition *part);
4376
4377/**
4378 * @brief Add a thread into a memory domain.
4379 *
4380 * Add a thread into a memory domain.
4381 *
4382 * @param domain The memory domain that the thread is going to be added into.
4383 * @param thread ID of thread going to be added into the memory domain.
4384 */
4385
4386extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4387 k_tid_t thread);
4388
4389/**
4390 * @brief Remove a thread from its memory domain.
4391 *
4392 * Remove a thread from its memory domain.
4393 *
4394 * @param thread ID of thread going to be removed from its memory domain.
4395 */
4396
4397extern void k_mem_domain_remove_thread(k_tid_t thread);
4398
Anas Nashif166f5192018-02-25 08:02:36 -06004399/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004400
Andrew Boie756f9072017-10-10 16:01:49 -07004401/**
4402 * @brief Emit a character buffer to the console device
4403 *
4404 * @param c String of characters to print
4405 * @param n The length of the string
4406 */
4407__syscall void k_str_out(char *c, size_t n);
4408
Andy Rosse7172672018-01-24 15:48:32 -08004409/**
4410 * @brief Start a numbered CPU on a MP-capable system
4411
4412 * This starts and initializes a specific CPU. The main thread on
4413 * startup is running on CPU zero, other processors are numbered
4414 * sequentially. On return from this function, the CPU is known to
4415 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004416 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004417 * with the provided key will work to enable them.
4418 *
4419 * Normally, in SMP mode this function will be called by the kernel
4420 * initialization and should not be used as a user API. But it is
4421 * defined here for special-purpose apps which want Zephyr running on
4422 * one core and to use others for design-specific processing.
4423 *
4424 * @param cpu_num Integer number of the CPU
4425 * @param stack Stack memory for the CPU
4426 * @param sz Stack buffer size, in bytes
4427 * @param fn Function to begin running on the CPU. First argument is
4428 * an irq_unlock() key.
4429 * @param arg Untyped argument to be passed to "fn"
4430 */
4431extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4432 void (*fn)(int, void *), void *arg);
4433
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004434#ifdef __cplusplus
4435}
4436#endif
4437
Andrew Boiee004dec2016-11-07 09:01:19 -08004438#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4439/*
4440 * Define new and delete operators.
4441 * At this moment, the operators do nothing since objects are supposed
4442 * to be statically allocated.
4443 */
4444inline void operator delete(void *ptr)
4445{
4446 (void)ptr;
4447}
4448
4449inline void operator delete[](void *ptr)
4450{
4451 (void)ptr;
4452}
4453
4454inline void *operator new(size_t size)
4455{
4456 (void)size;
4457 return NULL;
4458}
4459
4460inline void *operator new[](size_t size)
4461{
4462 (void)size;
4463 return NULL;
4464}
4465
4466/* Placement versions of operator new and delete */
4467inline void operator delete(void *ptr1, void *ptr2)
4468{
4469 (void)ptr1;
4470 (void)ptr2;
4471}
4472
4473inline void operator delete[](void *ptr1, void *ptr2)
4474{
4475 (void)ptr1;
4476 (void)ptr2;
4477}
4478
4479inline void *operator new(size_t size, void *ptr)
4480{
4481 (void)size;
4482 return ptr;
4483}
4484
4485inline void *operator new[](size_t size, void *ptr)
4486{
4487 (void)size;
4488 return ptr;
4489}
4490
4491#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4492
Andrew Boiefa94ee72017-09-28 16:54:35 -07004493#include <syscalls/kernel.h>
4494
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004495#endif /* !_ASMLANGUAGE */
4496
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004497#endif /* _kernel__h_ */