blob: c67380ec077155c933e6c6d694adc48cd8ce7464 [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 Boiea89bf012017-10-09 14:47:55 -0700226static inline void _impl_k_object_access_revoke(void *object,
227 struct k_thread *thread)
228{
229 ARG_UNUSED(object);
230 ARG_UNUSED(thread);
231}
232
Andrew Boie41bab6e2017-10-14 14:42:23 -0700233static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700234{
235 ARG_UNUSED(object);
236}
237#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700238
239/**
240 * grant a thread access to a kernel object
241 *
242 * The thread will be granted access to the object if the caller is from
243 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700244 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700245 *
246 * @param object Address of kernel object
247 * @param thread Thread to grant access to the object
248 */
Andrew Boie743e4682017-10-04 12:25:50 -0700249__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700250
Andrew Boiea89bf012017-10-09 14:47:55 -0700251/**
252 * grant a thread access to a kernel object
253 *
254 * The thread will lose access to the object if the caller is from
255 * supervisor mode, or the caller is from user mode AND has permissions
256 * on both the object and the thread whose access is being revoked.
257 *
258 * @param object Address of kernel object
259 * @param thread Thread to remove access to the object
260 */
261__syscall void k_object_access_revoke(void *object, struct k_thread *thread);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700262
263/**
264 * grant all present and future threads access to an object
265 *
266 * If the caller is from supervisor mode, or the caller is from user mode and
267 * have sufficient permissions on the object, then that object will have
268 * permissions granted to it for *all* current and future threads running in
269 * the system, effectively becoming a public kernel object.
270 *
271 * Use of this API should be avoided on systems that are running untrusted code
272 * as it is possible for such code to derive the addresses of kernel objects
273 * and perform unwanted operations on them.
274 *
Andrew Boie04caa672017-10-13 13:57:07 -0700275 * It is not possible to revoke permissions on public objects; once public,
276 * any thread may use it.
277 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700278 * @param object Address of kernel object
279 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700280void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700281
Andrew Boie31bdfc02017-11-08 16:38:03 -0800282#ifdef CONFIG_DYNAMIC_OBJECTS
283/**
284 * Allocate a kernel object of a designated type
285 *
286 * This will instantiate at runtime a kernel object of the specified type,
287 * returning a pointer to it. The object will be returned in an uninitialized
288 * state, with the calling thread being granted permission on it. The memory
289 * for the object will be allocated out of the kernel's heap.
290 *
291 * Currently, allocation of thread stacks is not supported.
292 *
293 * @param otype Requested kernel object type
294 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
295 * available
296 */
297void *k_object_alloc(enum k_objects otype);
298
299/**
300 * Free a kernel object previously allocated with k_object_alloc()
301 *
302 * This will return memory for a kernel object back to the system heap.
303 * Care must be exercised that the object will not be used during or after
304 * when this call is made.
305 *
306 * @param obj Pointer to the kernel object memory address.
307 */
308void k_object_free(void *obj);
309#endif /* CONFIG_DYNAMIC_OBJECTS */
310
Andrew Boiebca15da2017-10-15 14:17:48 -0700311/* Using typedef deliberately here, this is quite intended to be an opaque
312 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
313 *
314 * The purpose of this data type is to clearly distinguish between the
315 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
316 * buffer which composes the stack data actually used by the underlying
317 * thread; they cannot be used interchangably as some arches precede the
318 * stack buffer region with guard areas that trigger a MPU or MMU fault
319 * if written to.
320 *
321 * APIs that want to work with the buffer inside should continue to use
322 * char *.
323 *
324 * Stacks should always be created with K_THREAD_STACK_DEFINE().
325 */
326struct __packed _k_thread_stack_element {
327 char data;
328};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700329typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700330
Andrew Boie73abd322017-04-04 13:19:13 -0700331/* timeouts */
332
333struct _timeout;
334typedef void (*_timeout_func_t)(struct _timeout *t);
335
336struct _timeout {
337 sys_dnode_t node;
338 struct k_thread *thread;
339 sys_dlist_t *wait_q;
340 s32_t delta_ticks_from_prev;
341 _timeout_func_t func;
342};
343
344extern s32_t _timeout_remaining_get(struct _timeout *timeout);
345
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700346/**
347 * @typedef k_thread_entry_t
348 * @brief Thread entry point function type.
349 *
350 * A thread's entry point function is invoked when the thread starts executing.
351 * Up to 3 argument values can be passed to the function.
352 *
353 * The thread terminates execution permanently if the entry point function
354 * returns. The thread is responsible for releasing any shared resources
355 * it may own (such as mutexes and dynamically allocated memory), prior to
356 * returning.
357 *
358 * @param p1 First argument.
359 * @param p2 Second argument.
360 * @param p3 Third argument.
361 *
362 * @return N/A
363 */
364typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700365
366#ifdef CONFIG_THREAD_MONITOR
367struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700368 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700369 void *parameter1;
370 void *parameter2;
371 void *parameter3;
372};
373#endif
374
375/* can be used for creating 'dummy' threads, e.g. for pending on objects */
376struct _thread_base {
377
378 /* this thread's entry in a ready/wait queue */
379 sys_dnode_t k_q_node;
380
381 /* user facing 'thread options'; values defined in include/kernel.h */
382 u8_t user_options;
383
384 /* thread state */
385 u8_t thread_state;
386
387 /*
388 * scheduler lock count and thread priority
389 *
390 * These two fields control the preemptibility of a thread.
391 *
392 * When the scheduler is locked, sched_locked is decremented, which
393 * means that the scheduler is locked for values from 0xff to 0x01. A
394 * thread is coop if its prio is negative, thus 0x80 to 0xff when
395 * looked at the value as unsigned.
396 *
397 * By putting them end-to-end, this means that a thread is
398 * non-preemptible if the bundled value is greater than or equal to
399 * 0x0080.
400 */
401 union {
402 struct {
403#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
404 u8_t sched_locked;
405 s8_t prio;
406#else /* LITTLE and PDP */
407 s8_t prio;
408 u8_t sched_locked;
409#endif
410 };
411 u16_t preempt;
412 };
413
Andy Ross2724fd12018-01-29 14:55:20 -0800414#ifdef CONFIG_SMP
415 /* True for the per-CPU idle threads */
416 u8_t is_idle;
417
418 /* Non-zero when actively running on a CPU */
419 u8_t active;
420
421 /* CPU index on which thread was last run */
422 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700423
424 /* Recursive count of irq_lock() calls */
425 u8_t global_lock_count;
Andy Ross2724fd12018-01-29 14:55:20 -0800426#endif
427
Andrew Boie73abd322017-04-04 13:19:13 -0700428 /* data returned by APIs */
429 void *swap_data;
430
431#ifdef CONFIG_SYS_CLOCK_EXISTS
432 /* this thread's entry in a timeout queue */
433 struct _timeout timeout;
434#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700435};
436
437typedef struct _thread_base _thread_base_t;
438
439#if defined(CONFIG_THREAD_STACK_INFO)
440/* Contains the stack information of a thread */
441struct _thread_stack_info {
442 /* Stack Start */
443 u32_t start;
444 /* Stack Size */
445 u32_t size;
446};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700447
448typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700449#endif /* CONFIG_THREAD_STACK_INFO */
450
Chunlin Hane9c97022017-07-07 20:29:30 +0800451#if defined(CONFIG_USERSPACE)
452struct _mem_domain_info {
453 /* memory domain queue node */
454 sys_dnode_t mem_domain_q_node;
455 /* memory domain of the thread */
456 struct k_mem_domain *mem_domain;
457};
458
459#endif /* CONFIG_USERSPACE */
460
Andrew Boie73abd322017-04-04 13:19:13 -0700461struct k_thread {
462
463 struct _thread_base base;
464
465 /* defined by the architecture, but all archs need these */
466 struct _caller_saved caller_saved;
467 struct _callee_saved callee_saved;
468
469 /* static thread init data */
470 void *init_data;
471
472 /* abort function */
473 void (*fn_abort)(void);
474
475#if defined(CONFIG_THREAD_MONITOR)
476 /* thread entry and parameters description */
477 struct __thread_entry *entry;
478
479 /* next item in list of all threads */
480 struct k_thread *next_thread;
481#endif
482
483#ifdef CONFIG_THREAD_CUSTOM_DATA
484 /* crude thread-local storage */
485 void *custom_data;
486#endif
487
488#ifdef CONFIG_ERRNO
489 /* per-thread errno variable */
490 int errno_var;
491#endif
492
493#if defined(CONFIG_THREAD_STACK_INFO)
494 /* Stack Info */
495 struct _thread_stack_info stack_info;
496#endif /* CONFIG_THREAD_STACK_INFO */
497
Chunlin Hane9c97022017-07-07 20:29:30 +0800498#if defined(CONFIG_USERSPACE)
499 /* memory domain info of the thread */
500 struct _mem_domain_info mem_domain_info;
Andrew Boiebca15da2017-10-15 14:17:48 -0700501 /* Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700502 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800503#endif /* CONFIG_USERSPACE */
504
Andy Ross042d8ec2017-12-09 08:37:20 -0800505#if defined(CONFIG_USE_SWITCH)
506 /* When using __switch() a few previously arch-specific items
507 * become part of the core OS
508 */
509
510 /* _Swap() return value */
511 int swap_retval;
512
513 /* Context handle returned via _arch_switch() */
514 void *switch_handle;
515#endif
516
Andrew Boie73abd322017-04-04 13:19:13 -0700517 /* arch-specifics: must always be at the end */
518 struct _thread_arch arch;
519};
520
521typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400522typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700523#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400524
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400525enum execution_context_types {
526 K_ISR = 0,
527 K_COOP_THREAD,
528 K_PREEMPT_THREAD,
529};
530
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400531/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100532 * @defgroup profiling_apis Profiling APIs
533 * @ingroup kernel_apis
534 * @{
535 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530536typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
537 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100538
539/**
540 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
541 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700542 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100543 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
544 *
545 * CONFIG_MAIN_STACK_SIZE
546 * CONFIG_IDLE_STACK_SIZE
547 * CONFIG_ISR_STACK_SIZE
548 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
549 *
550 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
551 * produce output.
552 *
553 * @return N/A
554 */
555extern void k_call_stacks_analyze(void);
556
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530557/**
558 * @brief Iterate over all the threads in the system.
559 *
560 * This routine iterates over all the threads in the system and
561 * calls the user_cb function for each thread.
562 *
563 * @param user_cb Pointer to the user callback function.
564 * @param user_data Pointer to user data.
565 *
566 * @note CONFIG_THREAD_MONITOR must be set for this function
567 * to be effective. Also this API uses irq_lock to protect the
568 * _kernel.threads list which means creation of new threads and
569 * terminations of existing threads are blocked until this
570 * API returns.
571 *
572 * @return N/A
573 */
574extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
575
Anas Nashif166f5192018-02-25 08:02:36 -0600576/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100577
578/**
Allan Stephensc98da842016-11-11 15:45:03 -0500579 * @defgroup thread_apis Thread APIs
580 * @ingroup kernel_apis
581 * @{
582 */
583
Benjamin Walshed240f22017-01-22 13:05:08 -0500584#endif /* !_ASMLANGUAGE */
585
586
587/*
588 * Thread user options. May be needed by assembly code. Common part uses low
589 * bits, arch-specific use high bits.
590 */
591
592/* system thread that must not abort */
593#define K_ESSENTIAL (1 << 0)
594
595#if defined(CONFIG_FP_SHARING)
596/* thread uses floating point registers */
597#define K_FP_REGS (1 << 1)
598#endif
599
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700600/* This thread has dropped from supervisor mode to user mode and consequently
601 * has additional restrictions
602 */
603#define K_USER (1 << 2)
604
Andrew Boie47f8fd12017-10-05 11:11:02 -0700605/* Indicates that the thread being created should inherit all kernel object
606 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
607 * is not enabled.
608 */
609#define K_INHERIT_PERMS (1 << 3)
610
Benjamin Walshed240f22017-01-22 13:05:08 -0500611#ifdef CONFIG_X86
612/* x86 Bitmask definitions for threads user options */
613
614#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
615/* thread uses SSEx (and also FP) registers */
616#define K_SSE_REGS (1 << 7)
617#endif
618#endif
619
620/* end - thread options */
621
622#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400623/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700624 * @brief Create a thread.
625 *
626 * This routine initializes a thread, then schedules it for execution.
627 *
628 * The new thread may be scheduled for immediate execution or a delayed start.
629 * If the newly spawned thread does not have a delayed start the kernel
630 * scheduler may preempt the current thread to allow the new thread to
631 * execute.
632 *
633 * Thread options are architecture-specific, and can include K_ESSENTIAL,
634 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
635 * them using "|" (the logical OR operator).
636 *
637 * Historically, users often would use the beginning of the stack memory region
638 * to store the struct k_thread data, although corruption will occur if the
639 * stack overflows this region and stack protection features may not detect this
640 * situation.
641 *
642 * @param new_thread Pointer to uninitialized struct k_thread
643 * @param stack Pointer to the stack space.
644 * @param stack_size Stack size in bytes.
645 * @param entry Thread entry function.
646 * @param p1 1st entry point parameter.
647 * @param p2 2nd entry point parameter.
648 * @param p3 3rd entry point parameter.
649 * @param prio Thread priority.
650 * @param options Thread options.
651 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
652 *
653 * @return ID of new thread.
654 */
Andrew Boie662c3452017-10-02 10:51:18 -0700655__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700656 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700657 size_t stack_size,
658 k_thread_entry_t entry,
659 void *p1, void *p2, void *p3,
660 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700661
Andrew Boie3f091b52017-08-30 14:34:14 -0700662/**
663 * @brief Drop a thread's privileges permanently to user mode
664 *
665 * @param entry Function to start executing from
666 * @param p1 1st entry point parameter
667 * @param p2 2nd entry point parameter
668 * @param p3 3rd entry point parameter
669 */
670extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
671 void *p1, void *p2,
672 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700673
Andrew Boied26cf2d2017-03-30 13:07:02 -0700674/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700675 * @brief Grant a thread access to a NULL-terminated set of kernel objects
676 *
677 * This is a convenience function. For the provided thread, grant access to
678 * the remaining arguments, which must be pointers to kernel objects.
679 * The final argument must be a NULL.
680 *
681 * The thread object must be initialized (i.e. running). The objects don't
682 * need to be.
683 *
684 * @param thread Thread to grant access to objects
685 * @param ... NULL-terminated list of kernel object pointers
686 */
687extern void __attribute__((sentinel))
688 k_thread_access_grant(struct k_thread *thread, ...);
689
690/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500691 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400692 *
Allan Stephensc98da842016-11-11 15:45:03 -0500693 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500694 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400695 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500696 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400697 *
698 * @return N/A
699 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700700__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400701
702/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500703 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400704 *
705 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500706 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400707 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400708 * @return N/A
709 */
Kumar Galacc334c72017-04-21 10:55:34 -0500710extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400711
712/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500713 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400714 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500715 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400716 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500717 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400718 *
719 * @return N/A
720 */
Andrew Boie468190a2017-09-29 14:00:48 -0700721__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400722
723/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500724 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400725 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500726 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400727 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500728 * If @a thread is not currently sleeping, the routine has no effect.
729 *
730 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400731 *
732 * @return N/A
733 */
Andrew Boie468190a2017-09-29 14:00:48 -0700734__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400735
736/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500737 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400738 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500739 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400740 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700741__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400742
743/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500744 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400745 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500746 * This routine prevents @a thread from executing if it has not yet started
747 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400748 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500749 * @param thread ID of thread to cancel.
750 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700751 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500752 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700753 *
754 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400755 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700756__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400757
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400758/**
Allan Stephensc98da842016-11-11 15:45:03 -0500759 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400760 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500761 * This routine permanently stops execution of @a thread. The thread is taken
762 * off all kernel queues it is part of (i.e. the ready queue, the timeout
763 * queue, or a kernel object wait queue). However, any kernel resources the
764 * thread might currently own (such as mutexes or memory blocks) are not
765 * released. It is the responsibility of the caller of this routine to ensure
766 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400767 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500768 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400769 *
770 * @return N/A
771 */
Andrew Boie468190a2017-09-29 14:00:48 -0700772__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400773
Andrew Boie7d627c52017-08-30 11:01:56 -0700774
775/**
776 * @brief Start an inactive thread
777 *
778 * If a thread was created with K_FOREVER in the delay parameter, it will
779 * not be added to the scheduling queue until this function is called
780 * on it.
781 *
782 * @param thread thread to start
783 */
Andrew Boie468190a2017-09-29 14:00:48 -0700784__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700785
Allan Stephensc98da842016-11-11 15:45:03 -0500786/**
787 * @cond INTERNAL_HIDDEN
788 */
789
Benjamin Walshd211a522016-12-06 11:44:01 -0500790/* timeout has timed out and is not on _timeout_q anymore */
791#define _EXPIRED (-2)
792
793/* timeout is not in use */
794#define _INACTIVE (-1)
795
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400796struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700797 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700798 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400799 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700800 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500801 void *init_p1;
802 void *init_p2;
803 void *init_p3;
804 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500805 u32_t init_options;
806 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500807 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400808};
809
Andrew Boied26cf2d2017-03-30 13:07:02 -0700810#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400811 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500812 prio, options, delay, abort, groups) \
813 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700814 .init_thread = (thread), \
815 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500816 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700817 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400818 .init_p1 = (void *)p1, \
819 .init_p2 = (void *)p2, \
820 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500821 .init_prio = (prio), \
822 .init_options = (options), \
823 .init_delay = (delay), \
824 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400825 }
826
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400827/**
Allan Stephensc98da842016-11-11 15:45:03 -0500828 * INTERNAL_HIDDEN @endcond
829 */
830
831/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500832 * @brief Statically define and initialize a thread.
833 *
834 * The thread may be scheduled for immediate execution or a delayed start.
835 *
836 * Thread options are architecture-specific, and can include K_ESSENTIAL,
837 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
838 * them using "|" (the logical OR operator).
839 *
840 * The ID of the thread can be accessed using:
841 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500842 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500843 *
844 * @param name Name of the thread.
845 * @param stack_size Stack size in bytes.
846 * @param entry Thread entry function.
847 * @param p1 1st entry point parameter.
848 * @param p2 2nd entry point parameter.
849 * @param p3 3rd entry point parameter.
850 * @param prio Thread priority.
851 * @param options Thread options.
852 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400853 *
854 * @internal It has been observed that the x86 compiler by default aligns
855 * these _static_thread_data structures to 32-byte boundaries, thereby
856 * wasting space. To work around this, force a 4-byte alignment.
857 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500858#define K_THREAD_DEFINE(name, stack_size, \
859 entry, p1, p2, p3, \
860 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700861 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700862 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500863 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500864 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700865 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
866 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500867 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700868 NULL, 0); \
869 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400870
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400871/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500872 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400873 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500874 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400875 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500876 * @param thread ID of thread whose priority is needed.
877 *
878 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400879 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700880__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400881
882/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500883 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400884 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500885 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400886 *
887 * Rescheduling can occur immediately depending on the priority @a thread is
888 * set to:
889 *
890 * - If its priority is raised above the priority of the caller of this
891 * function, and the caller is preemptible, @a thread will be scheduled in.
892 *
893 * - If the caller operates on itself, it lowers its priority below that of
894 * other threads in the system, and the caller is preemptible, the thread of
895 * highest priority will be scheduled in.
896 *
897 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
898 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
899 * highest priority.
900 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500901 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400902 * @param prio New priority.
903 *
904 * @warning Changing the priority of a thread currently involved in mutex
905 * priority inheritance may result in undefined behavior.
906 *
907 * @return N/A
908 */
Andrew Boie468190a2017-09-29 14:00:48 -0700909__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400910
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400911/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500912 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400913 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500914 * This routine prevents the kernel scheduler from making @a thread the
915 * current thread. All other internal operations on @a thread are still
916 * performed; for example, any timeout it is waiting on keeps ticking,
917 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400918 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500919 * If @a thread is already suspended, the routine has no effect.
920 *
921 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400922 *
923 * @return N/A
924 */
Andrew Boie468190a2017-09-29 14:00:48 -0700925__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400926
927/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500928 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400929 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500930 * This routine allows the kernel scheduler to make @a thread the current
931 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400932 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500933 * If @a thread is not currently suspended, the routine has no effect.
934 *
935 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400936 *
937 * @return N/A
938 */
Andrew Boie468190a2017-09-29 14:00:48 -0700939__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400940
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400941/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500942 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400943 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500944 * This routine specifies how the scheduler will perform time slicing of
945 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400946 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500947 * To enable time slicing, @a slice must be non-zero. The scheduler
948 * ensures that no thread runs for more than the specified time limit
949 * before other threads of that priority are given a chance to execute.
950 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700951 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400952 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500953 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400954 * execute. Once the scheduler selects a thread for execution, there is no
955 * minimum guaranteed time the thread will execute before threads of greater or
956 * equal priority are scheduled.
957 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500958 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400959 * for execution, this routine has no effect; the thread is immediately
960 * rescheduled after the slice period expires.
961 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500962 * To disable timeslicing, set both @a slice and @a prio to zero.
963 *
964 * @param slice Maximum time slice length (in milliseconds).
965 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400966 *
967 * @return N/A
968 */
Kumar Galacc334c72017-04-21 10:55:34 -0500969extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400970
Anas Nashif166f5192018-02-25 08:02:36 -0600971/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -0500972
973/**
974 * @addtogroup isr_apis
975 * @{
976 */
977
978/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500979 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400980 *
Allan Stephensc98da842016-11-11 15:45:03 -0500981 * This routine allows the caller to customize its actions, depending on
982 * whether it is a thread or an ISR.
983 *
984 * @note Can be called by ISRs.
985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500986 * @return 0 if invoked by a thread.
987 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400988 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500989extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400990
Benjamin Walsh445830d2016-11-10 15:54:27 -0500991/**
992 * @brief Determine if code is running in a preemptible thread.
993 *
Allan Stephensc98da842016-11-11 15:45:03 -0500994 * This routine allows the caller to customize its actions, depending on
995 * whether it can be preempted by another thread. The routine returns a 'true'
996 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500997 *
Allan Stephensc98da842016-11-11 15:45:03 -0500998 * - The code is running in a thread, not at ISR.
999 * - The thread's priority is in the preemptible range.
1000 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001001 *
Allan Stephensc98da842016-11-11 15:45:03 -05001002 * @note Can be called by ISRs.
1003 *
1004 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001005 * @return Non-zero if invoked by a preemptible thread.
1006 */
Andrew Boie468190a2017-09-29 14:00:48 -07001007__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001008
Allan Stephensc98da842016-11-11 15:45:03 -05001009/**
Anas Nashif166f5192018-02-25 08:02:36 -06001010 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001011 */
1012
1013/**
1014 * @addtogroup thread_apis
1015 * @{
1016 */
1017
1018/**
1019 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001020 *
Allan Stephensc98da842016-11-11 15:45:03 -05001021 * This routine prevents the current thread from being preempted by another
1022 * thread by instructing the scheduler to treat it as a cooperative thread.
1023 * If the thread subsequently performs an operation that makes it unready,
1024 * it will be context switched out in the normal manner. When the thread
1025 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001026 *
Allan Stephensc98da842016-11-11 15:45:03 -05001027 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001028 *
Allan Stephensc98da842016-11-11 15:45:03 -05001029 * @note k_sched_lock() and k_sched_unlock() should normally be used
1030 * when the operation being performed can be safely interrupted by ISRs.
1031 * However, if the amount of processing involved is very small, better
1032 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001033 *
1034 * @return N/A
1035 */
1036extern void k_sched_lock(void);
1037
Allan Stephensc98da842016-11-11 15:45:03 -05001038/**
1039 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001040 *
Allan Stephensc98da842016-11-11 15:45:03 -05001041 * This routine reverses the effect of a previous call to k_sched_lock().
1042 * A thread must call the routine once for each time it called k_sched_lock()
1043 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001044 *
1045 * @return N/A
1046 */
1047extern void k_sched_unlock(void);
1048
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001049/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001050 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001051 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001052 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001053 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001054 * Custom data is not used by the kernel itself, and is freely available
1055 * for a thread to use as it sees fit. It can be used as a framework
1056 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001058 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001059 *
1060 * @return N/A
1061 */
Andrew Boie468190a2017-09-29 14:00:48 -07001062__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001063
1064/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001065 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001066 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001067 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001068 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001069 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001070 */
Andrew Boie468190a2017-09-29 14:00:48 -07001071__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001072
1073/**
Anas Nashif166f5192018-02-25 08:02:36 -06001074 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001075 */
1076
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001077#include <sys_clock.h>
1078
Allan Stephensc2f15a42016-11-17 12:24:22 -05001079/**
1080 * @addtogroup clock_apis
1081 * @{
1082 */
1083
1084/**
1085 * @brief Generate null timeout delay.
1086 *
1087 * This macro generates a timeout delay that that instructs a kernel API
1088 * not to wait if the requested operation cannot be performed immediately.
1089 *
1090 * @return Timeout delay value.
1091 */
1092#define K_NO_WAIT 0
1093
1094/**
1095 * @brief Generate timeout delay from milliseconds.
1096 *
1097 * This macro generates a timeout delay that that instructs a kernel API
1098 * to wait up to @a ms milliseconds to perform the requested operation.
1099 *
1100 * @param ms Duration in milliseconds.
1101 *
1102 * @return Timeout delay value.
1103 */
Johan Hedberg14471692016-11-13 10:52:15 +02001104#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001105
1106/**
1107 * @brief Generate timeout delay from seconds.
1108 *
1109 * This macro generates a timeout delay that that instructs a kernel API
1110 * to wait up to @a s seconds to perform the requested operation.
1111 *
1112 * @param s Duration in seconds.
1113 *
1114 * @return Timeout delay value.
1115 */
Johan Hedberg14471692016-11-13 10:52:15 +02001116#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001117
1118/**
1119 * @brief Generate timeout delay from minutes.
1120 *
1121 * This macro generates a timeout delay that that instructs a kernel API
1122 * to wait up to @a m minutes to perform the requested operation.
1123 *
1124 * @param m Duration in minutes.
1125 *
1126 * @return Timeout delay value.
1127 */
Johan Hedberg14471692016-11-13 10:52:15 +02001128#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001129
1130/**
1131 * @brief Generate timeout delay from hours.
1132 *
1133 * This macro generates a timeout delay that that instructs a kernel API
1134 * to wait up to @a h hours to perform the requested operation.
1135 *
1136 * @param h Duration in hours.
1137 *
1138 * @return Timeout delay value.
1139 */
Johan Hedberg14471692016-11-13 10:52:15 +02001140#define K_HOURS(h) K_MINUTES((h) * 60)
1141
Allan Stephensc98da842016-11-11 15:45:03 -05001142/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001143 * @brief Generate infinite timeout delay.
1144 *
1145 * This macro generates a timeout delay that that instructs a kernel API
1146 * to wait as long as necessary to perform the requested operation.
1147 *
1148 * @return Timeout delay value.
1149 */
1150#define K_FOREVER (-1)
1151
1152/**
Anas Nashif166f5192018-02-25 08:02:36 -06001153 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001154 */
1155
1156/**
Allan Stephensc98da842016-11-11 15:45:03 -05001157 * @cond INTERNAL_HIDDEN
1158 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001159
Benjamin Walsh62092182016-12-20 14:39:08 -05001160/* kernel clocks */
1161
1162#if (sys_clock_ticks_per_sec == 1000) || \
1163 (sys_clock_ticks_per_sec == 500) || \
1164 (sys_clock_ticks_per_sec == 250) || \
1165 (sys_clock_ticks_per_sec == 125) || \
1166 (sys_clock_ticks_per_sec == 100) || \
1167 (sys_clock_ticks_per_sec == 50) || \
1168 (sys_clock_ticks_per_sec == 25) || \
1169 (sys_clock_ticks_per_sec == 20) || \
1170 (sys_clock_ticks_per_sec == 10) || \
1171 (sys_clock_ticks_per_sec == 1)
1172
1173 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1174#else
1175 /* yields horrible 64-bit math on many architectures: try to avoid */
1176 #define _NON_OPTIMIZED_TICKS_PER_SEC
1177#endif
1178
1179#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001180extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001181#else
Kumar Galacc334c72017-04-21 10:55:34 -05001182static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001183{
Kumar Galacc334c72017-04-21 10:55:34 -05001184 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001185}
1186#endif
1187
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001188/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001189#ifdef CONFIG_TICKLESS_KERNEL
1190#define _TICK_ALIGN 0
1191#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001192#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001193#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001194
Kumar Galacc334c72017-04-21 10:55:34 -05001195static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001196{
Benjamin Walsh62092182016-12-20 14:39:08 -05001197#ifdef CONFIG_SYS_CLOCK_EXISTS
1198
1199#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001200 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001201#else
Kumar Galacc334c72017-04-21 10:55:34 -05001202 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001203#endif
1204
1205#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001206 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001207 return 0;
1208#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001209}
1210
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001211struct k_timer {
1212 /*
1213 * _timeout structure must be first here if we want to use
1214 * dynamic timer allocation. timeout.node is used in the double-linked
1215 * list of free timers
1216 */
1217 struct _timeout timeout;
1218
Allan Stephens45bfa372016-10-12 12:39:42 -05001219 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001220 _wait_q_t wait_q;
1221
1222 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001223 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001224
1225 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001226 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001227
1228 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001229 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001230
Allan Stephens45bfa372016-10-12 12:39:42 -05001231 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001232 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001233
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001234 /* user-specific data, also used to support legacy features */
1235 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001236
Anas Nashif2f203c22016-12-18 06:57:45 -05001237 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001238};
1239
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001240#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001241 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001242 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001243 .timeout.wait_q = NULL, \
1244 .timeout.thread = NULL, \
1245 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001246 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001247 .expiry_fn = expiry, \
1248 .stop_fn = stop, \
1249 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001250 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001251 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001252 }
1253
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001254#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1255
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001256/**
Allan Stephensc98da842016-11-11 15:45:03 -05001257 * INTERNAL_HIDDEN @endcond
1258 */
1259
1260/**
1261 * @defgroup timer_apis Timer APIs
1262 * @ingroup kernel_apis
1263 * @{
1264 */
1265
1266/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001267 * @typedef k_timer_expiry_t
1268 * @brief Timer expiry function type.
1269 *
1270 * A timer's expiry function is executed by the system clock interrupt handler
1271 * each time the timer expires. The expiry function is optional, and is only
1272 * invoked if the timer has been initialized with one.
1273 *
1274 * @param timer Address of timer.
1275 *
1276 * @return N/A
1277 */
1278typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1279
1280/**
1281 * @typedef k_timer_stop_t
1282 * @brief Timer stop function type.
1283 *
1284 * A timer's stop function is executed if the timer is stopped prematurely.
1285 * The function runs in the context of the thread that stops the timer.
1286 * The stop function is optional, and is only invoked if the timer has been
1287 * initialized with one.
1288 *
1289 * @param timer Address of timer.
1290 *
1291 * @return N/A
1292 */
1293typedef void (*k_timer_stop_t)(struct k_timer *timer);
1294
1295/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001296 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001297 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001298 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001299 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001300 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001301 *
1302 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001303 * @param expiry_fn Function to invoke each time the timer expires.
1304 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001305 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001306#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001307 struct k_timer name \
1308 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001309 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001310
Allan Stephens45bfa372016-10-12 12:39:42 -05001311/**
1312 * @brief Initialize a timer.
1313 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001314 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001315 *
1316 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001317 * @param expiry_fn Function to invoke each time the timer expires.
1318 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001319 *
1320 * @return N/A
1321 */
1322extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001323 k_timer_expiry_t expiry_fn,
1324 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001325
Allan Stephens45bfa372016-10-12 12:39:42 -05001326/**
1327 * @brief Start a timer.
1328 *
1329 * This routine starts a timer, and resets its status to zero. The timer
1330 * begins counting down using the specified duration and period values.
1331 *
1332 * Attempting to start a timer that is already running is permitted.
1333 * The timer's status is reset to zero and the timer begins counting down
1334 * using the new duration and period values.
1335 *
1336 * @param timer Address of timer.
1337 * @param duration Initial timer duration (in milliseconds).
1338 * @param period Timer period (in milliseconds).
1339 *
1340 * @return N/A
1341 */
Andrew Boiea354d492017-09-29 16:22:28 -07001342__syscall void k_timer_start(struct k_timer *timer,
1343 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001344
1345/**
1346 * @brief Stop a timer.
1347 *
1348 * This routine stops a running timer prematurely. The timer's stop function,
1349 * if one exists, is invoked by the caller.
1350 *
1351 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001352 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001353 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001354 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1355 * if @a k_timer_stop is to be called from ISRs.
1356 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001357 * @param timer Address of timer.
1358 *
1359 * @return N/A
1360 */
Andrew Boiea354d492017-09-29 16:22:28 -07001361__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001362
1363/**
1364 * @brief Read timer status.
1365 *
1366 * This routine reads the timer's status, which indicates the number of times
1367 * it has expired since its status was last read.
1368 *
1369 * Calling this routine resets the timer's status to zero.
1370 *
1371 * @param timer Address of timer.
1372 *
1373 * @return Timer status.
1374 */
Andrew Boiea354d492017-09-29 16:22:28 -07001375__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001376
1377/**
1378 * @brief Synchronize thread to timer expiration.
1379 *
1380 * This routine blocks the calling thread until the timer's status is non-zero
1381 * (indicating that it has expired at least once since it was last examined)
1382 * or the timer is stopped. If the timer status is already non-zero,
1383 * or the timer is already stopped, the caller continues without waiting.
1384 *
1385 * Calling this routine resets the timer's status to zero.
1386 *
1387 * This routine must not be used by interrupt handlers, since they are not
1388 * allowed to block.
1389 *
1390 * @param timer Address of timer.
1391 *
1392 * @return Timer status.
1393 */
Andrew Boiea354d492017-09-29 16:22:28 -07001394__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001395
1396/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001397 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001398 *
1399 * This routine computes the (approximate) time remaining before a running
1400 * timer next expires. If the timer is not running, it returns zero.
1401 *
1402 * @param timer Address of timer.
1403 *
1404 * @return Remaining time (in milliseconds).
1405 */
Andrew Boiea354d492017-09-29 16:22:28 -07001406__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1407
1408static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001409{
1410 return _timeout_remaining_get(&timer->timeout);
1411}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001412
Allan Stephensc98da842016-11-11 15:45:03 -05001413/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001414 * @brief Associate user-specific data with a timer.
1415 *
1416 * This routine records the @a user_data with the @a timer, to be retrieved
1417 * later.
1418 *
1419 * It can be used e.g. in a timer handler shared across multiple subsystems to
1420 * retrieve data specific to the subsystem this timer is associated with.
1421 *
1422 * @param timer Address of timer.
1423 * @param user_data User data to associate with the timer.
1424 *
1425 * @return N/A
1426 */
Andrew Boiea354d492017-09-29 16:22:28 -07001427__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1428
Anas Nashif954d5502018-02-25 08:37:28 -06001429/**
1430 * @internal
1431 */
Andrew Boiea354d492017-09-29 16:22:28 -07001432static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1433 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001434{
1435 timer->user_data = user_data;
1436}
1437
1438/**
1439 * @brief Retrieve the user-specific data from a timer.
1440 *
1441 * @param timer Address of timer.
1442 *
1443 * @return The user data.
1444 */
Andrew Boiea354d492017-09-29 16:22:28 -07001445__syscall void *k_timer_user_data_get(struct k_timer *timer);
1446
1447static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001448{
1449 return timer->user_data;
1450}
1451
Anas Nashif166f5192018-02-25 08:02:36 -06001452/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001453
Allan Stephensc98da842016-11-11 15:45:03 -05001454/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001455 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001456 * @{
1457 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001458
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001459/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001460 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001461 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001462 * This routine returns the elapsed time since the system booted,
1463 * in milliseconds.
1464 *
1465 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001466 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001467__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001468
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001469/**
1470 * @brief Enable clock always on in tickless kernel
1471 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001472 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001473 * there are no timer events programmed in tickless kernel
1474 * scheduling. This is necessary if the clock is used to track
1475 * passage of time.
1476 *
1477 * @retval prev_status Previous status of always on flag
1478 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301479#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001480static inline int k_enable_sys_clock_always_on(void)
1481{
1482 int prev_status = _sys_clock_always_on;
1483
1484 _sys_clock_always_on = 1;
1485 _enable_sys_clock();
1486
1487 return prev_status;
1488}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301489#else
1490#define k_enable_sys_clock_always_on() do { } while ((0))
1491#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001492
1493/**
1494 * @brief Disable clock always on in tickless kernel
1495 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001496 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001497 * there are no timer events programmed in tickless kernel
1498 * scheduling. To save power, this routine should be called
1499 * immediately when clock is not used to track time.
1500 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301501#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001502static inline void k_disable_sys_clock_always_on(void)
1503{
1504 _sys_clock_always_on = 0;
1505}
1506#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001507#define k_disable_sys_clock_always_on() do { } while ((0))
1508#endif
1509
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001510/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001511 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001512 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001513 * This routine returns the lower 32-bits of the elapsed time since the system
1514 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001515 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001516 * This routine can be more efficient than k_uptime_get(), as it reduces the
1517 * need for interrupt locking and 64-bit math. However, the 32-bit result
1518 * cannot hold a system uptime time larger than approximately 50 days, so the
1519 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001520 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001521 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001522 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001523__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001524
1525/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001526 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001527 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001528 * This routine computes the elapsed time between the current system uptime
1529 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001530 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001531 * @param reftime Pointer to a reference time, which is updated to the current
1532 * uptime upon return.
1533 *
1534 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001535 */
Kumar Galacc334c72017-04-21 10:55:34 -05001536extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001537
1538/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001539 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001540 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001541 * This routine computes the elapsed time between the current system uptime
1542 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001543 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001544 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1545 * need for interrupt locking and 64-bit math. However, the 32-bit result
1546 * cannot hold an elapsed time larger than approximately 50 days, so the
1547 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001548 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001549 * @param reftime Pointer to a reference time, which is updated to the current
1550 * uptime upon return.
1551 *
1552 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001553 */
Kumar Galacc334c72017-04-21 10:55:34 -05001554extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001555
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001556/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001557 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001558 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001559 * This routine returns the current time, as measured by the system's hardware
1560 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001561 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001562 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001563 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001564#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001565
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001566/**
Anas Nashif166f5192018-02-25 08:02:36 -06001567 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001568 */
1569
Allan Stephensc98da842016-11-11 15:45:03 -05001570/**
1571 * @cond INTERNAL_HIDDEN
1572 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001573
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001574struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001575 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001576 union {
1577 _wait_q_t wait_q;
1578
1579 _POLL_EVENT;
1580 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001581
1582 _OBJECT_TRACING_NEXT_PTR(k_queue);
1583};
1584
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001585#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001586 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001587 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Michael Hope5f67a612018-03-17 12:44:40 +01001588 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001589 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001590 _OBJECT_TRACING_INIT \
1591 }
1592
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001593#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1594
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001595/**
1596 * INTERNAL_HIDDEN @endcond
1597 */
1598
1599/**
1600 * @defgroup queue_apis Queue APIs
1601 * @ingroup kernel_apis
1602 * @{
1603 */
1604
1605/**
1606 * @brief Initialize a queue.
1607 *
1608 * This routine initializes a queue object, prior to its first use.
1609 *
1610 * @param queue Address of the queue.
1611 *
1612 * @return N/A
1613 */
1614extern void k_queue_init(struct k_queue *queue);
1615
1616/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001617 * @brief Cancel waiting on a queue.
1618 *
1619 * This routine causes first thread pending on @a queue, if any, to
1620 * return from k_queue_get() call with NULL value (as if timeout expired).
1621 *
1622 * @note Can be called by ISRs.
1623 *
1624 * @param queue Address of the queue.
1625 *
1626 * @return N/A
1627 */
1628extern void k_queue_cancel_wait(struct k_queue *queue);
1629
1630/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001631 * @brief Append an element to the end of a queue.
1632 *
1633 * This routine appends a data item to @a queue. A queue data item must be
1634 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1635 * reserved for the kernel's use.
1636 *
1637 * @note Can be called by ISRs.
1638 *
1639 * @param queue Address of the queue.
1640 * @param data Address of the data item.
1641 *
1642 * @return N/A
1643 */
1644extern void k_queue_append(struct k_queue *queue, void *data);
1645
1646/**
1647 * @brief Prepend an element to a queue.
1648 *
1649 * This routine prepends a data item to @a queue. A queue data item must be
1650 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1651 * reserved for the kernel's use.
1652 *
1653 * @note Can be called by ISRs.
1654 *
1655 * @param queue Address of the queue.
1656 * @param data Address of the data item.
1657 *
1658 * @return N/A
1659 */
1660extern void k_queue_prepend(struct k_queue *queue, void *data);
1661
1662/**
1663 * @brief Inserts an element to a queue.
1664 *
1665 * This routine inserts a data item to @a queue after previous item. A queue
1666 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1667 * item are reserved for the kernel's use.
1668 *
1669 * @note Can be called by ISRs.
1670 *
1671 * @param queue Address of the queue.
1672 * @param prev Address of the previous data item.
1673 * @param data Address of the data item.
1674 *
1675 * @return N/A
1676 */
1677extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1678
1679/**
1680 * @brief Atomically append a list of elements to a queue.
1681 *
1682 * This routine adds a list of data items to @a queue in one operation.
1683 * The data items must be in a singly-linked list, with the first 32 bits
1684 * in each data item pointing to the next data item; the list must be
1685 * NULL-terminated.
1686 *
1687 * @note Can be called by ISRs.
1688 *
1689 * @param queue Address of the queue.
1690 * @param head Pointer to first node in singly-linked list.
1691 * @param tail Pointer to last node in singly-linked list.
1692 *
1693 * @return N/A
1694 */
1695extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1696
1697/**
1698 * @brief Atomically add a list of elements to a queue.
1699 *
1700 * This routine adds a list of data items to @a queue in one operation.
1701 * The data items must be in a singly-linked list implemented using a
1702 * sys_slist_t object. Upon completion, the original list is empty.
1703 *
1704 * @note Can be called by ISRs.
1705 *
1706 * @param queue Address of the queue.
1707 * @param list Pointer to sys_slist_t object.
1708 *
1709 * @return N/A
1710 */
1711extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1712
1713/**
1714 * @brief Get an element from a queue.
1715 *
1716 * This routine removes first data item from @a queue. The first 32 bits of the
1717 * data item are reserved for the kernel's use.
1718 *
1719 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1720 *
1721 * @param queue Address of the queue.
1722 * @param timeout Waiting period to obtain a data item (in milliseconds),
1723 * or one of the special values K_NO_WAIT and K_FOREVER.
1724 *
1725 * @return Address of the data item if successful; NULL if returned
1726 * without waiting, or waiting period timed out.
1727 */
Kumar Galacc334c72017-04-21 10:55:34 -05001728extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001729
1730/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001731 * @brief Remove an element from a queue.
1732 *
1733 * This routine removes data item from @a queue. The first 32 bits of the
1734 * data item are reserved for the kernel's use. Removing elements from k_queue
1735 * rely on sys_slist_find_and_remove which is not a constant time operation.
1736 *
1737 * @note Can be called by ISRs
1738 *
1739 * @param queue Address of the queue.
1740 * @param data Address of the data item.
1741 *
1742 * @return true if data item was removed
1743 */
1744static inline bool k_queue_remove(struct k_queue *queue, void *data)
1745{
1746 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1747}
1748
1749/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001750 * @brief Query a queue to see if it has data available.
1751 *
1752 * Note that the data might be already gone by the time this function returns
1753 * if other threads are also trying to read from the queue.
1754 *
1755 * @note Can be called by ISRs.
1756 *
1757 * @param queue Address of the queue.
1758 *
1759 * @return Non-zero if the queue is empty.
1760 * @return 0 if data is available.
1761 */
1762static inline int k_queue_is_empty(struct k_queue *queue)
1763{
1764 return (int)sys_slist_is_empty(&queue->data_q);
1765}
1766
1767/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001768 * @brief Peek element at the head of queue.
1769 *
1770 * Return element from the head of queue without removing it.
1771 *
1772 * @param queue Address of the queue.
1773 *
1774 * @return Head element, or NULL if queue is empty.
1775 */
1776static inline void *k_queue_peek_head(struct k_queue *queue)
1777{
1778 return sys_slist_peek_head(&queue->data_q);
1779}
1780
1781/**
1782 * @brief Peek element at the tail of queue.
1783 *
1784 * Return element from the tail of queue without removing it.
1785 *
1786 * @param queue Address of the queue.
1787 *
1788 * @return Tail element, or NULL if queue is empty.
1789 */
1790static inline void *k_queue_peek_tail(struct k_queue *queue)
1791{
1792 return sys_slist_peek_tail(&queue->data_q);
1793}
1794
1795/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001796 * @brief Statically define and initialize a queue.
1797 *
1798 * The queue can be accessed outside the module where it is defined using:
1799 *
1800 * @code extern struct k_queue <name>; @endcode
1801 *
1802 * @param name Name of the queue.
1803 */
1804#define K_QUEUE_DEFINE(name) \
1805 struct k_queue name \
1806 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001807 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001808
Anas Nashif166f5192018-02-25 08:02:36 -06001809/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001810
1811/**
1812 * @cond INTERNAL_HIDDEN
1813 */
1814
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001815struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001816 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001817};
1818
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001819#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001820 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001821 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001822 }
1823
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001824#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1825
Allan Stephensc98da842016-11-11 15:45:03 -05001826/**
1827 * INTERNAL_HIDDEN @endcond
1828 */
1829
1830/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001831 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05001832 * @ingroup kernel_apis
1833 * @{
1834 */
1835
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001836/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001837 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001838 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001839 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001840 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001841 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001842 *
1843 * @return N/A
1844 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001845#define k_fifo_init(fifo) \
1846 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001847
1848/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001849 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001850 *
1851 * This routine causes first thread pending on @a fifo, if any, to
1852 * return from k_fifo_get() call with NULL value (as if timeout
1853 * expired).
1854 *
1855 * @note Can be called by ISRs.
1856 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001857 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001858 *
1859 * @return N/A
1860 */
1861#define k_fifo_cancel_wait(fifo) \
1862 k_queue_cancel_wait((struct k_queue *) fifo)
1863
1864/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001865 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001866 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001867 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001868 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1869 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001871 * @note Can be called by ISRs.
1872 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001873 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001874 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001875 *
1876 * @return N/A
1877 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001878#define k_fifo_put(fifo, data) \
1879 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001880
1881/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001882 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001884 * This routine adds a list of data items to @a fifo in one operation.
1885 * The data items must be in a singly-linked list, with the first 32 bits
1886 * each data item pointing to the next data item; the list must be
1887 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001888 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001889 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001890 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001891 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001892 * @param head Pointer to first node in singly-linked list.
1893 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001894 *
1895 * @return N/A
1896 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001897#define k_fifo_put_list(fifo, head, tail) \
1898 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001899
1900/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001901 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001902 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001903 * This routine adds a list of data items to @a fifo in one operation.
1904 * The data items must be in a singly-linked list implemented using a
1905 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001906 * and must be re-initialized via sys_slist_init().
1907 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001908 * @note Can be called by ISRs.
1909 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001910 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001911 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001912 *
1913 * @return N/A
1914 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001915#define k_fifo_put_slist(fifo, list) \
1916 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001917
1918/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001919 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001920 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001921 * This routine removes a data item from @a fifo in a "first in, first out"
1922 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001923 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001924 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1925 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001926 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001927 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001928 * or one of the special values K_NO_WAIT and K_FOREVER.
1929 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001930 * @return Address of the data item if successful; NULL if returned
1931 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001932 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001933#define k_fifo_get(fifo, timeout) \
1934 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001935
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001936/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001937 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001938 *
1939 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06001940 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001941 *
1942 * @note Can be called by ISRs.
1943 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001944 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001945 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001946 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001947 * @return 0 if data is available.
1948 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001949#define k_fifo_is_empty(fifo) \
1950 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001951
1952/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001953 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001954 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001955 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05301956 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001957 * on each iteration of processing, a head container will be peeked,
1958 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06001959 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001960 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001961 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001962 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001963 * @return Head element, or NULL if the FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001964 */
1965#define k_fifo_peek_head(fifo) \
1966 k_queue_peek_head((struct k_queue *) fifo)
1967
1968/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001969 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001970 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001971 * Return element from the tail of FIFO queue (without removing it). A usecase
1972 * for this is if elements of the FIFO queue are themselves containers. Then
1973 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001974 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001975 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001976 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001977 * @return Tail element, or NULL if a FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001978 */
1979#define k_fifo_peek_tail(fifo) \
1980 k_queue_peek_tail((struct k_queue *) fifo)
1981
1982/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001983 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001984 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001985 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001986 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001987 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001988 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001989 * @param name Name of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001990 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001991#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001992 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001993 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001994 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001995
Anas Nashif166f5192018-02-25 08:02:36 -06001996/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001997
1998/**
1999 * @cond INTERNAL_HIDDEN
2000 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002001
2002struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002003 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002004};
2005
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002006#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002007 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002008 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002009 }
2010
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002011#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2012
Allan Stephensc98da842016-11-11 15:45:03 -05002013/**
2014 * INTERNAL_HIDDEN @endcond
2015 */
2016
2017/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002018 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002019 * @ingroup kernel_apis
2020 * @{
2021 */
2022
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002023/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002024 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002025 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002026 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002027 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002028 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002029 *
2030 * @return N/A
2031 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002032#define k_lifo_init(lifo) \
2033 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002034
2035/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002036 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002037 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002038 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002039 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2040 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002041 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002042 * @note Can be called by ISRs.
2043 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002044 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002045 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002046 *
2047 * @return N/A
2048 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002049#define k_lifo_put(lifo, data) \
2050 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002051
2052/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002053 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002054 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002055 * This routine removes a data item from @a lifo in a "last in, first out"
2056 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002058 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2059 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002060 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002061 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002062 * or one of the special values K_NO_WAIT and K_FOREVER.
2063 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002064 * @return Address of the data item if successful; NULL if returned
2065 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002066 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002067#define k_lifo_get(lifo, timeout) \
2068 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002069
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002070/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002071 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002072 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002073 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002074 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002075 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002076 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002077 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002078 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002079#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002080 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002081 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002082 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002083
Anas Nashif166f5192018-02-25 08:02:36 -06002084/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002085
2086/**
2087 * @cond INTERNAL_HIDDEN
2088 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002089
2090struct k_stack {
2091 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002092 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002093
Anas Nashif2f203c22016-12-18 06:57:45 -05002094 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002095};
2096
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002097#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002098 { \
2099 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2100 .base = stack_buffer, \
2101 .next = stack_buffer, \
2102 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002103 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002104 }
2105
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002106#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2107
Allan Stephensc98da842016-11-11 15:45:03 -05002108/**
2109 * INTERNAL_HIDDEN @endcond
2110 */
2111
2112/**
2113 * @defgroup stack_apis Stack APIs
2114 * @ingroup kernel_apis
2115 * @{
2116 */
2117
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002118/**
2119 * @brief Initialize a stack.
2120 *
2121 * This routine initializes a stack object, prior to its first use.
2122 *
2123 * @param stack Address of the stack.
2124 * @param buffer Address of array used to hold stacked values.
2125 * @param num_entries Maximum number of values that can be stacked.
2126 *
2127 * @return N/A
2128 */
Andrew Boiee8734462017-09-29 16:42:07 -07002129__syscall void k_stack_init(struct k_stack *stack,
Andrew Boie8e3e6d02017-10-12 14:01:25 -07002130 u32_t *buffer, unsigned int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002131
2132/**
2133 * @brief Push an element onto a stack.
2134 *
2135 * This routine adds a 32-bit value @a data to @a stack.
2136 *
2137 * @note Can be called by ISRs.
2138 *
2139 * @param stack Address of the stack.
2140 * @param data Value to push onto the stack.
2141 *
2142 * @return N/A
2143 */
Andrew Boiee8734462017-09-29 16:42:07 -07002144__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002145
2146/**
2147 * @brief Pop an element from a stack.
2148 *
2149 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2150 * manner and stores the value in @a data.
2151 *
2152 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2153 *
2154 * @param stack Address of the stack.
2155 * @param data Address of area to hold the value popped from the stack.
2156 * @param timeout Waiting period to obtain a value (in milliseconds),
2157 * or one of the special values K_NO_WAIT and K_FOREVER.
2158 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002159 * @retval 0 Element popped from stack.
2160 * @retval -EBUSY Returned without waiting.
2161 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002162 */
Andrew Boiee8734462017-09-29 16:42:07 -07002163__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002164
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002165/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002166 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002167 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002168 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002169 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002170 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002171 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002172 * @param name Name of the stack.
2173 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002174 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002175#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002176 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002177 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002178 struct k_stack name \
2179 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002180 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002181 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002182
Anas Nashif166f5192018-02-25 08:02:36 -06002183/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002184
Allan Stephens6bba9b02016-11-16 14:56:54 -05002185struct k_work;
2186
Allan Stephensc98da842016-11-11 15:45:03 -05002187/**
2188 * @defgroup workqueue_apis Workqueue Thread APIs
2189 * @ingroup kernel_apis
2190 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002191 */
2192
Allan Stephens6bba9b02016-11-16 14:56:54 -05002193/**
2194 * @typedef k_work_handler_t
2195 * @brief Work item handler function type.
2196 *
2197 * A work item's handler function is executed by a workqueue's thread
2198 * when the work item is processed by the workqueue.
2199 *
2200 * @param work Address of the work item.
2201 *
2202 * @return N/A
2203 */
2204typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002205
2206/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002207 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002208 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002209
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002210struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002211 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002212 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002213};
2214
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002215enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002216 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002217};
2218
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002219struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002220 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002221 k_work_handler_t handler;
2222 atomic_t flags[1];
2223};
2224
Allan Stephens6bba9b02016-11-16 14:56:54 -05002225struct k_delayed_work {
2226 struct k_work work;
2227 struct _timeout timeout;
2228 struct k_work_q *work_q;
2229};
2230
2231extern struct k_work_q k_sys_work_q;
2232
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002233/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002234 * INTERNAL_HIDDEN @endcond
2235 */
2236
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002237#define _K_WORK_INITIALIZER(work_handler) \
2238 { \
2239 ._reserved = NULL, \
2240 .handler = work_handler, \
2241 .flags = { 0 } \
2242 }
2243
2244#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2245
Allan Stephens6bba9b02016-11-16 14:56:54 -05002246/**
2247 * @brief Initialize a statically-defined work item.
2248 *
2249 * This macro can be used to initialize a statically-defined workqueue work
2250 * item, prior to its first use. For example,
2251 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002252 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002253 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002254 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002255 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002256 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002257#define K_WORK_DEFINE(work, work_handler) \
2258 struct k_work work \
2259 __in_section(_k_work, static, work) = \
2260 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002261
2262/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002263 * @brief Initialize a work item.
2264 *
2265 * This routine initializes a workqueue work item, prior to its first use.
2266 *
2267 * @param work Address of work item.
2268 * @param handler Function to invoke each time work item is processed.
2269 *
2270 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002271 */
2272static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2273{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002274 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002275 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002276 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002277}
2278
2279/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002280 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002281 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002282 * This routine submits work item @a work to be processed by workqueue
2283 * @a work_q. If the work item is already pending in the workqueue's queue
2284 * as a result of an earlier submission, this routine has no effect on the
2285 * work item. If the work item has already been processed, or is currently
2286 * being processed, its work is considered complete and the work item can be
2287 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002288 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002289 * @warning
2290 * A submitted work item must not be modified until it has been processed
2291 * by the workqueue.
2292 *
2293 * @note Can be called by ISRs.
2294 *
2295 * @param work_q Address of workqueue.
2296 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002297 *
2298 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002299 */
2300static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2301 struct k_work *work)
2302{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002303 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002304 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002305 }
2306}
2307
2308/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002309 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002310 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002311 * This routine indicates if work item @a work is pending in a workqueue's
2312 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002313 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002314 * @note Can be called by ISRs.
2315 *
2316 * @param work Address of work item.
2317 *
2318 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002319 */
2320static inline int k_work_pending(struct k_work *work)
2321{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002322 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002323}
2324
2325/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002326 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002327 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002328 * This routine starts workqueue @a work_q. The workqueue spawns its work
2329 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002330 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002331 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002332 * @param stack Pointer to work queue thread's stack space, as defined by
2333 * K_THREAD_STACK_DEFINE()
2334 * @param stack_size Size of the work queue thread's stack (in bytes), which
2335 * should either be the same constant passed to
2336 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002337 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002338 *
2339 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002340 */
Andrew Boie507852a2017-07-25 18:47:07 -07002341extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002342 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002343 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002344
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002345/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002346 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002347 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002348 * This routine initializes a workqueue delayed work item, prior to
2349 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002350 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002351 * @param work Address of delayed work item.
2352 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002353 *
2354 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002355 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002356extern void k_delayed_work_init(struct k_delayed_work *work,
2357 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002358
2359/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002360 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002361 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002362 * This routine schedules work item @a work to be processed by workqueue
2363 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002364 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002365 * Only when the countdown completes is the work item actually submitted to
2366 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002367 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002368 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002369 * counting down cancels the existing submission and restarts the
2370 * countdown using the new delay. Note that this behavior is
2371 * inherently subject to race conditions with the pre-existing
2372 * timeouts and work queue, so care must be taken to synchronize such
2373 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002374 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002375 * @warning
2376 * A delayed work item must not be modified until it has been processed
2377 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002378 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002379 * @note Can be called by ISRs.
2380 *
2381 * @param work_q Address of workqueue.
2382 * @param work Address of delayed work item.
2383 * @param delay Delay before submitting the work item (in milliseconds).
2384 *
2385 * @retval 0 Work item countdown started.
2386 * @retval -EINPROGRESS Work item is already pending.
2387 * @retval -EINVAL Work item is being processed or has completed its work.
2388 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002389 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002390extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2391 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002392 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002393
2394/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002395 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002396 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002397 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002398 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002399 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002400 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002401 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002402 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002403 * @param work Address of delayed work item.
2404 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002405 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002406 * @retval -EINPROGRESS Work item is already pending.
2407 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002408 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002409extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002410
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002411/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002412 * @brief Submit a work item to the system workqueue.
2413 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002414 * This routine submits work item @a work to be processed by the system
2415 * workqueue. If the work item is already pending in the workqueue's queue
2416 * as a result of an earlier submission, this routine has no effect on the
2417 * work item. If the work item has already been processed, or is currently
2418 * being processed, its work is considered complete and the work item can be
2419 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002420 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002421 * @warning
2422 * Work items submitted to the system workqueue should avoid using handlers
2423 * that block or yield since this may prevent the system workqueue from
2424 * processing other work items in a timely manner.
2425 *
2426 * @note Can be called by ISRs.
2427 *
2428 * @param work Address of work item.
2429 *
2430 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002431 */
2432static inline void k_work_submit(struct k_work *work)
2433{
2434 k_work_submit_to_queue(&k_sys_work_q, work);
2435}
2436
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002437/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002438 * @brief Submit a delayed work item to the system workqueue.
2439 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002440 * This routine schedules work item @a work to be processed by the system
2441 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002442 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002443 * Only when the countdown completes is the work item actually submitted to
2444 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002445 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002446 * Submitting a previously submitted delayed work item that is still
2447 * counting down cancels the existing submission and restarts the countdown
2448 * using the new delay. If the work item is currently pending on the
2449 * workqueue's queue because the countdown has completed it is too late to
2450 * resubmit the item, and resubmission fails without impacting the work item.
2451 * If the work item has already been processed, or is currently being processed,
2452 * its work is considered complete and the work item can be resubmitted.
2453 *
2454 * @warning
2455 * Work items submitted to the system workqueue should avoid using handlers
2456 * that block or yield since this may prevent the system workqueue from
2457 * processing other work items in a timely manner.
2458 *
2459 * @note Can be called by ISRs.
2460 *
2461 * @param work Address of delayed work item.
2462 * @param delay Delay before submitting the work item (in milliseconds).
2463 *
2464 * @retval 0 Work item countdown started.
2465 * @retval -EINPROGRESS Work item is already pending.
2466 * @retval -EINVAL Work item is being processed or has completed its work.
2467 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002468 */
2469static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002470 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002471{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002472 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002473}
2474
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002475/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002476 * @brief Get time remaining before a delayed work gets scheduled.
2477 *
2478 * This routine computes the (approximate) time remaining before a
2479 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002480 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002481 *
2482 * @param work Delayed work item.
2483 *
2484 * @return Remaining time (in milliseconds).
2485 */
Kumar Galacc334c72017-04-21 10:55:34 -05002486static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002487{
2488 return _timeout_remaining_get(&work->timeout);
2489}
2490
Anas Nashif166f5192018-02-25 08:02:36 -06002491/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002492
Allan Stephensc98da842016-11-11 15:45:03 -05002493/**
2494 * @cond INTERNAL_HIDDEN
2495 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002496
2497struct k_mutex {
2498 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002499 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002500 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002501 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002502
Anas Nashif2f203c22016-12-18 06:57:45 -05002503 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002504};
2505
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002506#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002507 { \
2508 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2509 .owner = NULL, \
2510 .lock_count = 0, \
2511 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002512 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002513 }
2514
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002515#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2516
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002517/**
Allan Stephensc98da842016-11-11 15:45:03 -05002518 * INTERNAL_HIDDEN @endcond
2519 */
2520
2521/**
2522 * @defgroup mutex_apis Mutex APIs
2523 * @ingroup kernel_apis
2524 * @{
2525 */
2526
2527/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002528 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002529 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002530 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002531 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002532 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002533 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002534 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002535 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002536#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002537 struct k_mutex name \
2538 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002539 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002540
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002541/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002542 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002543 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002544 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002545 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002546 * Upon completion, the mutex is available and does not have an owner.
2547 *
2548 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002549 *
2550 * @return N/A
2551 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002552__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002553
2554/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002555 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002556 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002557 * This routine locks @a mutex. If the mutex is locked by another thread,
2558 * the calling thread waits until the mutex becomes available or until
2559 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002560 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002561 * A thread is permitted to lock a mutex it has already locked. The operation
2562 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002563 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002564 * @param mutex Address of the mutex.
2565 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002566 * or one of the special values K_NO_WAIT and K_FOREVER.
2567 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002568 * @retval 0 Mutex locked.
2569 * @retval -EBUSY Returned without waiting.
2570 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002571 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002572__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002573
2574/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002575 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002576 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002577 * This routine unlocks @a mutex. The mutex must already be locked by the
2578 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002579 *
2580 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002581 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002582 * thread.
2583 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002584 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002585 *
2586 * @return N/A
2587 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002588__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002589
Allan Stephensc98da842016-11-11 15:45:03 -05002590/**
Anas Nashif166f5192018-02-25 08:02:36 -06002591 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002592 */
2593
2594/**
2595 * @cond INTERNAL_HIDDEN
2596 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002597
2598struct k_sem {
2599 _wait_q_t wait_q;
2600 unsigned int count;
2601 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002602 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002603
Anas Nashif2f203c22016-12-18 06:57:45 -05002604 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002605};
2606
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002607#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002608 { \
2609 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2610 .count = initial_count, \
2611 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002612 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002613 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002614 }
2615
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002616#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2617
Allan Stephensc98da842016-11-11 15:45:03 -05002618/**
2619 * INTERNAL_HIDDEN @endcond
2620 */
2621
2622/**
2623 * @defgroup semaphore_apis Semaphore APIs
2624 * @ingroup kernel_apis
2625 * @{
2626 */
2627
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002628/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002629 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002630 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002631 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002632 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002633 * @param sem Address of the semaphore.
2634 * @param initial_count Initial semaphore count.
2635 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002636 *
2637 * @return N/A
2638 */
Andrew Boie99280232017-09-29 14:17:47 -07002639__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2640 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002641
2642/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002643 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002644 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002645 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002646 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002647 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2648 *
2649 * @param sem Address of the semaphore.
2650 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002651 * or one of the special values K_NO_WAIT and K_FOREVER.
2652 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002653 * @note When porting code from the nanokernel legacy API to the new API, be
2654 * careful with the return value of this function. The return value is the
2655 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2656 * non-zero means failure, while the nano_sem_take family returns 1 for success
2657 * and 0 for failure.
2658 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002659 * @retval 0 Semaphore taken.
2660 * @retval -EBUSY Returned without waiting.
2661 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002662 */
Andrew Boie99280232017-09-29 14:17:47 -07002663__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002664
2665/**
2666 * @brief Give a semaphore.
2667 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002668 * This routine gives @a sem, unless the semaphore is already at its maximum
2669 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002670 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002671 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002672 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002673 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002674 *
2675 * @return N/A
2676 */
Andrew Boie99280232017-09-29 14:17:47 -07002677__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002678
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002679/**
2680 * @brief Reset a semaphore's count to zero.
2681 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002682 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002683 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002684 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002685 *
2686 * @return N/A
2687 */
Andrew Boie990bf162017-10-03 12:36:49 -07002688__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002689
Anas Nashif954d5502018-02-25 08:37:28 -06002690/**
2691 * @internal
2692 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002693static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002694{
2695 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002696}
2697
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002698/**
2699 * @brief Get a semaphore's count.
2700 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002701 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002702 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002703 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002704 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002705 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002706 */
Andrew Boie990bf162017-10-03 12:36:49 -07002707__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002708
Anas Nashif954d5502018-02-25 08:37:28 -06002709/**
2710 * @internal
2711 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002712static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002713{
2714 return sem->count;
2715}
2716
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002717/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002718 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002719 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002720 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002721 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002722 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002723 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002724 * @param name Name of the semaphore.
2725 * @param initial_count Initial semaphore count.
2726 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002727 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002728#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002729 struct k_sem name \
2730 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07002731 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05302732 BUILD_ASSERT(((count_limit) != 0) && \
2733 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002734
Anas Nashif166f5192018-02-25 08:02:36 -06002735/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002736
2737/**
2738 * @defgroup alert_apis Alert APIs
2739 * @ingroup kernel_apis
2740 * @{
2741 */
2742
Allan Stephens5eceb852016-11-16 10:16:30 -05002743/**
2744 * @typedef k_alert_handler_t
2745 * @brief Alert handler function type.
2746 *
2747 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002748 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002749 * and is only invoked if the alert has been initialized with one.
2750 *
2751 * @param alert Address of the alert.
2752 *
2753 * @return 0 if alert has been consumed; non-zero if alert should pend.
2754 */
2755typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002756
Anas Nashif166f5192018-02-25 08:02:36 -06002757/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002758
2759/**
2760 * @cond INTERNAL_HIDDEN
2761 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002762
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002763#define K_ALERT_DEFAULT NULL
2764#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002765
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002766struct k_alert {
2767 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002768 atomic_t send_count;
2769 struct k_work work_item;
2770 struct k_sem sem;
2771
Anas Nashif2f203c22016-12-18 06:57:45 -05002772 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002773};
2774
Anas Nashif954d5502018-02-25 08:37:28 -06002775/**
2776 * @internal
2777 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002778extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002779
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002780#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002781 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002782 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002783 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002784 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2785 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002786 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002787 }
2788
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002789#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2790
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002791/**
Allan Stephensc98da842016-11-11 15:45:03 -05002792 * INTERNAL_HIDDEN @endcond
2793 */
2794
2795/**
2796 * @addtogroup alert_apis
2797 * @{
2798 */
2799
2800/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002801 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002802 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002803 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002804 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002805 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002806 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002807 * @param name Name of the alert.
2808 * @param alert_handler Action to take when alert is sent. Specify either
2809 * the address of a function to be invoked by the system workqueue
2810 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2811 * K_ALERT_DEFAULT (which causes the alert to pend).
2812 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002813 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002814#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002815 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002816 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002817 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002818 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002819
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002820/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002821 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002822 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002823 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002824 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002825 * @param alert Address of the alert.
2826 * @param handler Action to take when alert is sent. Specify either the address
2827 * of a function to be invoked by the system workqueue thread,
2828 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2829 * K_ALERT_DEFAULT (which causes the alert to pend).
2830 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002831 *
2832 * @return N/A
2833 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002834extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2835 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002836
2837/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002838 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002839 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002840 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002841 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002842 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2843 *
2844 * @param alert Address of the alert.
2845 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002846 * or one of the special values K_NO_WAIT and K_FOREVER.
2847 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002848 * @retval 0 Alert received.
2849 * @retval -EBUSY Returned without waiting.
2850 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002851 */
Andrew Boie310e9872017-09-29 04:41:15 -07002852__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002853
2854/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002855 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002856 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002857 * This routine signals @a alert. The action specified for @a alert will
2858 * be taken, which may trigger the execution of an alert handler function
2859 * and/or cause the alert to pend (assuming the alert has not reached its
2860 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002862 * @note Can be called by ISRs.
2863 *
2864 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002865 *
2866 * @return N/A
2867 */
Andrew Boie310e9872017-09-29 04:41:15 -07002868__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002869
2870/**
Anas Nashif166f5192018-02-25 08:02:36 -06002871 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002872 */
2873
Allan Stephensc98da842016-11-11 15:45:03 -05002874/**
2875 * @cond INTERNAL_HIDDEN
2876 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002877
2878struct k_msgq {
2879 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002880 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002881 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002882 char *buffer_start;
2883 char *buffer_end;
2884 char *read_ptr;
2885 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002886 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002887
Anas Nashif2f203c22016-12-18 06:57:45 -05002888 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002889};
2890
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002891#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002892 { \
2893 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002894 .max_msgs = q_max_msgs, \
2895 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002896 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002897 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002898 .read_ptr = q_buffer, \
2899 .write_ptr = q_buffer, \
2900 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002901 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002902 }
2903
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002904#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2905
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05302906struct k_msgq_attrs {
2907 size_t msg_size;
2908 u32_t max_msgs;
2909 u32_t used_msgs;
2910};
2911
Peter Mitsis1da807e2016-10-06 11:36:59 -04002912/**
Allan Stephensc98da842016-11-11 15:45:03 -05002913 * INTERNAL_HIDDEN @endcond
2914 */
2915
2916/**
2917 * @defgroup msgq_apis Message Queue APIs
2918 * @ingroup kernel_apis
2919 * @{
2920 */
2921
2922/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002923 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002924 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002925 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2926 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002927 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2928 * message is similarly aligned to this boundary, @a q_msg_size must also be
2929 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002930 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002931 * The message queue can be accessed outside the module where it is defined
2932 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002933 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002934 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002935 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002936 * @param q_name Name of the message queue.
2937 * @param q_msg_size Message size (in bytes).
2938 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002939 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002940 */
2941#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2942 static char __noinit __aligned(q_align) \
2943 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002944 struct k_msgq q_name \
2945 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002946 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002947 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002948
Peter Mitsisd7a37502016-10-13 11:37:40 -04002949/**
2950 * @brief Initialize a message queue.
2951 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002952 * This routine initializes a message queue object, prior to its first use.
2953 *
Allan Stephensda827222016-11-09 14:23:58 -06002954 * The message queue's ring buffer must contain space for @a max_msgs messages,
2955 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2956 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2957 * that each message is similarly aligned to this boundary, @a q_msg_size
2958 * must also be a multiple of N.
2959 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002960 * @param q Address of the message queue.
2961 * @param buffer Pointer to ring buffer that holds queued messages.
2962 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002963 * @param max_msgs Maximum number of messages that can be queued.
2964 *
2965 * @return N/A
2966 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002967__syscall void k_msgq_init(struct k_msgq *q, char *buffer,
2968 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002969
2970/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002971 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002972 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002973 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002974 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002975 * @note Can be called by ISRs.
2976 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002977 * @param q Address of the message queue.
2978 * @param data Pointer to the message.
2979 * @param timeout Waiting period to add the message (in milliseconds),
2980 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002981 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002982 * @retval 0 Message sent.
2983 * @retval -ENOMSG Returned without waiting or queue purged.
2984 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002985 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002986__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002987
2988/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002989 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002990 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002991 * This routine receives a message from message queue @a q in a "first in,
2992 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002993 *
Allan Stephensc98da842016-11-11 15:45:03 -05002994 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002995 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002996 * @param q Address of the message queue.
2997 * @param data Address of area to hold the received message.
2998 * @param timeout Waiting period to receive the message (in milliseconds),
2999 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003000 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003001 * @retval 0 Message received.
3002 * @retval -ENOMSG Returned without waiting.
3003 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003004 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003005__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003006
3007/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003008 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003009 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003010 * This routine discards all unreceived messages in a message queue's ring
3011 * buffer. Any threads that are blocked waiting to send a message to the
3012 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003013 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003014 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003015 *
3016 * @return N/A
3017 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003018__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003019
Peter Mitsis67be2492016-10-07 11:44:34 -04003020/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003021 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003022 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003023 * This routine returns the number of unused entries in a message queue's
3024 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003025 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003026 * @param q Address of the message queue.
3027 *
3028 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04003029 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003030__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3031
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303032/**
3033 * @brief Get basic attributes of a message queue.
3034 *
3035 * This routine fetches basic attributes of message queue into attr argument.
3036 *
3037 * @param q Address of the message queue.
3038 * @param attrs pointer to message queue attribute structure.
3039 *
3040 * @return N/A
3041 */
3042__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3043
3044
Andrew Boie82edb6e2017-10-02 10:53:06 -07003045static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003046{
3047 return q->max_msgs - q->used_msgs;
3048}
3049
Peter Mitsisd7a37502016-10-13 11:37:40 -04003050/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003051 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003052 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003053 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003054 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003055 * @param q Address of the message queue.
3056 *
3057 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003058 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003059__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3060
3061static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003062{
3063 return q->used_msgs;
3064}
3065
Anas Nashif166f5192018-02-25 08:02:36 -06003066/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003067
3068/**
3069 * @defgroup mem_pool_apis Memory Pool APIs
3070 * @ingroup kernel_apis
3071 * @{
3072 */
3073
Andy Ross73cb9582017-05-09 10:42:39 -07003074/* Note on sizing: the use of a 20 bit field for block means that,
3075 * assuming a reasonable minimum block size of 16 bytes, we're limited
3076 * to 16M of memory managed by a single pool. Long term it would be
3077 * good to move to a variable bit size based on configuration.
3078 */
3079struct k_mem_block_id {
3080 u32_t pool : 8;
3081 u32_t level : 4;
3082 u32_t block : 20;
3083};
3084
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003085struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003086 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003087 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003088};
3089
Anas Nashif166f5192018-02-25 08:02:36 -06003090/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003091
3092/**
3093 * @defgroup mailbox_apis Mailbox APIs
3094 * @ingroup kernel_apis
3095 * @{
3096 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003097
3098struct k_mbox_msg {
3099 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003100 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003101 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003102 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003103 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003104 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003105 /** sender's message data buffer */
3106 void *tx_data;
3107 /** internal use only - needed for legacy API support */
3108 void *_rx_data;
3109 /** message data block descriptor */
3110 struct k_mem_block tx_block;
3111 /** source thread id */
3112 k_tid_t rx_source_thread;
3113 /** target thread id */
3114 k_tid_t tx_target_thread;
3115 /** internal use only - thread waiting on send (may be a dummy) */
3116 k_tid_t _syncing_thread;
3117#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3118 /** internal use only - semaphore used during asynchronous send */
3119 struct k_sem *_async_sem;
3120#endif
3121};
3122
Allan Stephensc98da842016-11-11 15:45:03 -05003123/**
3124 * @cond INTERNAL_HIDDEN
3125 */
3126
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003127struct k_mbox {
3128 _wait_q_t tx_msg_queue;
3129 _wait_q_t rx_msg_queue;
3130
Anas Nashif2f203c22016-12-18 06:57:45 -05003131 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003132};
3133
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003134#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003135 { \
3136 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3137 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003138 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003139 }
3140
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003141#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3142
Peter Mitsis12092702016-10-14 12:57:23 -04003143/**
Allan Stephensc98da842016-11-11 15:45:03 -05003144 * INTERNAL_HIDDEN @endcond
3145 */
3146
3147/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003148 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003149 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003150 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003151 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003152 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003153 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003154 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003155 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003156#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003157 struct k_mbox name \
3158 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003159 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003160
Peter Mitsis12092702016-10-14 12:57:23 -04003161/**
3162 * @brief Initialize a mailbox.
3163 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003164 * This routine initializes a mailbox object, prior to its first use.
3165 *
3166 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003167 *
3168 * @return N/A
3169 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003170extern void k_mbox_init(struct k_mbox *mbox);
3171
Peter Mitsis12092702016-10-14 12:57:23 -04003172/**
3173 * @brief Send a mailbox message in a synchronous manner.
3174 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003175 * This routine sends a message to @a mbox and waits for a receiver to both
3176 * receive and process it. The message data may be in a buffer, in a memory
3177 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003178 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003179 * @param mbox Address of the mailbox.
3180 * @param tx_msg Address of the transmit message descriptor.
3181 * @param timeout Waiting period for the message to be received (in
3182 * milliseconds), or one of the special values K_NO_WAIT
3183 * and K_FOREVER. Once the message has been received,
3184 * this routine waits as long as necessary for the message
3185 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003186 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003187 * @retval 0 Message sent.
3188 * @retval -ENOMSG Returned without waiting.
3189 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003190 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003191extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003192 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003193
Peter Mitsis12092702016-10-14 12:57:23 -04003194/**
3195 * @brief Send a mailbox message in an asynchronous manner.
3196 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003197 * This routine sends a message to @a mbox without waiting for a receiver
3198 * to process it. The message data may be in a buffer, in a memory pool block,
3199 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3200 * will be given when the message has been both received and completely
3201 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003202 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003203 * @param mbox Address of the mailbox.
3204 * @param tx_msg Address of the transmit message descriptor.
3205 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003206 *
3207 * @return N/A
3208 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003209extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003210 struct k_sem *sem);
3211
Peter Mitsis12092702016-10-14 12:57:23 -04003212/**
3213 * @brief Receive a mailbox message.
3214 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003215 * This routine receives a message from @a mbox, then optionally retrieves
3216 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003217 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003218 * @param mbox Address of the mailbox.
3219 * @param rx_msg Address of the receive message descriptor.
3220 * @param buffer Address of the buffer to receive data, or NULL to defer data
3221 * retrieval and message disposal until later.
3222 * @param timeout Waiting period for a message to be received (in
3223 * milliseconds), or one of the special values K_NO_WAIT
3224 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003225 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003226 * @retval 0 Message received.
3227 * @retval -ENOMSG Returned without waiting.
3228 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003229 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003230extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003231 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003232
3233/**
3234 * @brief Retrieve mailbox message data into a buffer.
3235 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003236 * This routine completes the processing of a received message by retrieving
3237 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003238 *
3239 * Alternatively, this routine can be used to dispose of a received message
3240 * without retrieving its data.
3241 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003242 * @param rx_msg Address of the receive message descriptor.
3243 * @param buffer Address of the buffer to receive data, or NULL to discard
3244 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003245 *
3246 * @return N/A
3247 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003248extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003249
3250/**
3251 * @brief Retrieve mailbox message data into a memory pool block.
3252 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003253 * This routine completes the processing of a received message by retrieving
3254 * its data into a memory pool block, then disposing of the message.
3255 * The memory pool block that results from successful retrieval must be
3256 * returned to the pool once the data has been processed, even in cases
3257 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003258 *
3259 * Alternatively, this routine can be used to dispose of a received message
3260 * without retrieving its data. In this case there is no need to return a
3261 * memory pool block to the pool.
3262 *
3263 * This routine allocates a new memory pool block for the data only if the
3264 * data is not already in one. If a new block cannot be allocated, the routine
3265 * returns a failure code and the received message is left unchanged. This
3266 * permits the caller to reattempt data retrieval at a later time or to dispose
3267 * of the received message without retrieving its data.
3268 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003269 * @param rx_msg Address of a receive message descriptor.
3270 * @param pool Address of memory pool, or NULL to discard data.
3271 * @param block Address of the area to hold memory pool block info.
3272 * @param timeout Waiting period to wait for a memory pool block (in
3273 * milliseconds), or one of the special values K_NO_WAIT
3274 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003275 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003276 * @retval 0 Data retrieved.
3277 * @retval -ENOMEM Returned without waiting.
3278 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003279 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003280extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003281 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003282 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003283
Anas Nashif166f5192018-02-25 08:02:36 -06003284/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003285
3286/**
3287 * @cond INTERNAL_HIDDEN
3288 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003289
3290struct k_pipe {
3291 unsigned char *buffer; /* Pipe buffer: may be NULL */
3292 size_t size; /* Buffer size */
3293 size_t bytes_used; /* # bytes used in buffer */
3294 size_t read_index; /* Where in buffer to read from */
3295 size_t write_index; /* Where in buffer to write */
3296
3297 struct {
3298 _wait_q_t readers; /* Reader wait queue */
3299 _wait_q_t writers; /* Writer wait queue */
3300 } wait_q;
3301
Anas Nashif2f203c22016-12-18 06:57:45 -05003302 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003303};
3304
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003305#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003306 { \
3307 .buffer = pipe_buffer, \
3308 .size = pipe_buffer_size, \
3309 .bytes_used = 0, \
3310 .read_index = 0, \
3311 .write_index = 0, \
3312 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3313 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003314 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003315 }
3316
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003317#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3318
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003319/**
Allan Stephensc98da842016-11-11 15:45:03 -05003320 * INTERNAL_HIDDEN @endcond
3321 */
3322
3323/**
3324 * @defgroup pipe_apis Pipe APIs
3325 * @ingroup kernel_apis
3326 * @{
3327 */
3328
3329/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003330 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003331 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003332 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003333 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003334 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003335 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003336 * @param name Name of the pipe.
3337 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3338 * or zero if no ring buffer is used.
3339 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003340 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003341#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3342 static unsigned char __noinit __aligned(pipe_align) \
3343 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003344 struct k_pipe name \
3345 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003346 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003347
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003348/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003349 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003350 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003351 * This routine initializes a pipe object, prior to its first use.
3352 *
3353 * @param pipe Address of the pipe.
3354 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3355 * is used.
3356 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3357 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003358 *
3359 * @return N/A
3360 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003361__syscall void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3362 size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003363
3364/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003365 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003366 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003367 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003368 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003369 * @param pipe Address of the pipe.
3370 * @param data Address of data to write.
3371 * @param bytes_to_write Size of data (in bytes).
3372 * @param bytes_written Address of area to hold the number of bytes written.
3373 * @param min_xfer Minimum number of bytes to write.
3374 * @param timeout Waiting period to wait for the data to be written (in
3375 * milliseconds), or one of the special values K_NO_WAIT
3376 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003377 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003378 * @retval 0 At least @a min_xfer bytes of data were written.
3379 * @retval -EIO Returned without waiting; zero data bytes were written.
3380 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003381 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003382 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003383__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3384 size_t bytes_to_write, size_t *bytes_written,
3385 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003386
3387/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003388 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003389 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003390 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003391 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003392 * @param pipe Address of the pipe.
3393 * @param data Address to place the data read from pipe.
3394 * @param bytes_to_read Maximum number of data bytes to read.
3395 * @param bytes_read Address of area to hold the number of bytes read.
3396 * @param min_xfer Minimum number of data bytes to read.
3397 * @param timeout Waiting period to wait for the data to be read (in
3398 * milliseconds), or one of the special values K_NO_WAIT
3399 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003400 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003401 * @retval 0 At least @a min_xfer bytes of data were read.
3402 * @retval -EIO Returned without waiting; zero data bytes were read.
3403 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003404 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003405 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003406__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3407 size_t bytes_to_read, size_t *bytes_read,
3408 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003409
3410/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003411 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003412 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003413 * This routine writes the data contained in a memory block to @a pipe.
3414 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003415 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003416 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003417 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003418 * @param block Memory block containing data to send
3419 * @param size Number of data bytes in memory block to send
3420 * @param sem Semaphore to signal upon completion (else NULL)
3421 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003422 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003423 */
3424extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3425 size_t size, struct k_sem *sem);
3426
Anas Nashif166f5192018-02-25 08:02:36 -06003427/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003428
Allan Stephensc98da842016-11-11 15:45:03 -05003429/**
3430 * @cond INTERNAL_HIDDEN
3431 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003432
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003433struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003434 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003435 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003436 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003437 char *buffer;
3438 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003439 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003440
Anas Nashif2f203c22016-12-18 06:57:45 -05003441 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003442};
3443
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003444#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003445 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003446 { \
3447 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003448 .num_blocks = slab_num_blocks, \
3449 .block_size = slab_block_size, \
3450 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003451 .free_list = NULL, \
3452 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003453 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003454 }
3455
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003456#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3457
3458
Peter Mitsis578f9112016-10-07 13:50:31 -04003459/**
Allan Stephensc98da842016-11-11 15:45:03 -05003460 * INTERNAL_HIDDEN @endcond
3461 */
3462
3463/**
3464 * @defgroup mem_slab_apis Memory Slab APIs
3465 * @ingroup kernel_apis
3466 * @{
3467 */
3468
3469/**
Allan Stephensda827222016-11-09 14:23:58 -06003470 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003471 *
Allan Stephensda827222016-11-09 14:23:58 -06003472 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003473 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003474 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3475 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003476 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003477 *
Allan Stephensda827222016-11-09 14:23:58 -06003478 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003479 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003480 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003481 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003482 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003483 * @param name Name of the memory slab.
3484 * @param slab_block_size Size of each memory block (in bytes).
3485 * @param slab_num_blocks Number memory blocks.
3486 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003487 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003488#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3489 char __noinit __aligned(slab_align) \
3490 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3491 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003492 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003493 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003494 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003495
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003496/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003497 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003498 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003499 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003500 *
Allan Stephensda827222016-11-09 14:23:58 -06003501 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3502 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3503 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3504 * To ensure that each memory block is similarly aligned to this boundary,
3505 * @a slab_block_size must also be a multiple of N.
3506 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003507 * @param slab Address of the memory slab.
3508 * @param buffer Pointer to buffer used for the memory blocks.
3509 * @param block_size Size of each memory block (in bytes).
3510 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003511 *
3512 * @return N/A
3513 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003514extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003515 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003516
3517/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003518 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003519 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003520 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003521 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003522 * @param slab Address of the memory slab.
3523 * @param mem Pointer to block address area.
3524 * @param timeout Maximum time to wait for operation to complete
3525 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3526 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003527 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003528 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003529 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003530 * @retval -ENOMEM Returned without waiting.
3531 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003532 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003533extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003534 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003535
3536/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003537 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003538 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003539 * This routine releases a previously allocated memory block back to its
3540 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003541 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003542 * @param slab Address of the memory slab.
3543 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003544 *
3545 * @return N/A
3546 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003547extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003548
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003549/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003550 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003551 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003552 * This routine gets the number of memory blocks that are currently
3553 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003554 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003555 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003556 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003557 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003558 */
Kumar Galacc334c72017-04-21 10:55:34 -05003559static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003560{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003561 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003562}
3563
Peter Mitsisc001aa82016-10-13 13:53:37 -04003564/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003565 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003566 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003567 * This routine gets the number of memory blocks that are currently
3568 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003569 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003570 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003571 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003572 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003573 */
Kumar Galacc334c72017-04-21 10:55:34 -05003574static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003575{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003576 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003577}
3578
Anas Nashif166f5192018-02-25 08:02:36 -06003579/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003580
3581/**
3582 * @cond INTERNAL_HIDDEN
3583 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003584
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003585struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003586 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003587 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003588};
3589
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003590/**
Allan Stephensc98da842016-11-11 15:45:03 -05003591 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003592 */
3593
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003594/**
Allan Stephensc98da842016-11-11 15:45:03 -05003595 * @addtogroup mem_pool_apis
3596 * @{
3597 */
3598
3599/**
3600 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003601 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003602 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3603 * long. The memory pool allows blocks to be repeatedly partitioned into
3604 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003605 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003606 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003607 * If the pool is to be accessed outside the module where it is defined, it
3608 * can be declared via
3609 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003610 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003611 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003612 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003613 * @param minsz Size of the smallest blocks in the pool (in bytes).
3614 * @param maxsz Size of the largest blocks in the pool (in bytes).
3615 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003616 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003617 */
Andy Ross73cb9582017-05-09 10:42:39 -07003618#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3619 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3620 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08003621 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07003622 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08003623 .base = { \
3624 .buf = _mpool_buf_##name, \
3625 .max_sz = maxsz, \
3626 .n_max = nmax, \
3627 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3628 .levels = _mpool_lvls_##name, \
3629 .flags = SYS_MEM_POOL_KERNEL \
3630 } \
Andy Ross73cb9582017-05-09 10:42:39 -07003631 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003632
Peter Mitsis937042c2016-10-13 13:18:26 -04003633/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003634 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003635 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003636 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003637 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003638 * @param pool Address of the memory pool.
3639 * @param block Pointer to block descriptor for the allocated memory.
3640 * @param size Amount of memory to allocate (in bytes).
3641 * @param timeout Maximum time to wait for operation to complete
3642 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3643 * or K_FOREVER to wait as long as necessary.
3644 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003645 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003646 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003647 * @retval -ENOMEM Returned without waiting.
3648 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003649 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003650extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003651 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003652
3653/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003654 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003655 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003656 * This routine releases a previously allocated memory block back to its
3657 * memory pool.
3658 *
3659 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003660 *
3661 * @return N/A
3662 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003663extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003664
3665/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003666 * @brief Free memory allocated from a memory pool.
3667 *
3668 * This routine releases a previously allocated memory block back to its
3669 * memory pool
3670 *
3671 * @param id Memory block identifier.
3672 *
3673 * @return N/A
3674 */
3675extern void k_mem_pool_free_id(struct k_mem_block_id *id);
3676
3677/**
Anas Nashif166f5192018-02-25 08:02:36 -06003678 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003679 */
3680
3681/**
3682 * @defgroup heap_apis Heap Memory Pool APIs
3683 * @ingroup kernel_apis
3684 * @{
3685 */
3686
3687/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003688 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003689 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003690 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003691 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003692 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003693 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003694 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003695 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003696 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003697extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003698
3699/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003700 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003701 *
3702 * This routine provides traditional free() semantics. The memory being
3703 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003704 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003705 * If @a ptr is NULL, no operation is performed.
3706 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003707 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003708 *
3709 * @return N/A
3710 */
3711extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003712
Allan Stephensc98da842016-11-11 15:45:03 -05003713/**
Andrew Boie7f95e832017-11-08 14:40:01 -08003714 * @brief Allocate memory from heap, array style
3715 *
3716 * This routine provides traditional calloc() semantics. Memory is
3717 * allocated from the heap memory pool and zeroed.
3718 *
3719 * @param nmemb Number of elements in the requested array
3720 * @param size Size of each array element (in bytes).
3721 *
3722 * @return Address of the allocated memory if successful; otherwise NULL.
3723 */
3724extern void *k_calloc(size_t nmemb, size_t size);
3725
Anas Nashif166f5192018-02-25 08:02:36 -06003726/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003727
Benjamin Walshacc68c12017-01-29 18:57:45 -05003728/* polling API - PRIVATE */
3729
Benjamin Walshb0179862017-02-02 16:39:57 -05003730#ifdef CONFIG_POLL
3731#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3732#else
3733#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3734#endif
3735
Benjamin Walshacc68c12017-01-29 18:57:45 -05003736/* private - implementation data created as needed, per-type */
3737struct _poller {
3738 struct k_thread *thread;
3739};
3740
3741/* private - types bit positions */
3742enum _poll_types_bits {
3743 /* can be used to ignore an event */
3744 _POLL_TYPE_IGNORE,
3745
3746 /* to be signaled by k_poll_signal() */
3747 _POLL_TYPE_SIGNAL,
3748
3749 /* semaphore availability */
3750 _POLL_TYPE_SEM_AVAILABLE,
3751
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003752 /* queue/fifo/lifo data availability */
3753 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003754
3755 _POLL_NUM_TYPES
3756};
3757
3758#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3759
3760/* private - states bit positions */
3761enum _poll_states_bits {
3762 /* default state when creating event */
3763 _POLL_STATE_NOT_READY,
3764
Benjamin Walshacc68c12017-01-29 18:57:45 -05003765 /* signaled by k_poll_signal() */
3766 _POLL_STATE_SIGNALED,
3767
3768 /* semaphore is available */
3769 _POLL_STATE_SEM_AVAILABLE,
3770
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003771 /* data is available to read on queue/fifo/lifo */
3772 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003773
3774 _POLL_NUM_STATES
3775};
3776
3777#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3778
3779#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003780 (32 - (0 \
3781 + 8 /* tag */ \
3782 + _POLL_NUM_TYPES \
3783 + _POLL_NUM_STATES \
3784 + 1 /* modes */ \
3785 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003786
Benjamin Walshacc68c12017-01-29 18:57:45 -05003787/* end of polling API - PRIVATE */
3788
3789
3790/**
3791 * @defgroup poll_apis Async polling APIs
3792 * @ingroup kernel_apis
3793 * @{
3794 */
3795
3796/* Public polling API */
3797
3798/* public - values for k_poll_event.type bitfield */
3799#define K_POLL_TYPE_IGNORE 0
3800#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3801#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003802#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3803#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003804
3805/* public - polling modes */
3806enum k_poll_modes {
3807 /* polling thread does not take ownership of objects when available */
3808 K_POLL_MODE_NOTIFY_ONLY = 0,
3809
3810 K_POLL_NUM_MODES
3811};
3812
3813/* public - values for k_poll_event.state bitfield */
3814#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003815#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3816#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003817#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3818#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003819
3820/* public - poll signal object */
3821struct k_poll_signal {
3822 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003823 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003824
3825 /*
3826 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3827 * user resets it to 0.
3828 */
3829 unsigned int signaled;
3830
3831 /* custom result value passed to k_poll_signal() if needed */
3832 int result;
3833};
3834
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003835#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003836 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003837 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003838 .signaled = 0, \
3839 .result = 0, \
3840 }
3841
3842struct k_poll_event {
3843 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003844 sys_dnode_t _node;
3845
3846 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003847 struct _poller *poller;
3848
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003849 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003850 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003851
Benjamin Walshacc68c12017-01-29 18:57:45 -05003852 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003853 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003854
3855 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003856 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003857
3858 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003859 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003860
3861 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003862 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003863
3864 /* per-type data */
3865 union {
3866 void *obj;
3867 struct k_poll_signal *signal;
3868 struct k_sem *sem;
3869 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003870 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003871 };
3872};
3873
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003874#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003875 { \
3876 .poller = NULL, \
3877 .type = event_type, \
3878 .state = K_POLL_STATE_NOT_READY, \
3879 .mode = event_mode, \
3880 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003881 { .obj = event_obj }, \
3882 }
3883
3884#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3885 event_tag) \
3886 { \
3887 .type = event_type, \
3888 .tag = event_tag, \
3889 .state = K_POLL_STATE_NOT_READY, \
3890 .mode = event_mode, \
3891 .unused = 0, \
3892 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003893 }
3894
3895/**
3896 * @brief Initialize one struct k_poll_event instance
3897 *
3898 * After this routine is called on a poll event, the event it ready to be
3899 * placed in an event array to be passed to k_poll().
3900 *
3901 * @param event The event to initialize.
3902 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3903 * values. Only values that apply to the same object being polled
3904 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3905 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003906 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003907 * @param obj Kernel object or poll signal.
3908 *
3909 * @return N/A
3910 */
3911
Kumar Galacc334c72017-04-21 10:55:34 -05003912extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003913 int mode, void *obj);
3914
3915/**
3916 * @brief Wait for one or many of multiple poll events to occur
3917 *
3918 * This routine allows a thread to wait concurrently for one or many of
3919 * multiple poll events to have occurred. Such events can be a kernel object
3920 * being available, like a semaphore, or a poll signal event.
3921 *
3922 * When an event notifies that a kernel object is available, the kernel object
3923 * is not "given" to the thread calling k_poll(): it merely signals the fact
3924 * that the object was available when the k_poll() call was in effect. Also,
3925 * all threads trying to acquire an object the regular way, i.e. by pending on
3926 * the object, have precedence over the thread polling on the object. This
3927 * means that the polling thread will never get the poll event on an object
3928 * until the object becomes available and its pend queue is empty. For this
3929 * reason, the k_poll() call is more effective when the objects being polled
3930 * only have one thread, the polling thread, trying to acquire them.
3931 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003932 * When k_poll() returns 0, the caller should loop on all the events that were
3933 * passed to k_poll() and check the state field for the values that were
3934 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003935 *
3936 * Before being reused for another call to k_poll(), the user has to reset the
3937 * state field to K_POLL_STATE_NOT_READY.
3938 *
3939 * @param events An array of pointers to events to be polled for.
3940 * @param num_events The number of events in the array.
3941 * @param timeout Waiting period for an event to be ready (in milliseconds),
3942 * or one of the special values K_NO_WAIT and K_FOREVER.
3943 *
3944 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003945 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02003946 * @retval -EINTR Poller thread has been interrupted.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003947 */
3948
3949extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003950 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003951
3952/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003953 * @brief Initialize a poll signal object.
3954 *
3955 * Ready a poll signal object to be signaled via k_poll_signal().
3956 *
3957 * @param signal A poll signal.
3958 *
3959 * @return N/A
3960 */
3961
3962extern void k_poll_signal_init(struct k_poll_signal *signal);
3963
3964/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003965 * @brief Signal a poll signal object.
3966 *
3967 * This routine makes ready a poll signal, which is basically a poll event of
3968 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3969 * made ready to run. A @a result value can be specified.
3970 *
3971 * The poll signal contains a 'signaled' field that, when set by
3972 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3973 * be reset by the user before being passed again to k_poll() or k_poll() will
3974 * consider it being signaled, and will return immediately.
3975 *
3976 * @param signal A poll signal.
3977 * @param result The value to store in the result field of the signal.
3978 *
3979 * @retval 0 The signal was delivered successfully.
3980 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3981 */
3982
3983extern int k_poll_signal(struct k_poll_signal *signal, int result);
3984
Anas Nashif954d5502018-02-25 08:37:28 -06003985/**
3986 * @internal
3987 */
Andy Ross8606fab2018-03-26 10:54:40 -07003988extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003989
Anas Nashif166f5192018-02-25 08:02:36 -06003990/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003991
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003992/**
3993 * @brief Make the CPU idle.
3994 *
3995 * This function makes the CPU idle until an event wakes it up.
3996 *
3997 * In a regular system, the idle thread should be the only thread responsible
3998 * for making the CPU idle and triggering any type of power management.
3999 * However, in some more constrained systems, such as a single-threaded system,
4000 * the only thread would be responsible for this if needed.
4001 *
4002 * @return N/A
4003 */
4004extern void k_cpu_idle(void);
4005
4006/**
4007 * @brief Make the CPU idle in an atomic fashion.
4008 *
4009 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4010 * must be done atomically before making the CPU idle.
4011 *
4012 * @param key Interrupt locking key obtained from irq_lock().
4013 *
4014 * @return N/A
4015 */
4016extern void k_cpu_atomic_idle(unsigned int key);
4017
Anas Nashif954d5502018-02-25 08:37:28 -06004018
4019/**
4020 * @internal
4021 */
Kumar Galacc334c72017-04-21 10:55:34 -05004022extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004023
Andrew Boiecdb94d62017-04-18 15:22:05 -07004024#ifdef _ARCH_EXCEPT
4025/* This archtecture has direct support for triggering a CPU exception */
4026#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4027#else
4028
Andrew Boiecdb94d62017-04-18 15:22:05 -07004029/* NOTE: This is the implementation for arches that do not implement
4030 * _ARCH_EXCEPT() to generate a real CPU exception.
4031 *
4032 * We won't have a real exception frame to determine the PC value when
4033 * the oops occurred, so print file and line number before we jump into
4034 * the fatal error handler.
4035 */
4036#define _k_except_reason(reason) do { \
4037 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4038 _NanoFatalErrorHandler(reason, &_default_esf); \
4039 CODE_UNREACHABLE; \
4040 } while (0)
4041
4042#endif /* _ARCH__EXCEPT */
4043
4044/**
4045 * @brief Fatally terminate a thread
4046 *
4047 * This should be called when a thread has encountered an unrecoverable
4048 * runtime condition and needs to terminate. What this ultimately
4049 * means is determined by the _fatal_error_handler() implementation, which
4050 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4051 *
4052 * If this is called from ISR context, the default system fatal error handler
4053 * will treat it as an unrecoverable system error, just like k_panic().
4054 */
4055#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4056
4057/**
4058 * @brief Fatally terminate the system
4059 *
4060 * This should be called when the Zephyr kernel has encountered an
4061 * unrecoverable runtime condition and needs to terminate. What this ultimately
4062 * means is determined by the _fatal_error_handler() implementation, which
4063 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4064 */
4065#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4066
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004067/*
4068 * private APIs that are utilized by one or more public APIs
4069 */
4070
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004071#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004072/**
4073 * @internal
4074 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004075extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004076#else
Anas Nashif954d5502018-02-25 08:37:28 -06004077/**
4078 * @internal
4079 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004080#define _init_static_threads() do { } while ((0))
4081#endif
4082
Anas Nashif954d5502018-02-25 08:37:28 -06004083/**
4084 * @internal
4085 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004086extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004087/**
4088 * @internal
4089 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004090extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004091
Andrew Boiedc5d9352017-06-02 12:56:47 -07004092/* arch/cpu.h may declare an architecture or platform-specific macro
4093 * for properly declaring stacks, compatible with MMU/MPU constraints if
4094 * enabled
4095 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004096
4097/**
4098 * @brief Obtain an extern reference to a stack
4099 *
4100 * This macro properly brings the symbol of a thread stack declared
4101 * elsewhere into scope.
4102 *
4103 * @param sym Thread stack symbol name
4104 */
4105#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4106
Andrew Boiedc5d9352017-06-02 12:56:47 -07004107#ifdef _ARCH_THREAD_STACK_DEFINE
4108#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4109#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4110 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4111#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4112#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004113static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004114{
4115 return _ARCH_THREAD_STACK_BUFFER(sym);
4116}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004117#else
4118/**
4119 * @brief Declare a toplevel thread stack memory region
4120 *
4121 * This declares a region of memory suitable for use as a thread's stack.
4122 *
4123 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4124 * 'noinit' section so that it isn't zeroed at boot
4125 *
Andrew Boie507852a2017-07-25 18:47:07 -07004126 * The declared symbol will always be a k_thread_stack_t which can be passed to
4127 * k_thread_create, but should otherwise not be manipulated. If the buffer
4128 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004129 *
4130 * It is legal to precede this definition with the 'static' keyword.
4131 *
4132 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4133 * parameter of k_thread_create(), it may not be the same as the
4134 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4135 *
4136 * @param sym Thread stack symbol name
4137 * @param size Size of the stack memory region
4138 */
4139#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004140 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004141
4142/**
4143 * @brief Declare a toplevel array of thread stack memory regions
4144 *
4145 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4146 * definition for additional details and constraints.
4147 *
4148 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4149 * 'noinit' section so that it isn't zeroed at boot
4150 *
4151 * @param sym Thread stack symbol name
4152 * @param nmemb Number of stacks to declare
4153 * @param size Size of the stack memory region
4154 */
4155
4156#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004157 struct _k_thread_stack_element __noinit \
4158 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004159
4160/**
4161 * @brief Declare an embedded stack memory region
4162 *
4163 * Used for stacks embedded within other data structures. Use is highly
4164 * discouraged but in some cases necessary. For memory protection scenarios,
4165 * it is very important that any RAM preceding this member not be writable
4166 * by threads else a stack overflow will lead to silent corruption. In other
4167 * words, the containing data structure should live in RAM owned by the kernel.
4168 *
4169 * @param sym Thread stack symbol name
4170 * @param size Size of the stack memory region
4171 */
4172#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004173 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004174
4175/**
4176 * @brief Return the size in bytes of a stack memory region
4177 *
4178 * Convenience macro for passing the desired stack size to k_thread_create()
4179 * since the underlying implementation may actually create something larger
4180 * (for instance a guard area).
4181 *
4182 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004183 * passed to K_THREAD_STACK_DEFINE.
4184 *
4185 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4186 * it is not guaranteed to return the original value since each array
4187 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004188 *
4189 * @param sym Stack memory symbol
4190 * @return Size of the stack
4191 */
4192#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4193
4194/**
4195 * @brief Get a pointer to the physical stack buffer
4196 *
4197 * Convenience macro to get at the real underlying stack buffer used by
4198 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4199 * This is really only intended for diagnostic tools which want to examine
4200 * stack memory contents.
4201 *
4202 * @param sym Declared stack symbol name
4203 * @return The buffer itself, a char *
4204 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004205static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004206{
4207 return (char *)sym;
4208}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004209
4210#endif /* _ARCH_DECLARE_STACK */
4211
Chunlin Hane9c97022017-07-07 20:29:30 +08004212/**
4213 * @defgroup mem_domain_apis Memory domain APIs
4214 * @ingroup kernel_apis
4215 * @{
4216 */
4217
4218/** @def MEM_PARTITION_ENTRY
4219 * @brief Used to declare a memory partition entry
4220 */
4221#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4222 {\
4223 .start = _start, \
4224 .size = _size, \
4225 .attr = _attr, \
4226 }
4227
4228/** @def K_MEM_PARTITION_DEFINE
4229 * @brief Used to declare a memory partition
4230 */
4231#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4232#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4233 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304234 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004235 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4236#else
4237#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304238 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004239 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4240#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4241
Chunlin Hane9c97022017-07-07 20:29:30 +08004242/* memory partition */
4243struct k_mem_partition {
4244 /* start address of memory partition */
4245 u32_t start;
4246 /* size of memory partition */
4247 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304248#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004249 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304250 k_mem_partition_attr_t attr;
4251#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004252};
4253
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304254/* memory domian
4255 * Note: Always declare this structure with __kernel prefix
4256 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004257struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304258#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004259 /* partitions in the domain */
4260 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304261#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004262 /* domain q */
4263 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004264 /* number of partitions in the domain */
4265 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004266};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304267
Chunlin Hane9c97022017-07-07 20:29:30 +08004268
4269/**
4270 * @brief Initialize a memory domain.
4271 *
4272 * Initialize a memory domain with given name and memory partitions.
4273 *
4274 * @param domain The memory domain to be initialized.
4275 * @param num_parts The number of array items of "parts" parameter.
4276 * @param parts An array of pointers to the memory partitions. Can be NULL
4277 * if num_parts is zero.
4278 */
4279
Leandro Pereira08de6582018-02-28 14:22:57 -08004280extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004281 struct k_mem_partition *parts[]);
4282/**
4283 * @brief Destroy a memory domain.
4284 *
4285 * Destroy a memory domain.
4286 *
4287 * @param domain The memory domain to be destroyed.
4288 */
4289
4290extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4291
4292/**
4293 * @brief Add a memory partition into a memory domain.
4294 *
4295 * Add a memory partition into a memory domain.
4296 *
4297 * @param domain The memory domain to be added a memory partition.
4298 * @param part The memory partition to be added
4299 */
4300
4301extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4302 struct k_mem_partition *part);
4303
4304/**
4305 * @brief Remove a memory partition from a memory domain.
4306 *
4307 * Remove a memory partition from a memory domain.
4308 *
4309 * @param domain The memory domain to be removed a memory partition.
4310 * @param part The memory partition to be removed
4311 */
4312
4313extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4314 struct k_mem_partition *part);
4315
4316/**
4317 * @brief Add a thread into a memory domain.
4318 *
4319 * Add a thread into a memory domain.
4320 *
4321 * @param domain The memory domain that the thread is going to be added into.
4322 * @param thread ID of thread going to be added into the memory domain.
4323 */
4324
4325extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4326 k_tid_t thread);
4327
4328/**
4329 * @brief Remove a thread from its memory domain.
4330 *
4331 * Remove a thread from its memory domain.
4332 *
4333 * @param thread ID of thread going to be removed from its memory domain.
4334 */
4335
4336extern void k_mem_domain_remove_thread(k_tid_t thread);
4337
Anas Nashif166f5192018-02-25 08:02:36 -06004338/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004339
Andrew Boie756f9072017-10-10 16:01:49 -07004340/**
4341 * @brief Emit a character buffer to the console device
4342 *
4343 * @param c String of characters to print
4344 * @param n The length of the string
4345 */
4346__syscall void k_str_out(char *c, size_t n);
4347
Andy Rosse7172672018-01-24 15:48:32 -08004348/**
4349 * @brief Start a numbered CPU on a MP-capable system
4350
4351 * This starts and initializes a specific CPU. The main thread on
4352 * startup is running on CPU zero, other processors are numbered
4353 * sequentially. On return from this function, the CPU is known to
4354 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004355 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004356 * with the provided key will work to enable them.
4357 *
4358 * Normally, in SMP mode this function will be called by the kernel
4359 * initialization and should not be used as a user API. But it is
4360 * defined here for special-purpose apps which want Zephyr running on
4361 * one core and to use others for design-specific processing.
4362 *
4363 * @param cpu_num Integer number of the CPU
4364 * @param stack Stack memory for the CPU
4365 * @param sz Stack buffer size, in bytes
4366 * @param fn Function to begin running on the CPU. First argument is
4367 * an irq_unlock() key.
4368 * @param arg Untyped argument to be passed to "fn"
4369 */
4370extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4371 void (*fn)(int, void *), void *arg);
4372
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004373#ifdef __cplusplus
4374}
4375#endif
4376
Andrew Boiee004dec2016-11-07 09:01:19 -08004377#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4378/*
4379 * Define new and delete operators.
4380 * At this moment, the operators do nothing since objects are supposed
4381 * to be statically allocated.
4382 */
4383inline void operator delete(void *ptr)
4384{
4385 (void)ptr;
4386}
4387
4388inline void operator delete[](void *ptr)
4389{
4390 (void)ptr;
4391}
4392
4393inline void *operator new(size_t size)
4394{
4395 (void)size;
4396 return NULL;
4397}
4398
4399inline void *operator new[](size_t size)
4400{
4401 (void)size;
4402 return NULL;
4403}
4404
4405/* Placement versions of operator new and delete */
4406inline void operator delete(void *ptr1, void *ptr2)
4407{
4408 (void)ptr1;
4409 (void)ptr2;
4410}
4411
4412inline void operator delete[](void *ptr1, void *ptr2)
4413{
4414 (void)ptr1;
4415 (void)ptr2;
4416}
4417
4418inline void *operator new(size_t size, void *ptr)
4419{
4420 (void)size;
4421 return ptr;
4422}
4423
4424inline void *operator new[](size_t size, void *ptr)
4425{
4426 (void)size;
4427 return ptr;
4428}
4429
4430#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4431
Andrew Boiefa94ee72017-09-28 16:54:35 -07004432#include <syscalls/kernel.h>
4433
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004434#endif /* !_ASMLANGUAGE */
4435
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004436#endif /* _kernel__h_ */