blob: 27fa8ea5da9fa0d3036f7e5b37a8760a13dac495 [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
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530554 *
555 * @deprecated This API is deprecated. Use k_thread_foreach().
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100556 */
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530557__deprecated extern void k_call_stacks_analyze(void);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100558
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530559/**
560 * @brief Iterate over all the threads in the system.
561 *
562 * This routine iterates over all the threads in the system and
563 * calls the user_cb function for each thread.
564 *
565 * @param user_cb Pointer to the user callback function.
566 * @param user_data Pointer to user data.
567 *
568 * @note CONFIG_THREAD_MONITOR must be set for this function
569 * to be effective. Also this API uses irq_lock to protect the
570 * _kernel.threads list which means creation of new threads and
571 * terminations of existing threads are blocked until this
572 * API returns.
573 *
574 * @return N/A
575 */
576extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
577
Anas Nashif166f5192018-02-25 08:02:36 -0600578/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100579
580/**
Allan Stephensc98da842016-11-11 15:45:03 -0500581 * @defgroup thread_apis Thread APIs
582 * @ingroup kernel_apis
583 * @{
584 */
585
Benjamin Walshed240f22017-01-22 13:05:08 -0500586#endif /* !_ASMLANGUAGE */
587
588
589/*
590 * Thread user options. May be needed by assembly code. Common part uses low
591 * bits, arch-specific use high bits.
592 */
593
594/* system thread that must not abort */
595#define K_ESSENTIAL (1 << 0)
596
597#if defined(CONFIG_FP_SHARING)
598/* thread uses floating point registers */
599#define K_FP_REGS (1 << 1)
600#endif
601
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700602/* This thread has dropped from supervisor mode to user mode and consequently
603 * has additional restrictions
604 */
605#define K_USER (1 << 2)
606
Andrew Boie47f8fd12017-10-05 11:11:02 -0700607/* Indicates that the thread being created should inherit all kernel object
608 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
609 * is not enabled.
610 */
611#define K_INHERIT_PERMS (1 << 3)
612
Benjamin Walshed240f22017-01-22 13:05:08 -0500613#ifdef CONFIG_X86
614/* x86 Bitmask definitions for threads user options */
615
616#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
617/* thread uses SSEx (and also FP) registers */
618#define K_SSE_REGS (1 << 7)
619#endif
620#endif
621
622/* end - thread options */
623
624#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400625/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700626 * @brief Create a thread.
627 *
628 * This routine initializes a thread, then schedules it for execution.
629 *
630 * The new thread may be scheduled for immediate execution or a delayed start.
631 * If the newly spawned thread does not have a delayed start the kernel
632 * scheduler may preempt the current thread to allow the new thread to
633 * execute.
634 *
635 * Thread options are architecture-specific, and can include K_ESSENTIAL,
636 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
637 * them using "|" (the logical OR operator).
638 *
639 * Historically, users often would use the beginning of the stack memory region
640 * to store the struct k_thread data, although corruption will occur if the
641 * stack overflows this region and stack protection features may not detect this
642 * situation.
643 *
644 * @param new_thread Pointer to uninitialized struct k_thread
645 * @param stack Pointer to the stack space.
646 * @param stack_size Stack size in bytes.
647 * @param entry Thread entry function.
648 * @param p1 1st entry point parameter.
649 * @param p2 2nd entry point parameter.
650 * @param p3 3rd entry point parameter.
651 * @param prio Thread priority.
652 * @param options Thread options.
653 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
654 *
655 * @return ID of new thread.
656 */
Andrew Boie662c3452017-10-02 10:51:18 -0700657__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700658 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700659 size_t stack_size,
660 k_thread_entry_t entry,
661 void *p1, void *p2, void *p3,
662 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700663
Andrew Boie3f091b52017-08-30 14:34:14 -0700664/**
665 * @brief Drop a thread's privileges permanently to user mode
666 *
667 * @param entry Function to start executing from
668 * @param p1 1st entry point parameter
669 * @param p2 2nd entry point parameter
670 * @param p3 3rd entry point parameter
671 */
672extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
673 void *p1, void *p2,
674 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700675
Andrew Boied26cf2d2017-03-30 13:07:02 -0700676/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700677 * @brief Grant a thread access to a NULL-terminated set of kernel objects
678 *
679 * This is a convenience function. For the provided thread, grant access to
680 * the remaining arguments, which must be pointers to kernel objects.
681 * The final argument must be a NULL.
682 *
683 * The thread object must be initialized (i.e. running). The objects don't
684 * need to be.
685 *
686 * @param thread Thread to grant access to objects
687 * @param ... NULL-terminated list of kernel object pointers
688 */
689extern void __attribute__((sentinel))
690 k_thread_access_grant(struct k_thread *thread, ...);
691
692/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500693 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400694 *
Allan Stephensc98da842016-11-11 15:45:03 -0500695 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500696 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400697 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500698 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400699 *
700 * @return N/A
701 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700702__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400703
704/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500705 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400706 *
707 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500708 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400709 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400710 * @return N/A
711 */
Kumar Galacc334c72017-04-21 10:55:34 -0500712extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400713
714/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500715 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400716 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500717 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400718 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500719 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400720 *
721 * @return N/A
722 */
Andrew Boie468190a2017-09-29 14:00:48 -0700723__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400724
725/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500726 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400727 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500728 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400729 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500730 * If @a thread is not currently sleeping, the routine has no effect.
731 *
732 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400733 *
734 * @return N/A
735 */
Andrew Boie468190a2017-09-29 14:00:48 -0700736__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400737
738/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500739 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400740 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500741 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400742 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700743__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400744
745/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500746 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400747 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500748 * This routine prevents @a thread from executing if it has not yet started
749 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400750 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500751 * @param thread ID of thread to cancel.
752 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700753 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500754 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700755 *
756 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400757 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700758__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400759
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400760/**
Allan Stephensc98da842016-11-11 15:45:03 -0500761 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400762 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500763 * This routine permanently stops execution of @a thread. The thread is taken
764 * off all kernel queues it is part of (i.e. the ready queue, the timeout
765 * queue, or a kernel object wait queue). However, any kernel resources the
766 * thread might currently own (such as mutexes or memory blocks) are not
767 * released. It is the responsibility of the caller of this routine to ensure
768 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400769 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500770 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400771 *
772 * @return N/A
773 */
Andrew Boie468190a2017-09-29 14:00:48 -0700774__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400775
Andrew Boie7d627c52017-08-30 11:01:56 -0700776
777/**
778 * @brief Start an inactive thread
779 *
780 * If a thread was created with K_FOREVER in the delay parameter, it will
781 * not be added to the scheduling queue until this function is called
782 * on it.
783 *
784 * @param thread thread to start
785 */
Andrew Boie468190a2017-09-29 14:00:48 -0700786__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700787
Allan Stephensc98da842016-11-11 15:45:03 -0500788/**
789 * @cond INTERNAL_HIDDEN
790 */
791
Benjamin Walshd211a522016-12-06 11:44:01 -0500792/* timeout has timed out and is not on _timeout_q anymore */
793#define _EXPIRED (-2)
794
795/* timeout is not in use */
796#define _INACTIVE (-1)
797
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400798struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700799 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700800 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400801 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700802 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500803 void *init_p1;
804 void *init_p2;
805 void *init_p3;
806 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500807 u32_t init_options;
808 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500809 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400810};
811
Andrew Boied26cf2d2017-03-30 13:07:02 -0700812#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400813 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500814 prio, options, delay, abort, groups) \
815 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700816 .init_thread = (thread), \
817 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500818 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700819 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400820 .init_p1 = (void *)p1, \
821 .init_p2 = (void *)p2, \
822 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500823 .init_prio = (prio), \
824 .init_options = (options), \
825 .init_delay = (delay), \
826 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400827 }
828
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400829/**
Allan Stephensc98da842016-11-11 15:45:03 -0500830 * INTERNAL_HIDDEN @endcond
831 */
832
833/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500834 * @brief Statically define and initialize a thread.
835 *
836 * The thread may be scheduled for immediate execution or a delayed start.
837 *
838 * Thread options are architecture-specific, and can include K_ESSENTIAL,
839 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
840 * them using "|" (the logical OR operator).
841 *
842 * The ID of the thread can be accessed using:
843 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500844 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500845 *
846 * @param name Name of the thread.
847 * @param stack_size Stack size in bytes.
848 * @param entry Thread entry function.
849 * @param p1 1st entry point parameter.
850 * @param p2 2nd entry point parameter.
851 * @param p3 3rd entry point parameter.
852 * @param prio Thread priority.
853 * @param options Thread options.
854 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400855 *
856 * @internal It has been observed that the x86 compiler by default aligns
857 * these _static_thread_data structures to 32-byte boundaries, thereby
858 * wasting space. To work around this, force a 4-byte alignment.
859 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500860#define K_THREAD_DEFINE(name, stack_size, \
861 entry, p1, p2, p3, \
862 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700863 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700864 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500865 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500866 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700867 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
868 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500869 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700870 NULL, 0); \
871 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400872
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400873/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500874 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400875 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500876 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400877 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500878 * @param thread ID of thread whose priority is needed.
879 *
880 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400881 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700882__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400883
884/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500885 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400886 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500887 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400888 *
889 * Rescheduling can occur immediately depending on the priority @a thread is
890 * set to:
891 *
892 * - If its priority is raised above the priority of the caller of this
893 * function, and the caller is preemptible, @a thread will be scheduled in.
894 *
895 * - If the caller operates on itself, it lowers its priority below that of
896 * other threads in the system, and the caller is preemptible, the thread of
897 * highest priority will be scheduled in.
898 *
899 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
900 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
901 * highest priority.
902 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500903 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400904 * @param prio New priority.
905 *
906 * @warning Changing the priority of a thread currently involved in mutex
907 * priority inheritance may result in undefined behavior.
908 *
909 * @return N/A
910 */
Andrew Boie468190a2017-09-29 14:00:48 -0700911__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400912
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400913/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500914 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400915 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500916 * This routine prevents the kernel scheduler from making @a thread the
917 * current thread. All other internal operations on @a thread are still
918 * performed; for example, any timeout it is waiting on keeps ticking,
919 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400920 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500921 * If @a thread is already suspended, the routine has no effect.
922 *
923 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400924 *
925 * @return N/A
926 */
Andrew Boie468190a2017-09-29 14:00:48 -0700927__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400928
929/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500930 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400931 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500932 * This routine allows the kernel scheduler to make @a thread the current
933 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400934 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500935 * If @a thread is not currently suspended, the routine has no effect.
936 *
937 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400938 *
939 * @return N/A
940 */
Andrew Boie468190a2017-09-29 14:00:48 -0700941__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400942
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400943/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500944 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400945 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500946 * This routine specifies how the scheduler will perform time slicing of
947 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400948 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500949 * To enable time slicing, @a slice must be non-zero. The scheduler
950 * ensures that no thread runs for more than the specified time limit
951 * before other threads of that priority are given a chance to execute.
952 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700953 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400954 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500955 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400956 * execute. Once the scheduler selects a thread for execution, there is no
957 * minimum guaranteed time the thread will execute before threads of greater or
958 * equal priority are scheduled.
959 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500960 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400961 * for execution, this routine has no effect; the thread is immediately
962 * rescheduled after the slice period expires.
963 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500964 * To disable timeslicing, set both @a slice and @a prio to zero.
965 *
966 * @param slice Maximum time slice length (in milliseconds).
967 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400968 *
969 * @return N/A
970 */
Kumar Galacc334c72017-04-21 10:55:34 -0500971extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400972
Anas Nashif166f5192018-02-25 08:02:36 -0600973/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -0500974
975/**
976 * @addtogroup isr_apis
977 * @{
978 */
979
980/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500981 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400982 *
Allan Stephensc98da842016-11-11 15:45:03 -0500983 * This routine allows the caller to customize its actions, depending on
984 * whether it is a thread or an ISR.
985 *
986 * @note Can be called by ISRs.
987 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500988 * @return 0 if invoked by a thread.
989 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400990 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500991extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400992
Benjamin Walsh445830d2016-11-10 15:54:27 -0500993/**
994 * @brief Determine if code is running in a preemptible thread.
995 *
Allan Stephensc98da842016-11-11 15:45:03 -0500996 * This routine allows the caller to customize its actions, depending on
997 * whether it can be preempted by another thread. The routine returns a 'true'
998 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500999 *
Allan Stephensc98da842016-11-11 15:45:03 -05001000 * - The code is running in a thread, not at ISR.
1001 * - The thread's priority is in the preemptible range.
1002 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001003 *
Allan Stephensc98da842016-11-11 15:45:03 -05001004 * @note Can be called by ISRs.
1005 *
1006 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001007 * @return Non-zero if invoked by a preemptible thread.
1008 */
Andrew Boie468190a2017-09-29 14:00:48 -07001009__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001010
Allan Stephensc98da842016-11-11 15:45:03 -05001011/**
Anas Nashif166f5192018-02-25 08:02:36 -06001012 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001013 */
1014
1015/**
1016 * @addtogroup thread_apis
1017 * @{
1018 */
1019
1020/**
1021 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001022 *
Allan Stephensc98da842016-11-11 15:45:03 -05001023 * This routine prevents the current thread from being preempted by another
1024 * thread by instructing the scheduler to treat it as a cooperative thread.
1025 * If the thread subsequently performs an operation that makes it unready,
1026 * it will be context switched out in the normal manner. When the thread
1027 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001028 *
Allan Stephensc98da842016-11-11 15:45:03 -05001029 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001030 *
Allan Stephensc98da842016-11-11 15:45:03 -05001031 * @note k_sched_lock() and k_sched_unlock() should normally be used
1032 * when the operation being performed can be safely interrupted by ISRs.
1033 * However, if the amount of processing involved is very small, better
1034 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001035 *
1036 * @return N/A
1037 */
1038extern void k_sched_lock(void);
1039
Allan Stephensc98da842016-11-11 15:45:03 -05001040/**
1041 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001042 *
Allan Stephensc98da842016-11-11 15:45:03 -05001043 * This routine reverses the effect of a previous call to k_sched_lock().
1044 * A thread must call the routine once for each time it called k_sched_lock()
1045 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001046 *
1047 * @return N/A
1048 */
1049extern void k_sched_unlock(void);
1050
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001051/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001052 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001053 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001054 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001055 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001056 * Custom data is not used by the kernel itself, and is freely available
1057 * for a thread to use as it sees fit. It can be used as a framework
1058 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001059 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001060 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001061 *
1062 * @return N/A
1063 */
Andrew Boie468190a2017-09-29 14:00:48 -07001064__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001065
1066/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001067 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001068 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001069 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001070 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001071 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001072 */
Andrew Boie468190a2017-09-29 14:00:48 -07001073__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001074
1075/**
Anas Nashif166f5192018-02-25 08:02:36 -06001076 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001077 */
1078
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001079#include <sys_clock.h>
1080
Allan Stephensc2f15a42016-11-17 12:24:22 -05001081/**
1082 * @addtogroup clock_apis
1083 * @{
1084 */
1085
1086/**
1087 * @brief Generate null timeout delay.
1088 *
1089 * This macro generates a timeout delay that that instructs a kernel API
1090 * not to wait if the requested operation cannot be performed immediately.
1091 *
1092 * @return Timeout delay value.
1093 */
1094#define K_NO_WAIT 0
1095
1096/**
1097 * @brief Generate timeout delay from milliseconds.
1098 *
1099 * This macro generates a timeout delay that that instructs a kernel API
1100 * to wait up to @a ms milliseconds to perform the requested operation.
1101 *
1102 * @param ms Duration in milliseconds.
1103 *
1104 * @return Timeout delay value.
1105 */
Johan Hedberg14471692016-11-13 10:52:15 +02001106#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001107
1108/**
1109 * @brief Generate timeout delay from seconds.
1110 *
1111 * This macro generates a timeout delay that that instructs a kernel API
1112 * to wait up to @a s seconds to perform the requested operation.
1113 *
1114 * @param s Duration in seconds.
1115 *
1116 * @return Timeout delay value.
1117 */
Johan Hedberg14471692016-11-13 10:52:15 +02001118#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001119
1120/**
1121 * @brief Generate timeout delay from minutes.
1122 *
1123 * This macro generates a timeout delay that that instructs a kernel API
1124 * to wait up to @a m minutes to perform the requested operation.
1125 *
1126 * @param m Duration in minutes.
1127 *
1128 * @return Timeout delay value.
1129 */
Johan Hedberg14471692016-11-13 10:52:15 +02001130#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001131
1132/**
1133 * @brief Generate timeout delay from hours.
1134 *
1135 * This macro generates a timeout delay that that instructs a kernel API
1136 * to wait up to @a h hours to perform the requested operation.
1137 *
1138 * @param h Duration in hours.
1139 *
1140 * @return Timeout delay value.
1141 */
Johan Hedberg14471692016-11-13 10:52:15 +02001142#define K_HOURS(h) K_MINUTES((h) * 60)
1143
Allan Stephensc98da842016-11-11 15:45:03 -05001144/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001145 * @brief Generate infinite timeout delay.
1146 *
1147 * This macro generates a timeout delay that that instructs a kernel API
1148 * to wait as long as necessary to perform the requested operation.
1149 *
1150 * @return Timeout delay value.
1151 */
1152#define K_FOREVER (-1)
1153
1154/**
Anas Nashif166f5192018-02-25 08:02:36 -06001155 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001156 */
1157
1158/**
Allan Stephensc98da842016-11-11 15:45:03 -05001159 * @cond INTERNAL_HIDDEN
1160 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001161
Benjamin Walsh62092182016-12-20 14:39:08 -05001162/* kernel clocks */
1163
1164#if (sys_clock_ticks_per_sec == 1000) || \
1165 (sys_clock_ticks_per_sec == 500) || \
1166 (sys_clock_ticks_per_sec == 250) || \
1167 (sys_clock_ticks_per_sec == 125) || \
1168 (sys_clock_ticks_per_sec == 100) || \
1169 (sys_clock_ticks_per_sec == 50) || \
1170 (sys_clock_ticks_per_sec == 25) || \
1171 (sys_clock_ticks_per_sec == 20) || \
1172 (sys_clock_ticks_per_sec == 10) || \
1173 (sys_clock_ticks_per_sec == 1)
1174
1175 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1176#else
1177 /* yields horrible 64-bit math on many architectures: try to avoid */
1178 #define _NON_OPTIMIZED_TICKS_PER_SEC
1179#endif
1180
1181#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001182extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001183#else
Kumar Galacc334c72017-04-21 10:55:34 -05001184static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001185{
Kumar Galacc334c72017-04-21 10:55:34 -05001186 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001187}
1188#endif
1189
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001190/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001191#ifdef CONFIG_TICKLESS_KERNEL
1192#define _TICK_ALIGN 0
1193#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001194#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001195#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001196
Kumar Galacc334c72017-04-21 10:55:34 -05001197static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001198{
Benjamin Walsh62092182016-12-20 14:39:08 -05001199#ifdef CONFIG_SYS_CLOCK_EXISTS
1200
1201#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001202 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001203#else
Kumar Galacc334c72017-04-21 10:55:34 -05001204 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001205#endif
1206
1207#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001208 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001209 return 0;
1210#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001211}
1212
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001213struct k_timer {
1214 /*
1215 * _timeout structure must be first here if we want to use
1216 * dynamic timer allocation. timeout.node is used in the double-linked
1217 * list of free timers
1218 */
1219 struct _timeout timeout;
1220
Allan Stephens45bfa372016-10-12 12:39:42 -05001221 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001222 _wait_q_t wait_q;
1223
1224 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001225 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001226
1227 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001228 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001229
1230 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001231 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001232
Allan Stephens45bfa372016-10-12 12:39:42 -05001233 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001234 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001235
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001236 /* user-specific data, also used to support legacy features */
1237 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001238
Anas Nashif2f203c22016-12-18 06:57:45 -05001239 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001240};
1241
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001242#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001243 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001244 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001245 .timeout.wait_q = NULL, \
1246 .timeout.thread = NULL, \
1247 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001248 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001249 .expiry_fn = expiry, \
1250 .stop_fn = stop, \
1251 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001252 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001253 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001254 }
1255
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001256#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1257
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001258/**
Allan Stephensc98da842016-11-11 15:45:03 -05001259 * INTERNAL_HIDDEN @endcond
1260 */
1261
1262/**
1263 * @defgroup timer_apis Timer APIs
1264 * @ingroup kernel_apis
1265 * @{
1266 */
1267
1268/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001269 * @typedef k_timer_expiry_t
1270 * @brief Timer expiry function type.
1271 *
1272 * A timer's expiry function is executed by the system clock interrupt handler
1273 * each time the timer expires. The expiry function is optional, and is only
1274 * invoked if the timer has been initialized with one.
1275 *
1276 * @param timer Address of timer.
1277 *
1278 * @return N/A
1279 */
1280typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1281
1282/**
1283 * @typedef k_timer_stop_t
1284 * @brief Timer stop function type.
1285 *
1286 * A timer's stop function is executed if the timer is stopped prematurely.
1287 * The function runs in the context of the thread that stops the timer.
1288 * The stop function is optional, and is only invoked if the timer has been
1289 * initialized with one.
1290 *
1291 * @param timer Address of timer.
1292 *
1293 * @return N/A
1294 */
1295typedef void (*k_timer_stop_t)(struct k_timer *timer);
1296
1297/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001298 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001299 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001300 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001301 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001302 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001303 *
1304 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001305 * @param expiry_fn Function to invoke each time the timer expires.
1306 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001307 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001308#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001309 struct k_timer name \
1310 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001311 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001312
Allan Stephens45bfa372016-10-12 12:39:42 -05001313/**
1314 * @brief Initialize a timer.
1315 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001316 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001317 *
1318 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001319 * @param expiry_fn Function to invoke each time the timer expires.
1320 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001321 *
1322 * @return N/A
1323 */
1324extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001325 k_timer_expiry_t expiry_fn,
1326 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001327
Allan Stephens45bfa372016-10-12 12:39:42 -05001328/**
1329 * @brief Start a timer.
1330 *
1331 * This routine starts a timer, and resets its status to zero. The timer
1332 * begins counting down using the specified duration and period values.
1333 *
1334 * Attempting to start a timer that is already running is permitted.
1335 * The timer's status is reset to zero and the timer begins counting down
1336 * using the new duration and period values.
1337 *
1338 * @param timer Address of timer.
1339 * @param duration Initial timer duration (in milliseconds).
1340 * @param period Timer period (in milliseconds).
1341 *
1342 * @return N/A
1343 */
Andrew Boiea354d492017-09-29 16:22:28 -07001344__syscall void k_timer_start(struct k_timer *timer,
1345 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001346
1347/**
1348 * @brief Stop a timer.
1349 *
1350 * This routine stops a running timer prematurely. The timer's stop function,
1351 * if one exists, is invoked by the caller.
1352 *
1353 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001354 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001355 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001356 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1357 * if @a k_timer_stop is to be called from ISRs.
1358 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001359 * @param timer Address of timer.
1360 *
1361 * @return N/A
1362 */
Andrew Boiea354d492017-09-29 16:22:28 -07001363__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001364
1365/**
1366 * @brief Read timer status.
1367 *
1368 * This routine reads the timer's status, which indicates the number of times
1369 * it has expired since its status was last read.
1370 *
1371 * Calling this routine resets the timer's status to zero.
1372 *
1373 * @param timer Address of timer.
1374 *
1375 * @return Timer status.
1376 */
Andrew Boiea354d492017-09-29 16:22:28 -07001377__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001378
1379/**
1380 * @brief Synchronize thread to timer expiration.
1381 *
1382 * This routine blocks the calling thread until the timer's status is non-zero
1383 * (indicating that it has expired at least once since it was last examined)
1384 * or the timer is stopped. If the timer status is already non-zero,
1385 * or the timer is already stopped, the caller continues without waiting.
1386 *
1387 * Calling this routine resets the timer's status to zero.
1388 *
1389 * This routine must not be used by interrupt handlers, since they are not
1390 * allowed to block.
1391 *
1392 * @param timer Address of timer.
1393 *
1394 * @return Timer status.
1395 */
Andrew Boiea354d492017-09-29 16:22:28 -07001396__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001397
1398/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001399 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001400 *
1401 * This routine computes the (approximate) time remaining before a running
1402 * timer next expires. If the timer is not running, it returns zero.
1403 *
1404 * @param timer Address of timer.
1405 *
1406 * @return Remaining time (in milliseconds).
1407 */
Andrew Boiea354d492017-09-29 16:22:28 -07001408__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1409
1410static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001411{
1412 return _timeout_remaining_get(&timer->timeout);
1413}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001414
Allan Stephensc98da842016-11-11 15:45:03 -05001415/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001416 * @brief Associate user-specific data with a timer.
1417 *
1418 * This routine records the @a user_data with the @a timer, to be retrieved
1419 * later.
1420 *
1421 * It can be used e.g. in a timer handler shared across multiple subsystems to
1422 * retrieve data specific to the subsystem this timer is associated with.
1423 *
1424 * @param timer Address of timer.
1425 * @param user_data User data to associate with the timer.
1426 *
1427 * @return N/A
1428 */
Andrew Boiea354d492017-09-29 16:22:28 -07001429__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1430
Anas Nashif954d5502018-02-25 08:37:28 -06001431/**
1432 * @internal
1433 */
Andrew Boiea354d492017-09-29 16:22:28 -07001434static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1435 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001436{
1437 timer->user_data = user_data;
1438}
1439
1440/**
1441 * @brief Retrieve the user-specific data from a timer.
1442 *
1443 * @param timer Address of timer.
1444 *
1445 * @return The user data.
1446 */
Andrew Boiea354d492017-09-29 16:22:28 -07001447__syscall void *k_timer_user_data_get(struct k_timer *timer);
1448
1449static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001450{
1451 return timer->user_data;
1452}
1453
Anas Nashif166f5192018-02-25 08:02:36 -06001454/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001455
Allan Stephensc98da842016-11-11 15:45:03 -05001456/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001457 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001458 * @{
1459 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001460
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001461/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001462 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001463 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001464 * This routine returns the elapsed time since the system booted,
1465 * in milliseconds.
1466 *
1467 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001468 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001469__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001470
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001471/**
1472 * @brief Enable clock always on in tickless kernel
1473 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001474 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001475 * there are no timer events programmed in tickless kernel
1476 * scheduling. This is necessary if the clock is used to track
1477 * passage of time.
1478 *
1479 * @retval prev_status Previous status of always on flag
1480 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301481#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001482static inline int k_enable_sys_clock_always_on(void)
1483{
1484 int prev_status = _sys_clock_always_on;
1485
1486 _sys_clock_always_on = 1;
1487 _enable_sys_clock();
1488
1489 return prev_status;
1490}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301491#else
1492#define k_enable_sys_clock_always_on() do { } while ((0))
1493#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001494
1495/**
1496 * @brief Disable clock always on in tickless kernel
1497 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001498 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001499 * there are no timer events programmed in tickless kernel
1500 * scheduling. To save power, this routine should be called
1501 * immediately when clock is not used to track time.
1502 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301503#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001504static inline void k_disable_sys_clock_always_on(void)
1505{
1506 _sys_clock_always_on = 0;
1507}
1508#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001509#define k_disable_sys_clock_always_on() do { } while ((0))
1510#endif
1511
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001512/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001513 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001514 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001515 * This routine returns the lower 32-bits of the elapsed time since the system
1516 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001517 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001518 * This routine can be more efficient than k_uptime_get(), as it reduces the
1519 * need for interrupt locking and 64-bit math. However, the 32-bit result
1520 * cannot hold a system uptime time larger than approximately 50 days, so the
1521 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001522 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001523 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001524 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001525__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001526
1527/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001528 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001529 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001530 * This routine computes the elapsed time between the current system uptime
1531 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001532 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001533 * @param reftime Pointer to a reference time, which is updated to the current
1534 * uptime upon return.
1535 *
1536 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001537 */
Kumar Galacc334c72017-04-21 10:55:34 -05001538extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001539
1540/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001541 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001542 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001543 * This routine computes the elapsed time between the current system uptime
1544 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001545 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001546 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1547 * need for interrupt locking and 64-bit math. However, the 32-bit result
1548 * cannot hold an elapsed time larger than approximately 50 days, so the
1549 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001550 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001551 * @param reftime Pointer to a reference time, which is updated to the current
1552 * uptime upon return.
1553 *
1554 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001555 */
Kumar Galacc334c72017-04-21 10:55:34 -05001556extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001557
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001558/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001559 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001560 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001561 * This routine returns the current time, as measured by the system's hardware
1562 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001563 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001564 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001565 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001566#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001567
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001568/**
Anas Nashif166f5192018-02-25 08:02:36 -06001569 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001570 */
1571
Allan Stephensc98da842016-11-11 15:45:03 -05001572/**
1573 * @cond INTERNAL_HIDDEN
1574 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001575
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001576struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001577 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001578 union {
1579 _wait_q_t wait_q;
1580
1581 _POLL_EVENT;
1582 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001583
1584 _OBJECT_TRACING_NEXT_PTR(k_queue);
1585};
1586
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001587#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001588 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001589 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Michael Hope5f67a612018-03-17 12:44:40 +01001590 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001591 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001592 _OBJECT_TRACING_INIT \
1593 }
1594
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001595#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1596
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001597/**
1598 * INTERNAL_HIDDEN @endcond
1599 */
1600
1601/**
1602 * @defgroup queue_apis Queue APIs
1603 * @ingroup kernel_apis
1604 * @{
1605 */
1606
1607/**
1608 * @brief Initialize a queue.
1609 *
1610 * This routine initializes a queue object, prior to its first use.
1611 *
1612 * @param queue Address of the queue.
1613 *
1614 * @return N/A
1615 */
1616extern void k_queue_init(struct k_queue *queue);
1617
1618/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001619 * @brief Cancel waiting on a queue.
1620 *
1621 * This routine causes first thread pending on @a queue, if any, to
1622 * return from k_queue_get() call with NULL value (as if timeout expired).
1623 *
1624 * @note Can be called by ISRs.
1625 *
1626 * @param queue Address of the queue.
1627 *
1628 * @return N/A
1629 */
1630extern void k_queue_cancel_wait(struct k_queue *queue);
1631
1632/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001633 * @brief Append an element to the end of a queue.
1634 *
1635 * This routine appends a data item to @a queue. A queue data item must be
1636 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1637 * reserved for the kernel's use.
1638 *
1639 * @note Can be called by ISRs.
1640 *
1641 * @param queue Address of the queue.
1642 * @param data Address of the data item.
1643 *
1644 * @return N/A
1645 */
1646extern void k_queue_append(struct k_queue *queue, void *data);
1647
1648/**
1649 * @brief Prepend an element to a queue.
1650 *
1651 * This routine prepends a data item to @a queue. A queue data item must be
1652 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1653 * reserved for the kernel's use.
1654 *
1655 * @note Can be called by ISRs.
1656 *
1657 * @param queue Address of the queue.
1658 * @param data Address of the data item.
1659 *
1660 * @return N/A
1661 */
1662extern void k_queue_prepend(struct k_queue *queue, void *data);
1663
1664/**
1665 * @brief Inserts an element to a queue.
1666 *
1667 * This routine inserts a data item to @a queue after previous item. A queue
1668 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1669 * item are reserved for the kernel's use.
1670 *
1671 * @note Can be called by ISRs.
1672 *
1673 * @param queue Address of the queue.
1674 * @param prev Address of the previous data item.
1675 * @param data Address of the data item.
1676 *
1677 * @return N/A
1678 */
1679extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1680
1681/**
1682 * @brief Atomically append a list of elements to a queue.
1683 *
1684 * This routine adds a list of data items to @a queue in one operation.
1685 * The data items must be in a singly-linked list, with the first 32 bits
1686 * in each data item pointing to the next data item; the list must be
1687 * NULL-terminated.
1688 *
1689 * @note Can be called by ISRs.
1690 *
1691 * @param queue Address of the queue.
1692 * @param head Pointer to first node in singly-linked list.
1693 * @param tail Pointer to last node in singly-linked list.
1694 *
1695 * @return N/A
1696 */
1697extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1698
1699/**
1700 * @brief Atomically add a list of elements to a queue.
1701 *
1702 * This routine adds a list of data items to @a queue in one operation.
1703 * The data items must be in a singly-linked list implemented using a
1704 * sys_slist_t object. Upon completion, the original list is empty.
1705 *
1706 * @note Can be called by ISRs.
1707 *
1708 * @param queue Address of the queue.
1709 * @param list Pointer to sys_slist_t object.
1710 *
1711 * @return N/A
1712 */
1713extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1714
1715/**
1716 * @brief Get an element from a queue.
1717 *
1718 * This routine removes first data item from @a queue. The first 32 bits of the
1719 * data item are reserved for the kernel's use.
1720 *
1721 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1722 *
1723 * @param queue Address of the queue.
1724 * @param timeout Waiting period to obtain a data item (in milliseconds),
1725 * or one of the special values K_NO_WAIT and K_FOREVER.
1726 *
1727 * @return Address of the data item if successful; NULL if returned
1728 * without waiting, or waiting period timed out.
1729 */
Kumar Galacc334c72017-04-21 10:55:34 -05001730extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001731
1732/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001733 * @brief Remove an element from a queue.
1734 *
1735 * This routine removes data item from @a queue. The first 32 bits of the
1736 * data item are reserved for the kernel's use. Removing elements from k_queue
1737 * rely on sys_slist_find_and_remove which is not a constant time operation.
1738 *
1739 * @note Can be called by ISRs
1740 *
1741 * @param queue Address of the queue.
1742 * @param data Address of the data item.
1743 *
1744 * @return true if data item was removed
1745 */
1746static inline bool k_queue_remove(struct k_queue *queue, void *data)
1747{
1748 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1749}
1750
1751/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001752 * @brief Query a queue to see if it has data available.
1753 *
1754 * Note that the data might be already gone by the time this function returns
1755 * if other threads are also trying to read from the queue.
1756 *
1757 * @note Can be called by ISRs.
1758 *
1759 * @param queue Address of the queue.
1760 *
1761 * @return Non-zero if the queue is empty.
1762 * @return 0 if data is available.
1763 */
1764static inline int k_queue_is_empty(struct k_queue *queue)
1765{
1766 return (int)sys_slist_is_empty(&queue->data_q);
1767}
1768
1769/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001770 * @brief Peek element at the head of queue.
1771 *
1772 * Return element from the head of queue without removing it.
1773 *
1774 * @param queue Address of the queue.
1775 *
1776 * @return Head element, or NULL if queue is empty.
1777 */
1778static inline void *k_queue_peek_head(struct k_queue *queue)
1779{
1780 return sys_slist_peek_head(&queue->data_q);
1781}
1782
1783/**
1784 * @brief Peek element at the tail of queue.
1785 *
1786 * Return element from the tail of queue without removing it.
1787 *
1788 * @param queue Address of the queue.
1789 *
1790 * @return Tail element, or NULL if queue is empty.
1791 */
1792static inline void *k_queue_peek_tail(struct k_queue *queue)
1793{
1794 return sys_slist_peek_tail(&queue->data_q);
1795}
1796
1797/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001798 * @brief Statically define and initialize a queue.
1799 *
1800 * The queue can be accessed outside the module where it is defined using:
1801 *
1802 * @code extern struct k_queue <name>; @endcode
1803 *
1804 * @param name Name of the queue.
1805 */
1806#define K_QUEUE_DEFINE(name) \
1807 struct k_queue name \
1808 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001809 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001810
Anas Nashif166f5192018-02-25 08:02:36 -06001811/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001812
1813/**
1814 * @cond INTERNAL_HIDDEN
1815 */
1816
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001817struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001818 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001819};
1820
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001821#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001822 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001823 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001824 }
1825
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001826#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1827
Allan Stephensc98da842016-11-11 15:45:03 -05001828/**
1829 * INTERNAL_HIDDEN @endcond
1830 */
1831
1832/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001833 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05001834 * @ingroup kernel_apis
1835 * @{
1836 */
1837
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001838/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001839 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001840 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001841 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001842 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001843 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001844 *
1845 * @return N/A
1846 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001847#define k_fifo_init(fifo) \
1848 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001849
1850/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001851 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001852 *
1853 * This routine causes first thread pending on @a fifo, if any, to
1854 * return from k_fifo_get() call with NULL value (as if timeout
1855 * expired).
1856 *
1857 * @note Can be called by ISRs.
1858 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001859 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001860 *
1861 * @return N/A
1862 */
1863#define k_fifo_cancel_wait(fifo) \
1864 k_queue_cancel_wait((struct k_queue *) fifo)
1865
1866/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001867 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001868 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001869 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001870 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1871 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001872 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001873 * @note Can be called by ISRs.
1874 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001875 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001876 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001877 *
1878 * @return N/A
1879 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001880#define k_fifo_put(fifo, data) \
1881 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001882
1883/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001884 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001885 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001886 * This routine adds a list of data items to @a fifo in one operation.
1887 * The data items must be in a singly-linked list, with the first 32 bits
1888 * each data item pointing to the next data item; the list must be
1889 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001890 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001891 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001892 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001893 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001894 * @param head Pointer to first node in singly-linked list.
1895 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001896 *
1897 * @return N/A
1898 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001899#define k_fifo_put_list(fifo, head, tail) \
1900 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001901
1902/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001903 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001904 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001905 * This routine adds a list of data items to @a fifo in one operation.
1906 * The data items must be in a singly-linked list implemented using a
1907 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001908 * and must be re-initialized via sys_slist_init().
1909 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001910 * @note Can be called by ISRs.
1911 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001912 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001913 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001914 *
1915 * @return N/A
1916 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001917#define k_fifo_put_slist(fifo, list) \
1918 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001919
1920/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001921 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001922 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001923 * This routine removes a data item from @a fifo in a "first in, first out"
1924 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001925 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001926 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1927 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001928 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001929 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001930 * or one of the special values K_NO_WAIT and K_FOREVER.
1931 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001932 * @return Address of the data item if successful; NULL if returned
1933 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001934 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001935#define k_fifo_get(fifo, timeout) \
1936 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001937
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001938/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001939 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001940 *
1941 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06001942 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001943 *
1944 * @note Can be called by ISRs.
1945 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001946 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001947 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001948 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001949 * @return 0 if data is available.
1950 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001951#define k_fifo_is_empty(fifo) \
1952 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001953
1954/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001955 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001956 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001957 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05301958 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001959 * on each iteration of processing, a head container will be peeked,
1960 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06001961 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001962 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001963 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001964 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001965 * @return Head element, or NULL if the FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001966 */
1967#define k_fifo_peek_head(fifo) \
1968 k_queue_peek_head((struct k_queue *) fifo)
1969
1970/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001971 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001972 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001973 * Return element from the tail of FIFO queue (without removing it). A usecase
1974 * for this is if elements of the FIFO queue are themselves containers. Then
1975 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001976 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001977 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001978 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001979 * @return Tail element, or NULL if a FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001980 */
1981#define k_fifo_peek_tail(fifo) \
1982 k_queue_peek_tail((struct k_queue *) fifo)
1983
1984/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001985 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001986 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001987 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001988 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001989 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001990 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001991 * @param name Name of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001992 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001993#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001994 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001995 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001996 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001997
Anas Nashif166f5192018-02-25 08:02:36 -06001998/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001999
2000/**
2001 * @cond INTERNAL_HIDDEN
2002 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002003
2004struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002005 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002006};
2007
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002008#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002009 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002010 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002011 }
2012
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002013#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2014
Allan Stephensc98da842016-11-11 15:45:03 -05002015/**
2016 * INTERNAL_HIDDEN @endcond
2017 */
2018
2019/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002020 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002021 * @ingroup kernel_apis
2022 * @{
2023 */
2024
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002025/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002026 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002027 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002028 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002029 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002030 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002031 *
2032 * @return N/A
2033 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002034#define k_lifo_init(lifo) \
2035 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002036
2037/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002038 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002039 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002040 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002041 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2042 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002043 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002044 * @note Can be called by ISRs.
2045 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002046 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002047 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002048 *
2049 * @return N/A
2050 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002051#define k_lifo_put(lifo, data) \
2052 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002053
2054/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002055 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002056 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002057 * This routine removes a data item from @a lifo in a "last in, first out"
2058 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002059 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002060 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2061 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002062 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002063 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002064 * or one of the special values K_NO_WAIT and K_FOREVER.
2065 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002066 * @return Address of the data item if successful; NULL if returned
2067 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002068 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002069#define k_lifo_get(lifo, timeout) \
2070 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002071
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002072/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002073 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002074 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002075 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002076 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002077 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002078 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002079 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002080 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002081#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002082 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002083 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002084 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002085
Anas Nashif166f5192018-02-25 08:02:36 -06002086/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002087
2088/**
2089 * @cond INTERNAL_HIDDEN
2090 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002091
2092struct k_stack {
2093 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002094 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002095
Anas Nashif2f203c22016-12-18 06:57:45 -05002096 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002097};
2098
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002099#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002100 { \
2101 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2102 .base = stack_buffer, \
2103 .next = stack_buffer, \
2104 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002105 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002106 }
2107
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002108#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2109
Allan Stephensc98da842016-11-11 15:45:03 -05002110/**
2111 * INTERNAL_HIDDEN @endcond
2112 */
2113
2114/**
2115 * @defgroup stack_apis Stack APIs
2116 * @ingroup kernel_apis
2117 * @{
2118 */
2119
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002120/**
2121 * @brief Initialize a stack.
2122 *
2123 * This routine initializes a stack object, prior to its first use.
2124 *
2125 * @param stack Address of the stack.
2126 * @param buffer Address of array used to hold stacked values.
2127 * @param num_entries Maximum number of values that can be stacked.
2128 *
2129 * @return N/A
2130 */
Andrew Boiee8734462017-09-29 16:42:07 -07002131__syscall void k_stack_init(struct k_stack *stack,
Andrew Boie8e3e6d02017-10-12 14:01:25 -07002132 u32_t *buffer, unsigned int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002133
2134/**
2135 * @brief Push an element onto a stack.
2136 *
2137 * This routine adds a 32-bit value @a data to @a stack.
2138 *
2139 * @note Can be called by ISRs.
2140 *
2141 * @param stack Address of the stack.
2142 * @param data Value to push onto the stack.
2143 *
2144 * @return N/A
2145 */
Andrew Boiee8734462017-09-29 16:42:07 -07002146__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002147
2148/**
2149 * @brief Pop an element from a stack.
2150 *
2151 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2152 * manner and stores the value in @a data.
2153 *
2154 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2155 *
2156 * @param stack Address of the stack.
2157 * @param data Address of area to hold the value popped from the stack.
2158 * @param timeout Waiting period to obtain a value (in milliseconds),
2159 * or one of the special values K_NO_WAIT and K_FOREVER.
2160 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002161 * @retval 0 Element popped from stack.
2162 * @retval -EBUSY Returned without waiting.
2163 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002164 */
Andrew Boiee8734462017-09-29 16:42:07 -07002165__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002166
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002167/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002168 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002169 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002170 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002171 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002172 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002173 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002174 * @param name Name of the stack.
2175 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002176 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002177#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002178 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002179 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002180 struct k_stack name \
2181 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002182 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002183 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002184
Anas Nashif166f5192018-02-25 08:02:36 -06002185/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002186
Allan Stephens6bba9b02016-11-16 14:56:54 -05002187struct k_work;
2188
Allan Stephensc98da842016-11-11 15:45:03 -05002189/**
2190 * @defgroup workqueue_apis Workqueue Thread APIs
2191 * @ingroup kernel_apis
2192 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002193 */
2194
Allan Stephens6bba9b02016-11-16 14:56:54 -05002195/**
2196 * @typedef k_work_handler_t
2197 * @brief Work item handler function type.
2198 *
2199 * A work item's handler function is executed by a workqueue's thread
2200 * when the work item is processed by the workqueue.
2201 *
2202 * @param work Address of the work item.
2203 *
2204 * @return N/A
2205 */
2206typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002207
2208/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002209 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002210 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002211
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002212struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002213 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002214 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002215};
2216
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002217enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002218 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002219};
2220
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002221struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002222 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002223 k_work_handler_t handler;
2224 atomic_t flags[1];
2225};
2226
Allan Stephens6bba9b02016-11-16 14:56:54 -05002227struct k_delayed_work {
2228 struct k_work work;
2229 struct _timeout timeout;
2230 struct k_work_q *work_q;
2231};
2232
2233extern struct k_work_q k_sys_work_q;
2234
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002235/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002236 * INTERNAL_HIDDEN @endcond
2237 */
2238
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002239#define _K_WORK_INITIALIZER(work_handler) \
2240 { \
2241 ._reserved = NULL, \
2242 .handler = work_handler, \
2243 .flags = { 0 } \
2244 }
2245
2246#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2247
Allan Stephens6bba9b02016-11-16 14:56:54 -05002248/**
2249 * @brief Initialize a statically-defined work item.
2250 *
2251 * This macro can be used to initialize a statically-defined workqueue work
2252 * item, prior to its first use. For example,
2253 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002254 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002255 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002256 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002257 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002258 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002259#define K_WORK_DEFINE(work, work_handler) \
2260 struct k_work work \
2261 __in_section(_k_work, static, work) = \
2262 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002263
2264/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002265 * @brief Initialize a work item.
2266 *
2267 * This routine initializes a workqueue work item, prior to its first use.
2268 *
2269 * @param work Address of work item.
2270 * @param handler Function to invoke each time work item is processed.
2271 *
2272 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002273 */
2274static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2275{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002276 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002277 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002278 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002279}
2280
2281/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002282 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002283 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002284 * This routine submits work item @a work to be processed by workqueue
2285 * @a work_q. If the work item is already pending in the workqueue's queue
2286 * as a result of an earlier submission, this routine has no effect on the
2287 * work item. If the work item has already been processed, or is currently
2288 * being processed, its work is considered complete and the work item can be
2289 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002290 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002291 * @warning
2292 * A submitted work item must not be modified until it has been processed
2293 * by the workqueue.
2294 *
2295 * @note Can be called by ISRs.
2296 *
2297 * @param work_q Address of workqueue.
2298 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002299 *
2300 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002301 */
2302static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2303 struct k_work *work)
2304{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002305 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002306 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002307 }
2308}
2309
2310/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002311 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002312 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002313 * This routine indicates if work item @a work is pending in a workqueue's
2314 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002315 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002316 * @note Can be called by ISRs.
2317 *
2318 * @param work Address of work item.
2319 *
2320 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002321 */
2322static inline int k_work_pending(struct k_work *work)
2323{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002324 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002325}
2326
2327/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002328 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002329 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002330 * This routine starts workqueue @a work_q. The workqueue spawns its work
2331 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002332 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002333 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002334 * @param stack Pointer to work queue thread's stack space, as defined by
2335 * K_THREAD_STACK_DEFINE()
2336 * @param stack_size Size of the work queue thread's stack (in bytes), which
2337 * should either be the same constant passed to
2338 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002339 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002340 *
2341 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002342 */
Andrew Boie507852a2017-07-25 18:47:07 -07002343extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002344 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002345 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002346
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002347/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002348 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002349 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002350 * This routine initializes a workqueue delayed work item, prior to
2351 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002352 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002353 * @param work Address of delayed work item.
2354 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002355 *
2356 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002357 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002358extern void k_delayed_work_init(struct k_delayed_work *work,
2359 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002360
2361/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002362 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002363 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002364 * This routine schedules work item @a work to be processed by workqueue
2365 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002366 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002367 * Only when the countdown completes is the work item actually submitted to
2368 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002369 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002370 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002371 * counting down cancels the existing submission and restarts the
2372 * countdown using the new delay. Note that this behavior is
2373 * inherently subject to race conditions with the pre-existing
2374 * timeouts and work queue, so care must be taken to synchronize such
2375 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002376 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002377 * @warning
2378 * A delayed work item must not be modified until it has been processed
2379 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002380 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002381 * @note Can be called by ISRs.
2382 *
2383 * @param work_q Address of workqueue.
2384 * @param work Address of delayed work item.
2385 * @param delay Delay before submitting the work item (in milliseconds).
2386 *
2387 * @retval 0 Work item countdown started.
2388 * @retval -EINPROGRESS Work item is already pending.
2389 * @retval -EINVAL Work item is being processed or has completed its work.
2390 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002391 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002392extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2393 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002394 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002395
2396/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002397 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002398 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002399 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002400 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002401 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002402 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002403 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002404 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002405 * @param work Address of delayed work item.
2406 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002407 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002408 * @retval -EINPROGRESS Work item is already pending.
2409 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002410 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002411extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002412
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002413/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002414 * @brief Submit a work item to the system workqueue.
2415 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002416 * This routine submits work item @a work to be processed by the system
2417 * workqueue. If the work item is already pending in the workqueue's queue
2418 * as a result of an earlier submission, this routine has no effect on the
2419 * work item. If the work item has already been processed, or is currently
2420 * being processed, its work is considered complete and the work item can be
2421 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002422 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002423 * @warning
2424 * Work items submitted to the system workqueue should avoid using handlers
2425 * that block or yield since this may prevent the system workqueue from
2426 * processing other work items in a timely manner.
2427 *
2428 * @note Can be called by ISRs.
2429 *
2430 * @param work Address of work item.
2431 *
2432 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002433 */
2434static inline void k_work_submit(struct k_work *work)
2435{
2436 k_work_submit_to_queue(&k_sys_work_q, work);
2437}
2438
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002439/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002440 * @brief Submit a delayed work item to the system workqueue.
2441 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002442 * This routine schedules work item @a work to be processed by the system
2443 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002444 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002445 * Only when the countdown completes is the work item actually submitted to
2446 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002447 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002448 * Submitting a previously submitted delayed work item that is still
2449 * counting down cancels the existing submission and restarts the countdown
2450 * using the new delay. If the work item is currently pending on the
2451 * workqueue's queue because the countdown has completed it is too late to
2452 * resubmit the item, and resubmission fails without impacting the work item.
2453 * If the work item has already been processed, or is currently being processed,
2454 * its work is considered complete and the work item can be resubmitted.
2455 *
2456 * @warning
2457 * Work items submitted to the system workqueue should avoid using handlers
2458 * that block or yield since this may prevent the system workqueue from
2459 * processing other work items in a timely manner.
2460 *
2461 * @note Can be called by ISRs.
2462 *
2463 * @param work Address of delayed work item.
2464 * @param delay Delay before submitting the work item (in milliseconds).
2465 *
2466 * @retval 0 Work item countdown started.
2467 * @retval -EINPROGRESS Work item is already pending.
2468 * @retval -EINVAL Work item is being processed or has completed its work.
2469 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002470 */
2471static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002472 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002473{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002474 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002475}
2476
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002477/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002478 * @brief Get time remaining before a delayed work gets scheduled.
2479 *
2480 * This routine computes the (approximate) time remaining before a
2481 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002482 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002483 *
2484 * @param work Delayed work item.
2485 *
2486 * @return Remaining time (in milliseconds).
2487 */
Kumar Galacc334c72017-04-21 10:55:34 -05002488static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002489{
2490 return _timeout_remaining_get(&work->timeout);
2491}
2492
Anas Nashif166f5192018-02-25 08:02:36 -06002493/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002494
Allan Stephensc98da842016-11-11 15:45:03 -05002495/**
2496 * @cond INTERNAL_HIDDEN
2497 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002498
2499struct k_mutex {
2500 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002501 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002502 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002503 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002504
Anas Nashif2f203c22016-12-18 06:57:45 -05002505 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002506};
2507
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002508#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002509 { \
2510 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2511 .owner = NULL, \
2512 .lock_count = 0, \
2513 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002514 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002515 }
2516
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002517#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2518
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002519/**
Allan Stephensc98da842016-11-11 15:45:03 -05002520 * INTERNAL_HIDDEN @endcond
2521 */
2522
2523/**
2524 * @defgroup mutex_apis Mutex APIs
2525 * @ingroup kernel_apis
2526 * @{
2527 */
2528
2529/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002530 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002531 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002532 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002533 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002534 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002535 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002536 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002537 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002538#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002539 struct k_mutex name \
2540 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002541 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002542
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002543/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002544 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002545 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002546 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002547 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002548 * Upon completion, the mutex is available and does not have an owner.
2549 *
2550 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002551 *
2552 * @return N/A
2553 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002554__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002555
2556/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002557 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002558 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002559 * This routine locks @a mutex. If the mutex is locked by another thread,
2560 * the calling thread waits until the mutex becomes available or until
2561 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002562 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002563 * A thread is permitted to lock a mutex it has already locked. The operation
2564 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002565 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002566 * @param mutex Address of the mutex.
2567 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002568 * or one of the special values K_NO_WAIT and K_FOREVER.
2569 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002570 * @retval 0 Mutex locked.
2571 * @retval -EBUSY Returned without waiting.
2572 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002573 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002574__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002575
2576/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002577 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002578 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002579 * This routine unlocks @a mutex. The mutex must already be locked by the
2580 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002581 *
2582 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002583 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002584 * thread.
2585 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002586 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002587 *
2588 * @return N/A
2589 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002590__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002591
Allan Stephensc98da842016-11-11 15:45:03 -05002592/**
Anas Nashif166f5192018-02-25 08:02:36 -06002593 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002594 */
2595
2596/**
2597 * @cond INTERNAL_HIDDEN
2598 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002599
2600struct k_sem {
2601 _wait_q_t wait_q;
2602 unsigned int count;
2603 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002604 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002605
Anas Nashif2f203c22016-12-18 06:57:45 -05002606 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002607};
2608
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002609#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002610 { \
2611 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2612 .count = initial_count, \
2613 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002614 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002615 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002616 }
2617
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002618#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2619
Allan Stephensc98da842016-11-11 15:45:03 -05002620/**
2621 * INTERNAL_HIDDEN @endcond
2622 */
2623
2624/**
2625 * @defgroup semaphore_apis Semaphore APIs
2626 * @ingroup kernel_apis
2627 * @{
2628 */
2629
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002630/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002631 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002632 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002633 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002634 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002635 * @param sem Address of the semaphore.
2636 * @param initial_count Initial semaphore count.
2637 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002638 *
2639 * @return N/A
2640 */
Andrew Boie99280232017-09-29 14:17:47 -07002641__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2642 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002643
2644/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002645 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002646 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002647 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002648 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002649 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2650 *
2651 * @param sem Address of the semaphore.
2652 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002653 * or one of the special values K_NO_WAIT and K_FOREVER.
2654 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002655 * @note When porting code from the nanokernel legacy API to the new API, be
2656 * careful with the return value of this function. The return value is the
2657 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2658 * non-zero means failure, while the nano_sem_take family returns 1 for success
2659 * and 0 for failure.
2660 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002661 * @retval 0 Semaphore taken.
2662 * @retval -EBUSY Returned without waiting.
2663 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002664 */
Andrew Boie99280232017-09-29 14:17:47 -07002665__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002666
2667/**
2668 * @brief Give a semaphore.
2669 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002670 * This routine gives @a sem, unless the semaphore is already at its maximum
2671 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002672 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002673 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002674 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002675 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002676 *
2677 * @return N/A
2678 */
Andrew Boie99280232017-09-29 14:17:47 -07002679__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002680
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002681/**
2682 * @brief Reset a semaphore's count to zero.
2683 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002684 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002685 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002686 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002687 *
2688 * @return N/A
2689 */
Andrew Boie990bf162017-10-03 12:36:49 -07002690__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002691
Anas Nashif954d5502018-02-25 08:37:28 -06002692/**
2693 * @internal
2694 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002695static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002696{
2697 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002698}
2699
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002700/**
2701 * @brief Get a semaphore's count.
2702 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002703 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002704 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002705 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002706 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002707 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002708 */
Andrew Boie990bf162017-10-03 12:36:49 -07002709__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002710
Anas Nashif954d5502018-02-25 08:37:28 -06002711/**
2712 * @internal
2713 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002714static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002715{
2716 return sem->count;
2717}
2718
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002719/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002720 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002722 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002723 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002724 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002725 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002726 * @param name Name of the semaphore.
2727 * @param initial_count Initial semaphore count.
2728 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002729 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002730#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002731 struct k_sem name \
2732 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07002733 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05302734 BUILD_ASSERT(((count_limit) != 0) && \
2735 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002736
Anas Nashif166f5192018-02-25 08:02:36 -06002737/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002738
2739/**
2740 * @defgroup alert_apis Alert APIs
2741 * @ingroup kernel_apis
2742 * @{
2743 */
2744
Allan Stephens5eceb852016-11-16 10:16:30 -05002745/**
2746 * @typedef k_alert_handler_t
2747 * @brief Alert handler function type.
2748 *
2749 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002750 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002751 * and is only invoked if the alert has been initialized with one.
2752 *
2753 * @param alert Address of the alert.
2754 *
2755 * @return 0 if alert has been consumed; non-zero if alert should pend.
2756 */
2757typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002758
Anas Nashif166f5192018-02-25 08:02:36 -06002759/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002760
2761/**
2762 * @cond INTERNAL_HIDDEN
2763 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002764
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002765#define K_ALERT_DEFAULT NULL
2766#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002767
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002768struct k_alert {
2769 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002770 atomic_t send_count;
2771 struct k_work work_item;
2772 struct k_sem sem;
2773
Anas Nashif2f203c22016-12-18 06:57:45 -05002774 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002775};
2776
Anas Nashif954d5502018-02-25 08:37:28 -06002777/**
2778 * @internal
2779 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002780extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002781
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002782#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002783 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002784 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002785 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002786 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2787 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002788 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002789 }
2790
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002791#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2792
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002793/**
Allan Stephensc98da842016-11-11 15:45:03 -05002794 * INTERNAL_HIDDEN @endcond
2795 */
2796
2797/**
2798 * @addtogroup alert_apis
2799 * @{
2800 */
2801
2802/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002803 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002804 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002805 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002806 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002807 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002808 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002809 * @param name Name of the alert.
2810 * @param alert_handler Action to take when alert is sent. Specify either
2811 * the address of a function to be invoked by the system workqueue
2812 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2813 * K_ALERT_DEFAULT (which causes the alert to pend).
2814 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002815 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002816#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002817 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002818 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002819 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002820 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002821
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002822/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002823 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002824 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002825 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002826 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002827 * @param alert Address of the alert.
2828 * @param handler Action to take when alert is sent. Specify either the address
2829 * of a function to be invoked by the system workqueue thread,
2830 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2831 * K_ALERT_DEFAULT (which causes the alert to pend).
2832 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002833 *
2834 * @return N/A
2835 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002836extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2837 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002838
2839/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002840 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002841 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002842 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002843 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002844 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2845 *
2846 * @param alert Address of the alert.
2847 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002848 * or one of the special values K_NO_WAIT and K_FOREVER.
2849 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002850 * @retval 0 Alert received.
2851 * @retval -EBUSY Returned without waiting.
2852 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002853 */
Andrew Boie310e9872017-09-29 04:41:15 -07002854__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002855
2856/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002857 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002858 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002859 * This routine signals @a alert. The action specified for @a alert will
2860 * be taken, which may trigger the execution of an alert handler function
2861 * and/or cause the alert to pend (assuming the alert has not reached its
2862 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002863 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002864 * @note Can be called by ISRs.
2865 *
2866 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002867 *
2868 * @return N/A
2869 */
Andrew Boie310e9872017-09-29 04:41:15 -07002870__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002871
2872/**
Anas Nashif166f5192018-02-25 08:02:36 -06002873 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002874 */
2875
Allan Stephensc98da842016-11-11 15:45:03 -05002876/**
2877 * @cond INTERNAL_HIDDEN
2878 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002879
2880struct k_msgq {
2881 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002882 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002883 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002884 char *buffer_start;
2885 char *buffer_end;
2886 char *read_ptr;
2887 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002888 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002889
Anas Nashif2f203c22016-12-18 06:57:45 -05002890 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002891};
2892
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002893#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002894 { \
2895 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002896 .max_msgs = q_max_msgs, \
2897 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002898 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002899 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002900 .read_ptr = q_buffer, \
2901 .write_ptr = q_buffer, \
2902 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002903 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002904 }
2905
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002906#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2907
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05302908struct k_msgq_attrs {
2909 size_t msg_size;
2910 u32_t max_msgs;
2911 u32_t used_msgs;
2912};
2913
Peter Mitsis1da807e2016-10-06 11:36:59 -04002914/**
Allan Stephensc98da842016-11-11 15:45:03 -05002915 * INTERNAL_HIDDEN @endcond
2916 */
2917
2918/**
2919 * @defgroup msgq_apis Message Queue APIs
2920 * @ingroup kernel_apis
2921 * @{
2922 */
2923
2924/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002925 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002926 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002927 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2928 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002929 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2930 * message is similarly aligned to this boundary, @a q_msg_size must also be
2931 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002932 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002933 * The message queue can be accessed outside the module where it is defined
2934 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002935 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002936 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002937 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002938 * @param q_name Name of the message queue.
2939 * @param q_msg_size Message size (in bytes).
2940 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002941 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002942 */
2943#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2944 static char __noinit __aligned(q_align) \
2945 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002946 struct k_msgq q_name \
2947 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002948 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002949 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002950
Peter Mitsisd7a37502016-10-13 11:37:40 -04002951/**
2952 * @brief Initialize a message queue.
2953 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002954 * This routine initializes a message queue object, prior to its first use.
2955 *
Allan Stephensda827222016-11-09 14:23:58 -06002956 * The message queue's ring buffer must contain space for @a max_msgs messages,
2957 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2958 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2959 * that each message is similarly aligned to this boundary, @a q_msg_size
2960 * must also be a multiple of N.
2961 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002962 * @param q Address of the message queue.
2963 * @param buffer Pointer to ring buffer that holds queued messages.
2964 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002965 * @param max_msgs Maximum number of messages that can be queued.
2966 *
2967 * @return N/A
2968 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002969__syscall void k_msgq_init(struct k_msgq *q, char *buffer,
2970 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002971
2972/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002973 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002975 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002976 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002977 * @note Can be called by ISRs.
2978 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002979 * @param q Address of the message queue.
2980 * @param data Pointer to the message.
2981 * @param timeout Waiting period to add the message (in milliseconds),
2982 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002983 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002984 * @retval 0 Message sent.
2985 * @retval -ENOMSG Returned without waiting or queue purged.
2986 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002987 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002988__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002989
2990/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002991 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002993 * This routine receives a message from message queue @a q in a "first in,
2994 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002995 *
Allan Stephensc98da842016-11-11 15:45:03 -05002996 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002997 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002998 * @param q Address of the message queue.
2999 * @param data Address of area to hold the received message.
3000 * @param timeout Waiting period to receive the message (in milliseconds),
3001 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003002 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003003 * @retval 0 Message received.
3004 * @retval -ENOMSG Returned without waiting.
3005 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003006 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003007__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003008
3009/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003010 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003011 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003012 * This routine discards all unreceived messages in a message queue's ring
3013 * buffer. Any threads that are blocked waiting to send a message to the
3014 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003015 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003016 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003017 *
3018 * @return N/A
3019 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003020__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003021
Peter Mitsis67be2492016-10-07 11:44:34 -04003022/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003023 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003024 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003025 * This routine returns the number of unused entries in a message queue's
3026 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003027 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003028 * @param q Address of the message queue.
3029 *
3030 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04003031 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003032__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3033
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303034/**
3035 * @brief Get basic attributes of a message queue.
3036 *
3037 * This routine fetches basic attributes of message queue into attr argument.
3038 *
3039 * @param q Address of the message queue.
3040 * @param attrs pointer to message queue attribute structure.
3041 *
3042 * @return N/A
3043 */
3044__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3045
3046
Andrew Boie82edb6e2017-10-02 10:53:06 -07003047static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003048{
3049 return q->max_msgs - q->used_msgs;
3050}
3051
Peter Mitsisd7a37502016-10-13 11:37:40 -04003052/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003053 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003054 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003055 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003056 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003057 * @param q Address of the message queue.
3058 *
3059 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003060 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003061__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3062
3063static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003064{
3065 return q->used_msgs;
3066}
3067
Anas Nashif166f5192018-02-25 08:02:36 -06003068/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003069
3070/**
3071 * @defgroup mem_pool_apis Memory Pool APIs
3072 * @ingroup kernel_apis
3073 * @{
3074 */
3075
Andy Ross73cb9582017-05-09 10:42:39 -07003076/* Note on sizing: the use of a 20 bit field for block means that,
3077 * assuming a reasonable minimum block size of 16 bytes, we're limited
3078 * to 16M of memory managed by a single pool. Long term it would be
3079 * good to move to a variable bit size based on configuration.
3080 */
3081struct k_mem_block_id {
3082 u32_t pool : 8;
3083 u32_t level : 4;
3084 u32_t block : 20;
3085};
3086
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003087struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003088 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003089 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003090};
3091
Anas Nashif166f5192018-02-25 08:02:36 -06003092/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003093
3094/**
3095 * @defgroup mailbox_apis Mailbox APIs
3096 * @ingroup kernel_apis
3097 * @{
3098 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003099
3100struct k_mbox_msg {
3101 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003102 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003103 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003104 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003105 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003106 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003107 /** sender's message data buffer */
3108 void *tx_data;
3109 /** internal use only - needed for legacy API support */
3110 void *_rx_data;
3111 /** message data block descriptor */
3112 struct k_mem_block tx_block;
3113 /** source thread id */
3114 k_tid_t rx_source_thread;
3115 /** target thread id */
3116 k_tid_t tx_target_thread;
3117 /** internal use only - thread waiting on send (may be a dummy) */
3118 k_tid_t _syncing_thread;
3119#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3120 /** internal use only - semaphore used during asynchronous send */
3121 struct k_sem *_async_sem;
3122#endif
3123};
3124
Allan Stephensc98da842016-11-11 15:45:03 -05003125/**
3126 * @cond INTERNAL_HIDDEN
3127 */
3128
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003129struct k_mbox {
3130 _wait_q_t tx_msg_queue;
3131 _wait_q_t rx_msg_queue;
3132
Anas Nashif2f203c22016-12-18 06:57:45 -05003133 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003134};
3135
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003136#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003137 { \
3138 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3139 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003140 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003141 }
3142
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003143#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3144
Peter Mitsis12092702016-10-14 12:57:23 -04003145/**
Allan Stephensc98da842016-11-11 15:45:03 -05003146 * INTERNAL_HIDDEN @endcond
3147 */
3148
3149/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003150 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003151 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003152 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003153 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003154 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003155 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003156 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003157 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003158#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003159 struct k_mbox name \
3160 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003161 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003162
Peter Mitsis12092702016-10-14 12:57:23 -04003163/**
3164 * @brief Initialize a mailbox.
3165 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003166 * This routine initializes a mailbox object, prior to its first use.
3167 *
3168 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003169 *
3170 * @return N/A
3171 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003172extern void k_mbox_init(struct k_mbox *mbox);
3173
Peter Mitsis12092702016-10-14 12:57:23 -04003174/**
3175 * @brief Send a mailbox message in a synchronous manner.
3176 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003177 * This routine sends a message to @a mbox and waits for a receiver to both
3178 * receive and process it. The message data may be in a buffer, in a memory
3179 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003180 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003181 * @param mbox Address of the mailbox.
3182 * @param tx_msg Address of the transmit message descriptor.
3183 * @param timeout Waiting period for the message to be received (in
3184 * milliseconds), or one of the special values K_NO_WAIT
3185 * and K_FOREVER. Once the message has been received,
3186 * this routine waits as long as necessary for the message
3187 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003188 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003189 * @retval 0 Message sent.
3190 * @retval -ENOMSG Returned without waiting.
3191 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003192 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003193extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003194 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003195
Peter Mitsis12092702016-10-14 12:57:23 -04003196/**
3197 * @brief Send a mailbox message in an asynchronous manner.
3198 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003199 * This routine sends a message to @a mbox without waiting for a receiver
3200 * to process it. The message data may be in a buffer, in a memory pool block,
3201 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3202 * will be given when the message has been both received and completely
3203 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003204 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003205 * @param mbox Address of the mailbox.
3206 * @param tx_msg Address of the transmit message descriptor.
3207 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003208 *
3209 * @return N/A
3210 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003211extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003212 struct k_sem *sem);
3213
Peter Mitsis12092702016-10-14 12:57:23 -04003214/**
3215 * @brief Receive a mailbox message.
3216 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003217 * This routine receives a message from @a mbox, then optionally retrieves
3218 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003219 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003220 * @param mbox Address of the mailbox.
3221 * @param rx_msg Address of the receive message descriptor.
3222 * @param buffer Address of the buffer to receive data, or NULL to defer data
3223 * retrieval and message disposal until later.
3224 * @param timeout Waiting period for a message to be received (in
3225 * milliseconds), or one of the special values K_NO_WAIT
3226 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003227 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003228 * @retval 0 Message received.
3229 * @retval -ENOMSG Returned without waiting.
3230 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003231 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003232extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003233 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003234
3235/**
3236 * @brief Retrieve mailbox message data into a buffer.
3237 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003238 * This routine completes the processing of a received message by retrieving
3239 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003240 *
3241 * Alternatively, this routine can be used to dispose of a received message
3242 * without retrieving its data.
3243 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003244 * @param rx_msg Address of the receive message descriptor.
3245 * @param buffer Address of the buffer to receive data, or NULL to discard
3246 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003247 *
3248 * @return N/A
3249 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003250extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003251
3252/**
3253 * @brief Retrieve mailbox message data into a memory pool block.
3254 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003255 * This routine completes the processing of a received message by retrieving
3256 * its data into a memory pool block, then disposing of the message.
3257 * The memory pool block that results from successful retrieval must be
3258 * returned to the pool once the data has been processed, even in cases
3259 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003260 *
3261 * Alternatively, this routine can be used to dispose of a received message
3262 * without retrieving its data. In this case there is no need to return a
3263 * memory pool block to the pool.
3264 *
3265 * This routine allocates a new memory pool block for the data only if the
3266 * data is not already in one. If a new block cannot be allocated, the routine
3267 * returns a failure code and the received message is left unchanged. This
3268 * permits the caller to reattempt data retrieval at a later time or to dispose
3269 * of the received message without retrieving its data.
3270 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003271 * @param rx_msg Address of a receive message descriptor.
3272 * @param pool Address of memory pool, or NULL to discard data.
3273 * @param block Address of the area to hold memory pool block info.
3274 * @param timeout Waiting period to wait for a memory pool block (in
3275 * milliseconds), or one of the special values K_NO_WAIT
3276 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003277 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003278 * @retval 0 Data retrieved.
3279 * @retval -ENOMEM Returned without waiting.
3280 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003281 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003282extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003283 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003284 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003285
Anas Nashif166f5192018-02-25 08:02:36 -06003286/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003287
3288/**
3289 * @cond INTERNAL_HIDDEN
3290 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003291
3292struct k_pipe {
3293 unsigned char *buffer; /* Pipe buffer: may be NULL */
3294 size_t size; /* Buffer size */
3295 size_t bytes_used; /* # bytes used in buffer */
3296 size_t read_index; /* Where in buffer to read from */
3297 size_t write_index; /* Where in buffer to write */
3298
3299 struct {
3300 _wait_q_t readers; /* Reader wait queue */
3301 _wait_q_t writers; /* Writer wait queue */
3302 } wait_q;
3303
Anas Nashif2f203c22016-12-18 06:57:45 -05003304 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003305};
3306
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003307#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003308 { \
3309 .buffer = pipe_buffer, \
3310 .size = pipe_buffer_size, \
3311 .bytes_used = 0, \
3312 .read_index = 0, \
3313 .write_index = 0, \
3314 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3315 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003316 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003317 }
3318
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003319#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3320
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003321/**
Allan Stephensc98da842016-11-11 15:45:03 -05003322 * INTERNAL_HIDDEN @endcond
3323 */
3324
3325/**
3326 * @defgroup pipe_apis Pipe APIs
3327 * @ingroup kernel_apis
3328 * @{
3329 */
3330
3331/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003332 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003333 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003334 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003335 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003336 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003337 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003338 * @param name Name of the pipe.
3339 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3340 * or zero if no ring buffer is used.
3341 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003342 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003343#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3344 static unsigned char __noinit __aligned(pipe_align) \
3345 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003346 struct k_pipe name \
3347 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003348 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003349
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003350/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003351 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003353 * This routine initializes a pipe object, prior to its first use.
3354 *
3355 * @param pipe Address of the pipe.
3356 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3357 * is used.
3358 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3359 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003360 *
3361 * @return N/A
3362 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003363__syscall void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3364 size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003365
3366/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003367 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003368 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003369 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003370 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003371 * @param pipe Address of the pipe.
3372 * @param data Address of data to write.
3373 * @param bytes_to_write Size of data (in bytes).
3374 * @param bytes_written Address of area to hold the number of bytes written.
3375 * @param min_xfer Minimum number of bytes to write.
3376 * @param timeout Waiting period to wait for the data to be written (in
3377 * milliseconds), or one of the special values K_NO_WAIT
3378 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003379 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003380 * @retval 0 At least @a min_xfer bytes of data were written.
3381 * @retval -EIO Returned without waiting; zero data bytes were written.
3382 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003383 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003384 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003385__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3386 size_t bytes_to_write, size_t *bytes_written,
3387 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003388
3389/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003390 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003391 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003392 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003393 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003394 * @param pipe Address of the pipe.
3395 * @param data Address to place the data read from pipe.
3396 * @param bytes_to_read Maximum number of data bytes to read.
3397 * @param bytes_read Address of area to hold the number of bytes read.
3398 * @param min_xfer Minimum number of data bytes to read.
3399 * @param timeout Waiting period to wait for the data to be read (in
3400 * milliseconds), or one of the special values K_NO_WAIT
3401 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003402 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003403 * @retval 0 At least @a min_xfer bytes of data were read.
3404 * @retval -EIO Returned without waiting; zero data bytes were read.
3405 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003406 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003407 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003408__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3409 size_t bytes_to_read, size_t *bytes_read,
3410 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003411
3412/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003413 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003414 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003415 * This routine writes the data contained in a memory block to @a pipe.
3416 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003417 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003418 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003419 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003420 * @param block Memory block containing data to send
3421 * @param size Number of data bytes in memory block to send
3422 * @param sem Semaphore to signal upon completion (else NULL)
3423 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003424 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003425 */
3426extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3427 size_t size, struct k_sem *sem);
3428
Anas Nashif166f5192018-02-25 08:02:36 -06003429/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003430
Allan Stephensc98da842016-11-11 15:45:03 -05003431/**
3432 * @cond INTERNAL_HIDDEN
3433 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003434
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003435struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003436 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003437 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003438 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003439 char *buffer;
3440 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003441 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003442
Anas Nashif2f203c22016-12-18 06:57:45 -05003443 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003444};
3445
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003446#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003447 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003448 { \
3449 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003450 .num_blocks = slab_num_blocks, \
3451 .block_size = slab_block_size, \
3452 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003453 .free_list = NULL, \
3454 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003455 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003456 }
3457
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003458#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3459
3460
Peter Mitsis578f9112016-10-07 13:50:31 -04003461/**
Allan Stephensc98da842016-11-11 15:45:03 -05003462 * INTERNAL_HIDDEN @endcond
3463 */
3464
3465/**
3466 * @defgroup mem_slab_apis Memory Slab APIs
3467 * @ingroup kernel_apis
3468 * @{
3469 */
3470
3471/**
Allan Stephensda827222016-11-09 14:23:58 -06003472 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003473 *
Allan Stephensda827222016-11-09 14:23:58 -06003474 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003475 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003476 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3477 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003478 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003479 *
Allan Stephensda827222016-11-09 14:23:58 -06003480 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003481 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003482 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003483 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003484 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003485 * @param name Name of the memory slab.
3486 * @param slab_block_size Size of each memory block (in bytes).
3487 * @param slab_num_blocks Number memory blocks.
3488 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003489 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003490#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3491 char __noinit __aligned(slab_align) \
3492 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3493 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003494 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003495 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003496 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003497
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003498/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003499 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003500 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003501 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003502 *
Allan Stephensda827222016-11-09 14:23:58 -06003503 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3504 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3505 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3506 * To ensure that each memory block is similarly aligned to this boundary,
3507 * @a slab_block_size must also be a multiple of N.
3508 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003509 * @param slab Address of the memory slab.
3510 * @param buffer Pointer to buffer used for the memory blocks.
3511 * @param block_size Size of each memory block (in bytes).
3512 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003513 *
3514 * @return N/A
3515 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003516extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003517 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003518
3519/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003520 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003521 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003522 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003523 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003524 * @param slab Address of the memory slab.
3525 * @param mem Pointer to block address area.
3526 * @param timeout Maximum time to wait for operation to complete
3527 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3528 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003529 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003530 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003531 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003532 * @retval -ENOMEM Returned without waiting.
3533 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003534 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003535extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003536 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003537
3538/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003539 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003540 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003541 * This routine releases a previously allocated memory block back to its
3542 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003543 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003544 * @param slab Address of the memory slab.
3545 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003546 *
3547 * @return N/A
3548 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003549extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003550
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003551/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003552 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003553 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003554 * This routine gets the number of memory blocks that are currently
3555 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003556 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003557 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003558 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003559 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003560 */
Kumar Galacc334c72017-04-21 10:55:34 -05003561static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003562{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003563 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003564}
3565
Peter Mitsisc001aa82016-10-13 13:53:37 -04003566/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003567 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003568 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003569 * This routine gets the number of memory blocks that are currently
3570 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003571 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003572 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003573 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003574 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003575 */
Kumar Galacc334c72017-04-21 10:55:34 -05003576static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003577{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003578 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003579}
3580
Anas Nashif166f5192018-02-25 08:02:36 -06003581/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003582
3583/**
3584 * @cond INTERNAL_HIDDEN
3585 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003586
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003587struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003588 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003589 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003590};
3591
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003592/**
Allan Stephensc98da842016-11-11 15:45:03 -05003593 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003594 */
3595
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003596/**
Allan Stephensc98da842016-11-11 15:45:03 -05003597 * @addtogroup mem_pool_apis
3598 * @{
3599 */
3600
3601/**
3602 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003603 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003604 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3605 * long. The memory pool allows blocks to be repeatedly partitioned into
3606 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003607 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003608 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003609 * If the pool is to be accessed outside the module where it is defined, it
3610 * can be declared via
3611 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003612 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003613 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003614 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003615 * @param minsz Size of the smallest blocks in the pool (in bytes).
3616 * @param maxsz Size of the largest blocks in the pool (in bytes).
3617 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003618 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003619 */
Andy Ross73cb9582017-05-09 10:42:39 -07003620#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3621 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3622 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08003623 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07003624 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08003625 .base = { \
3626 .buf = _mpool_buf_##name, \
3627 .max_sz = maxsz, \
3628 .n_max = nmax, \
3629 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3630 .levels = _mpool_lvls_##name, \
3631 .flags = SYS_MEM_POOL_KERNEL \
3632 } \
Andy Ross73cb9582017-05-09 10:42:39 -07003633 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003634
Peter Mitsis937042c2016-10-13 13:18:26 -04003635/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003636 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003637 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003638 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003639 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003640 * @param pool Address of the memory pool.
3641 * @param block Pointer to block descriptor for the allocated memory.
3642 * @param size Amount of memory to allocate (in bytes).
3643 * @param timeout Maximum time to wait for operation to complete
3644 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3645 * or K_FOREVER to wait as long as necessary.
3646 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003647 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003648 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003649 * @retval -ENOMEM Returned without waiting.
3650 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003651 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003652extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003653 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003654
3655/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07003656 * @brief Allocate memory from a memory pool with malloc() semantics
3657 *
3658 * Such memory must be released using k_free().
3659 *
3660 * @param pool Address of the memory pool.
3661 * @param size Amount of memory to allocate (in bytes).
3662 * @return Address of the allocated memory if successful, otherwise NULL
3663 */
3664extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
3665
3666/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003667 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003668 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003669 * This routine releases a previously allocated memory block back to its
3670 * memory pool.
3671 *
3672 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003673 *
3674 * @return N/A
3675 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003676extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003677
3678/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003679 * @brief Free memory allocated from a memory pool.
3680 *
3681 * This routine releases a previously allocated memory block back to its
3682 * memory pool
3683 *
3684 * @param id Memory block identifier.
3685 *
3686 * @return N/A
3687 */
3688extern void k_mem_pool_free_id(struct k_mem_block_id *id);
3689
3690/**
Anas Nashif166f5192018-02-25 08:02:36 -06003691 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003692 */
3693
3694/**
3695 * @defgroup heap_apis Heap Memory Pool APIs
3696 * @ingroup kernel_apis
3697 * @{
3698 */
3699
3700/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003701 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003702 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003703 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003704 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003705 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003706 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003707 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003708 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003709 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003710extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003711
3712/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003713 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003714 *
3715 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07003716 * returned must have been allocated from the heap memory pool or
3717 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04003718 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003719 * If @a ptr is NULL, no operation is performed.
3720 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003721 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003722 *
3723 * @return N/A
3724 */
3725extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003726
Allan Stephensc98da842016-11-11 15:45:03 -05003727/**
Andrew Boie7f95e832017-11-08 14:40:01 -08003728 * @brief Allocate memory from heap, array style
3729 *
3730 * This routine provides traditional calloc() semantics. Memory is
3731 * allocated from the heap memory pool and zeroed.
3732 *
3733 * @param nmemb Number of elements in the requested array
3734 * @param size Size of each array element (in bytes).
3735 *
3736 * @return Address of the allocated memory if successful; otherwise NULL.
3737 */
3738extern void *k_calloc(size_t nmemb, size_t size);
3739
Anas Nashif166f5192018-02-25 08:02:36 -06003740/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003741
Benjamin Walshacc68c12017-01-29 18:57:45 -05003742/* polling API - PRIVATE */
3743
Benjamin Walshb0179862017-02-02 16:39:57 -05003744#ifdef CONFIG_POLL
3745#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3746#else
3747#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3748#endif
3749
Benjamin Walshacc68c12017-01-29 18:57:45 -05003750/* private - implementation data created as needed, per-type */
3751struct _poller {
3752 struct k_thread *thread;
3753};
3754
3755/* private - types bit positions */
3756enum _poll_types_bits {
3757 /* can be used to ignore an event */
3758 _POLL_TYPE_IGNORE,
3759
3760 /* to be signaled by k_poll_signal() */
3761 _POLL_TYPE_SIGNAL,
3762
3763 /* semaphore availability */
3764 _POLL_TYPE_SEM_AVAILABLE,
3765
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003766 /* queue/fifo/lifo data availability */
3767 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003768
3769 _POLL_NUM_TYPES
3770};
3771
3772#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3773
3774/* private - states bit positions */
3775enum _poll_states_bits {
3776 /* default state when creating event */
3777 _POLL_STATE_NOT_READY,
3778
Benjamin Walshacc68c12017-01-29 18:57:45 -05003779 /* signaled by k_poll_signal() */
3780 _POLL_STATE_SIGNALED,
3781
3782 /* semaphore is available */
3783 _POLL_STATE_SEM_AVAILABLE,
3784
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003785 /* data is available to read on queue/fifo/lifo */
3786 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003787
3788 _POLL_NUM_STATES
3789};
3790
3791#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3792
3793#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003794 (32 - (0 \
3795 + 8 /* tag */ \
3796 + _POLL_NUM_TYPES \
3797 + _POLL_NUM_STATES \
3798 + 1 /* modes */ \
3799 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003800
Benjamin Walshacc68c12017-01-29 18:57:45 -05003801/* end of polling API - PRIVATE */
3802
3803
3804/**
3805 * @defgroup poll_apis Async polling APIs
3806 * @ingroup kernel_apis
3807 * @{
3808 */
3809
3810/* Public polling API */
3811
3812/* public - values for k_poll_event.type bitfield */
3813#define K_POLL_TYPE_IGNORE 0
3814#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3815#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003816#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3817#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003818
3819/* public - polling modes */
3820enum k_poll_modes {
3821 /* polling thread does not take ownership of objects when available */
3822 K_POLL_MODE_NOTIFY_ONLY = 0,
3823
3824 K_POLL_NUM_MODES
3825};
3826
3827/* public - values for k_poll_event.state bitfield */
3828#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003829#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3830#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003831#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3832#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003833
3834/* public - poll signal object */
3835struct k_poll_signal {
3836 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003837 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003838
3839 /*
3840 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3841 * user resets it to 0.
3842 */
3843 unsigned int signaled;
3844
3845 /* custom result value passed to k_poll_signal() if needed */
3846 int result;
3847};
3848
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003849#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003850 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003851 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003852 .signaled = 0, \
3853 .result = 0, \
3854 }
3855
3856struct k_poll_event {
3857 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003858 sys_dnode_t _node;
3859
3860 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003861 struct _poller *poller;
3862
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003863 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003864 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003865
Benjamin Walshacc68c12017-01-29 18:57:45 -05003866 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003867 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003868
3869 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003870 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003871
3872 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003873 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003874
3875 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003876 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003877
3878 /* per-type data */
3879 union {
3880 void *obj;
3881 struct k_poll_signal *signal;
3882 struct k_sem *sem;
3883 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003884 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003885 };
3886};
3887
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003888#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003889 { \
3890 .poller = NULL, \
3891 .type = event_type, \
3892 .state = K_POLL_STATE_NOT_READY, \
3893 .mode = event_mode, \
3894 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003895 { .obj = event_obj }, \
3896 }
3897
3898#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3899 event_tag) \
3900 { \
3901 .type = event_type, \
3902 .tag = event_tag, \
3903 .state = K_POLL_STATE_NOT_READY, \
3904 .mode = event_mode, \
3905 .unused = 0, \
3906 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003907 }
3908
3909/**
3910 * @brief Initialize one struct k_poll_event instance
3911 *
3912 * After this routine is called on a poll event, the event it ready to be
3913 * placed in an event array to be passed to k_poll().
3914 *
3915 * @param event The event to initialize.
3916 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3917 * values. Only values that apply to the same object being polled
3918 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3919 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003920 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003921 * @param obj Kernel object or poll signal.
3922 *
3923 * @return N/A
3924 */
3925
Kumar Galacc334c72017-04-21 10:55:34 -05003926extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003927 int mode, void *obj);
3928
3929/**
3930 * @brief Wait for one or many of multiple poll events to occur
3931 *
3932 * This routine allows a thread to wait concurrently for one or many of
3933 * multiple poll events to have occurred. Such events can be a kernel object
3934 * being available, like a semaphore, or a poll signal event.
3935 *
3936 * When an event notifies that a kernel object is available, the kernel object
3937 * is not "given" to the thread calling k_poll(): it merely signals the fact
3938 * that the object was available when the k_poll() call was in effect. Also,
3939 * all threads trying to acquire an object the regular way, i.e. by pending on
3940 * the object, have precedence over the thread polling on the object. This
3941 * means that the polling thread will never get the poll event on an object
3942 * until the object becomes available and its pend queue is empty. For this
3943 * reason, the k_poll() call is more effective when the objects being polled
3944 * only have one thread, the polling thread, trying to acquire them.
3945 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003946 * When k_poll() returns 0, the caller should loop on all the events that were
3947 * passed to k_poll() and check the state field for the values that were
3948 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003949 *
3950 * Before being reused for another call to k_poll(), the user has to reset the
3951 * state field to K_POLL_STATE_NOT_READY.
3952 *
3953 * @param events An array of pointers to events to be polled for.
3954 * @param num_events The number of events in the array.
3955 * @param timeout Waiting period for an event to be ready (in milliseconds),
3956 * or one of the special values K_NO_WAIT and K_FOREVER.
3957 *
3958 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003959 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02003960 * @retval -EINTR Poller thread has been interrupted.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003961 */
3962
3963extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003964 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003965
3966/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003967 * @brief Initialize a poll signal object.
3968 *
3969 * Ready a poll signal object to be signaled via k_poll_signal().
3970 *
3971 * @param signal A poll signal.
3972 *
3973 * @return N/A
3974 */
3975
3976extern void k_poll_signal_init(struct k_poll_signal *signal);
3977
3978/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003979 * @brief Signal a poll signal object.
3980 *
3981 * This routine makes ready a poll signal, which is basically a poll event of
3982 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3983 * made ready to run. A @a result value can be specified.
3984 *
3985 * The poll signal contains a 'signaled' field that, when set by
3986 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3987 * be reset by the user before being passed again to k_poll() or k_poll() will
3988 * consider it being signaled, and will return immediately.
3989 *
3990 * @param signal A poll signal.
3991 * @param result The value to store in the result field of the signal.
3992 *
3993 * @retval 0 The signal was delivered successfully.
3994 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3995 */
3996
3997extern int k_poll_signal(struct k_poll_signal *signal, int result);
3998
Anas Nashif954d5502018-02-25 08:37:28 -06003999/**
4000 * @internal
4001 */
Andy Ross8606fab2018-03-26 10:54:40 -07004002extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004003
Anas Nashif166f5192018-02-25 08:02:36 -06004004/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004005
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004006/**
4007 * @brief Make the CPU idle.
4008 *
4009 * This function makes the CPU idle until an event wakes it up.
4010 *
4011 * In a regular system, the idle thread should be the only thread responsible
4012 * for making the CPU idle and triggering any type of power management.
4013 * However, in some more constrained systems, such as a single-threaded system,
4014 * the only thread would be responsible for this if needed.
4015 *
4016 * @return N/A
4017 */
4018extern void k_cpu_idle(void);
4019
4020/**
4021 * @brief Make the CPU idle in an atomic fashion.
4022 *
4023 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4024 * must be done atomically before making the CPU idle.
4025 *
4026 * @param key Interrupt locking key obtained from irq_lock().
4027 *
4028 * @return N/A
4029 */
4030extern void k_cpu_atomic_idle(unsigned int key);
4031
Anas Nashif954d5502018-02-25 08:37:28 -06004032
4033/**
4034 * @internal
4035 */
Kumar Galacc334c72017-04-21 10:55:34 -05004036extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004037
Andrew Boiecdb94d62017-04-18 15:22:05 -07004038#ifdef _ARCH_EXCEPT
4039/* This archtecture has direct support for triggering a CPU exception */
4040#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4041#else
4042
Andrew Boiecdb94d62017-04-18 15:22:05 -07004043/* NOTE: This is the implementation for arches that do not implement
4044 * _ARCH_EXCEPT() to generate a real CPU exception.
4045 *
4046 * We won't have a real exception frame to determine the PC value when
4047 * the oops occurred, so print file and line number before we jump into
4048 * the fatal error handler.
4049 */
4050#define _k_except_reason(reason) do { \
4051 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4052 _NanoFatalErrorHandler(reason, &_default_esf); \
4053 CODE_UNREACHABLE; \
4054 } while (0)
4055
4056#endif /* _ARCH__EXCEPT */
4057
4058/**
4059 * @brief Fatally terminate a thread
4060 *
4061 * This should be called when a thread has encountered an unrecoverable
4062 * runtime condition and needs to terminate. What this ultimately
4063 * means is determined by the _fatal_error_handler() implementation, which
4064 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4065 *
4066 * If this is called from ISR context, the default system fatal error handler
4067 * will treat it as an unrecoverable system error, just like k_panic().
4068 */
4069#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4070
4071/**
4072 * @brief Fatally terminate the system
4073 *
4074 * This should be called when the Zephyr kernel has encountered an
4075 * unrecoverable runtime condition and needs to terminate. What this ultimately
4076 * means is determined by the _fatal_error_handler() implementation, which
4077 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4078 */
4079#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4080
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004081/*
4082 * private APIs that are utilized by one or more public APIs
4083 */
4084
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004085#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004086/**
4087 * @internal
4088 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004089extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004090#else
Anas Nashif954d5502018-02-25 08:37:28 -06004091/**
4092 * @internal
4093 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004094#define _init_static_threads() do { } while ((0))
4095#endif
4096
Anas Nashif954d5502018-02-25 08:37:28 -06004097/**
4098 * @internal
4099 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004100extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004101/**
4102 * @internal
4103 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004104extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004105
Andrew Boiedc5d9352017-06-02 12:56:47 -07004106/* arch/cpu.h may declare an architecture or platform-specific macro
4107 * for properly declaring stacks, compatible with MMU/MPU constraints if
4108 * enabled
4109 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004110
4111/**
4112 * @brief Obtain an extern reference to a stack
4113 *
4114 * This macro properly brings the symbol of a thread stack declared
4115 * elsewhere into scope.
4116 *
4117 * @param sym Thread stack symbol name
4118 */
4119#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4120
Andrew Boiedc5d9352017-06-02 12:56:47 -07004121#ifdef _ARCH_THREAD_STACK_DEFINE
4122#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4123#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4124 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4125#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4126#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004127static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004128{
4129 return _ARCH_THREAD_STACK_BUFFER(sym);
4130}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004131#else
4132/**
4133 * @brief Declare a toplevel thread stack memory region
4134 *
4135 * This declares a region of memory suitable for use as a thread's stack.
4136 *
4137 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4138 * 'noinit' section so that it isn't zeroed at boot
4139 *
Andrew Boie507852a2017-07-25 18:47:07 -07004140 * The declared symbol will always be a k_thread_stack_t which can be passed to
4141 * k_thread_create, but should otherwise not be manipulated. If the buffer
4142 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004143 *
4144 * It is legal to precede this definition with the 'static' keyword.
4145 *
4146 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4147 * parameter of k_thread_create(), it may not be the same as the
4148 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4149 *
4150 * @param sym Thread stack symbol name
4151 * @param size Size of the stack memory region
4152 */
4153#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004154 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004155
4156/**
4157 * @brief Declare a toplevel array of thread stack memory regions
4158 *
4159 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4160 * definition for additional details and constraints.
4161 *
4162 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4163 * 'noinit' section so that it isn't zeroed at boot
4164 *
4165 * @param sym Thread stack symbol name
4166 * @param nmemb Number of stacks to declare
4167 * @param size Size of the stack memory region
4168 */
4169
4170#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004171 struct _k_thread_stack_element __noinit \
4172 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004173
4174/**
4175 * @brief Declare an embedded stack memory region
4176 *
4177 * Used for stacks embedded within other data structures. Use is highly
4178 * discouraged but in some cases necessary. For memory protection scenarios,
4179 * it is very important that any RAM preceding this member not be writable
4180 * by threads else a stack overflow will lead to silent corruption. In other
4181 * words, the containing data structure should live in RAM owned by the kernel.
4182 *
4183 * @param sym Thread stack symbol name
4184 * @param size Size of the stack memory region
4185 */
4186#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004187 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004188
4189/**
4190 * @brief Return the size in bytes of a stack memory region
4191 *
4192 * Convenience macro for passing the desired stack size to k_thread_create()
4193 * since the underlying implementation may actually create something larger
4194 * (for instance a guard area).
4195 *
4196 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004197 * passed to K_THREAD_STACK_DEFINE.
4198 *
4199 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4200 * it is not guaranteed to return the original value since each array
4201 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004202 *
4203 * @param sym Stack memory symbol
4204 * @return Size of the stack
4205 */
4206#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4207
4208/**
4209 * @brief Get a pointer to the physical stack buffer
4210 *
4211 * Convenience macro to get at the real underlying stack buffer used by
4212 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4213 * This is really only intended for diagnostic tools which want to examine
4214 * stack memory contents.
4215 *
4216 * @param sym Declared stack symbol name
4217 * @return The buffer itself, a char *
4218 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004219static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004220{
4221 return (char *)sym;
4222}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004223
4224#endif /* _ARCH_DECLARE_STACK */
4225
Chunlin Hane9c97022017-07-07 20:29:30 +08004226/**
4227 * @defgroup mem_domain_apis Memory domain APIs
4228 * @ingroup kernel_apis
4229 * @{
4230 */
4231
4232/** @def MEM_PARTITION_ENTRY
4233 * @brief Used to declare a memory partition entry
4234 */
4235#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4236 {\
4237 .start = _start, \
4238 .size = _size, \
4239 .attr = _attr, \
4240 }
4241
4242/** @def K_MEM_PARTITION_DEFINE
4243 * @brief Used to declare a memory partition
4244 */
4245#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4246#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4247 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304248 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004249 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4250#else
4251#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304252 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004253 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4254#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4255
Chunlin Hane9c97022017-07-07 20:29:30 +08004256/* memory partition */
4257struct k_mem_partition {
4258 /* start address of memory partition */
4259 u32_t start;
4260 /* size of memory partition */
4261 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304262#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004263 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304264 k_mem_partition_attr_t attr;
4265#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004266};
4267
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304268/* memory domian
4269 * Note: Always declare this structure with __kernel prefix
4270 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004271struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304272#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004273 /* partitions in the domain */
4274 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304275#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004276 /* domain q */
4277 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004278 /* number of partitions in the domain */
4279 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004280};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304281
Chunlin Hane9c97022017-07-07 20:29:30 +08004282
4283/**
4284 * @brief Initialize a memory domain.
4285 *
4286 * Initialize a memory domain with given name and memory partitions.
4287 *
4288 * @param domain The memory domain to be initialized.
4289 * @param num_parts The number of array items of "parts" parameter.
4290 * @param parts An array of pointers to the memory partitions. Can be NULL
4291 * if num_parts is zero.
4292 */
4293
Leandro Pereira08de6582018-02-28 14:22:57 -08004294extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004295 struct k_mem_partition *parts[]);
4296/**
4297 * @brief Destroy a memory domain.
4298 *
4299 * Destroy a memory domain.
4300 *
4301 * @param domain The memory domain to be destroyed.
4302 */
4303
4304extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4305
4306/**
4307 * @brief Add a memory partition into a memory domain.
4308 *
4309 * Add a memory partition into a memory domain.
4310 *
4311 * @param domain The memory domain to be added a memory partition.
4312 * @param part The memory partition to be added
4313 */
4314
4315extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4316 struct k_mem_partition *part);
4317
4318/**
4319 * @brief Remove a memory partition from a memory domain.
4320 *
4321 * Remove a memory partition from a memory domain.
4322 *
4323 * @param domain The memory domain to be removed a memory partition.
4324 * @param part The memory partition to be removed
4325 */
4326
4327extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4328 struct k_mem_partition *part);
4329
4330/**
4331 * @brief Add a thread into a memory domain.
4332 *
4333 * Add a thread into a memory domain.
4334 *
4335 * @param domain The memory domain that the thread is going to be added into.
4336 * @param thread ID of thread going to be added into the memory domain.
4337 */
4338
4339extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4340 k_tid_t thread);
4341
4342/**
4343 * @brief Remove a thread from its memory domain.
4344 *
4345 * Remove a thread from its memory domain.
4346 *
4347 * @param thread ID of thread going to be removed from its memory domain.
4348 */
4349
4350extern void k_mem_domain_remove_thread(k_tid_t thread);
4351
Anas Nashif166f5192018-02-25 08:02:36 -06004352/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004353
Andrew Boie756f9072017-10-10 16:01:49 -07004354/**
4355 * @brief Emit a character buffer to the console device
4356 *
4357 * @param c String of characters to print
4358 * @param n The length of the string
4359 */
4360__syscall void k_str_out(char *c, size_t n);
4361
Andy Rosse7172672018-01-24 15:48:32 -08004362/**
4363 * @brief Start a numbered CPU on a MP-capable system
4364
4365 * This starts and initializes a specific CPU. The main thread on
4366 * startup is running on CPU zero, other processors are numbered
4367 * sequentially. On return from this function, the CPU is known to
4368 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004369 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004370 * with the provided key will work to enable them.
4371 *
4372 * Normally, in SMP mode this function will be called by the kernel
4373 * initialization and should not be used as a user API. But it is
4374 * defined here for special-purpose apps which want Zephyr running on
4375 * one core and to use others for design-specific processing.
4376 *
4377 * @param cpu_num Integer number of the CPU
4378 * @param stack Stack memory for the CPU
4379 * @param sz Stack buffer size, in bytes
4380 * @param fn Function to begin running on the CPU. First argument is
4381 * an irq_unlock() key.
4382 * @param arg Untyped argument to be passed to "fn"
4383 */
4384extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4385 void (*fn)(int, void *), void *arg);
4386
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004387#ifdef __cplusplus
4388}
4389#endif
4390
Andrew Boiee004dec2016-11-07 09:01:19 -08004391#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4392/*
4393 * Define new and delete operators.
4394 * At this moment, the operators do nothing since objects are supposed
4395 * to be statically allocated.
4396 */
4397inline void operator delete(void *ptr)
4398{
4399 (void)ptr;
4400}
4401
4402inline void operator delete[](void *ptr)
4403{
4404 (void)ptr;
4405}
4406
4407inline void *operator new(size_t size)
4408{
4409 (void)size;
4410 return NULL;
4411}
4412
4413inline void *operator new[](size_t size)
4414{
4415 (void)size;
4416 return NULL;
4417}
4418
4419/* Placement versions of operator new and delete */
4420inline void operator delete(void *ptr1, void *ptr2)
4421{
4422 (void)ptr1;
4423 (void)ptr2;
4424}
4425
4426inline void operator delete[](void *ptr1, void *ptr2)
4427{
4428 (void)ptr1;
4429 (void)ptr2;
4430}
4431
4432inline void *operator new(size_t size, void *ptr)
4433{
4434 (void)size;
4435 return ptr;
4436}
4437
4438inline void *operator new[](size_t size, void *ptr)
4439{
4440 (void)size;
4441 return ptr;
4442}
4443
4444#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4445
Andrew Boiefa94ee72017-09-28 16:54:35 -07004446#include <syscalls/kernel.h>
4447
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004448#endif /* !_ASMLANGUAGE */
4449
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004450#endif /* _kernel__h_ */