blob: 36bcfe746a04e4e789492b54b27275441f252b15 [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;
423#endif
424
Andrew Boie73abd322017-04-04 13:19:13 -0700425 /* data returned by APIs */
426 void *swap_data;
427
428#ifdef CONFIG_SYS_CLOCK_EXISTS
429 /* this thread's entry in a timeout queue */
430 struct _timeout timeout;
431#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700432};
433
434typedef struct _thread_base _thread_base_t;
435
436#if defined(CONFIG_THREAD_STACK_INFO)
437/* Contains the stack information of a thread */
438struct _thread_stack_info {
439 /* Stack Start */
440 u32_t start;
441 /* Stack Size */
442 u32_t size;
443};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700444
445typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700446#endif /* CONFIG_THREAD_STACK_INFO */
447
Chunlin Hane9c97022017-07-07 20:29:30 +0800448#if defined(CONFIG_USERSPACE)
449struct _mem_domain_info {
450 /* memory domain queue node */
451 sys_dnode_t mem_domain_q_node;
452 /* memory domain of the thread */
453 struct k_mem_domain *mem_domain;
454};
455
456#endif /* CONFIG_USERSPACE */
457
Andrew Boie73abd322017-04-04 13:19:13 -0700458struct k_thread {
459
460 struct _thread_base base;
461
462 /* defined by the architecture, but all archs need these */
463 struct _caller_saved caller_saved;
464 struct _callee_saved callee_saved;
465
466 /* static thread init data */
467 void *init_data;
468
469 /* abort function */
470 void (*fn_abort)(void);
471
472#if defined(CONFIG_THREAD_MONITOR)
473 /* thread entry and parameters description */
474 struct __thread_entry *entry;
475
476 /* next item in list of all threads */
477 struct k_thread *next_thread;
478#endif
479
480#ifdef CONFIG_THREAD_CUSTOM_DATA
481 /* crude thread-local storage */
482 void *custom_data;
483#endif
484
485#ifdef CONFIG_ERRNO
486 /* per-thread errno variable */
487 int errno_var;
488#endif
489
490#if defined(CONFIG_THREAD_STACK_INFO)
491 /* Stack Info */
492 struct _thread_stack_info stack_info;
493#endif /* CONFIG_THREAD_STACK_INFO */
494
Chunlin Hane9c97022017-07-07 20:29:30 +0800495#if defined(CONFIG_USERSPACE)
496 /* memory domain info of the thread */
497 struct _mem_domain_info mem_domain_info;
Andrew Boiebca15da2017-10-15 14:17:48 -0700498 /* Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700499 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800500#endif /* CONFIG_USERSPACE */
501
Andy Ross042d8ec2017-12-09 08:37:20 -0800502#if defined(CONFIG_USE_SWITCH)
503 /* When using __switch() a few previously arch-specific items
504 * become part of the core OS
505 */
506
507 /* _Swap() return value */
508 int swap_retval;
509
510 /* Context handle returned via _arch_switch() */
511 void *switch_handle;
512#endif
513
Andrew Boie73abd322017-04-04 13:19:13 -0700514 /* arch-specifics: must always be at the end */
515 struct _thread_arch arch;
516};
517
518typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400519typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700520#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400521
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400522enum execution_context_types {
523 K_ISR = 0,
524 K_COOP_THREAD,
525 K_PREEMPT_THREAD,
526};
527
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400528/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100529 * @defgroup profiling_apis Profiling APIs
530 * @ingroup kernel_apis
531 * @{
532 */
533
534/**
535 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
536 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700537 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100538 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
539 *
540 * CONFIG_MAIN_STACK_SIZE
541 * CONFIG_IDLE_STACK_SIZE
542 * CONFIG_ISR_STACK_SIZE
543 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
544 *
545 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
546 * produce output.
547 *
548 * @return N/A
549 */
550extern void k_call_stacks_analyze(void);
551
Anas Nashif166f5192018-02-25 08:02:36 -0600552/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100553
554/**
Allan Stephensc98da842016-11-11 15:45:03 -0500555 * @defgroup thread_apis Thread APIs
556 * @ingroup kernel_apis
557 * @{
558 */
559
Benjamin Walshed240f22017-01-22 13:05:08 -0500560#endif /* !_ASMLANGUAGE */
561
562
563/*
564 * Thread user options. May be needed by assembly code. Common part uses low
565 * bits, arch-specific use high bits.
566 */
567
568/* system thread that must not abort */
569#define K_ESSENTIAL (1 << 0)
570
571#if defined(CONFIG_FP_SHARING)
572/* thread uses floating point registers */
573#define K_FP_REGS (1 << 1)
574#endif
575
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700576/* This thread has dropped from supervisor mode to user mode and consequently
577 * has additional restrictions
578 */
579#define K_USER (1 << 2)
580
Andrew Boie47f8fd12017-10-05 11:11:02 -0700581/* Indicates that the thread being created should inherit all kernel object
582 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
583 * is not enabled.
584 */
585#define K_INHERIT_PERMS (1 << 3)
586
Benjamin Walshed240f22017-01-22 13:05:08 -0500587#ifdef CONFIG_X86
588/* x86 Bitmask definitions for threads user options */
589
590#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
591/* thread uses SSEx (and also FP) registers */
592#define K_SSE_REGS (1 << 7)
593#endif
594#endif
595
596/* end - thread options */
597
598#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400599/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700600 * @brief Create a thread.
601 *
602 * This routine initializes a thread, then schedules it for execution.
603 *
604 * The new thread may be scheduled for immediate execution or a delayed start.
605 * If the newly spawned thread does not have a delayed start the kernel
606 * scheduler may preempt the current thread to allow the new thread to
607 * execute.
608 *
609 * Thread options are architecture-specific, and can include K_ESSENTIAL,
610 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
611 * them using "|" (the logical OR operator).
612 *
613 * Historically, users often would use the beginning of the stack memory region
614 * to store the struct k_thread data, although corruption will occur if the
615 * stack overflows this region and stack protection features may not detect this
616 * situation.
617 *
618 * @param new_thread Pointer to uninitialized struct k_thread
619 * @param stack Pointer to the stack space.
620 * @param stack_size Stack size in bytes.
621 * @param entry Thread entry function.
622 * @param p1 1st entry point parameter.
623 * @param p2 2nd entry point parameter.
624 * @param p3 3rd entry point parameter.
625 * @param prio Thread priority.
626 * @param options Thread options.
627 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
628 *
629 * @return ID of new thread.
630 */
Andrew Boie662c3452017-10-02 10:51:18 -0700631__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700632 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700633 size_t stack_size,
634 k_thread_entry_t entry,
635 void *p1, void *p2, void *p3,
636 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700637
Andrew Boie3f091b52017-08-30 14:34:14 -0700638/**
639 * @brief Drop a thread's privileges permanently to user mode
640 *
641 * @param entry Function to start executing from
642 * @param p1 1st entry point parameter
643 * @param p2 2nd entry point parameter
644 * @param p3 3rd entry point parameter
645 */
646extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
647 void *p1, void *p2,
648 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700649
Andrew Boied26cf2d2017-03-30 13:07:02 -0700650/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700651 * @brief Grant a thread access to a NULL-terminated set of kernel objects
652 *
653 * This is a convenience function. For the provided thread, grant access to
654 * the remaining arguments, which must be pointers to kernel objects.
655 * The final argument must be a NULL.
656 *
657 * The thread object must be initialized (i.e. running). The objects don't
658 * need to be.
659 *
660 * @param thread Thread to grant access to objects
661 * @param ... NULL-terminated list of kernel object pointers
662 */
663extern void __attribute__((sentinel))
664 k_thread_access_grant(struct k_thread *thread, ...);
665
666/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500667 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400668 *
Allan Stephensc98da842016-11-11 15:45:03 -0500669 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500670 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400671 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500672 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400673 *
674 * @return N/A
675 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700676__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400677
678/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500679 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400680 *
681 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500682 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400683 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400684 * @return N/A
685 */
Kumar Galacc334c72017-04-21 10:55:34 -0500686extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400687
688/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500689 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400690 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500691 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400692 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500693 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400694 *
695 * @return N/A
696 */
Andrew Boie468190a2017-09-29 14:00:48 -0700697__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400698
699/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500700 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400701 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500702 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400703 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500704 * If @a thread is not currently sleeping, the routine has no effect.
705 *
706 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400707 *
708 * @return N/A
709 */
Andrew Boie468190a2017-09-29 14:00:48 -0700710__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400711
712/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500713 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400714 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500715 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400716 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700717__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400718
719/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500720 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500722 * This routine prevents @a thread from executing if it has not yet started
723 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400724 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500725 * @param thread ID of thread to cancel.
726 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700727 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500728 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700729 *
730 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400731 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700732__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400733
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400734/**
Allan Stephensc98da842016-11-11 15:45:03 -0500735 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400736 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500737 * This routine permanently stops execution of @a thread. The thread is taken
738 * off all kernel queues it is part of (i.e. the ready queue, the timeout
739 * queue, or a kernel object wait queue). However, any kernel resources the
740 * thread might currently own (such as mutexes or memory blocks) are not
741 * released. It is the responsibility of the caller of this routine to ensure
742 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400743 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500744 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400745 *
746 * @return N/A
747 */
Andrew Boie468190a2017-09-29 14:00:48 -0700748__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400749
Andrew Boie7d627c52017-08-30 11:01:56 -0700750
751/**
752 * @brief Start an inactive thread
753 *
754 * If a thread was created with K_FOREVER in the delay parameter, it will
755 * not be added to the scheduling queue until this function is called
756 * on it.
757 *
758 * @param thread thread to start
759 */
Andrew Boie468190a2017-09-29 14:00:48 -0700760__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700761
Allan Stephensc98da842016-11-11 15:45:03 -0500762/**
763 * @cond INTERNAL_HIDDEN
764 */
765
Benjamin Walshd211a522016-12-06 11:44:01 -0500766/* timeout has timed out and is not on _timeout_q anymore */
767#define _EXPIRED (-2)
768
769/* timeout is not in use */
770#define _INACTIVE (-1)
771
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400772struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700773 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700774 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400775 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700776 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500777 void *init_p1;
778 void *init_p2;
779 void *init_p3;
780 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500781 u32_t init_options;
782 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500783 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400784};
785
Andrew Boied26cf2d2017-03-30 13:07:02 -0700786#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400787 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500788 prio, options, delay, abort, groups) \
789 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700790 .init_thread = (thread), \
791 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500792 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700793 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400794 .init_p1 = (void *)p1, \
795 .init_p2 = (void *)p2, \
796 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500797 .init_prio = (prio), \
798 .init_options = (options), \
799 .init_delay = (delay), \
800 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400801 }
802
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400803/**
Allan Stephensc98da842016-11-11 15:45:03 -0500804 * INTERNAL_HIDDEN @endcond
805 */
806
807/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500808 * @brief Statically define and initialize a thread.
809 *
810 * The thread may be scheduled for immediate execution or a delayed start.
811 *
812 * Thread options are architecture-specific, and can include K_ESSENTIAL,
813 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
814 * them using "|" (the logical OR operator).
815 *
816 * The ID of the thread can be accessed using:
817 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500818 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500819 *
820 * @param name Name of the thread.
821 * @param stack_size Stack size in bytes.
822 * @param entry Thread entry function.
823 * @param p1 1st entry point parameter.
824 * @param p2 2nd entry point parameter.
825 * @param p3 3rd entry point parameter.
826 * @param prio Thread priority.
827 * @param options Thread options.
828 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400829 *
830 * @internal It has been observed that the x86 compiler by default aligns
831 * these _static_thread_data structures to 32-byte boundaries, thereby
832 * wasting space. To work around this, force a 4-byte alignment.
833 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500834#define K_THREAD_DEFINE(name, stack_size, \
835 entry, p1, p2, p3, \
836 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700837 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700838 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500839 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500840 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700841 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
842 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500843 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700844 NULL, 0); \
845 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400846
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400847/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500848 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400849 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500850 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500852 * @param thread ID of thread whose priority is needed.
853 *
854 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400855 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700856__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400857
858/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500859 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400860 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500861 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400862 *
863 * Rescheduling can occur immediately depending on the priority @a thread is
864 * set to:
865 *
866 * - If its priority is raised above the priority of the caller of this
867 * function, and the caller is preemptible, @a thread will be scheduled in.
868 *
869 * - If the caller operates on itself, it lowers its priority below that of
870 * other threads in the system, and the caller is preemptible, the thread of
871 * highest priority will be scheduled in.
872 *
873 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
874 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
875 * highest priority.
876 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500877 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400878 * @param prio New priority.
879 *
880 * @warning Changing the priority of a thread currently involved in mutex
881 * priority inheritance may result in undefined behavior.
882 *
883 * @return N/A
884 */
Andrew Boie468190a2017-09-29 14:00:48 -0700885__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400886
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400887/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500888 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400889 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500890 * This routine prevents the kernel scheduler from making @a thread the
891 * current thread. All other internal operations on @a thread are still
892 * performed; for example, any timeout it is waiting on keeps ticking,
893 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400894 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500895 * If @a thread is already suspended, the routine has no effect.
896 *
897 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400898 *
899 * @return N/A
900 */
Andrew Boie468190a2017-09-29 14:00:48 -0700901__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400902
903/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500904 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400905 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500906 * This routine allows the kernel scheduler to make @a thread the current
907 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400908 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500909 * If @a thread is not currently suspended, the routine has no effect.
910 *
911 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400912 *
913 * @return N/A
914 */
Andrew Boie468190a2017-09-29 14:00:48 -0700915__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400916
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400917/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500918 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400919 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500920 * This routine specifies how the scheduler will perform time slicing of
921 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400922 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500923 * To enable time slicing, @a slice must be non-zero. The scheduler
924 * ensures that no thread runs for more than the specified time limit
925 * before other threads of that priority are given a chance to execute.
926 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700927 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400928 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500929 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400930 * execute. Once the scheduler selects a thread for execution, there is no
931 * minimum guaranteed time the thread will execute before threads of greater or
932 * equal priority are scheduled.
933 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500934 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400935 * for execution, this routine has no effect; the thread is immediately
936 * rescheduled after the slice period expires.
937 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500938 * To disable timeslicing, set both @a slice and @a prio to zero.
939 *
940 * @param slice Maximum time slice length (in milliseconds).
941 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400942 *
943 * @return N/A
944 */
Kumar Galacc334c72017-04-21 10:55:34 -0500945extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400946
Anas Nashif166f5192018-02-25 08:02:36 -0600947/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -0500948
949/**
950 * @addtogroup isr_apis
951 * @{
952 */
953
954/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500955 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400956 *
Allan Stephensc98da842016-11-11 15:45:03 -0500957 * This routine allows the caller to customize its actions, depending on
958 * whether it is a thread or an ISR.
959 *
960 * @note Can be called by ISRs.
961 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500962 * @return 0 if invoked by a thread.
963 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400964 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500965extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400966
Benjamin Walsh445830d2016-11-10 15:54:27 -0500967/**
968 * @brief Determine if code is running in a preemptible thread.
969 *
Allan Stephensc98da842016-11-11 15:45:03 -0500970 * This routine allows the caller to customize its actions, depending on
971 * whether it can be preempted by another thread. The routine returns a 'true'
972 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500973 *
Allan Stephensc98da842016-11-11 15:45:03 -0500974 * - The code is running in a thread, not at ISR.
975 * - The thread's priority is in the preemptible range.
976 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500977 *
Allan Stephensc98da842016-11-11 15:45:03 -0500978 * @note Can be called by ISRs.
979 *
980 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500981 * @return Non-zero if invoked by a preemptible thread.
982 */
Andrew Boie468190a2017-09-29 14:00:48 -0700983__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -0500984
Allan Stephensc98da842016-11-11 15:45:03 -0500985/**
Anas Nashif166f5192018-02-25 08:02:36 -0600986 * @}
Allan Stephensc98da842016-11-11 15:45:03 -0500987 */
988
989/**
990 * @addtogroup thread_apis
991 * @{
992 */
993
994/**
995 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500996 *
Allan Stephensc98da842016-11-11 15:45:03 -0500997 * This routine prevents the current thread from being preempted by another
998 * thread by instructing the scheduler to treat it as a cooperative thread.
999 * If the thread subsequently performs an operation that makes it unready,
1000 * it will be context switched out in the normal manner. When the thread
1001 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001002 *
Allan Stephensc98da842016-11-11 15:45:03 -05001003 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001004 *
Allan Stephensc98da842016-11-11 15:45:03 -05001005 * @note k_sched_lock() and k_sched_unlock() should normally be used
1006 * when the operation being performed can be safely interrupted by ISRs.
1007 * However, if the amount of processing involved is very small, better
1008 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001009 *
1010 * @return N/A
1011 */
1012extern void k_sched_lock(void);
1013
Allan Stephensc98da842016-11-11 15:45:03 -05001014/**
1015 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001016 *
Allan Stephensc98da842016-11-11 15:45:03 -05001017 * This routine reverses the effect of a previous call to k_sched_lock().
1018 * A thread must call the routine once for each time it called k_sched_lock()
1019 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001020 *
1021 * @return N/A
1022 */
1023extern void k_sched_unlock(void);
1024
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001025/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001026 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001027 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001028 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001029 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001030 * Custom data is not used by the kernel itself, and is freely available
1031 * for a thread to use as it sees fit. It can be used as a framework
1032 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001033 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001034 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001035 *
1036 * @return N/A
1037 */
Andrew Boie468190a2017-09-29 14:00:48 -07001038__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001039
1040/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001041 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001042 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001043 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001044 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001045 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001046 */
Andrew Boie468190a2017-09-29 14:00:48 -07001047__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001048
1049/**
Anas Nashif166f5192018-02-25 08:02:36 -06001050 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001051 */
1052
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001053#include <sys_clock.h>
1054
Allan Stephensc2f15a42016-11-17 12:24:22 -05001055/**
1056 * @addtogroup clock_apis
1057 * @{
1058 */
1059
1060/**
1061 * @brief Generate null timeout delay.
1062 *
1063 * This macro generates a timeout delay that that instructs a kernel API
1064 * not to wait if the requested operation cannot be performed immediately.
1065 *
1066 * @return Timeout delay value.
1067 */
1068#define K_NO_WAIT 0
1069
1070/**
1071 * @brief Generate timeout delay from milliseconds.
1072 *
1073 * This macro generates a timeout delay that that instructs a kernel API
1074 * to wait up to @a ms milliseconds to perform the requested operation.
1075 *
1076 * @param ms Duration in milliseconds.
1077 *
1078 * @return Timeout delay value.
1079 */
Johan Hedberg14471692016-11-13 10:52:15 +02001080#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001081
1082/**
1083 * @brief Generate timeout delay from seconds.
1084 *
1085 * This macro generates a timeout delay that that instructs a kernel API
1086 * to wait up to @a s seconds to perform the requested operation.
1087 *
1088 * @param s Duration in seconds.
1089 *
1090 * @return Timeout delay value.
1091 */
Johan Hedberg14471692016-11-13 10:52:15 +02001092#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001093
1094/**
1095 * @brief Generate timeout delay from minutes.
1096 *
1097 * This macro generates a timeout delay that that instructs a kernel API
1098 * to wait up to @a m minutes to perform the requested operation.
1099 *
1100 * @param m Duration in minutes.
1101 *
1102 * @return Timeout delay value.
1103 */
Johan Hedberg14471692016-11-13 10:52:15 +02001104#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001105
1106/**
1107 * @brief Generate timeout delay from hours.
1108 *
1109 * This macro generates a timeout delay that that instructs a kernel API
1110 * to wait up to @a h hours to perform the requested operation.
1111 *
1112 * @param h Duration in hours.
1113 *
1114 * @return Timeout delay value.
1115 */
Johan Hedberg14471692016-11-13 10:52:15 +02001116#define K_HOURS(h) K_MINUTES((h) * 60)
1117
Allan Stephensc98da842016-11-11 15:45:03 -05001118/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001119 * @brief Generate infinite timeout delay.
1120 *
1121 * This macro generates a timeout delay that that instructs a kernel API
1122 * to wait as long as necessary to perform the requested operation.
1123 *
1124 * @return Timeout delay value.
1125 */
1126#define K_FOREVER (-1)
1127
1128/**
Anas Nashif166f5192018-02-25 08:02:36 -06001129 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001130 */
1131
1132/**
Allan Stephensc98da842016-11-11 15:45:03 -05001133 * @cond INTERNAL_HIDDEN
1134 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001135
Benjamin Walsh62092182016-12-20 14:39:08 -05001136/* kernel clocks */
1137
1138#if (sys_clock_ticks_per_sec == 1000) || \
1139 (sys_clock_ticks_per_sec == 500) || \
1140 (sys_clock_ticks_per_sec == 250) || \
1141 (sys_clock_ticks_per_sec == 125) || \
1142 (sys_clock_ticks_per_sec == 100) || \
1143 (sys_clock_ticks_per_sec == 50) || \
1144 (sys_clock_ticks_per_sec == 25) || \
1145 (sys_clock_ticks_per_sec == 20) || \
1146 (sys_clock_ticks_per_sec == 10) || \
1147 (sys_clock_ticks_per_sec == 1)
1148
1149 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1150#else
1151 /* yields horrible 64-bit math on many architectures: try to avoid */
1152 #define _NON_OPTIMIZED_TICKS_PER_SEC
1153#endif
1154
1155#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001156extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001157#else
Kumar Galacc334c72017-04-21 10:55:34 -05001158static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001159{
Kumar Galacc334c72017-04-21 10:55:34 -05001160 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001161}
1162#endif
1163
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001164/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001165#ifdef CONFIG_TICKLESS_KERNEL
1166#define _TICK_ALIGN 0
1167#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001168#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001169#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001170
Kumar Galacc334c72017-04-21 10:55:34 -05001171static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001172{
Benjamin Walsh62092182016-12-20 14:39:08 -05001173#ifdef CONFIG_SYS_CLOCK_EXISTS
1174
1175#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001176 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001177#else
Kumar Galacc334c72017-04-21 10:55:34 -05001178 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001179#endif
1180
1181#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001182 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001183 return 0;
1184#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001185}
1186
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001187struct k_timer {
1188 /*
1189 * _timeout structure must be first here if we want to use
1190 * dynamic timer allocation. timeout.node is used in the double-linked
1191 * list of free timers
1192 */
1193 struct _timeout timeout;
1194
Allan Stephens45bfa372016-10-12 12:39:42 -05001195 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001196 _wait_q_t wait_q;
1197
1198 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001199 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001200
1201 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001202 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001203
1204 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001205 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001206
Allan Stephens45bfa372016-10-12 12:39:42 -05001207 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001208 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001209
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001210 /* user-specific data, also used to support legacy features */
1211 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001212
Anas Nashif2f203c22016-12-18 06:57:45 -05001213 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001214};
1215
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001216#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001217 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001218 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001219 .timeout.wait_q = NULL, \
1220 .timeout.thread = NULL, \
1221 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001222 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001223 .expiry_fn = expiry, \
1224 .stop_fn = stop, \
1225 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001226 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001227 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001228 }
1229
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001230#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1231
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001232/**
Allan Stephensc98da842016-11-11 15:45:03 -05001233 * INTERNAL_HIDDEN @endcond
1234 */
1235
1236/**
1237 * @defgroup timer_apis Timer APIs
1238 * @ingroup kernel_apis
1239 * @{
1240 */
1241
1242/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001243 * @typedef k_timer_expiry_t
1244 * @brief Timer expiry function type.
1245 *
1246 * A timer's expiry function is executed by the system clock interrupt handler
1247 * each time the timer expires. The expiry function is optional, and is only
1248 * invoked if the timer has been initialized with one.
1249 *
1250 * @param timer Address of timer.
1251 *
1252 * @return N/A
1253 */
1254typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1255
1256/**
1257 * @typedef k_timer_stop_t
1258 * @brief Timer stop function type.
1259 *
1260 * A timer's stop function is executed if the timer is stopped prematurely.
1261 * The function runs in the context of the thread that stops the timer.
1262 * The stop function is optional, and is only invoked if the timer has been
1263 * initialized with one.
1264 *
1265 * @param timer Address of timer.
1266 *
1267 * @return N/A
1268 */
1269typedef void (*k_timer_stop_t)(struct k_timer *timer);
1270
1271/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001272 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001273 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001274 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001275 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001276 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001277 *
1278 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001279 * @param expiry_fn Function to invoke each time the timer expires.
1280 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001281 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001282#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001283 struct k_timer name \
1284 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001285 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001286
Allan Stephens45bfa372016-10-12 12:39:42 -05001287/**
1288 * @brief Initialize a timer.
1289 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001290 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001291 *
1292 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001293 * @param expiry_fn Function to invoke each time the timer expires.
1294 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001295 *
1296 * @return N/A
1297 */
1298extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001299 k_timer_expiry_t expiry_fn,
1300 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001301
Allan Stephens45bfa372016-10-12 12:39:42 -05001302/**
1303 * @brief Start a timer.
1304 *
1305 * This routine starts a timer, and resets its status to zero. The timer
1306 * begins counting down using the specified duration and period values.
1307 *
1308 * Attempting to start a timer that is already running is permitted.
1309 * The timer's status is reset to zero and the timer begins counting down
1310 * using the new duration and period values.
1311 *
1312 * @param timer Address of timer.
1313 * @param duration Initial timer duration (in milliseconds).
1314 * @param period Timer period (in milliseconds).
1315 *
1316 * @return N/A
1317 */
Andrew Boiea354d492017-09-29 16:22:28 -07001318__syscall void k_timer_start(struct k_timer *timer,
1319 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001320
1321/**
1322 * @brief Stop a timer.
1323 *
1324 * This routine stops a running timer prematurely. The timer's stop function,
1325 * if one exists, is invoked by the caller.
1326 *
1327 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001328 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001329 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001330 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1331 * if @a k_timer_stop is to be called from ISRs.
1332 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001333 * @param timer Address of timer.
1334 *
1335 * @return N/A
1336 */
Andrew Boiea354d492017-09-29 16:22:28 -07001337__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001338
1339/**
1340 * @brief Read timer status.
1341 *
1342 * This routine reads the timer's status, which indicates the number of times
1343 * it has expired since its status was last read.
1344 *
1345 * Calling this routine resets the timer's status to zero.
1346 *
1347 * @param timer Address of timer.
1348 *
1349 * @return Timer status.
1350 */
Andrew Boiea354d492017-09-29 16:22:28 -07001351__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001352
1353/**
1354 * @brief Synchronize thread to timer expiration.
1355 *
1356 * This routine blocks the calling thread until the timer's status is non-zero
1357 * (indicating that it has expired at least once since it was last examined)
1358 * or the timer is stopped. If the timer status is already non-zero,
1359 * or the timer is already stopped, the caller continues without waiting.
1360 *
1361 * Calling this routine resets the timer's status to zero.
1362 *
1363 * This routine must not be used by interrupt handlers, since they are not
1364 * allowed to block.
1365 *
1366 * @param timer Address of timer.
1367 *
1368 * @return Timer status.
1369 */
Andrew Boiea354d492017-09-29 16:22:28 -07001370__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001371
1372/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001373 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001374 *
1375 * This routine computes the (approximate) time remaining before a running
1376 * timer next expires. If the timer is not running, it returns zero.
1377 *
1378 * @param timer Address of timer.
1379 *
1380 * @return Remaining time (in milliseconds).
1381 */
Andrew Boiea354d492017-09-29 16:22:28 -07001382__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1383
1384static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001385{
1386 return _timeout_remaining_get(&timer->timeout);
1387}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001388
Allan Stephensc98da842016-11-11 15:45:03 -05001389/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001390 * @brief Associate user-specific data with a timer.
1391 *
1392 * This routine records the @a user_data with the @a timer, to be retrieved
1393 * later.
1394 *
1395 * It can be used e.g. in a timer handler shared across multiple subsystems to
1396 * retrieve data specific to the subsystem this timer is associated with.
1397 *
1398 * @param timer Address of timer.
1399 * @param user_data User data to associate with the timer.
1400 *
1401 * @return N/A
1402 */
Andrew Boiea354d492017-09-29 16:22:28 -07001403__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1404
Anas Nashif954d5502018-02-25 08:37:28 -06001405/**
1406 * @internal
1407 */
Andrew Boiea354d492017-09-29 16:22:28 -07001408static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1409 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001410{
1411 timer->user_data = user_data;
1412}
1413
1414/**
1415 * @brief Retrieve the user-specific data from a timer.
1416 *
1417 * @param timer Address of timer.
1418 *
1419 * @return The user data.
1420 */
Andrew Boiea354d492017-09-29 16:22:28 -07001421__syscall void *k_timer_user_data_get(struct k_timer *timer);
1422
1423static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001424{
1425 return timer->user_data;
1426}
1427
Anas Nashif166f5192018-02-25 08:02:36 -06001428/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001429
Allan Stephensc98da842016-11-11 15:45:03 -05001430/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001431 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001432 * @{
1433 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001434
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001435/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001436 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001437 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001438 * This routine returns the elapsed time since the system booted,
1439 * in milliseconds.
1440 *
1441 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001442 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001443__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001444
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001445/**
1446 * @brief Enable clock always on in tickless kernel
1447 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001448 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001449 * there are no timer events programmed in tickless kernel
1450 * scheduling. This is necessary if the clock is used to track
1451 * passage of time.
1452 *
1453 * @retval prev_status Previous status of always on flag
1454 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301455#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001456static inline int k_enable_sys_clock_always_on(void)
1457{
1458 int prev_status = _sys_clock_always_on;
1459
1460 _sys_clock_always_on = 1;
1461 _enable_sys_clock();
1462
1463 return prev_status;
1464}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301465#else
1466#define k_enable_sys_clock_always_on() do { } while ((0))
1467#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001468
1469/**
1470 * @brief Disable clock always on in tickless kernel
1471 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001472 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001473 * there are no timer events programmed in tickless kernel
1474 * scheduling. To save power, this routine should be called
1475 * immediately when clock is not used to track time.
1476 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301477#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001478static inline void k_disable_sys_clock_always_on(void)
1479{
1480 _sys_clock_always_on = 0;
1481}
1482#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001483#define k_disable_sys_clock_always_on() do { } while ((0))
1484#endif
1485
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001486/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001487 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001488 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001489 * This routine returns the lower 32-bits of the elapsed time since the system
1490 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001491 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001492 * This routine can be more efficient than k_uptime_get(), as it reduces the
1493 * need for interrupt locking and 64-bit math. However, the 32-bit result
1494 * cannot hold a system uptime time larger than approximately 50 days, so the
1495 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001496 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001497 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001498 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001499__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001500
1501/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001502 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001503 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001504 * This routine computes the elapsed time between the current system uptime
1505 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001506 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001507 * @param reftime Pointer to a reference time, which is updated to the current
1508 * uptime upon return.
1509 *
1510 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001511 */
Kumar Galacc334c72017-04-21 10:55:34 -05001512extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001513
1514/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001515 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001516 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001517 * This routine computes the elapsed time between the current system uptime
1518 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001519 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001520 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1521 * need for interrupt locking and 64-bit math. However, the 32-bit result
1522 * cannot hold an elapsed time larger than approximately 50 days, so the
1523 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001524 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001525 * @param reftime Pointer to a reference time, which is updated to the current
1526 * uptime upon return.
1527 *
1528 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001529 */
Kumar Galacc334c72017-04-21 10:55:34 -05001530extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001531
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001532/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001533 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001534 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001535 * This routine returns the current time, as measured by the system's hardware
1536 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001537 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001538 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001539 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001540#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001541
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001542/**
Anas Nashif166f5192018-02-25 08:02:36 -06001543 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001544 */
1545
Allan Stephensc98da842016-11-11 15:45:03 -05001546/**
1547 * @cond INTERNAL_HIDDEN
1548 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001549
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001550struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001551 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001552 union {
1553 _wait_q_t wait_q;
1554
1555 _POLL_EVENT;
1556 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001557
1558 _OBJECT_TRACING_NEXT_PTR(k_queue);
1559};
1560
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001561#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001562 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001563 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Michael Hope5f67a612018-03-17 12:44:40 +01001564 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001565 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001566 _OBJECT_TRACING_INIT \
1567 }
1568
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001569#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1570
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001571/**
1572 * INTERNAL_HIDDEN @endcond
1573 */
1574
1575/**
1576 * @defgroup queue_apis Queue APIs
1577 * @ingroup kernel_apis
1578 * @{
1579 */
1580
1581/**
1582 * @brief Initialize a queue.
1583 *
1584 * This routine initializes a queue object, prior to its first use.
1585 *
1586 * @param queue Address of the queue.
1587 *
1588 * @return N/A
1589 */
1590extern void k_queue_init(struct k_queue *queue);
1591
1592/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001593 * @brief Cancel waiting on a queue.
1594 *
1595 * This routine causes first thread pending on @a queue, if any, to
1596 * return from k_queue_get() call with NULL value (as if timeout expired).
1597 *
1598 * @note Can be called by ISRs.
1599 *
1600 * @param queue Address of the queue.
1601 *
1602 * @return N/A
1603 */
1604extern void k_queue_cancel_wait(struct k_queue *queue);
1605
1606/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001607 * @brief Append an element to the end of a queue.
1608 *
1609 * This routine appends a data item to @a queue. A queue data item must be
1610 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1611 * reserved for the kernel's use.
1612 *
1613 * @note Can be called by ISRs.
1614 *
1615 * @param queue Address of the queue.
1616 * @param data Address of the data item.
1617 *
1618 * @return N/A
1619 */
1620extern void k_queue_append(struct k_queue *queue, void *data);
1621
1622/**
1623 * @brief Prepend an element to a queue.
1624 *
1625 * This routine prepends a data item to @a queue. A queue data item must be
1626 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1627 * reserved for the kernel's use.
1628 *
1629 * @note Can be called by ISRs.
1630 *
1631 * @param queue Address of the queue.
1632 * @param data Address of the data item.
1633 *
1634 * @return N/A
1635 */
1636extern void k_queue_prepend(struct k_queue *queue, void *data);
1637
1638/**
1639 * @brief Inserts an element to a queue.
1640 *
1641 * This routine inserts a data item to @a queue after previous item. A queue
1642 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1643 * item are reserved for the kernel's use.
1644 *
1645 * @note Can be called by ISRs.
1646 *
1647 * @param queue Address of the queue.
1648 * @param prev Address of the previous data item.
1649 * @param data Address of the data item.
1650 *
1651 * @return N/A
1652 */
1653extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1654
1655/**
1656 * @brief Atomically append a list of elements to a queue.
1657 *
1658 * This routine adds a list of data items to @a queue in one operation.
1659 * The data items must be in a singly-linked list, with the first 32 bits
1660 * in each data item pointing to the next data item; the list must be
1661 * NULL-terminated.
1662 *
1663 * @note Can be called by ISRs.
1664 *
1665 * @param queue Address of the queue.
1666 * @param head Pointer to first node in singly-linked list.
1667 * @param tail Pointer to last node in singly-linked list.
1668 *
1669 * @return N/A
1670 */
1671extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1672
1673/**
1674 * @brief Atomically add a list of elements to a queue.
1675 *
1676 * This routine adds a list of data items to @a queue in one operation.
1677 * The data items must be in a singly-linked list implemented using a
1678 * sys_slist_t object. Upon completion, the original list is empty.
1679 *
1680 * @note Can be called by ISRs.
1681 *
1682 * @param queue Address of the queue.
1683 * @param list Pointer to sys_slist_t object.
1684 *
1685 * @return N/A
1686 */
1687extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1688
1689/**
1690 * @brief Get an element from a queue.
1691 *
1692 * This routine removes first data item from @a queue. The first 32 bits of the
1693 * data item are reserved for the kernel's use.
1694 *
1695 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1696 *
1697 * @param queue Address of the queue.
1698 * @param timeout Waiting period to obtain a data item (in milliseconds),
1699 * or one of the special values K_NO_WAIT and K_FOREVER.
1700 *
1701 * @return Address of the data item if successful; NULL if returned
1702 * without waiting, or waiting period timed out.
1703 */
Kumar Galacc334c72017-04-21 10:55:34 -05001704extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001705
1706/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001707 * @brief Remove an element from a queue.
1708 *
1709 * This routine removes data item from @a queue. The first 32 bits of the
1710 * data item are reserved for the kernel's use. Removing elements from k_queue
1711 * rely on sys_slist_find_and_remove which is not a constant time operation.
1712 *
1713 * @note Can be called by ISRs
1714 *
1715 * @param queue Address of the queue.
1716 * @param data Address of the data item.
1717 *
1718 * @return true if data item was removed
1719 */
1720static inline bool k_queue_remove(struct k_queue *queue, void *data)
1721{
1722 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1723}
1724
1725/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001726 * @brief Query a queue to see if it has data available.
1727 *
1728 * Note that the data might be already gone by the time this function returns
1729 * if other threads are also trying to read from the queue.
1730 *
1731 * @note Can be called by ISRs.
1732 *
1733 * @param queue Address of the queue.
1734 *
1735 * @return Non-zero if the queue is empty.
1736 * @return 0 if data is available.
1737 */
1738static inline int k_queue_is_empty(struct k_queue *queue)
1739{
1740 return (int)sys_slist_is_empty(&queue->data_q);
1741}
1742
1743/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001744 * @brief Peek element at the head of queue.
1745 *
1746 * Return element from the head of queue without removing it.
1747 *
1748 * @param queue Address of the queue.
1749 *
1750 * @return Head element, or NULL if queue is empty.
1751 */
1752static inline void *k_queue_peek_head(struct k_queue *queue)
1753{
1754 return sys_slist_peek_head(&queue->data_q);
1755}
1756
1757/**
1758 * @brief Peek element at the tail of queue.
1759 *
1760 * Return element from the tail of queue without removing it.
1761 *
1762 * @param queue Address of the queue.
1763 *
1764 * @return Tail element, or NULL if queue is empty.
1765 */
1766static inline void *k_queue_peek_tail(struct k_queue *queue)
1767{
1768 return sys_slist_peek_tail(&queue->data_q);
1769}
1770
1771/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001772 * @brief Statically define and initialize a queue.
1773 *
1774 * The queue can be accessed outside the module where it is defined using:
1775 *
1776 * @code extern struct k_queue <name>; @endcode
1777 *
1778 * @param name Name of the queue.
1779 */
1780#define K_QUEUE_DEFINE(name) \
1781 struct k_queue name \
1782 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001783 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001784
Anas Nashif166f5192018-02-25 08:02:36 -06001785/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001786
1787/**
1788 * @cond INTERNAL_HIDDEN
1789 */
1790
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001791struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001792 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001793};
1794
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001795#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001796 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001797 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001798 }
1799
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001800#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1801
Allan Stephensc98da842016-11-11 15:45:03 -05001802/**
1803 * INTERNAL_HIDDEN @endcond
1804 */
1805
1806/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001807 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05001808 * @ingroup kernel_apis
1809 * @{
1810 */
1811
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001812/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001813 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001814 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001815 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001816 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001817 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001818 *
1819 * @return N/A
1820 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001821#define k_fifo_init(fifo) \
1822 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001823
1824/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001825 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001826 *
1827 * This routine causes first thread pending on @a fifo, if any, to
1828 * return from k_fifo_get() call with NULL value (as if timeout
1829 * expired).
1830 *
1831 * @note Can be called by ISRs.
1832 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001833 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001834 *
1835 * @return N/A
1836 */
1837#define k_fifo_cancel_wait(fifo) \
1838 k_queue_cancel_wait((struct k_queue *) fifo)
1839
1840/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001841 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001842 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001843 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001844 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1845 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001846 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001847 * @note Can be called by ISRs.
1848 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001849 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001850 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001851 *
1852 * @return N/A
1853 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001854#define k_fifo_put(fifo, data) \
1855 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001856
1857/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001858 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001860 * This routine adds a list of data items to @a fifo in one operation.
1861 * The data items must be in a singly-linked list, with the first 32 bits
1862 * each data item pointing to the next data item; the list must be
1863 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001865 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001866 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001867 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001868 * @param head Pointer to first node in singly-linked list.
1869 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001870 *
1871 * @return N/A
1872 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001873#define k_fifo_put_list(fifo, head, tail) \
1874 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001875
1876/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001877 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001878 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001879 * This routine adds a list of data items to @a fifo in one operation.
1880 * The data items must be in a singly-linked list implemented using a
1881 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001882 * and must be re-initialized via sys_slist_init().
1883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001884 * @note Can be called by ISRs.
1885 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001886 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001887 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001888 *
1889 * @return N/A
1890 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001891#define k_fifo_put_slist(fifo, list) \
1892 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001893
1894/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001895 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001897 * This routine removes a data item from @a fifo in a "first in, first out"
1898 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001899 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001900 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1901 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001902 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001903 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001904 * or one of the special values K_NO_WAIT and K_FOREVER.
1905 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001906 * @return Address of the data item if successful; NULL if returned
1907 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001908 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001909#define k_fifo_get(fifo, timeout) \
1910 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001911
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001912/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001913 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001914 *
1915 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06001916 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001917 *
1918 * @note Can be called by ISRs.
1919 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001920 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001921 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001922 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001923 * @return 0 if data is available.
1924 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001925#define k_fifo_is_empty(fifo) \
1926 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001927
1928/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001929 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001930 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001931 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05301932 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001933 * on each iteration of processing, a head container will be peeked,
1934 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06001935 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001936 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001937 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001938 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001939 * @return Head element, or NULL if the FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001940 */
1941#define k_fifo_peek_head(fifo) \
1942 k_queue_peek_head((struct k_queue *) fifo)
1943
1944/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001945 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001946 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001947 * Return element from the tail of FIFO queue (without removing it). A usecase
1948 * for this is if elements of the FIFO queue are themselves containers. Then
1949 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001950 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001951 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001952 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001953 * @return Tail element, or NULL if a FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001954 */
1955#define k_fifo_peek_tail(fifo) \
1956 k_queue_peek_tail((struct k_queue *) fifo)
1957
1958/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001959 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001960 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001961 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001962 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001963 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001964 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001965 * @param name Name of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001966 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001967#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001968 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001969 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001970 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001971
Anas Nashif166f5192018-02-25 08:02:36 -06001972/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001973
1974/**
1975 * @cond INTERNAL_HIDDEN
1976 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001977
1978struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001979 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001980};
1981
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001982#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001983 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001984 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001985 }
1986
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001987#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1988
Allan Stephensc98da842016-11-11 15:45:03 -05001989/**
1990 * INTERNAL_HIDDEN @endcond
1991 */
1992
1993/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001994 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05001995 * @ingroup kernel_apis
1996 * @{
1997 */
1998
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001999/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002000 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002001 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002002 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002003 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002004 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002005 *
2006 * @return N/A
2007 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002008#define k_lifo_init(lifo) \
2009 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002010
2011/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002012 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002013 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002014 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002015 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2016 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002017 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002018 * @note Can be called by ISRs.
2019 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002020 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002021 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002022 *
2023 * @return N/A
2024 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002025#define k_lifo_put(lifo, data) \
2026 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002027
2028/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002029 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002030 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002031 * This routine removes a data item from @a lifo in a "last in, first out"
2032 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002033 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002034 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2035 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002036 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002037 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002038 * or one of the special values K_NO_WAIT and K_FOREVER.
2039 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002040 * @return Address of the data item if successful; NULL if returned
2041 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002042 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002043#define k_lifo_get(lifo, timeout) \
2044 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002045
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002046/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002047 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002048 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002049 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002050 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002051 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002052 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002053 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002054 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002055#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002056 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002057 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002058 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002059
Anas Nashif166f5192018-02-25 08:02:36 -06002060/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002061
2062/**
2063 * @cond INTERNAL_HIDDEN
2064 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002065
2066struct k_stack {
2067 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002068 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002069
Anas Nashif2f203c22016-12-18 06:57:45 -05002070 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002071};
2072
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002073#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002074 { \
2075 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2076 .base = stack_buffer, \
2077 .next = stack_buffer, \
2078 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002079 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002080 }
2081
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002082#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2083
Allan Stephensc98da842016-11-11 15:45:03 -05002084/**
2085 * INTERNAL_HIDDEN @endcond
2086 */
2087
2088/**
2089 * @defgroup stack_apis Stack APIs
2090 * @ingroup kernel_apis
2091 * @{
2092 */
2093
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002094/**
2095 * @brief Initialize a stack.
2096 *
2097 * This routine initializes a stack object, prior to its first use.
2098 *
2099 * @param stack Address of the stack.
2100 * @param buffer Address of array used to hold stacked values.
2101 * @param num_entries Maximum number of values that can be stacked.
2102 *
2103 * @return N/A
2104 */
Andrew Boiee8734462017-09-29 16:42:07 -07002105__syscall void k_stack_init(struct k_stack *stack,
Andrew Boie8e3e6d02017-10-12 14:01:25 -07002106 u32_t *buffer, unsigned int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002107
2108/**
2109 * @brief Push an element onto a stack.
2110 *
2111 * This routine adds a 32-bit value @a data to @a stack.
2112 *
2113 * @note Can be called by ISRs.
2114 *
2115 * @param stack Address of the stack.
2116 * @param data Value to push onto the stack.
2117 *
2118 * @return N/A
2119 */
Andrew Boiee8734462017-09-29 16:42:07 -07002120__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002121
2122/**
2123 * @brief Pop an element from a stack.
2124 *
2125 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2126 * manner and stores the value in @a data.
2127 *
2128 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2129 *
2130 * @param stack Address of the stack.
2131 * @param data Address of area to hold the value popped from the stack.
2132 * @param timeout Waiting period to obtain a value (in milliseconds),
2133 * or one of the special values K_NO_WAIT and K_FOREVER.
2134 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002135 * @retval 0 Element popped from stack.
2136 * @retval -EBUSY Returned without waiting.
2137 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002138 */
Andrew Boiee8734462017-09-29 16:42:07 -07002139__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002140
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002141/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002142 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002144 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002145 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002146 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002147 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002148 * @param name Name of the stack.
2149 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002150 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002151#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002152 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002153 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002154 struct k_stack name \
2155 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002156 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002157 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002158
Anas Nashif166f5192018-02-25 08:02:36 -06002159/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002160
Allan Stephens6bba9b02016-11-16 14:56:54 -05002161struct k_work;
2162
Allan Stephensc98da842016-11-11 15:45:03 -05002163/**
2164 * @defgroup workqueue_apis Workqueue Thread APIs
2165 * @ingroup kernel_apis
2166 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002167 */
2168
Allan Stephens6bba9b02016-11-16 14:56:54 -05002169/**
2170 * @typedef k_work_handler_t
2171 * @brief Work item handler function type.
2172 *
2173 * A work item's handler function is executed by a workqueue's thread
2174 * when the work item is processed by the workqueue.
2175 *
2176 * @param work Address of the work item.
2177 *
2178 * @return N/A
2179 */
2180typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002181
2182/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002183 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002184 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002185
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002186struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002187 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002188 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002189};
2190
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002191enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002192 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002193};
2194
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002195struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002196 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002197 k_work_handler_t handler;
2198 atomic_t flags[1];
2199};
2200
Allan Stephens6bba9b02016-11-16 14:56:54 -05002201struct k_delayed_work {
2202 struct k_work work;
2203 struct _timeout timeout;
2204 struct k_work_q *work_q;
2205};
2206
2207extern struct k_work_q k_sys_work_q;
2208
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002209/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002210 * INTERNAL_HIDDEN @endcond
2211 */
2212
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002213#define _K_WORK_INITIALIZER(work_handler) \
2214 { \
2215 ._reserved = NULL, \
2216 .handler = work_handler, \
2217 .flags = { 0 } \
2218 }
2219
2220#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2221
Allan Stephens6bba9b02016-11-16 14:56:54 -05002222/**
2223 * @brief Initialize a statically-defined work item.
2224 *
2225 * This macro can be used to initialize a statically-defined workqueue work
2226 * item, prior to its first use. For example,
2227 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002228 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002229 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002230 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002231 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002232 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002233#define K_WORK_DEFINE(work, work_handler) \
2234 struct k_work work \
2235 __in_section(_k_work, static, work) = \
2236 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002237
2238/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002239 * @brief Initialize a work item.
2240 *
2241 * This routine initializes a workqueue work item, prior to its first use.
2242 *
2243 * @param work Address of work item.
2244 * @param handler Function to invoke each time work item is processed.
2245 *
2246 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002247 */
2248static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2249{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002250 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002251 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002252 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002253}
2254
2255/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002256 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002257 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002258 * This routine submits work item @a work to be processed by workqueue
2259 * @a work_q. If the work item is already pending in the workqueue's queue
2260 * as a result of an earlier submission, this routine has no effect on the
2261 * work item. If the work item has already been processed, or is currently
2262 * being processed, its work is considered complete and the work item can be
2263 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002264 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002265 * @warning
2266 * A submitted work item must not be modified until it has been processed
2267 * by the workqueue.
2268 *
2269 * @note Can be called by ISRs.
2270 *
2271 * @param work_q Address of workqueue.
2272 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002273 *
2274 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002275 */
2276static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2277 struct k_work *work)
2278{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002279 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002280 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002281 }
2282}
2283
2284/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002285 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002286 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002287 * This routine indicates if work item @a work is pending in a workqueue's
2288 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002289 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002290 * @note Can be called by ISRs.
2291 *
2292 * @param work Address of work item.
2293 *
2294 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002295 */
2296static inline int k_work_pending(struct k_work *work)
2297{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002298 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002299}
2300
2301/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002302 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002303 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002304 * This routine starts workqueue @a work_q. The workqueue spawns its work
2305 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002306 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002307 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002308 * @param stack Pointer to work queue thread's stack space, as defined by
2309 * K_THREAD_STACK_DEFINE()
2310 * @param stack_size Size of the work queue thread's stack (in bytes), which
2311 * should either be the same constant passed to
2312 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002313 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002314 *
2315 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002316 */
Andrew Boie507852a2017-07-25 18:47:07 -07002317extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002318 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002319 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002320
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002321/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002322 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002323 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002324 * This routine initializes a workqueue delayed work item, prior to
2325 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002326 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002327 * @param work Address of delayed work item.
2328 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002329 *
2330 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002331 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002332extern void k_delayed_work_init(struct k_delayed_work *work,
2333 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002334
2335/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002336 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002337 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002338 * This routine schedules work item @a work to be processed by workqueue
2339 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002340 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002341 * Only when the countdown completes is the work item actually submitted to
2342 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002343 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002344 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002345 * counting down cancels the existing submission and restarts the
2346 * countdown using the new delay. Note that this behavior is
2347 * inherently subject to race conditions with the pre-existing
2348 * timeouts and work queue, so care must be taken to synchronize such
2349 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002350 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002351 * @warning
2352 * A delayed work item must not be modified until it has been processed
2353 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002354 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002355 * @note Can be called by ISRs.
2356 *
2357 * @param work_q Address of workqueue.
2358 * @param work Address of delayed work item.
2359 * @param delay Delay before submitting the work item (in milliseconds).
2360 *
2361 * @retval 0 Work item countdown started.
2362 * @retval -EINPROGRESS Work item is already pending.
2363 * @retval -EINVAL Work item is being processed or has completed its work.
2364 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002365 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002366extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2367 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002368 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002369
2370/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002371 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002372 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002373 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002374 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002375 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002376 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002377 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002378 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002379 * @param work Address of delayed work item.
2380 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002381 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002382 * @retval -EINPROGRESS Work item is already pending.
2383 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002384 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002385extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002386
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002387/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002388 * @brief Submit a work item to the system workqueue.
2389 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002390 * This routine submits work item @a work to be processed by the system
2391 * workqueue. If the work item is already pending in the workqueue's queue
2392 * as a result of an earlier submission, this routine has no effect on the
2393 * work item. If the work item has already been processed, or is currently
2394 * being processed, its work is considered complete and the work item can be
2395 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002396 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002397 * @warning
2398 * Work items submitted to the system workqueue should avoid using handlers
2399 * that block or yield since this may prevent the system workqueue from
2400 * processing other work items in a timely manner.
2401 *
2402 * @note Can be called by ISRs.
2403 *
2404 * @param work Address of work item.
2405 *
2406 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002407 */
2408static inline void k_work_submit(struct k_work *work)
2409{
2410 k_work_submit_to_queue(&k_sys_work_q, work);
2411}
2412
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002413/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002414 * @brief Submit a delayed work item to the system workqueue.
2415 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002416 * This routine schedules work item @a work to be processed by the system
2417 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002418 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002419 * Only when the countdown completes is the work item actually submitted to
2420 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002421 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002422 * Submitting a previously submitted delayed work item that is still
2423 * counting down cancels the existing submission and restarts the countdown
2424 * using the new delay. If the work item is currently pending on the
2425 * workqueue's queue because the countdown has completed it is too late to
2426 * resubmit the item, and resubmission fails without impacting the work item.
2427 * If the work item has already been processed, or is currently being processed,
2428 * its work is considered complete and the work item can be resubmitted.
2429 *
2430 * @warning
2431 * Work items submitted to the system workqueue should avoid using handlers
2432 * that block or yield since this may prevent the system workqueue from
2433 * processing other work items in a timely manner.
2434 *
2435 * @note Can be called by ISRs.
2436 *
2437 * @param work Address of delayed work item.
2438 * @param delay Delay before submitting the work item (in milliseconds).
2439 *
2440 * @retval 0 Work item countdown started.
2441 * @retval -EINPROGRESS Work item is already pending.
2442 * @retval -EINVAL Work item is being processed or has completed its work.
2443 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002444 */
2445static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002446 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002447{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002448 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002449}
2450
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002451/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002452 * @brief Get time remaining before a delayed work gets scheduled.
2453 *
2454 * This routine computes the (approximate) time remaining before a
2455 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002456 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002457 *
2458 * @param work Delayed work item.
2459 *
2460 * @return Remaining time (in milliseconds).
2461 */
Kumar Galacc334c72017-04-21 10:55:34 -05002462static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002463{
2464 return _timeout_remaining_get(&work->timeout);
2465}
2466
Anas Nashif166f5192018-02-25 08:02:36 -06002467/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002468
Allan Stephensc98da842016-11-11 15:45:03 -05002469/**
2470 * @cond INTERNAL_HIDDEN
2471 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002472
2473struct k_mutex {
2474 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002475 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002476 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002477 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002478
Anas Nashif2f203c22016-12-18 06:57:45 -05002479 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002480};
2481
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002482#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002483 { \
2484 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2485 .owner = NULL, \
2486 .lock_count = 0, \
2487 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002488 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002489 }
2490
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002491#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2492
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002493/**
Allan Stephensc98da842016-11-11 15:45:03 -05002494 * INTERNAL_HIDDEN @endcond
2495 */
2496
2497/**
2498 * @defgroup mutex_apis Mutex APIs
2499 * @ingroup kernel_apis
2500 * @{
2501 */
2502
2503/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002504 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002505 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002506 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002507 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002508 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002509 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002510 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002511 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002512#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002513 struct k_mutex name \
2514 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002515 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002516
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002517/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002518 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002519 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002520 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002521 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002522 * Upon completion, the mutex is available and does not have an owner.
2523 *
2524 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002525 *
2526 * @return N/A
2527 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002528__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002529
2530/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002531 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002532 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002533 * This routine locks @a mutex. If the mutex is locked by another thread,
2534 * the calling thread waits until the mutex becomes available or until
2535 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002536 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002537 * A thread is permitted to lock a mutex it has already locked. The operation
2538 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002539 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002540 * @param mutex Address of the mutex.
2541 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002542 * or one of the special values K_NO_WAIT and K_FOREVER.
2543 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002544 * @retval 0 Mutex locked.
2545 * @retval -EBUSY Returned without waiting.
2546 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002547 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002548__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002549
2550/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002551 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002552 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002553 * This routine unlocks @a mutex. The mutex must already be locked by the
2554 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002555 *
2556 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002557 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002558 * thread.
2559 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002560 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002561 *
2562 * @return N/A
2563 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002564__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002565
Allan Stephensc98da842016-11-11 15:45:03 -05002566/**
Anas Nashif166f5192018-02-25 08:02:36 -06002567 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002568 */
2569
2570/**
2571 * @cond INTERNAL_HIDDEN
2572 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002573
2574struct k_sem {
2575 _wait_q_t wait_q;
2576 unsigned int count;
2577 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002578 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002579
Anas Nashif2f203c22016-12-18 06:57:45 -05002580 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002581};
2582
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002583#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002584 { \
2585 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2586 .count = initial_count, \
2587 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002588 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002589 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002590 }
2591
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002592#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2593
Allan Stephensc98da842016-11-11 15:45:03 -05002594/**
2595 * INTERNAL_HIDDEN @endcond
2596 */
2597
2598/**
2599 * @defgroup semaphore_apis Semaphore APIs
2600 * @ingroup kernel_apis
2601 * @{
2602 */
2603
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002604/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002605 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002606 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002607 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002608 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002609 * @param sem Address of the semaphore.
2610 * @param initial_count Initial semaphore count.
2611 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002612 *
2613 * @return N/A
2614 */
Andrew Boie99280232017-09-29 14:17:47 -07002615__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2616 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002617
2618/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002619 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002620 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002621 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002622 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002623 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2624 *
2625 * @param sem Address of the semaphore.
2626 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002627 * or one of the special values K_NO_WAIT and K_FOREVER.
2628 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002629 * @note When porting code from the nanokernel legacy API to the new API, be
2630 * careful with the return value of this function. The return value is the
2631 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2632 * non-zero means failure, while the nano_sem_take family returns 1 for success
2633 * and 0 for failure.
2634 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002635 * @retval 0 Semaphore taken.
2636 * @retval -EBUSY Returned without waiting.
2637 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002638 */
Andrew Boie99280232017-09-29 14:17:47 -07002639__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002640
2641/**
2642 * @brief Give a semaphore.
2643 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002644 * This routine gives @a sem, unless the semaphore is already at its maximum
2645 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002646 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002647 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002648 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002649 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002650 *
2651 * @return N/A
2652 */
Andrew Boie99280232017-09-29 14:17:47 -07002653__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002654
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002655/**
2656 * @brief Reset a semaphore's count to zero.
2657 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002658 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002659 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002660 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002661 *
2662 * @return N/A
2663 */
Andrew Boie990bf162017-10-03 12:36:49 -07002664__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002665
Anas Nashif954d5502018-02-25 08:37:28 -06002666/**
2667 * @internal
2668 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002669static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002670{
2671 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002672}
2673
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002674/**
2675 * @brief Get a semaphore's count.
2676 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002677 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002678 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002679 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002680 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002681 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002682 */
Andrew Boie990bf162017-10-03 12:36:49 -07002683__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002684
Anas Nashif954d5502018-02-25 08:37:28 -06002685/**
2686 * @internal
2687 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002688static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002689{
2690 return sem->count;
2691}
2692
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002693/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002694 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002695 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002696 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002697 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002698 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002699 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002700 * @param name Name of the semaphore.
2701 * @param initial_count Initial semaphore count.
2702 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002703 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002704#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002705 struct k_sem name \
2706 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07002707 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05302708 BUILD_ASSERT(((count_limit) != 0) && \
2709 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002710
Anas Nashif166f5192018-02-25 08:02:36 -06002711/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002712
2713/**
2714 * @defgroup alert_apis Alert APIs
2715 * @ingroup kernel_apis
2716 * @{
2717 */
2718
Allan Stephens5eceb852016-11-16 10:16:30 -05002719/**
2720 * @typedef k_alert_handler_t
2721 * @brief Alert handler function type.
2722 *
2723 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002724 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002725 * and is only invoked if the alert has been initialized with one.
2726 *
2727 * @param alert Address of the alert.
2728 *
2729 * @return 0 if alert has been consumed; non-zero if alert should pend.
2730 */
2731typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002732
Anas Nashif166f5192018-02-25 08:02:36 -06002733/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002734
2735/**
2736 * @cond INTERNAL_HIDDEN
2737 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002738
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002739#define K_ALERT_DEFAULT NULL
2740#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002741
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002742struct k_alert {
2743 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002744 atomic_t send_count;
2745 struct k_work work_item;
2746 struct k_sem sem;
2747
Anas Nashif2f203c22016-12-18 06:57:45 -05002748 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002749};
2750
Anas Nashif954d5502018-02-25 08:37:28 -06002751/**
2752 * @internal
2753 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002754extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002755
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002756#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002757 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002758 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002759 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002760 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2761 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002762 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002763 }
2764
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002765#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2766
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002767/**
Allan Stephensc98da842016-11-11 15:45:03 -05002768 * INTERNAL_HIDDEN @endcond
2769 */
2770
2771/**
2772 * @addtogroup alert_apis
2773 * @{
2774 */
2775
2776/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002777 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002778 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002779 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002780 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002781 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002782 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002783 * @param name Name of the alert.
2784 * @param alert_handler Action to take when alert is sent. Specify either
2785 * the address of a function to be invoked by the system workqueue
2786 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2787 * K_ALERT_DEFAULT (which causes the alert to pend).
2788 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002789 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002790#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002791 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002792 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002793 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002794 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002795
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002796/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002797 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002799 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002800 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002801 * @param alert Address of the alert.
2802 * @param handler Action to take when alert is sent. Specify either the address
2803 * of a function to be invoked by the system workqueue thread,
2804 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2805 * K_ALERT_DEFAULT (which causes the alert to pend).
2806 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002807 *
2808 * @return N/A
2809 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002810extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2811 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002812
2813/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002814 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002815 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002816 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002817 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002818 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2819 *
2820 * @param alert Address of the alert.
2821 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002822 * or one of the special values K_NO_WAIT and K_FOREVER.
2823 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002824 * @retval 0 Alert received.
2825 * @retval -EBUSY Returned without waiting.
2826 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002827 */
Andrew Boie310e9872017-09-29 04:41:15 -07002828__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002829
2830/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002831 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002832 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002833 * This routine signals @a alert. The action specified for @a alert will
2834 * be taken, which may trigger the execution of an alert handler function
2835 * and/or cause the alert to pend (assuming the alert has not reached its
2836 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002837 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002838 * @note Can be called by ISRs.
2839 *
2840 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002841 *
2842 * @return N/A
2843 */
Andrew Boie310e9872017-09-29 04:41:15 -07002844__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002845
2846/**
Anas Nashif166f5192018-02-25 08:02:36 -06002847 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002848 */
2849
Allan Stephensc98da842016-11-11 15:45:03 -05002850/**
2851 * @cond INTERNAL_HIDDEN
2852 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002853
2854struct k_msgq {
2855 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002856 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002857 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002858 char *buffer_start;
2859 char *buffer_end;
2860 char *read_ptr;
2861 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002862 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002863
Anas Nashif2f203c22016-12-18 06:57:45 -05002864 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002865};
2866
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002867#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002868 { \
2869 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002870 .max_msgs = q_max_msgs, \
2871 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002872 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002873 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002874 .read_ptr = q_buffer, \
2875 .write_ptr = q_buffer, \
2876 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002877 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002878 }
2879
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002880#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2881
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05302882struct k_msgq_attrs {
2883 size_t msg_size;
2884 u32_t max_msgs;
2885 u32_t used_msgs;
2886};
2887
Peter Mitsis1da807e2016-10-06 11:36:59 -04002888/**
Allan Stephensc98da842016-11-11 15:45:03 -05002889 * INTERNAL_HIDDEN @endcond
2890 */
2891
2892/**
2893 * @defgroup msgq_apis Message Queue APIs
2894 * @ingroup kernel_apis
2895 * @{
2896 */
2897
2898/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002899 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002900 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002901 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2902 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002903 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2904 * message is similarly aligned to this boundary, @a q_msg_size must also be
2905 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002906 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002907 * The message queue can be accessed outside the module where it is defined
2908 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002909 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002910 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002911 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002912 * @param q_name Name of the message queue.
2913 * @param q_msg_size Message size (in bytes).
2914 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002915 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002916 */
2917#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2918 static char __noinit __aligned(q_align) \
2919 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002920 struct k_msgq q_name \
2921 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002922 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002923 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002924
Peter Mitsisd7a37502016-10-13 11:37:40 -04002925/**
2926 * @brief Initialize a message queue.
2927 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002928 * This routine initializes a message queue object, prior to its first use.
2929 *
Allan Stephensda827222016-11-09 14:23:58 -06002930 * The message queue's ring buffer must contain space for @a max_msgs messages,
2931 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2932 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2933 * that each message is similarly aligned to this boundary, @a q_msg_size
2934 * must also be a multiple of N.
2935 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002936 * @param q Address of the message queue.
2937 * @param buffer Pointer to ring buffer that holds queued messages.
2938 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002939 * @param max_msgs Maximum number of messages that can be queued.
2940 *
2941 * @return N/A
2942 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002943__syscall void k_msgq_init(struct k_msgq *q, char *buffer,
2944 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002945
2946/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002947 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002948 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002949 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002950 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002951 * @note Can be called by ISRs.
2952 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002953 * @param q Address of the message queue.
2954 * @param data Pointer to the message.
2955 * @param timeout Waiting period to add the message (in milliseconds),
2956 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002957 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002958 * @retval 0 Message sent.
2959 * @retval -ENOMSG Returned without waiting or queue purged.
2960 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002961 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002962__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002963
2964/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002965 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002966 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002967 * This routine receives a message from message queue @a q in a "first in,
2968 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002969 *
Allan Stephensc98da842016-11-11 15:45:03 -05002970 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002971 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002972 * @param q Address of the message queue.
2973 * @param data Address of area to hold the received message.
2974 * @param timeout Waiting period to receive the message (in milliseconds),
2975 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002976 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002977 * @retval 0 Message received.
2978 * @retval -ENOMSG Returned without waiting.
2979 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002980 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002981__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002982
2983/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002984 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002986 * This routine discards all unreceived messages in a message queue's ring
2987 * buffer. Any threads that are blocked waiting to send a message to the
2988 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002989 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002990 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002991 *
2992 * @return N/A
2993 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002994__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002995
Peter Mitsis67be2492016-10-07 11:44:34 -04002996/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002997 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002999 * This routine returns the number of unused entries in a message queue's
3000 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003001 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003002 * @param q Address of the message queue.
3003 *
3004 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04003005 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003006__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3007
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303008/**
3009 * @brief Get basic attributes of a message queue.
3010 *
3011 * This routine fetches basic attributes of message queue into attr argument.
3012 *
3013 * @param q Address of the message queue.
3014 * @param attrs pointer to message queue attribute structure.
3015 *
3016 * @return N/A
3017 */
3018__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3019
3020
Andrew Boie82edb6e2017-10-02 10:53:06 -07003021static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003022{
3023 return q->max_msgs - q->used_msgs;
3024}
3025
Peter Mitsisd7a37502016-10-13 11:37:40 -04003026/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003027 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003028 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003029 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003030 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003031 * @param q Address of the message queue.
3032 *
3033 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003034 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003035__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3036
3037static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003038{
3039 return q->used_msgs;
3040}
3041
Anas Nashif166f5192018-02-25 08:02:36 -06003042/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003043
3044/**
3045 * @defgroup mem_pool_apis Memory Pool APIs
3046 * @ingroup kernel_apis
3047 * @{
3048 */
3049
Andy Ross73cb9582017-05-09 10:42:39 -07003050/* Note on sizing: the use of a 20 bit field for block means that,
3051 * assuming a reasonable minimum block size of 16 bytes, we're limited
3052 * to 16M of memory managed by a single pool. Long term it would be
3053 * good to move to a variable bit size based on configuration.
3054 */
3055struct k_mem_block_id {
3056 u32_t pool : 8;
3057 u32_t level : 4;
3058 u32_t block : 20;
3059};
3060
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003061struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003062 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003063 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003064};
3065
Anas Nashif166f5192018-02-25 08:02:36 -06003066/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003067
3068/**
3069 * @defgroup mailbox_apis Mailbox APIs
3070 * @ingroup kernel_apis
3071 * @{
3072 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003073
3074struct k_mbox_msg {
3075 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003076 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003077 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003078 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003079 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003080 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003081 /** sender's message data buffer */
3082 void *tx_data;
3083 /** internal use only - needed for legacy API support */
3084 void *_rx_data;
3085 /** message data block descriptor */
3086 struct k_mem_block tx_block;
3087 /** source thread id */
3088 k_tid_t rx_source_thread;
3089 /** target thread id */
3090 k_tid_t tx_target_thread;
3091 /** internal use only - thread waiting on send (may be a dummy) */
3092 k_tid_t _syncing_thread;
3093#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3094 /** internal use only - semaphore used during asynchronous send */
3095 struct k_sem *_async_sem;
3096#endif
3097};
3098
Allan Stephensc98da842016-11-11 15:45:03 -05003099/**
3100 * @cond INTERNAL_HIDDEN
3101 */
3102
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003103struct k_mbox {
3104 _wait_q_t tx_msg_queue;
3105 _wait_q_t rx_msg_queue;
3106
Anas Nashif2f203c22016-12-18 06:57:45 -05003107 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003108};
3109
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003110#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003111 { \
3112 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3113 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003114 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003115 }
3116
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003117#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3118
Peter Mitsis12092702016-10-14 12:57:23 -04003119/**
Allan Stephensc98da842016-11-11 15:45:03 -05003120 * INTERNAL_HIDDEN @endcond
3121 */
3122
3123/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003124 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003125 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003126 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003127 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003128 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003129 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003130 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003131 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003132#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003133 struct k_mbox name \
3134 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003135 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003136
Peter Mitsis12092702016-10-14 12:57:23 -04003137/**
3138 * @brief Initialize a mailbox.
3139 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003140 * This routine initializes a mailbox object, prior to its first use.
3141 *
3142 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003143 *
3144 * @return N/A
3145 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003146extern void k_mbox_init(struct k_mbox *mbox);
3147
Peter Mitsis12092702016-10-14 12:57:23 -04003148/**
3149 * @brief Send a mailbox message in a synchronous manner.
3150 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003151 * This routine sends a message to @a mbox and waits for a receiver to both
3152 * receive and process it. The message data may be in a buffer, in a memory
3153 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003154 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003155 * @param mbox Address of the mailbox.
3156 * @param tx_msg Address of the transmit message descriptor.
3157 * @param timeout Waiting period for the message to be received (in
3158 * milliseconds), or one of the special values K_NO_WAIT
3159 * and K_FOREVER. Once the message has been received,
3160 * this routine waits as long as necessary for the message
3161 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003162 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003163 * @retval 0 Message sent.
3164 * @retval -ENOMSG Returned without waiting.
3165 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003166 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003167extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003168 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003169
Peter Mitsis12092702016-10-14 12:57:23 -04003170/**
3171 * @brief Send a mailbox message in an asynchronous manner.
3172 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003173 * This routine sends a message to @a mbox without waiting for a receiver
3174 * to process it. The message data may be in a buffer, in a memory pool block,
3175 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3176 * will be given when the message has been both received and completely
3177 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003178 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003179 * @param mbox Address of the mailbox.
3180 * @param tx_msg Address of the transmit message descriptor.
3181 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003182 *
3183 * @return N/A
3184 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003185extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003186 struct k_sem *sem);
3187
Peter Mitsis12092702016-10-14 12:57:23 -04003188/**
3189 * @brief Receive a mailbox message.
3190 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003191 * This routine receives a message from @a mbox, then optionally retrieves
3192 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003193 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003194 * @param mbox Address of the mailbox.
3195 * @param rx_msg Address of the receive message descriptor.
3196 * @param buffer Address of the buffer to receive data, or NULL to defer data
3197 * retrieval and message disposal until later.
3198 * @param timeout Waiting period for a message to be received (in
3199 * milliseconds), or one of the special values K_NO_WAIT
3200 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003201 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003202 * @retval 0 Message received.
3203 * @retval -ENOMSG Returned without waiting.
3204 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003205 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003206extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003207 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003208
3209/**
3210 * @brief Retrieve mailbox message data into a buffer.
3211 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003212 * This routine completes the processing of a received message by retrieving
3213 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003214 *
3215 * Alternatively, this routine can be used to dispose of a received message
3216 * without retrieving its data.
3217 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003218 * @param rx_msg Address of the receive message descriptor.
3219 * @param buffer Address of the buffer to receive data, or NULL to discard
3220 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003221 *
3222 * @return N/A
3223 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003224extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003225
3226/**
3227 * @brief Retrieve mailbox message data into a memory pool block.
3228 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003229 * This routine completes the processing of a received message by retrieving
3230 * its data into a memory pool block, then disposing of the message.
3231 * The memory pool block that results from successful retrieval must be
3232 * returned to the pool once the data has been processed, even in cases
3233 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003234 *
3235 * Alternatively, this routine can be used to dispose of a received message
3236 * without retrieving its data. In this case there is no need to return a
3237 * memory pool block to the pool.
3238 *
3239 * This routine allocates a new memory pool block for the data only if the
3240 * data is not already in one. If a new block cannot be allocated, the routine
3241 * returns a failure code and the received message is left unchanged. This
3242 * permits the caller to reattempt data retrieval at a later time or to dispose
3243 * of the received message without retrieving its data.
3244 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003245 * @param rx_msg Address of a receive message descriptor.
3246 * @param pool Address of memory pool, or NULL to discard data.
3247 * @param block Address of the area to hold memory pool block info.
3248 * @param timeout Waiting period to wait for a memory pool block (in
3249 * milliseconds), or one of the special values K_NO_WAIT
3250 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003251 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003252 * @retval 0 Data retrieved.
3253 * @retval -ENOMEM Returned without waiting.
3254 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003255 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003256extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003257 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003258 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003259
Anas Nashif166f5192018-02-25 08:02:36 -06003260/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003261
3262/**
3263 * @cond INTERNAL_HIDDEN
3264 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003265
3266struct k_pipe {
3267 unsigned char *buffer; /* Pipe buffer: may be NULL */
3268 size_t size; /* Buffer size */
3269 size_t bytes_used; /* # bytes used in buffer */
3270 size_t read_index; /* Where in buffer to read from */
3271 size_t write_index; /* Where in buffer to write */
3272
3273 struct {
3274 _wait_q_t readers; /* Reader wait queue */
3275 _wait_q_t writers; /* Writer wait queue */
3276 } wait_q;
3277
Anas Nashif2f203c22016-12-18 06:57:45 -05003278 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003279};
3280
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003281#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003282 { \
3283 .buffer = pipe_buffer, \
3284 .size = pipe_buffer_size, \
3285 .bytes_used = 0, \
3286 .read_index = 0, \
3287 .write_index = 0, \
3288 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3289 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003290 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003291 }
3292
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003293#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3294
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003295/**
Allan Stephensc98da842016-11-11 15:45:03 -05003296 * INTERNAL_HIDDEN @endcond
3297 */
3298
3299/**
3300 * @defgroup pipe_apis Pipe APIs
3301 * @ingroup kernel_apis
3302 * @{
3303 */
3304
3305/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003306 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003307 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003308 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003309 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003310 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003311 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003312 * @param name Name of the pipe.
3313 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3314 * or zero if no ring buffer is used.
3315 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003316 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003317#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3318 static unsigned char __noinit __aligned(pipe_align) \
3319 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003320 struct k_pipe name \
3321 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003322 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003323
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003324/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003325 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003326 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003327 * This routine initializes a pipe object, prior to its first use.
3328 *
3329 * @param pipe Address of the pipe.
3330 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3331 * is used.
3332 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3333 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003334 *
3335 * @return N/A
3336 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003337__syscall void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3338 size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003339
3340/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003341 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003342 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003343 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003344 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003345 * @param pipe Address of the pipe.
3346 * @param data Address of data to write.
3347 * @param bytes_to_write Size of data (in bytes).
3348 * @param bytes_written Address of area to hold the number of bytes written.
3349 * @param min_xfer Minimum number of bytes to write.
3350 * @param timeout Waiting period to wait for the data to be written (in
3351 * milliseconds), or one of the special values K_NO_WAIT
3352 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003353 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003354 * @retval 0 At least @a min_xfer bytes of data were written.
3355 * @retval -EIO Returned without waiting; zero data bytes were written.
3356 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003357 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003358 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003359__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3360 size_t bytes_to_write, size_t *bytes_written,
3361 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003362
3363/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003364 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003365 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003366 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003367 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003368 * @param pipe Address of the pipe.
3369 * @param data Address to place the data read from pipe.
3370 * @param bytes_to_read Maximum number of data bytes to read.
3371 * @param bytes_read Address of area to hold the number of bytes read.
3372 * @param min_xfer Minimum number of data bytes to read.
3373 * @param timeout Waiting period to wait for the data to be read (in
3374 * milliseconds), or one of the special values K_NO_WAIT
3375 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003376 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003377 * @retval 0 At least @a min_xfer bytes of data were read.
3378 * @retval -EIO Returned without waiting; zero data bytes were read.
3379 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003380 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003381 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003382__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3383 size_t bytes_to_read, size_t *bytes_read,
3384 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003385
3386/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003387 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003388 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003389 * This routine writes the data contained in a memory block to @a pipe.
3390 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003391 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003392 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003393 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003394 * @param block Memory block containing data to send
3395 * @param size Number of data bytes in memory block to send
3396 * @param sem Semaphore to signal upon completion (else NULL)
3397 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003398 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003399 */
3400extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3401 size_t size, struct k_sem *sem);
3402
Anas Nashif166f5192018-02-25 08:02:36 -06003403/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003404
Allan Stephensc98da842016-11-11 15:45:03 -05003405/**
3406 * @cond INTERNAL_HIDDEN
3407 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003408
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003409struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003410 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003411 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003412 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003413 char *buffer;
3414 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003415 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003416
Anas Nashif2f203c22016-12-18 06:57:45 -05003417 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003418};
3419
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003420#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003421 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003422 { \
3423 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003424 .num_blocks = slab_num_blocks, \
3425 .block_size = slab_block_size, \
3426 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003427 .free_list = NULL, \
3428 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003429 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003430 }
3431
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003432#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3433
3434
Peter Mitsis578f9112016-10-07 13:50:31 -04003435/**
Allan Stephensc98da842016-11-11 15:45:03 -05003436 * INTERNAL_HIDDEN @endcond
3437 */
3438
3439/**
3440 * @defgroup mem_slab_apis Memory Slab APIs
3441 * @ingroup kernel_apis
3442 * @{
3443 */
3444
3445/**
Allan Stephensda827222016-11-09 14:23:58 -06003446 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003447 *
Allan Stephensda827222016-11-09 14:23:58 -06003448 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003449 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003450 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3451 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003452 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003453 *
Allan Stephensda827222016-11-09 14:23:58 -06003454 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003455 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003456 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003457 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003458 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003459 * @param name Name of the memory slab.
3460 * @param slab_block_size Size of each memory block (in bytes).
3461 * @param slab_num_blocks Number memory blocks.
3462 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003463 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003464#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3465 char __noinit __aligned(slab_align) \
3466 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3467 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003468 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003469 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003470 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003471
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003472/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003473 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003474 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003475 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003476 *
Allan Stephensda827222016-11-09 14:23:58 -06003477 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3478 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3479 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3480 * To ensure that each memory block is similarly aligned to this boundary,
3481 * @a slab_block_size must also be a multiple of N.
3482 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003483 * @param slab Address of the memory slab.
3484 * @param buffer Pointer to buffer used for the memory blocks.
3485 * @param block_size Size of each memory block (in bytes).
3486 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003487 *
3488 * @return N/A
3489 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003490extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003491 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003492
3493/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003494 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003495 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003496 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003497 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003498 * @param slab Address of the memory slab.
3499 * @param mem Pointer to block address area.
3500 * @param timeout Maximum time to wait for operation to complete
3501 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3502 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003503 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003504 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003505 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003506 * @retval -ENOMEM Returned without waiting.
3507 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003508 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003509extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003510 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003511
3512/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003513 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003514 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003515 * This routine releases a previously allocated memory block back to its
3516 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003517 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003518 * @param slab Address of the memory slab.
3519 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003520 *
3521 * @return N/A
3522 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003523extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003524
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003525/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003526 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003527 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003528 * This routine gets the number of memory blocks that are currently
3529 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003530 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003531 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003532 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003533 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003534 */
Kumar Galacc334c72017-04-21 10:55:34 -05003535static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003536{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003537 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003538}
3539
Peter Mitsisc001aa82016-10-13 13:53:37 -04003540/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003541 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003542 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003543 * This routine gets the number of memory blocks that are currently
3544 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003545 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003546 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003547 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003548 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003549 */
Kumar Galacc334c72017-04-21 10:55:34 -05003550static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003551{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003552 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003553}
3554
Anas Nashif166f5192018-02-25 08:02:36 -06003555/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003556
3557/**
3558 * @cond INTERNAL_HIDDEN
3559 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003560
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003561struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003562 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003563 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003564};
3565
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003566/**
Allan Stephensc98da842016-11-11 15:45:03 -05003567 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003568 */
3569
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003570/**
Allan Stephensc98da842016-11-11 15:45:03 -05003571 * @addtogroup mem_pool_apis
3572 * @{
3573 */
3574
3575/**
3576 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003577 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003578 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3579 * long. The memory pool allows blocks to be repeatedly partitioned into
3580 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003581 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003582 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003583 * If the pool is to be accessed outside the module where it is defined, it
3584 * can be declared via
3585 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003586 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003587 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003588 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003589 * @param minsz Size of the smallest blocks in the pool (in bytes).
3590 * @param maxsz Size of the largest blocks in the pool (in bytes).
3591 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003592 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003593 */
Andy Ross73cb9582017-05-09 10:42:39 -07003594#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3595 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3596 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08003597 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07003598 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08003599 .base = { \
3600 .buf = _mpool_buf_##name, \
3601 .max_sz = maxsz, \
3602 .n_max = nmax, \
3603 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3604 .levels = _mpool_lvls_##name, \
3605 .flags = SYS_MEM_POOL_KERNEL \
3606 } \
Andy Ross73cb9582017-05-09 10:42:39 -07003607 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003608
Peter Mitsis937042c2016-10-13 13:18:26 -04003609/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003610 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003611 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003612 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003613 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003614 * @param pool Address of the memory pool.
3615 * @param block Pointer to block descriptor for the allocated memory.
3616 * @param size Amount of memory to allocate (in bytes).
3617 * @param timeout Maximum time to wait for operation to complete
3618 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3619 * or K_FOREVER to wait as long as necessary.
3620 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003621 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003622 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003623 * @retval -ENOMEM Returned without waiting.
3624 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003625 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003626extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003627 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003628
3629/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003630 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003631 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003632 * This routine releases a previously allocated memory block back to its
3633 * memory pool.
3634 *
3635 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003636 *
3637 * @return N/A
3638 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003639extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003640
3641/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003642 * @brief Free memory allocated from a memory pool.
3643 *
3644 * This routine releases a previously allocated memory block back to its
3645 * memory pool
3646 *
3647 * @param id Memory block identifier.
3648 *
3649 * @return N/A
3650 */
3651extern void k_mem_pool_free_id(struct k_mem_block_id *id);
3652
3653/**
Anas Nashif166f5192018-02-25 08:02:36 -06003654 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003655 */
3656
3657/**
3658 * @defgroup heap_apis Heap Memory Pool APIs
3659 * @ingroup kernel_apis
3660 * @{
3661 */
3662
3663/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003664 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003665 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003666 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003667 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003668 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003669 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003670 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003671 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003672 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003673extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003674
3675/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003676 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003677 *
3678 * This routine provides traditional free() semantics. The memory being
3679 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003680 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003681 * If @a ptr is NULL, no operation is performed.
3682 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003683 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003684 *
3685 * @return N/A
3686 */
3687extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003688
Allan Stephensc98da842016-11-11 15:45:03 -05003689/**
Andrew Boie7f95e832017-11-08 14:40:01 -08003690 * @brief Allocate memory from heap, array style
3691 *
3692 * This routine provides traditional calloc() semantics. Memory is
3693 * allocated from the heap memory pool and zeroed.
3694 *
3695 * @param nmemb Number of elements in the requested array
3696 * @param size Size of each array element (in bytes).
3697 *
3698 * @return Address of the allocated memory if successful; otherwise NULL.
3699 */
3700extern void *k_calloc(size_t nmemb, size_t size);
3701
Anas Nashif166f5192018-02-25 08:02:36 -06003702/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003703
Benjamin Walshacc68c12017-01-29 18:57:45 -05003704/* polling API - PRIVATE */
3705
Benjamin Walshb0179862017-02-02 16:39:57 -05003706#ifdef CONFIG_POLL
3707#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3708#else
3709#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3710#endif
3711
Benjamin Walshacc68c12017-01-29 18:57:45 -05003712/* private - implementation data created as needed, per-type */
3713struct _poller {
3714 struct k_thread *thread;
3715};
3716
3717/* private - types bit positions */
3718enum _poll_types_bits {
3719 /* can be used to ignore an event */
3720 _POLL_TYPE_IGNORE,
3721
3722 /* to be signaled by k_poll_signal() */
3723 _POLL_TYPE_SIGNAL,
3724
3725 /* semaphore availability */
3726 _POLL_TYPE_SEM_AVAILABLE,
3727
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003728 /* queue/fifo/lifo data availability */
3729 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003730
3731 _POLL_NUM_TYPES
3732};
3733
3734#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3735
3736/* private - states bit positions */
3737enum _poll_states_bits {
3738 /* default state when creating event */
3739 _POLL_STATE_NOT_READY,
3740
Benjamin Walshacc68c12017-01-29 18:57:45 -05003741 /* signaled by k_poll_signal() */
3742 _POLL_STATE_SIGNALED,
3743
3744 /* semaphore is available */
3745 _POLL_STATE_SEM_AVAILABLE,
3746
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003747 /* data is available to read on queue/fifo/lifo */
3748 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003749
3750 _POLL_NUM_STATES
3751};
3752
3753#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3754
3755#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003756 (32 - (0 \
3757 + 8 /* tag */ \
3758 + _POLL_NUM_TYPES \
3759 + _POLL_NUM_STATES \
3760 + 1 /* modes */ \
3761 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003762
Benjamin Walshacc68c12017-01-29 18:57:45 -05003763/* end of polling API - PRIVATE */
3764
3765
3766/**
3767 * @defgroup poll_apis Async polling APIs
3768 * @ingroup kernel_apis
3769 * @{
3770 */
3771
3772/* Public polling API */
3773
3774/* public - values for k_poll_event.type bitfield */
3775#define K_POLL_TYPE_IGNORE 0
3776#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3777#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003778#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3779#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003780
3781/* public - polling modes */
3782enum k_poll_modes {
3783 /* polling thread does not take ownership of objects when available */
3784 K_POLL_MODE_NOTIFY_ONLY = 0,
3785
3786 K_POLL_NUM_MODES
3787};
3788
3789/* public - values for k_poll_event.state bitfield */
3790#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003791#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3792#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003793#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3794#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003795
3796/* public - poll signal object */
3797struct k_poll_signal {
3798 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003799 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003800
3801 /*
3802 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3803 * user resets it to 0.
3804 */
3805 unsigned int signaled;
3806
3807 /* custom result value passed to k_poll_signal() if needed */
3808 int result;
3809};
3810
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003811#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003812 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003813 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003814 .signaled = 0, \
3815 .result = 0, \
3816 }
3817
3818struct k_poll_event {
3819 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003820 sys_dnode_t _node;
3821
3822 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003823 struct _poller *poller;
3824
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003825 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003826 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003827
Benjamin Walshacc68c12017-01-29 18:57:45 -05003828 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003829 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003830
3831 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003832 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003833
3834 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003835 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003836
3837 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003838 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003839
3840 /* per-type data */
3841 union {
3842 void *obj;
3843 struct k_poll_signal *signal;
3844 struct k_sem *sem;
3845 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003846 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003847 };
3848};
3849
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003850#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003851 { \
3852 .poller = NULL, \
3853 .type = event_type, \
3854 .state = K_POLL_STATE_NOT_READY, \
3855 .mode = event_mode, \
3856 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003857 { .obj = event_obj }, \
3858 }
3859
3860#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3861 event_tag) \
3862 { \
3863 .type = event_type, \
3864 .tag = event_tag, \
3865 .state = K_POLL_STATE_NOT_READY, \
3866 .mode = event_mode, \
3867 .unused = 0, \
3868 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003869 }
3870
3871/**
3872 * @brief Initialize one struct k_poll_event instance
3873 *
3874 * After this routine is called on a poll event, the event it ready to be
3875 * placed in an event array to be passed to k_poll().
3876 *
3877 * @param event The event to initialize.
3878 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3879 * values. Only values that apply to the same object being polled
3880 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3881 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003882 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003883 * @param obj Kernel object or poll signal.
3884 *
3885 * @return N/A
3886 */
3887
Kumar Galacc334c72017-04-21 10:55:34 -05003888extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003889 int mode, void *obj);
3890
3891/**
3892 * @brief Wait for one or many of multiple poll events to occur
3893 *
3894 * This routine allows a thread to wait concurrently for one or many of
3895 * multiple poll events to have occurred. Such events can be a kernel object
3896 * being available, like a semaphore, or a poll signal event.
3897 *
3898 * When an event notifies that a kernel object is available, the kernel object
3899 * is not "given" to the thread calling k_poll(): it merely signals the fact
3900 * that the object was available when the k_poll() call was in effect. Also,
3901 * all threads trying to acquire an object the regular way, i.e. by pending on
3902 * the object, have precedence over the thread polling on the object. This
3903 * means that the polling thread will never get the poll event on an object
3904 * until the object becomes available and its pend queue is empty. For this
3905 * reason, the k_poll() call is more effective when the objects being polled
3906 * only have one thread, the polling thread, trying to acquire them.
3907 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003908 * When k_poll() returns 0, the caller should loop on all the events that were
3909 * passed to k_poll() and check the state field for the values that were
3910 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003911 *
3912 * Before being reused for another call to k_poll(), the user has to reset the
3913 * state field to K_POLL_STATE_NOT_READY.
3914 *
3915 * @param events An array of pointers to events to be polled for.
3916 * @param num_events The number of events in the array.
3917 * @param timeout Waiting period for an event to be ready (in milliseconds),
3918 * or one of the special values K_NO_WAIT and K_FOREVER.
3919 *
3920 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003921 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02003922 * @retval -EINTR Poller thread has been interrupted.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003923 */
3924
3925extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003926 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003927
3928/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003929 * @brief Initialize a poll signal object.
3930 *
3931 * Ready a poll signal object to be signaled via k_poll_signal().
3932 *
3933 * @param signal A poll signal.
3934 *
3935 * @return N/A
3936 */
3937
3938extern void k_poll_signal_init(struct k_poll_signal *signal);
3939
3940/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003941 * @brief Signal a poll signal object.
3942 *
3943 * This routine makes ready a poll signal, which is basically a poll event of
3944 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3945 * made ready to run. A @a result value can be specified.
3946 *
3947 * The poll signal contains a 'signaled' field that, when set by
3948 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3949 * be reset by the user before being passed again to k_poll() or k_poll() will
3950 * consider it being signaled, and will return immediately.
3951 *
3952 * @param signal A poll signal.
3953 * @param result The value to store in the result field of the signal.
3954 *
3955 * @retval 0 The signal was delivered successfully.
3956 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3957 */
3958
3959extern int k_poll_signal(struct k_poll_signal *signal, int result);
3960
Anas Nashif954d5502018-02-25 08:37:28 -06003961/**
3962 * @internal
3963 */
Andy Ross8606fab2018-03-26 10:54:40 -07003964extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003965
Anas Nashif166f5192018-02-25 08:02:36 -06003966/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003967
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003968/**
3969 * @brief Make the CPU idle.
3970 *
3971 * This function makes the CPU idle until an event wakes it up.
3972 *
3973 * In a regular system, the idle thread should be the only thread responsible
3974 * for making the CPU idle and triggering any type of power management.
3975 * However, in some more constrained systems, such as a single-threaded system,
3976 * the only thread would be responsible for this if needed.
3977 *
3978 * @return N/A
3979 */
3980extern void k_cpu_idle(void);
3981
3982/**
3983 * @brief Make the CPU idle in an atomic fashion.
3984 *
3985 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3986 * must be done atomically before making the CPU idle.
3987 *
3988 * @param key Interrupt locking key obtained from irq_lock().
3989 *
3990 * @return N/A
3991 */
3992extern void k_cpu_atomic_idle(unsigned int key);
3993
Anas Nashif954d5502018-02-25 08:37:28 -06003994
3995/**
3996 * @internal
3997 */
Kumar Galacc334c72017-04-21 10:55:34 -05003998extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003999
Andrew Boiecdb94d62017-04-18 15:22:05 -07004000#ifdef _ARCH_EXCEPT
4001/* This archtecture has direct support for triggering a CPU exception */
4002#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4003#else
4004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004005/* NOTE: This is the implementation for arches that do not implement
4006 * _ARCH_EXCEPT() to generate a real CPU exception.
4007 *
4008 * We won't have a real exception frame to determine the PC value when
4009 * the oops occurred, so print file and line number before we jump into
4010 * the fatal error handler.
4011 */
4012#define _k_except_reason(reason) do { \
4013 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4014 _NanoFatalErrorHandler(reason, &_default_esf); \
4015 CODE_UNREACHABLE; \
4016 } while (0)
4017
4018#endif /* _ARCH__EXCEPT */
4019
4020/**
4021 * @brief Fatally terminate a thread
4022 *
4023 * This should be called when a thread has encountered an unrecoverable
4024 * runtime condition and needs to terminate. What this ultimately
4025 * means is determined by the _fatal_error_handler() implementation, which
4026 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4027 *
4028 * If this is called from ISR context, the default system fatal error handler
4029 * will treat it as an unrecoverable system error, just like k_panic().
4030 */
4031#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4032
4033/**
4034 * @brief Fatally terminate the system
4035 *
4036 * This should be called when the Zephyr kernel has encountered an
4037 * unrecoverable runtime condition and needs to terminate. What this ultimately
4038 * means is determined by the _fatal_error_handler() implementation, which
4039 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4040 */
4041#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4042
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004043/*
4044 * private APIs that are utilized by one or more public APIs
4045 */
4046
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004047#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004048/**
4049 * @internal
4050 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004051extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004052#else
Anas Nashif954d5502018-02-25 08:37:28 -06004053/**
4054 * @internal
4055 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004056#define _init_static_threads() do { } while ((0))
4057#endif
4058
Anas Nashif954d5502018-02-25 08:37:28 -06004059/**
4060 * @internal
4061 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004062extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004063/**
4064 * @internal
4065 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004066extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004067
Andrew Boiedc5d9352017-06-02 12:56:47 -07004068/* arch/cpu.h may declare an architecture or platform-specific macro
4069 * for properly declaring stacks, compatible with MMU/MPU constraints if
4070 * enabled
4071 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004072
4073/**
4074 * @brief Obtain an extern reference to a stack
4075 *
4076 * This macro properly brings the symbol of a thread stack declared
4077 * elsewhere into scope.
4078 *
4079 * @param sym Thread stack symbol name
4080 */
4081#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4082
Andrew Boiedc5d9352017-06-02 12:56:47 -07004083#ifdef _ARCH_THREAD_STACK_DEFINE
4084#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4085#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4086 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4087#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4088#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004089static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004090{
4091 return _ARCH_THREAD_STACK_BUFFER(sym);
4092}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004093#else
4094/**
4095 * @brief Declare a toplevel thread stack memory region
4096 *
4097 * This declares a region of memory suitable for use as a thread's stack.
4098 *
4099 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4100 * 'noinit' section so that it isn't zeroed at boot
4101 *
Andrew Boie507852a2017-07-25 18:47:07 -07004102 * The declared symbol will always be a k_thread_stack_t which can be passed to
4103 * k_thread_create, but should otherwise not be manipulated. If the buffer
4104 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004105 *
4106 * It is legal to precede this definition with the 'static' keyword.
4107 *
4108 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4109 * parameter of k_thread_create(), it may not be the same as the
4110 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4111 *
4112 * @param sym Thread stack symbol name
4113 * @param size Size of the stack memory region
4114 */
4115#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004116 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004117
4118/**
4119 * @brief Declare a toplevel array of thread stack memory regions
4120 *
4121 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4122 * definition for additional details and constraints.
4123 *
4124 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4125 * 'noinit' section so that it isn't zeroed at boot
4126 *
4127 * @param sym Thread stack symbol name
4128 * @param nmemb Number of stacks to declare
4129 * @param size Size of the stack memory region
4130 */
4131
4132#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004133 struct _k_thread_stack_element __noinit \
4134 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004135
4136/**
4137 * @brief Declare an embedded stack memory region
4138 *
4139 * Used for stacks embedded within other data structures. Use is highly
4140 * discouraged but in some cases necessary. For memory protection scenarios,
4141 * it is very important that any RAM preceding this member not be writable
4142 * by threads else a stack overflow will lead to silent corruption. In other
4143 * words, the containing data structure should live in RAM owned by the kernel.
4144 *
4145 * @param sym Thread stack symbol name
4146 * @param size Size of the stack memory region
4147 */
4148#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004149 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004150
4151/**
4152 * @brief Return the size in bytes of a stack memory region
4153 *
4154 * Convenience macro for passing the desired stack size to k_thread_create()
4155 * since the underlying implementation may actually create something larger
4156 * (for instance a guard area).
4157 *
4158 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004159 * passed to K_THREAD_STACK_DEFINE.
4160 *
4161 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4162 * it is not guaranteed to return the original value since each array
4163 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004164 *
4165 * @param sym Stack memory symbol
4166 * @return Size of the stack
4167 */
4168#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4169
4170/**
4171 * @brief Get a pointer to the physical stack buffer
4172 *
4173 * Convenience macro to get at the real underlying stack buffer used by
4174 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4175 * This is really only intended for diagnostic tools which want to examine
4176 * stack memory contents.
4177 *
4178 * @param sym Declared stack symbol name
4179 * @return The buffer itself, a char *
4180 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004181static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004182{
4183 return (char *)sym;
4184}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004185
4186#endif /* _ARCH_DECLARE_STACK */
4187
Chunlin Hane9c97022017-07-07 20:29:30 +08004188/**
4189 * @defgroup mem_domain_apis Memory domain APIs
4190 * @ingroup kernel_apis
4191 * @{
4192 */
4193
4194/** @def MEM_PARTITION_ENTRY
4195 * @brief Used to declare a memory partition entry
4196 */
4197#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4198 {\
4199 .start = _start, \
4200 .size = _size, \
4201 .attr = _attr, \
4202 }
4203
4204/** @def K_MEM_PARTITION_DEFINE
4205 * @brief Used to declare a memory partition
4206 */
4207#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4208#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4209 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304210 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004211 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4212#else
4213#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304214 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004215 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4216#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4217
Chunlin Hane9c97022017-07-07 20:29:30 +08004218/* memory partition */
4219struct k_mem_partition {
4220 /* start address of memory partition */
4221 u32_t start;
4222 /* size of memory partition */
4223 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304224#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004225 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304226 k_mem_partition_attr_t attr;
4227#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004228};
4229
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304230/* memory domian
4231 * Note: Always declare this structure with __kernel prefix
4232 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004233struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304234#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004235 /* partitions in the domain */
4236 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304237#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004238 /* domain q */
4239 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004240 /* number of partitions in the domain */
4241 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004242};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304243
Chunlin Hane9c97022017-07-07 20:29:30 +08004244
4245/**
4246 * @brief Initialize a memory domain.
4247 *
4248 * Initialize a memory domain with given name and memory partitions.
4249 *
4250 * @param domain The memory domain to be initialized.
4251 * @param num_parts The number of array items of "parts" parameter.
4252 * @param parts An array of pointers to the memory partitions. Can be NULL
4253 * if num_parts is zero.
4254 */
4255
Leandro Pereira08de6582018-02-28 14:22:57 -08004256extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004257 struct k_mem_partition *parts[]);
4258/**
4259 * @brief Destroy a memory domain.
4260 *
4261 * Destroy a memory domain.
4262 *
4263 * @param domain The memory domain to be destroyed.
4264 */
4265
4266extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4267
4268/**
4269 * @brief Add a memory partition into a memory domain.
4270 *
4271 * Add a memory partition into a memory domain.
4272 *
4273 * @param domain The memory domain to be added a memory partition.
4274 * @param part The memory partition to be added
4275 */
4276
4277extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4278 struct k_mem_partition *part);
4279
4280/**
4281 * @brief Remove a memory partition from a memory domain.
4282 *
4283 * Remove a memory partition from a memory domain.
4284 *
4285 * @param domain The memory domain to be removed a memory partition.
4286 * @param part The memory partition to be removed
4287 */
4288
4289extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4290 struct k_mem_partition *part);
4291
4292/**
4293 * @brief Add a thread into a memory domain.
4294 *
4295 * Add a thread into a memory domain.
4296 *
4297 * @param domain The memory domain that the thread is going to be added into.
4298 * @param thread ID of thread going to be added into the memory domain.
4299 */
4300
4301extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4302 k_tid_t thread);
4303
4304/**
4305 * @brief Remove a thread from its memory domain.
4306 *
4307 * Remove a thread from its memory domain.
4308 *
4309 * @param thread ID of thread going to be removed from its memory domain.
4310 */
4311
4312extern void k_mem_domain_remove_thread(k_tid_t thread);
4313
Anas Nashif166f5192018-02-25 08:02:36 -06004314/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004315
Andrew Boie756f9072017-10-10 16:01:49 -07004316/**
4317 * @brief Emit a character buffer to the console device
4318 *
4319 * @param c String of characters to print
4320 * @param n The length of the string
4321 */
4322__syscall void k_str_out(char *c, size_t n);
4323
Andy Rosse7172672018-01-24 15:48:32 -08004324/**
4325 * @brief Start a numbered CPU on a MP-capable system
4326
4327 * This starts and initializes a specific CPU. The main thread on
4328 * startup is running on CPU zero, other processors are numbered
4329 * sequentially. On return from this function, the CPU is known to
4330 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004331 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004332 * with the provided key will work to enable them.
4333 *
4334 * Normally, in SMP mode this function will be called by the kernel
4335 * initialization and should not be used as a user API. But it is
4336 * defined here for special-purpose apps which want Zephyr running on
4337 * one core and to use others for design-specific processing.
4338 *
4339 * @param cpu_num Integer number of the CPU
4340 * @param stack Stack memory for the CPU
4341 * @param sz Stack buffer size, in bytes
4342 * @param fn Function to begin running on the CPU. First argument is
4343 * an irq_unlock() key.
4344 * @param arg Untyped argument to be passed to "fn"
4345 */
4346extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4347 void (*fn)(int, void *), void *arg);
4348
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004349#ifdef __cplusplus
4350}
4351#endif
4352
Andrew Boiee004dec2016-11-07 09:01:19 -08004353#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4354/*
4355 * Define new and delete operators.
4356 * At this moment, the operators do nothing since objects are supposed
4357 * to be statically allocated.
4358 */
4359inline void operator delete(void *ptr)
4360{
4361 (void)ptr;
4362}
4363
4364inline void operator delete[](void *ptr)
4365{
4366 (void)ptr;
4367}
4368
4369inline void *operator new(size_t size)
4370{
4371 (void)size;
4372 return NULL;
4373}
4374
4375inline void *operator new[](size_t size)
4376{
4377 (void)size;
4378 return NULL;
4379}
4380
4381/* Placement versions of operator new and delete */
4382inline void operator delete(void *ptr1, void *ptr2)
4383{
4384 (void)ptr1;
4385 (void)ptr2;
4386}
4387
4388inline void operator delete[](void *ptr1, void *ptr2)
4389{
4390 (void)ptr1;
4391 (void)ptr2;
4392}
4393
4394inline void *operator new(size_t size, void *ptr)
4395{
4396 (void)size;
4397 return ptr;
4398}
4399
4400inline void *operator new[](size_t size, void *ptr)
4401{
4402 (void)size;
4403 return ptr;
4404}
4405
4406#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4407
Andrew Boiefa94ee72017-09-28 16:54:35 -07004408#include <syscalls/kernel.h>
4409
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004410#endif /* !_ASMLANGUAGE */
4411
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004412#endif /* _kernel__h_ */