blob: aba1d7b9553a26947af049a10c1dd72ae4fb6695 [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @file
9 *
10 * @brief Public kernel APIs.
11 */
12
13#ifndef _kernel__h_
14#define _kernel__h_
15
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050016#if !defined(_ASMLANGUAGE)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040017#include <stddef.h>
Kumar Gala78908162017-04-19 10:32:08 -050018#include <zephyr/types.h>
Anas Nashif173902f2017-01-17 07:08:56 -050019#include <limits.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020#include <toolchain.h>
Anas Nashif397d29d2017-06-17 11:30:47 -040021#include <linker/sections.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040022#include <atomic.h>
23#include <errno.h>
24#include <misc/__assert.h>
25#include <misc/dlist.h>
26#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050027#include <misc/util.h>
Andrew Boieaa6de292018-03-06 17:12:37 -080028#include <misc/mempool_base.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050029#include <kernel_version.h>
Leandro Pereiraadce1d12017-10-13 15:45:02 -070030#include <random/rand32.h>
Andrew Boie73abd322017-04-04 13:19:13 -070031#include <kernel_arch_thread.h>
Andrew Boie13ca6fe2017-09-23 12:05:49 -070032#include <syscall.h>
Andrew Boie43263fc2017-11-02 11:07:31 -070033#include <misc/printk.h>
34#include <arch/cpu.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040035
36#ifdef __cplusplus
37extern "C" {
38#endif
39
Anas Nashifbbb157d2017-01-15 08:46:31 -050040/**
41 * @brief Kernel APIs
42 * @defgroup kernel_apis Kernel APIs
43 * @{
44 * @}
45 */
46
Anas Nashif61f4b242016-11-18 10:53:59 -050047#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040048#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
49#else
50#define K_DEBUG(fmt, ...)
51#endif
52
Benjamin Walsh2f280412017-01-14 19:23:46 -050053#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
54#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
55#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
56#elif defined(CONFIG_COOP_ENABLED)
57#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
58#define _NUM_PREEMPT_PRIO (0)
59#elif defined(CONFIG_PREEMPT_ENABLED)
60#define _NUM_COOP_PRIO (0)
61#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
62#else
63#error "invalid configuration"
64#endif
65
66#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040067#define K_PRIO_PREEMPT(x) (x)
68
Benjamin Walsh456c6da2016-09-02 18:55:39 -040069#define K_ANY NULL
70#define K_END NULL
71
Benjamin Walshedb35702017-01-14 18:47:22 -050072#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040073#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050074#elif defined(CONFIG_COOP_ENABLED)
75#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
76#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040077#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050078#else
79#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040080#endif
81
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050082#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040083#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
84#else
85#define K_LOWEST_THREAD_PRIO -1
86#endif
87
Benjamin Walshfab8d922016-11-08 15:36:36 -050088#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
89
Benjamin Walsh456c6da2016-09-02 18:55:39 -040090#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
91#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
92
93typedef sys_dlist_t _wait_q_t;
94
Anas Nashif2f203c22016-12-18 06:57:45 -050095#ifdef CONFIG_OBJECT_TRACING
96#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
97#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040098#else
Anas Nashif2f203c22016-12-18 06:57:45 -050099#define _OBJECT_TRACING_INIT
100#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400101#endif
102
Benjamin Walshacc68c12017-01-29 18:57:45 -0500103#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300104#define _POLL_EVENT_OBJ_INIT(obj) \
105 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
106#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500107#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300108#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500109#define _POLL_EVENT
110#endif
111
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500112struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400113struct k_mutex;
114struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400115struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400116struct k_msgq;
117struct k_mbox;
118struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200119struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400120struct k_fifo;
121struct k_lifo;
122struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400123struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400124struct k_mem_pool;
125struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500126struct k_poll_event;
127struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800128struct k_mem_domain;
129struct k_mem_partition;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400130
Andrew Boie5bd891d2017-09-27 12:59:28 -0700131/* This enumeration needs to be kept in sync with the lists of kernel objects
132 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
133 * function in kernel/userspace.c
134 */
Andrew Boie945af952017-08-22 13:15:23 -0700135enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700136 K_OBJ_ANY,
137
Leandro Pereirac2003672018-04-04 13:50:32 -0700138 /** @cond
139 * Doxygen should ignore this build-time generated include file
140 * when genrating API documentation. Enumeration values are
141 * generated during build by gen_kobject_list.py. It includes
142 * basic kernel objects (e.g. pipes and mutexes) and driver types.
143 */
144#include <kobj-types-enum.h>
145 /** @endcond
146 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700147
Andrew Boie945af952017-08-22 13:15:23 -0700148 K_OBJ_LAST
149};
150
151#ifdef CONFIG_USERSPACE
152/* Table generated by gperf, these objects are retrieved via
153 * _k_object_find() */
154struct _k_object {
155 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700156 u8_t perms[CONFIG_MAX_THREAD_BYTES];
157 u8_t type;
158 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700159 u32_t data;
Andrew Boie945af952017-08-22 13:15:23 -0700160} __packed;
161
Andrew Boie877f82e2017-10-17 11:20:22 -0700162struct _k_object_assignment {
163 struct k_thread *thread;
164 void * const *objects;
165};
166
167/**
168 * @brief Grant a static thread access to a list of kernel objects
169 *
170 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
171 * a set of kernel objects. These objects do not need to be in an initialized
172 * state. The permissions will be granted when the threads are initialized
173 * in the early boot sequence.
174 *
175 * All arguments beyond the first must be pointers to kernel objects.
176 *
177 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
178 */
179#define K_THREAD_ACCESS_GRANT(name_, ...) \
180 static void * const _CONCAT(_object_list_, name_)[] = \
181 { __VA_ARGS__, NULL }; \
182 static __used __in_section_unique(object_access) \
183 const struct _k_object_assignment \
184 _CONCAT(_object_access_, name_) = \
185 { (&_k_thread_obj_ ## name_), \
186 (_CONCAT(_object_list_, name_)) }
187
Andrew Boie945af952017-08-22 13:15:23 -0700188#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700189#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie945af952017-08-22 13:15:23 -0700190
191/**
192 * Lookup a kernel object and init its metadata if it exists
193 *
194 * Calling this on an object will make it usable from userspace.
195 * Intended to be called as the last statement in kernel object init
196 * functions.
197 *
198 * @param object Address of the kernel object
199 */
200void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700201#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700202
203#define K_THREAD_ACCESS_GRANT(thread, ...)
204
Anas Nashif954d5502018-02-25 08:37:28 -0600205/**
206 * @internal
207 */
Andrew Boie743e4682017-10-04 12:25:50 -0700208static inline void _k_object_init(void *obj)
209{
210 ARG_UNUSED(obj);
211}
212
Anas Nashif954d5502018-02-25 08:37:28 -0600213/**
214 * @internal
215 */
Andrew Boie743e4682017-10-04 12:25:50 -0700216static inline void _impl_k_object_access_grant(void *object,
217 struct k_thread *thread)
218{
219 ARG_UNUSED(object);
220 ARG_UNUSED(thread);
221}
222
Anas Nashif954d5502018-02-25 08:37:28 -0600223/**
224 * @internal
225 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700226static inline void k_object_access_revoke(void *object,
227 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700228{
229 ARG_UNUSED(object);
230 ARG_UNUSED(thread);
231}
232
Andrew Boiee9cfc542018-04-13 13:15:28 -0700233/**
234 * @internal
235 */
236static inline void _impl_k_object_release(void *object)
237{
238 ARG_UNUSED(object);
239}
240
Andrew Boie41bab6e2017-10-14 14:42:23 -0700241static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700242{
243 ARG_UNUSED(object);
244}
245#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700246
247/**
248 * grant a thread access to a kernel object
249 *
250 * The thread will be granted access to the object if the caller is from
251 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700252 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700253 *
254 * @param object Address of kernel object
255 * @param thread Thread to grant access to the object
256 */
Andrew Boie743e4682017-10-04 12:25:50 -0700257__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700258
Andrew Boiea89bf012017-10-09 14:47:55 -0700259/**
260 * grant a thread access to a kernel object
261 *
262 * The thread will lose access to the object if the caller is from
263 * supervisor mode, or the caller is from user mode AND has permissions
264 * on both the object and the thread whose access is being revoked.
265 *
266 * @param object Address of kernel object
267 * @param thread Thread to remove access to the object
268 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700269void k_object_access_revoke(void *object, struct k_thread *thread);
270
271
272__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700273
274/**
275 * grant all present and future threads access to an object
276 *
277 * If the caller is from supervisor mode, or the caller is from user mode and
278 * have sufficient permissions on the object, then that object will have
279 * permissions granted to it for *all* current and future threads running in
280 * the system, effectively becoming a public kernel object.
281 *
282 * Use of this API should be avoided on systems that are running untrusted code
283 * as it is possible for such code to derive the addresses of kernel objects
284 * and perform unwanted operations on them.
285 *
Andrew Boie04caa672017-10-13 13:57:07 -0700286 * It is not possible to revoke permissions on public objects; once public,
287 * any thread may use it.
288 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700289 * @param object Address of kernel object
290 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700291void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700292
Andrew Boie31bdfc02017-11-08 16:38:03 -0800293#ifdef CONFIG_DYNAMIC_OBJECTS
294/**
295 * Allocate a kernel object of a designated type
296 *
297 * This will instantiate at runtime a kernel object of the specified type,
298 * returning a pointer to it. The object will be returned in an uninitialized
299 * state, with the calling thread being granted permission on it. The memory
300 * for the object will be allocated out of the kernel's heap.
301 *
302 * Currently, allocation of thread stacks is not supported.
303 *
304 * @param otype Requested kernel object type
305 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
306 * available
307 */
308void *k_object_alloc(enum k_objects otype);
309
310/**
311 * Free a kernel object previously allocated with k_object_alloc()
312 *
313 * This will return memory for a kernel object back to the system heap.
314 * Care must be exercised that the object will not be used during or after
315 * when this call is made.
316 *
317 * @param obj Pointer to the kernel object memory address.
318 */
319void k_object_free(void *obj);
320#endif /* CONFIG_DYNAMIC_OBJECTS */
321
Andrew Boiebca15da2017-10-15 14:17:48 -0700322/* Using typedef deliberately here, this is quite intended to be an opaque
323 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
324 *
325 * The purpose of this data type is to clearly distinguish between the
326 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
327 * buffer which composes the stack data actually used by the underlying
328 * thread; they cannot be used interchangably as some arches precede the
329 * stack buffer region with guard areas that trigger a MPU or MMU fault
330 * if written to.
331 *
332 * APIs that want to work with the buffer inside should continue to use
333 * char *.
334 *
335 * Stacks should always be created with K_THREAD_STACK_DEFINE().
336 */
337struct __packed _k_thread_stack_element {
338 char data;
339};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700340typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700341
Andrew Boie73abd322017-04-04 13:19:13 -0700342/* timeouts */
343
344struct _timeout;
345typedef void (*_timeout_func_t)(struct _timeout *t);
346
347struct _timeout {
348 sys_dnode_t node;
349 struct k_thread *thread;
350 sys_dlist_t *wait_q;
351 s32_t delta_ticks_from_prev;
352 _timeout_func_t func;
353};
354
355extern s32_t _timeout_remaining_get(struct _timeout *timeout);
356
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700357/**
358 * @typedef k_thread_entry_t
359 * @brief Thread entry point function type.
360 *
361 * A thread's entry point function is invoked when the thread starts executing.
362 * Up to 3 argument values can be passed to the function.
363 *
364 * The thread terminates execution permanently if the entry point function
365 * returns. The thread is responsible for releasing any shared resources
366 * it may own (such as mutexes and dynamically allocated memory), prior to
367 * returning.
368 *
369 * @param p1 First argument.
370 * @param p2 Second argument.
371 * @param p3 Third argument.
372 *
373 * @return N/A
374 */
375typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700376
377#ifdef CONFIG_THREAD_MONITOR
378struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700379 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700380 void *parameter1;
381 void *parameter2;
382 void *parameter3;
383};
384#endif
385
386/* can be used for creating 'dummy' threads, e.g. for pending on objects */
387struct _thread_base {
388
389 /* this thread's entry in a ready/wait queue */
390 sys_dnode_t k_q_node;
391
392 /* user facing 'thread options'; values defined in include/kernel.h */
393 u8_t user_options;
394
395 /* thread state */
396 u8_t thread_state;
397
398 /*
399 * scheduler lock count and thread priority
400 *
401 * These two fields control the preemptibility of a thread.
402 *
403 * When the scheduler is locked, sched_locked is decremented, which
404 * means that the scheduler is locked for values from 0xff to 0x01. A
405 * thread is coop if its prio is negative, thus 0x80 to 0xff when
406 * looked at the value as unsigned.
407 *
408 * By putting them end-to-end, this means that a thread is
409 * non-preemptible if the bundled value is greater than or equal to
410 * 0x0080.
411 */
412 union {
413 struct {
414#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
415 u8_t sched_locked;
416 s8_t prio;
417#else /* LITTLE and PDP */
418 s8_t prio;
419 u8_t sched_locked;
420#endif
421 };
422 u16_t preempt;
423 };
424
Andy Ross2724fd12018-01-29 14:55:20 -0800425#ifdef CONFIG_SMP
426 /* True for the per-CPU idle threads */
427 u8_t is_idle;
428
429 /* Non-zero when actively running on a CPU */
430 u8_t active;
431
432 /* CPU index on which thread was last run */
433 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700434
435 /* Recursive count of irq_lock() calls */
436 u8_t global_lock_count;
Andy Ross2724fd12018-01-29 14:55:20 -0800437#endif
438
Andrew Boie73abd322017-04-04 13:19:13 -0700439 /* data returned by APIs */
440 void *swap_data;
441
442#ifdef CONFIG_SYS_CLOCK_EXISTS
443 /* this thread's entry in a timeout queue */
444 struct _timeout timeout;
445#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700446};
447
448typedef struct _thread_base _thread_base_t;
449
450#if defined(CONFIG_THREAD_STACK_INFO)
451/* Contains the stack information of a thread */
452struct _thread_stack_info {
453 /* Stack Start */
454 u32_t start;
455 /* Stack Size */
456 u32_t size;
457};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700458
459typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700460#endif /* CONFIG_THREAD_STACK_INFO */
461
Chunlin Hane9c97022017-07-07 20:29:30 +0800462#if defined(CONFIG_USERSPACE)
463struct _mem_domain_info {
464 /* memory domain queue node */
465 sys_dnode_t mem_domain_q_node;
466 /* memory domain of the thread */
467 struct k_mem_domain *mem_domain;
468};
469
470#endif /* CONFIG_USERSPACE */
471
Andrew Boie73abd322017-04-04 13:19:13 -0700472struct k_thread {
473
474 struct _thread_base base;
475
476 /* defined by the architecture, but all archs need these */
477 struct _caller_saved caller_saved;
478 struct _callee_saved callee_saved;
479
480 /* static thread init data */
481 void *init_data;
482
483 /* abort function */
484 void (*fn_abort)(void);
485
486#if defined(CONFIG_THREAD_MONITOR)
487 /* thread entry and parameters description */
488 struct __thread_entry *entry;
489
490 /* next item in list of all threads */
491 struct k_thread *next_thread;
492#endif
493
494#ifdef CONFIG_THREAD_CUSTOM_DATA
495 /* crude thread-local storage */
496 void *custom_data;
497#endif
498
499#ifdef CONFIG_ERRNO
500 /* per-thread errno variable */
501 int errno_var;
502#endif
503
504#if defined(CONFIG_THREAD_STACK_INFO)
505 /* Stack Info */
506 struct _thread_stack_info stack_info;
507#endif /* CONFIG_THREAD_STACK_INFO */
508
Chunlin Hane9c97022017-07-07 20:29:30 +0800509#if defined(CONFIG_USERSPACE)
510 /* memory domain info of the thread */
511 struct _mem_domain_info mem_domain_info;
Andrew Boiebca15da2017-10-15 14:17:48 -0700512 /* Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700513 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800514#endif /* CONFIG_USERSPACE */
515
Andy Ross042d8ec2017-12-09 08:37:20 -0800516#if defined(CONFIG_USE_SWITCH)
517 /* When using __switch() a few previously arch-specific items
518 * become part of the core OS
519 */
520
521 /* _Swap() return value */
522 int swap_retval;
523
524 /* Context handle returned via _arch_switch() */
525 void *switch_handle;
526#endif
527
Andrew Boie73abd322017-04-04 13:19:13 -0700528 /* arch-specifics: must always be at the end */
529 struct _thread_arch arch;
530};
531
532typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400533typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700534#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400535
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400536enum execution_context_types {
537 K_ISR = 0,
538 K_COOP_THREAD,
539 K_PREEMPT_THREAD,
540};
541
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400542/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100543 * @defgroup profiling_apis Profiling APIs
544 * @ingroup kernel_apis
545 * @{
546 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530547typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
548 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100549
550/**
551 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
552 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700553 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100554 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
555 *
556 * CONFIG_MAIN_STACK_SIZE
557 * CONFIG_IDLE_STACK_SIZE
558 * CONFIG_ISR_STACK_SIZE
559 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
560 *
561 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
562 * produce output.
563 *
564 * @return N/A
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530565 *
566 * @deprecated This API is deprecated. Use k_thread_foreach().
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100567 */
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530568__deprecated extern void k_call_stacks_analyze(void);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100569
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530570/**
571 * @brief Iterate over all the threads in the system.
572 *
573 * This routine iterates over all the threads in the system and
574 * calls the user_cb function for each thread.
575 *
576 * @param user_cb Pointer to the user callback function.
577 * @param user_data Pointer to user data.
578 *
579 * @note CONFIG_THREAD_MONITOR must be set for this function
580 * to be effective. Also this API uses irq_lock to protect the
581 * _kernel.threads list which means creation of new threads and
582 * terminations of existing threads are blocked until this
583 * API returns.
584 *
585 * @return N/A
586 */
587extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
588
Anas Nashif166f5192018-02-25 08:02:36 -0600589/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100590
591/**
Allan Stephensc98da842016-11-11 15:45:03 -0500592 * @defgroup thread_apis Thread APIs
593 * @ingroup kernel_apis
594 * @{
595 */
596
Benjamin Walshed240f22017-01-22 13:05:08 -0500597#endif /* !_ASMLANGUAGE */
598
599
600/*
601 * Thread user options. May be needed by assembly code. Common part uses low
602 * bits, arch-specific use high bits.
603 */
604
605/* system thread that must not abort */
606#define K_ESSENTIAL (1 << 0)
607
608#if defined(CONFIG_FP_SHARING)
609/* thread uses floating point registers */
610#define K_FP_REGS (1 << 1)
611#endif
612
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700613/* This thread has dropped from supervisor mode to user mode and consequently
614 * has additional restrictions
615 */
616#define K_USER (1 << 2)
617
Andrew Boie47f8fd12017-10-05 11:11:02 -0700618/* Indicates that the thread being created should inherit all kernel object
619 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
620 * is not enabled.
621 */
622#define K_INHERIT_PERMS (1 << 3)
623
Benjamin Walshed240f22017-01-22 13:05:08 -0500624#ifdef CONFIG_X86
625/* x86 Bitmask definitions for threads user options */
626
627#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
628/* thread uses SSEx (and also FP) registers */
629#define K_SSE_REGS (1 << 7)
630#endif
631#endif
632
633/* end - thread options */
634
635#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400636/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700637 * @brief Create a thread.
638 *
639 * This routine initializes a thread, then schedules it for execution.
640 *
641 * The new thread may be scheduled for immediate execution or a delayed start.
642 * If the newly spawned thread does not have a delayed start the kernel
643 * scheduler may preempt the current thread to allow the new thread to
644 * execute.
645 *
646 * Thread options are architecture-specific, and can include K_ESSENTIAL,
647 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
648 * them using "|" (the logical OR operator).
649 *
650 * Historically, users often would use the beginning of the stack memory region
651 * to store the struct k_thread data, although corruption will occur if the
652 * stack overflows this region and stack protection features may not detect this
653 * situation.
654 *
655 * @param new_thread Pointer to uninitialized struct k_thread
656 * @param stack Pointer to the stack space.
657 * @param stack_size Stack size in bytes.
658 * @param entry Thread entry function.
659 * @param p1 1st entry point parameter.
660 * @param p2 2nd entry point parameter.
661 * @param p3 3rd entry point parameter.
662 * @param prio Thread priority.
663 * @param options Thread options.
664 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
665 *
666 * @return ID of new thread.
667 */
Andrew Boie662c3452017-10-02 10:51:18 -0700668__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700669 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700670 size_t stack_size,
671 k_thread_entry_t entry,
672 void *p1, void *p2, void *p3,
673 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700674
Andrew Boie3f091b52017-08-30 14:34:14 -0700675/**
676 * @brief Drop a thread's privileges permanently to user mode
677 *
678 * @param entry Function to start executing from
679 * @param p1 1st entry point parameter
680 * @param p2 2nd entry point parameter
681 * @param p3 3rd entry point parameter
682 */
683extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
684 void *p1, void *p2,
685 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700686
Andrew Boied26cf2d2017-03-30 13:07:02 -0700687/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700688 * @brief Grant a thread access to a NULL-terminated set of kernel objects
689 *
690 * This is a convenience function. For the provided thread, grant access to
691 * the remaining arguments, which must be pointers to kernel objects.
692 * The final argument must be a NULL.
693 *
694 * The thread object must be initialized (i.e. running). The objects don't
695 * need to be.
696 *
697 * @param thread Thread to grant access to objects
698 * @param ... NULL-terminated list of kernel object pointers
699 */
700extern void __attribute__((sentinel))
701 k_thread_access_grant(struct k_thread *thread, ...);
702
703/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500704 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400705 *
Allan Stephensc98da842016-11-11 15:45:03 -0500706 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500707 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400708 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500709 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400710 *
711 * @return N/A
712 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700713__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400714
715/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500716 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400717 *
718 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500719 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400720 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400721 * @return N/A
722 */
Kumar Galacc334c72017-04-21 10:55:34 -0500723extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400724
725/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500726 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400727 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500728 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400729 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500730 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400731 *
732 * @return N/A
733 */
Andrew Boie468190a2017-09-29 14:00:48 -0700734__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400735
736/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500737 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400738 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500739 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400740 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500741 * If @a thread is not currently sleeping, the routine has no effect.
742 *
743 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400744 *
745 * @return N/A
746 */
Andrew Boie468190a2017-09-29 14:00:48 -0700747__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400748
749/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500750 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400751 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500752 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400753 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700754__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400755
756/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500757 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400758 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500759 * This routine prevents @a thread from executing if it has not yet started
760 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400761 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500762 * @param thread ID of thread to cancel.
763 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700764 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500765 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700766 *
767 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400768 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700769__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400770
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400771/**
Allan Stephensc98da842016-11-11 15:45:03 -0500772 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400773 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500774 * This routine permanently stops execution of @a thread. The thread is taken
775 * off all kernel queues it is part of (i.e. the ready queue, the timeout
776 * queue, or a kernel object wait queue). However, any kernel resources the
777 * thread might currently own (such as mutexes or memory blocks) are not
778 * released. It is the responsibility of the caller of this routine to ensure
779 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400780 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500781 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400782 *
783 * @return N/A
784 */
Andrew Boie468190a2017-09-29 14:00:48 -0700785__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400786
Andrew Boie7d627c52017-08-30 11:01:56 -0700787
788/**
789 * @brief Start an inactive thread
790 *
791 * If a thread was created with K_FOREVER in the delay parameter, it will
792 * not be added to the scheduling queue until this function is called
793 * on it.
794 *
795 * @param thread thread to start
796 */
Andrew Boie468190a2017-09-29 14:00:48 -0700797__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700798
Allan Stephensc98da842016-11-11 15:45:03 -0500799/**
800 * @cond INTERNAL_HIDDEN
801 */
802
Benjamin Walshd211a522016-12-06 11:44:01 -0500803/* timeout has timed out and is not on _timeout_q anymore */
804#define _EXPIRED (-2)
805
806/* timeout is not in use */
807#define _INACTIVE (-1)
808
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400809struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700810 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700811 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400812 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700813 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500814 void *init_p1;
815 void *init_p2;
816 void *init_p3;
817 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500818 u32_t init_options;
819 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500820 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400821};
822
Andrew Boied26cf2d2017-03-30 13:07:02 -0700823#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400824 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500825 prio, options, delay, abort, groups) \
826 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700827 .init_thread = (thread), \
828 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500829 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700830 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400831 .init_p1 = (void *)p1, \
832 .init_p2 = (void *)p2, \
833 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500834 .init_prio = (prio), \
835 .init_options = (options), \
836 .init_delay = (delay), \
837 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400838 }
839
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400840/**
Allan Stephensc98da842016-11-11 15:45:03 -0500841 * INTERNAL_HIDDEN @endcond
842 */
843
844/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500845 * @brief Statically define and initialize a thread.
846 *
847 * The thread may be scheduled for immediate execution or a delayed start.
848 *
849 * Thread options are architecture-specific, and can include K_ESSENTIAL,
850 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
851 * them using "|" (the logical OR operator).
852 *
853 * The ID of the thread can be accessed using:
854 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500855 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500856 *
857 * @param name Name of the thread.
858 * @param stack_size Stack size in bytes.
859 * @param entry Thread entry function.
860 * @param p1 1st entry point parameter.
861 * @param p2 2nd entry point parameter.
862 * @param p3 3rd entry point parameter.
863 * @param prio Thread priority.
864 * @param options Thread options.
865 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400866 *
867 * @internal It has been observed that the x86 compiler by default aligns
868 * these _static_thread_data structures to 32-byte boundaries, thereby
869 * wasting space. To work around this, force a 4-byte alignment.
870 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500871#define K_THREAD_DEFINE(name, stack_size, \
872 entry, p1, p2, p3, \
873 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700874 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700875 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500876 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500877 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700878 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
879 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500880 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700881 NULL, 0); \
882 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400883
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400884/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500885 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400886 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500887 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400888 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500889 * @param thread ID of thread whose priority is needed.
890 *
891 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400892 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700893__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400894
895/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500896 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400897 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500898 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400899 *
900 * Rescheduling can occur immediately depending on the priority @a thread is
901 * set to:
902 *
903 * - If its priority is raised above the priority of the caller of this
904 * function, and the caller is preemptible, @a thread will be scheduled in.
905 *
906 * - If the caller operates on itself, it lowers its priority below that of
907 * other threads in the system, and the caller is preemptible, the thread of
908 * highest priority will be scheduled in.
909 *
910 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
911 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
912 * highest priority.
913 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500914 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400915 * @param prio New priority.
916 *
917 * @warning Changing the priority of a thread currently involved in mutex
918 * priority inheritance may result in undefined behavior.
919 *
920 * @return N/A
921 */
Andrew Boie468190a2017-09-29 14:00:48 -0700922__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400923
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400924/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500925 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400926 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500927 * This routine prevents the kernel scheduler from making @a thread the
928 * current thread. All other internal operations on @a thread are still
929 * performed; for example, any timeout it is waiting on keeps ticking,
930 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400931 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500932 * If @a thread is already suspended, the routine has no effect.
933 *
934 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400935 *
936 * @return N/A
937 */
Andrew Boie468190a2017-09-29 14:00:48 -0700938__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400939
940/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500941 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400942 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500943 * This routine allows the kernel scheduler to make @a thread the current
944 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400945 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500946 * If @a thread is not currently suspended, the routine has no effect.
947 *
948 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400949 *
950 * @return N/A
951 */
Andrew Boie468190a2017-09-29 14:00:48 -0700952__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400953
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400954/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500955 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400956 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500957 * This routine specifies how the scheduler will perform time slicing of
958 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400959 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500960 * To enable time slicing, @a slice must be non-zero. The scheduler
961 * ensures that no thread runs for more than the specified time limit
962 * before other threads of that priority are given a chance to execute.
963 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700964 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400965 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500966 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400967 * execute. Once the scheduler selects a thread for execution, there is no
968 * minimum guaranteed time the thread will execute before threads of greater or
969 * equal priority are scheduled.
970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500971 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400972 * for execution, this routine has no effect; the thread is immediately
973 * rescheduled after the slice period expires.
974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500975 * To disable timeslicing, set both @a slice and @a prio to zero.
976 *
977 * @param slice Maximum time slice length (in milliseconds).
978 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400979 *
980 * @return N/A
981 */
Kumar Galacc334c72017-04-21 10:55:34 -0500982extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400983
Anas Nashif166f5192018-02-25 08:02:36 -0600984/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -0500985
986/**
987 * @addtogroup isr_apis
988 * @{
989 */
990
991/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500992 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400993 *
Allan Stephensc98da842016-11-11 15:45:03 -0500994 * This routine allows the caller to customize its actions, depending on
995 * whether it is a thread or an ISR.
996 *
997 * @note Can be called by ISRs.
998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500999 * @return 0 if invoked by a thread.
1000 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001001 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -05001002extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001003
Benjamin Walsh445830d2016-11-10 15:54:27 -05001004/**
1005 * @brief Determine if code is running in a preemptible thread.
1006 *
Allan Stephensc98da842016-11-11 15:45:03 -05001007 * This routine allows the caller to customize its actions, depending on
1008 * whether it can be preempted by another thread. The routine returns a 'true'
1009 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001010 *
Allan Stephensc98da842016-11-11 15:45:03 -05001011 * - The code is running in a thread, not at ISR.
1012 * - The thread's priority is in the preemptible range.
1013 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001014 *
Allan Stephensc98da842016-11-11 15:45:03 -05001015 * @note Can be called by ISRs.
1016 *
1017 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001018 * @return Non-zero if invoked by a preemptible thread.
1019 */
Andrew Boie468190a2017-09-29 14:00:48 -07001020__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001021
Allan Stephensc98da842016-11-11 15:45:03 -05001022/**
Anas Nashif166f5192018-02-25 08:02:36 -06001023 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001024 */
1025
1026/**
1027 * @addtogroup thread_apis
1028 * @{
1029 */
1030
1031/**
1032 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001033 *
Allan Stephensc98da842016-11-11 15:45:03 -05001034 * This routine prevents the current thread from being preempted by another
1035 * thread by instructing the scheduler to treat it as a cooperative thread.
1036 * If the thread subsequently performs an operation that makes it unready,
1037 * it will be context switched out in the normal manner. When the thread
1038 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001039 *
Allan Stephensc98da842016-11-11 15:45:03 -05001040 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001041 *
Allan Stephensc98da842016-11-11 15:45:03 -05001042 * @note k_sched_lock() and k_sched_unlock() should normally be used
1043 * when the operation being performed can be safely interrupted by ISRs.
1044 * However, if the amount of processing involved is very small, better
1045 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001046 *
1047 * @return N/A
1048 */
1049extern void k_sched_lock(void);
1050
Allan Stephensc98da842016-11-11 15:45:03 -05001051/**
1052 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001053 *
Allan Stephensc98da842016-11-11 15:45:03 -05001054 * This routine reverses the effect of a previous call to k_sched_lock().
1055 * A thread must call the routine once for each time it called k_sched_lock()
1056 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001057 *
1058 * @return N/A
1059 */
1060extern void k_sched_unlock(void);
1061
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001062/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001063 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001064 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001065 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001066 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001067 * Custom data is not used by the kernel itself, and is freely available
1068 * for a thread to use as it sees fit. It can be used as a framework
1069 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001070 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001071 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001072 *
1073 * @return N/A
1074 */
Andrew Boie468190a2017-09-29 14:00:48 -07001075__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001076
1077/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001078 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001079 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001080 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001081 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001082 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001083 */
Andrew Boie468190a2017-09-29 14:00:48 -07001084__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001085
1086/**
Anas Nashif166f5192018-02-25 08:02:36 -06001087 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001088 */
1089
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001090#include <sys_clock.h>
1091
Allan Stephensc2f15a42016-11-17 12:24:22 -05001092/**
1093 * @addtogroup clock_apis
1094 * @{
1095 */
1096
1097/**
1098 * @brief Generate null timeout delay.
1099 *
1100 * This macro generates a timeout delay that that instructs a kernel API
1101 * not to wait if the requested operation cannot be performed immediately.
1102 *
1103 * @return Timeout delay value.
1104 */
1105#define K_NO_WAIT 0
1106
1107/**
1108 * @brief Generate timeout delay from milliseconds.
1109 *
1110 * This macro generates a timeout delay that that instructs a kernel API
1111 * to wait up to @a ms milliseconds to perform the requested operation.
1112 *
1113 * @param ms Duration in milliseconds.
1114 *
1115 * @return Timeout delay value.
1116 */
Johan Hedberg14471692016-11-13 10:52:15 +02001117#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001118
1119/**
1120 * @brief Generate timeout delay from seconds.
1121 *
1122 * This macro generates a timeout delay that that instructs a kernel API
1123 * to wait up to @a s seconds to perform the requested operation.
1124 *
1125 * @param s Duration in seconds.
1126 *
1127 * @return Timeout delay value.
1128 */
Johan Hedberg14471692016-11-13 10:52:15 +02001129#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001130
1131/**
1132 * @brief Generate timeout delay from minutes.
1133 *
1134 * This macro generates a timeout delay that that instructs a kernel API
1135 * to wait up to @a m minutes to perform the requested operation.
1136 *
1137 * @param m Duration in minutes.
1138 *
1139 * @return Timeout delay value.
1140 */
Johan Hedberg14471692016-11-13 10:52:15 +02001141#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001142
1143/**
1144 * @brief Generate timeout delay from hours.
1145 *
1146 * This macro generates a timeout delay that that instructs a kernel API
1147 * to wait up to @a h hours to perform the requested operation.
1148 *
1149 * @param h Duration in hours.
1150 *
1151 * @return Timeout delay value.
1152 */
Johan Hedberg14471692016-11-13 10:52:15 +02001153#define K_HOURS(h) K_MINUTES((h) * 60)
1154
Allan Stephensc98da842016-11-11 15:45:03 -05001155/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001156 * @brief Generate infinite timeout delay.
1157 *
1158 * This macro generates a timeout delay that that instructs a kernel API
1159 * to wait as long as necessary to perform the requested operation.
1160 *
1161 * @return Timeout delay value.
1162 */
1163#define K_FOREVER (-1)
1164
1165/**
Anas Nashif166f5192018-02-25 08:02:36 -06001166 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001167 */
1168
1169/**
Allan Stephensc98da842016-11-11 15:45:03 -05001170 * @cond INTERNAL_HIDDEN
1171 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001172
Benjamin Walsh62092182016-12-20 14:39:08 -05001173/* kernel clocks */
1174
1175#if (sys_clock_ticks_per_sec == 1000) || \
1176 (sys_clock_ticks_per_sec == 500) || \
1177 (sys_clock_ticks_per_sec == 250) || \
1178 (sys_clock_ticks_per_sec == 125) || \
1179 (sys_clock_ticks_per_sec == 100) || \
1180 (sys_clock_ticks_per_sec == 50) || \
1181 (sys_clock_ticks_per_sec == 25) || \
1182 (sys_clock_ticks_per_sec == 20) || \
1183 (sys_clock_ticks_per_sec == 10) || \
1184 (sys_clock_ticks_per_sec == 1)
1185
1186 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1187#else
1188 /* yields horrible 64-bit math on many architectures: try to avoid */
1189 #define _NON_OPTIMIZED_TICKS_PER_SEC
1190#endif
1191
1192#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001193extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001194#else
Kumar Galacc334c72017-04-21 10:55:34 -05001195static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001196{
Kumar Galacc334c72017-04-21 10:55:34 -05001197 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001198}
1199#endif
1200
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001201/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001202#ifdef CONFIG_TICKLESS_KERNEL
1203#define _TICK_ALIGN 0
1204#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001205#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001206#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001207
Kumar Galacc334c72017-04-21 10:55:34 -05001208static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001209{
Benjamin Walsh62092182016-12-20 14:39:08 -05001210#ifdef CONFIG_SYS_CLOCK_EXISTS
1211
1212#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001213 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001214#else
Kumar Galacc334c72017-04-21 10:55:34 -05001215 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001216#endif
1217
1218#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001219 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001220 return 0;
1221#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001222}
1223
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001224struct k_timer {
1225 /*
1226 * _timeout structure must be first here if we want to use
1227 * dynamic timer allocation. timeout.node is used in the double-linked
1228 * list of free timers
1229 */
1230 struct _timeout timeout;
1231
Allan Stephens45bfa372016-10-12 12:39:42 -05001232 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001233 _wait_q_t wait_q;
1234
1235 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001236 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001237
1238 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001239 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001240
1241 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001242 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001243
Allan Stephens45bfa372016-10-12 12:39:42 -05001244 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001245 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001246
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001247 /* user-specific data, also used to support legacy features */
1248 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001249
Anas Nashif2f203c22016-12-18 06:57:45 -05001250 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001251};
1252
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001253#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001254 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001255 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001256 .timeout.wait_q = NULL, \
1257 .timeout.thread = NULL, \
1258 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001259 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001260 .expiry_fn = expiry, \
1261 .stop_fn = stop, \
1262 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001263 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001264 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001265 }
1266
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001267#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1268
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001269/**
Allan Stephensc98da842016-11-11 15:45:03 -05001270 * INTERNAL_HIDDEN @endcond
1271 */
1272
1273/**
1274 * @defgroup timer_apis Timer APIs
1275 * @ingroup kernel_apis
1276 * @{
1277 */
1278
1279/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001280 * @typedef k_timer_expiry_t
1281 * @brief Timer expiry function type.
1282 *
1283 * A timer's expiry function is executed by the system clock interrupt handler
1284 * each time the timer expires. The expiry function is optional, and is only
1285 * invoked if the timer has been initialized with one.
1286 *
1287 * @param timer Address of timer.
1288 *
1289 * @return N/A
1290 */
1291typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1292
1293/**
1294 * @typedef k_timer_stop_t
1295 * @brief Timer stop function type.
1296 *
1297 * A timer's stop function is executed if the timer is stopped prematurely.
1298 * The function runs in the context of the thread that stops the timer.
1299 * The stop function is optional, and is only invoked if the timer has been
1300 * initialized with one.
1301 *
1302 * @param timer Address of timer.
1303 *
1304 * @return N/A
1305 */
1306typedef void (*k_timer_stop_t)(struct k_timer *timer);
1307
1308/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001309 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001310 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001311 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001312 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001313 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001314 *
1315 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001316 * @param expiry_fn Function to invoke each time the timer expires.
1317 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001318 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001319#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001320 struct k_timer name \
1321 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001322 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001323
Allan Stephens45bfa372016-10-12 12:39:42 -05001324/**
1325 * @brief Initialize a timer.
1326 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001327 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001328 *
1329 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001330 * @param expiry_fn Function to invoke each time the timer expires.
1331 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001332 *
1333 * @return N/A
1334 */
1335extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001336 k_timer_expiry_t expiry_fn,
1337 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001338
Allan Stephens45bfa372016-10-12 12:39:42 -05001339/**
1340 * @brief Start a timer.
1341 *
1342 * This routine starts a timer, and resets its status to zero. The timer
1343 * begins counting down using the specified duration and period values.
1344 *
1345 * Attempting to start a timer that is already running is permitted.
1346 * The timer's status is reset to zero and the timer begins counting down
1347 * using the new duration and period values.
1348 *
1349 * @param timer Address of timer.
1350 * @param duration Initial timer duration (in milliseconds).
1351 * @param period Timer period (in milliseconds).
1352 *
1353 * @return N/A
1354 */
Andrew Boiea354d492017-09-29 16:22:28 -07001355__syscall void k_timer_start(struct k_timer *timer,
1356 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001357
1358/**
1359 * @brief Stop a timer.
1360 *
1361 * This routine stops a running timer prematurely. The timer's stop function,
1362 * if one exists, is invoked by the caller.
1363 *
1364 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001365 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001366 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001367 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1368 * if @a k_timer_stop is to be called from ISRs.
1369 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001370 * @param timer Address of timer.
1371 *
1372 * @return N/A
1373 */
Andrew Boiea354d492017-09-29 16:22:28 -07001374__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001375
1376/**
1377 * @brief Read timer status.
1378 *
1379 * This routine reads the timer's status, which indicates the number of times
1380 * it has expired since its status was last read.
1381 *
1382 * Calling this routine resets the timer's status to zero.
1383 *
1384 * @param timer Address of timer.
1385 *
1386 * @return Timer status.
1387 */
Andrew Boiea354d492017-09-29 16:22:28 -07001388__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001389
1390/**
1391 * @brief Synchronize thread to timer expiration.
1392 *
1393 * This routine blocks the calling thread until the timer's status is non-zero
1394 * (indicating that it has expired at least once since it was last examined)
1395 * or the timer is stopped. If the timer status is already non-zero,
1396 * or the timer is already stopped, the caller continues without waiting.
1397 *
1398 * Calling this routine resets the timer's status to zero.
1399 *
1400 * This routine must not be used by interrupt handlers, since they are not
1401 * allowed to block.
1402 *
1403 * @param timer Address of timer.
1404 *
1405 * @return Timer status.
1406 */
Andrew Boiea354d492017-09-29 16:22:28 -07001407__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001408
1409/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001410 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001411 *
1412 * This routine computes the (approximate) time remaining before a running
1413 * timer next expires. If the timer is not running, it returns zero.
1414 *
1415 * @param timer Address of timer.
1416 *
1417 * @return Remaining time (in milliseconds).
1418 */
Andrew Boiea354d492017-09-29 16:22:28 -07001419__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1420
1421static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001422{
1423 return _timeout_remaining_get(&timer->timeout);
1424}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001425
Allan Stephensc98da842016-11-11 15:45:03 -05001426/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001427 * @brief Associate user-specific data with a timer.
1428 *
1429 * This routine records the @a user_data with the @a timer, to be retrieved
1430 * later.
1431 *
1432 * It can be used e.g. in a timer handler shared across multiple subsystems to
1433 * retrieve data specific to the subsystem this timer is associated with.
1434 *
1435 * @param timer Address of timer.
1436 * @param user_data User data to associate with the timer.
1437 *
1438 * @return N/A
1439 */
Andrew Boiea354d492017-09-29 16:22:28 -07001440__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1441
Anas Nashif954d5502018-02-25 08:37:28 -06001442/**
1443 * @internal
1444 */
Andrew Boiea354d492017-09-29 16:22:28 -07001445static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1446 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001447{
1448 timer->user_data = user_data;
1449}
1450
1451/**
1452 * @brief Retrieve the user-specific data from a timer.
1453 *
1454 * @param timer Address of timer.
1455 *
1456 * @return The user data.
1457 */
Andrew Boiea354d492017-09-29 16:22:28 -07001458__syscall void *k_timer_user_data_get(struct k_timer *timer);
1459
1460static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001461{
1462 return timer->user_data;
1463}
1464
Anas Nashif166f5192018-02-25 08:02:36 -06001465/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001466
Allan Stephensc98da842016-11-11 15:45:03 -05001467/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001468 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001469 * @{
1470 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001471
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001472/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001473 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001474 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001475 * This routine returns the elapsed time since the system booted,
1476 * in milliseconds.
1477 *
1478 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001479 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001480__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001481
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001482/**
1483 * @brief Enable clock always on in tickless kernel
1484 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001485 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001486 * there are no timer events programmed in tickless kernel
1487 * scheduling. This is necessary if the clock is used to track
1488 * passage of time.
1489 *
1490 * @retval prev_status Previous status of always on flag
1491 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301492#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001493static inline int k_enable_sys_clock_always_on(void)
1494{
1495 int prev_status = _sys_clock_always_on;
1496
1497 _sys_clock_always_on = 1;
1498 _enable_sys_clock();
1499
1500 return prev_status;
1501}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301502#else
1503#define k_enable_sys_clock_always_on() do { } while ((0))
1504#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001505
1506/**
1507 * @brief Disable clock always on in tickless kernel
1508 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001509 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001510 * there are no timer events programmed in tickless kernel
1511 * scheduling. To save power, this routine should be called
1512 * immediately when clock is not used to track time.
1513 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301514#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001515static inline void k_disable_sys_clock_always_on(void)
1516{
1517 _sys_clock_always_on = 0;
1518}
1519#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001520#define k_disable_sys_clock_always_on() do { } while ((0))
1521#endif
1522
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001523/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001524 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001525 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001526 * This routine returns the lower 32-bits of the elapsed time since the system
1527 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001528 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001529 * This routine can be more efficient than k_uptime_get(), as it reduces the
1530 * need for interrupt locking and 64-bit math. However, the 32-bit result
1531 * cannot hold a system uptime time larger than approximately 50 days, so the
1532 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001533 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001534 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001535 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001536__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001537
1538/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001539 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001540 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001541 * This routine computes the elapsed time between the current system uptime
1542 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001543 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001544 * @param reftime Pointer to a reference time, which is updated to the current
1545 * uptime upon return.
1546 *
1547 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001548 */
Kumar Galacc334c72017-04-21 10:55:34 -05001549extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001550
1551/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001552 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001553 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001554 * This routine computes the elapsed time between the current system uptime
1555 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001556 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001557 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1558 * need for interrupt locking and 64-bit math. However, the 32-bit result
1559 * cannot hold an elapsed time larger than approximately 50 days, so the
1560 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001561 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001562 * @param reftime Pointer to a reference time, which is updated to the current
1563 * uptime upon return.
1564 *
1565 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001566 */
Kumar Galacc334c72017-04-21 10:55:34 -05001567extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001568
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001569/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001570 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001571 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001572 * This routine returns the current time, as measured by the system's hardware
1573 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001574 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001575 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001576 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001577#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001578
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001579/**
Anas Nashif166f5192018-02-25 08:02:36 -06001580 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001581 */
1582
Allan Stephensc98da842016-11-11 15:45:03 -05001583/**
1584 * @cond INTERNAL_HIDDEN
1585 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001586
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001587struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001588 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001589 union {
1590 _wait_q_t wait_q;
1591
1592 _POLL_EVENT;
1593 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001594
1595 _OBJECT_TRACING_NEXT_PTR(k_queue);
1596};
1597
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001598#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001599 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001600 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Michael Hope5f67a612018-03-17 12:44:40 +01001601 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001602 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001603 _OBJECT_TRACING_INIT \
1604 }
1605
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001606#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1607
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001608/**
1609 * INTERNAL_HIDDEN @endcond
1610 */
1611
1612/**
1613 * @defgroup queue_apis Queue APIs
1614 * @ingroup kernel_apis
1615 * @{
1616 */
1617
1618/**
1619 * @brief Initialize a queue.
1620 *
1621 * This routine initializes a queue object, prior to its first use.
1622 *
1623 * @param queue Address of the queue.
1624 *
1625 * @return N/A
1626 */
1627extern void k_queue_init(struct k_queue *queue);
1628
1629/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001630 * @brief Cancel waiting on a queue.
1631 *
1632 * This routine causes first thread pending on @a queue, if any, to
1633 * return from k_queue_get() call with NULL value (as if timeout expired).
1634 *
1635 * @note Can be called by ISRs.
1636 *
1637 * @param queue Address of the queue.
1638 *
1639 * @return N/A
1640 */
1641extern void k_queue_cancel_wait(struct k_queue *queue);
1642
1643/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001644 * @brief Append an element to the end of a queue.
1645 *
1646 * This routine appends a data item to @a queue. A queue data item must be
1647 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1648 * reserved for the kernel's use.
1649 *
1650 * @note Can be called by ISRs.
1651 *
1652 * @param queue Address of the queue.
1653 * @param data Address of the data item.
1654 *
1655 * @return N/A
1656 */
1657extern void k_queue_append(struct k_queue *queue, void *data);
1658
1659/**
1660 * @brief Prepend an element to a queue.
1661 *
1662 * This routine prepends a data item to @a queue. A queue data item must be
1663 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1664 * reserved for the kernel's use.
1665 *
1666 * @note Can be called by ISRs.
1667 *
1668 * @param queue Address of the queue.
1669 * @param data Address of the data item.
1670 *
1671 * @return N/A
1672 */
1673extern void k_queue_prepend(struct k_queue *queue, void *data);
1674
1675/**
1676 * @brief Inserts an element to a queue.
1677 *
1678 * This routine inserts a data item to @a queue after previous item. A queue
1679 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1680 * item are reserved for the kernel's use.
1681 *
1682 * @note Can be called by ISRs.
1683 *
1684 * @param queue Address of the queue.
1685 * @param prev Address of the previous data item.
1686 * @param data Address of the data item.
1687 *
1688 * @return N/A
1689 */
1690extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1691
1692/**
1693 * @brief Atomically append a list of elements to a queue.
1694 *
1695 * This routine adds a list of data items to @a queue in one operation.
1696 * The data items must be in a singly-linked list, with the first 32 bits
1697 * in each data item pointing to the next data item; the list must be
1698 * NULL-terminated.
1699 *
1700 * @note Can be called by ISRs.
1701 *
1702 * @param queue Address of the queue.
1703 * @param head Pointer to first node in singly-linked list.
1704 * @param tail Pointer to last node in singly-linked list.
1705 *
1706 * @return N/A
1707 */
1708extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1709
1710/**
1711 * @brief Atomically add a list of elements to a queue.
1712 *
1713 * This routine adds a list of data items to @a queue in one operation.
1714 * The data items must be in a singly-linked list implemented using a
1715 * sys_slist_t object. Upon completion, the original list is empty.
1716 *
1717 * @note Can be called by ISRs.
1718 *
1719 * @param queue Address of the queue.
1720 * @param list Pointer to sys_slist_t object.
1721 *
1722 * @return N/A
1723 */
1724extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1725
1726/**
1727 * @brief Get an element from a queue.
1728 *
1729 * This routine removes first data item from @a queue. The first 32 bits of the
1730 * data item are reserved for the kernel's use.
1731 *
1732 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1733 *
1734 * @param queue Address of the queue.
1735 * @param timeout Waiting period to obtain a data item (in milliseconds),
1736 * or one of the special values K_NO_WAIT and K_FOREVER.
1737 *
1738 * @return Address of the data item if successful; NULL if returned
1739 * without waiting, or waiting period timed out.
1740 */
Kumar Galacc334c72017-04-21 10:55:34 -05001741extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001742
1743/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001744 * @brief Remove an element from a queue.
1745 *
1746 * This routine removes data item from @a queue. The first 32 bits of the
1747 * data item are reserved for the kernel's use. Removing elements from k_queue
1748 * rely on sys_slist_find_and_remove which is not a constant time operation.
1749 *
1750 * @note Can be called by ISRs
1751 *
1752 * @param queue Address of the queue.
1753 * @param data Address of the data item.
1754 *
1755 * @return true if data item was removed
1756 */
1757static inline bool k_queue_remove(struct k_queue *queue, void *data)
1758{
1759 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1760}
1761
1762/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001763 * @brief Query a queue to see if it has data available.
1764 *
1765 * Note that the data might be already gone by the time this function returns
1766 * if other threads are also trying to read from the queue.
1767 *
1768 * @note Can be called by ISRs.
1769 *
1770 * @param queue Address of the queue.
1771 *
1772 * @return Non-zero if the queue is empty.
1773 * @return 0 if data is available.
1774 */
1775static inline int k_queue_is_empty(struct k_queue *queue)
1776{
1777 return (int)sys_slist_is_empty(&queue->data_q);
1778}
1779
1780/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001781 * @brief Peek element at the head of queue.
1782 *
1783 * Return element from the head of queue without removing it.
1784 *
1785 * @param queue Address of the queue.
1786 *
1787 * @return Head element, or NULL if queue is empty.
1788 */
1789static inline void *k_queue_peek_head(struct k_queue *queue)
1790{
1791 return sys_slist_peek_head(&queue->data_q);
1792}
1793
1794/**
1795 * @brief Peek element at the tail of queue.
1796 *
1797 * Return element from the tail of queue without removing it.
1798 *
1799 * @param queue Address of the queue.
1800 *
1801 * @return Tail element, or NULL if queue is empty.
1802 */
1803static inline void *k_queue_peek_tail(struct k_queue *queue)
1804{
1805 return sys_slist_peek_tail(&queue->data_q);
1806}
1807
1808/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001809 * @brief Statically define and initialize a queue.
1810 *
1811 * The queue can be accessed outside the module where it is defined using:
1812 *
1813 * @code extern struct k_queue <name>; @endcode
1814 *
1815 * @param name Name of the queue.
1816 */
1817#define K_QUEUE_DEFINE(name) \
1818 struct k_queue name \
1819 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001820 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001821
Anas Nashif166f5192018-02-25 08:02:36 -06001822/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001823
1824/**
1825 * @cond INTERNAL_HIDDEN
1826 */
1827
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001828struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001829 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001830};
1831
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001832#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001833 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001834 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001835 }
1836
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001837#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1838
Allan Stephensc98da842016-11-11 15:45:03 -05001839/**
1840 * INTERNAL_HIDDEN @endcond
1841 */
1842
1843/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001844 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05001845 * @ingroup kernel_apis
1846 * @{
1847 */
1848
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001849/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001850 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001851 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001852 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001853 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001854 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001855 *
1856 * @return N/A
1857 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001858#define k_fifo_init(fifo) \
1859 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001860
1861/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001862 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001863 *
1864 * This routine causes first thread pending on @a fifo, if any, to
1865 * return from k_fifo_get() call with NULL value (as if timeout
1866 * expired).
1867 *
1868 * @note Can be called by ISRs.
1869 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001870 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001871 *
1872 * @return N/A
1873 */
1874#define k_fifo_cancel_wait(fifo) \
1875 k_queue_cancel_wait((struct k_queue *) fifo)
1876
1877/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001878 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001879 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001880 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001881 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1882 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001883 *
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.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001887 * @param data Address of the data item.
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(fifo, data) \
1892 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001893
1894/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001895 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001897 * This routine adds a list of data items to @a fifo in one operation.
1898 * The data items must be in a singly-linked list, with the first 32 bits
1899 * each data item pointing to the next data item; the list must be
1900 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001901 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001902 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001903 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001904 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001905 * @param head Pointer to first node in singly-linked list.
1906 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001907 *
1908 * @return N/A
1909 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001910#define k_fifo_put_list(fifo, head, tail) \
1911 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001912
1913/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001914 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001915 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001916 * This routine adds a list of data items to @a fifo in one operation.
1917 * The data items must be in a singly-linked list implemented using a
1918 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001919 * and must be re-initialized via sys_slist_init().
1920 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001921 * @note Can be called by ISRs.
1922 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001923 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001924 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001925 *
1926 * @return N/A
1927 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001928#define k_fifo_put_slist(fifo, list) \
1929 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001930
1931/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001932 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001933 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001934 * This routine removes a data item from @a fifo in a "first in, first out"
1935 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001936 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001937 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1938 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001939 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001940 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001941 * or one of the special values K_NO_WAIT and K_FOREVER.
1942 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001943 * @return Address of the data item if successful; NULL if returned
1944 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001945 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001946#define k_fifo_get(fifo, timeout) \
1947 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001948
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001949/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001950 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001951 *
1952 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06001953 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001954 *
1955 * @note Can be called by ISRs.
1956 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001957 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001958 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001959 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001960 * @return 0 if data is available.
1961 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001962#define k_fifo_is_empty(fifo) \
1963 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001964
1965/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001966 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001967 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001968 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05301969 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001970 * on each iteration of processing, a head container will be peeked,
1971 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06001972 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001973 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001974 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001975 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001976 * @return Head element, or NULL if the FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001977 */
1978#define k_fifo_peek_head(fifo) \
1979 k_queue_peek_head((struct k_queue *) fifo)
1980
1981/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001982 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001983 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001984 * Return element from the tail of FIFO queue (without removing it). A usecase
1985 * for this is if elements of the FIFO queue are themselves containers. Then
1986 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001987 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001988 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001989 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001990 * @return Tail element, or NULL if a FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001991 */
1992#define k_fifo_peek_tail(fifo) \
1993 k_queue_peek_tail((struct k_queue *) fifo)
1994
1995/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001996 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001997 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001998 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001999 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002000 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002001 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002002 * @param name Name of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002003 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002004#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002005 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002006 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002007 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002008
Anas Nashif166f5192018-02-25 08:02:36 -06002009/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002010
2011/**
2012 * @cond INTERNAL_HIDDEN
2013 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002014
2015struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002016 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002017};
2018
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002019#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002020 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002021 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002022 }
2023
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002024#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2025
Allan Stephensc98da842016-11-11 15:45:03 -05002026/**
2027 * INTERNAL_HIDDEN @endcond
2028 */
2029
2030/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002031 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002032 * @ingroup kernel_apis
2033 * @{
2034 */
2035
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002036/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002037 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002038 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002039 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002040 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002041 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002042 *
2043 * @return N/A
2044 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002045#define k_lifo_init(lifo) \
2046 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002047
2048/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002049 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002050 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002051 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002052 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2053 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002054 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002055 * @note Can be called by ISRs.
2056 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002057 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002058 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002059 *
2060 * @return N/A
2061 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002062#define k_lifo_put(lifo, data) \
2063 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002064
2065/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002066 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002067 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002068 * This routine removes a data item from @a lifo in a "last in, first out"
2069 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002070 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002071 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2072 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002073 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002074 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002075 * or one of the special values K_NO_WAIT and K_FOREVER.
2076 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002077 * @return Address of the data item if successful; NULL if returned
2078 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002079 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002080#define k_lifo_get(lifo, timeout) \
2081 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002082
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002083/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002084 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002085 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002086 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002087 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002088 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002089 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002090 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002091 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002092#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002093 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002094 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002095 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002096
Anas Nashif166f5192018-02-25 08:02:36 -06002097/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002098
2099/**
2100 * @cond INTERNAL_HIDDEN
2101 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002102
2103struct k_stack {
2104 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002105 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002106
Anas Nashif2f203c22016-12-18 06:57:45 -05002107 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002108};
2109
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002110#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002111 { \
2112 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2113 .base = stack_buffer, \
2114 .next = stack_buffer, \
2115 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002116 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002117 }
2118
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002119#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2120
Allan Stephensc98da842016-11-11 15:45:03 -05002121/**
2122 * INTERNAL_HIDDEN @endcond
2123 */
2124
2125/**
2126 * @defgroup stack_apis Stack APIs
2127 * @ingroup kernel_apis
2128 * @{
2129 */
2130
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002131/**
2132 * @brief Initialize a stack.
2133 *
2134 * This routine initializes a stack object, prior to its first use.
2135 *
2136 * @param stack Address of the stack.
2137 * @param buffer Address of array used to hold stacked values.
2138 * @param num_entries Maximum number of values that can be stacked.
2139 *
2140 * @return N/A
2141 */
Andrew Boiee8734462017-09-29 16:42:07 -07002142__syscall void k_stack_init(struct k_stack *stack,
Andrew Boie8e3e6d02017-10-12 14:01:25 -07002143 u32_t *buffer, unsigned int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002144
2145/**
2146 * @brief Push an element onto a stack.
2147 *
2148 * This routine adds a 32-bit value @a data to @a stack.
2149 *
2150 * @note Can be called by ISRs.
2151 *
2152 * @param stack Address of the stack.
2153 * @param data Value to push onto the stack.
2154 *
2155 * @return N/A
2156 */
Andrew Boiee8734462017-09-29 16:42:07 -07002157__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002158
2159/**
2160 * @brief Pop an element from a stack.
2161 *
2162 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2163 * manner and stores the value in @a data.
2164 *
2165 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2166 *
2167 * @param stack Address of the stack.
2168 * @param data Address of area to hold the value popped from the stack.
2169 * @param timeout Waiting period to obtain a value (in milliseconds),
2170 * or one of the special values K_NO_WAIT and K_FOREVER.
2171 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002172 * @retval 0 Element popped from stack.
2173 * @retval -EBUSY Returned without waiting.
2174 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002175 */
Andrew Boiee8734462017-09-29 16:42:07 -07002176__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002177
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002178/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002179 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002180 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002181 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002182 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002183 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002184 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002185 * @param name Name of the stack.
2186 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002187 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002188#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002189 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002190 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002191 struct k_stack name \
2192 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002193 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002194 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002195
Anas Nashif166f5192018-02-25 08:02:36 -06002196/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002197
Allan Stephens6bba9b02016-11-16 14:56:54 -05002198struct k_work;
2199
Allan Stephensc98da842016-11-11 15:45:03 -05002200/**
2201 * @defgroup workqueue_apis Workqueue Thread APIs
2202 * @ingroup kernel_apis
2203 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002204 */
2205
Allan Stephens6bba9b02016-11-16 14:56:54 -05002206/**
2207 * @typedef k_work_handler_t
2208 * @brief Work item handler function type.
2209 *
2210 * A work item's handler function is executed by a workqueue's thread
2211 * when the work item is processed by the workqueue.
2212 *
2213 * @param work Address of the work item.
2214 *
2215 * @return N/A
2216 */
2217typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002218
2219/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002220 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002221 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002222
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002223struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002224 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002225 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002226};
2227
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002228enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002229 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002230};
2231
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002232struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002233 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002234 k_work_handler_t handler;
2235 atomic_t flags[1];
2236};
2237
Allan Stephens6bba9b02016-11-16 14:56:54 -05002238struct k_delayed_work {
2239 struct k_work work;
2240 struct _timeout timeout;
2241 struct k_work_q *work_q;
2242};
2243
2244extern struct k_work_q k_sys_work_q;
2245
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002246/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002247 * INTERNAL_HIDDEN @endcond
2248 */
2249
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002250#define _K_WORK_INITIALIZER(work_handler) \
2251 { \
2252 ._reserved = NULL, \
2253 .handler = work_handler, \
2254 .flags = { 0 } \
2255 }
2256
2257#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2258
Allan Stephens6bba9b02016-11-16 14:56:54 -05002259/**
2260 * @brief Initialize a statically-defined work item.
2261 *
2262 * This macro can be used to initialize a statically-defined workqueue work
2263 * item, prior to its first use. For example,
2264 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002265 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002266 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002267 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002268 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002269 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002270#define K_WORK_DEFINE(work, work_handler) \
2271 struct k_work work \
2272 __in_section(_k_work, static, work) = \
2273 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002274
2275/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002276 * @brief Initialize a work item.
2277 *
2278 * This routine initializes a workqueue work item, prior to its first use.
2279 *
2280 * @param work Address of work item.
2281 * @param handler Function to invoke each time work item is processed.
2282 *
2283 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002284 */
2285static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2286{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002287 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002288 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002289 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002290}
2291
2292/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002293 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002294 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002295 * This routine submits work item @a work to be processed by workqueue
2296 * @a work_q. If the work item is already pending in the workqueue's queue
2297 * as a result of an earlier submission, this routine has no effect on the
2298 * work item. If the work item has already been processed, or is currently
2299 * being processed, its work is considered complete and the work item can be
2300 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002301 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002302 * @warning
2303 * A submitted work item must not be modified until it has been processed
2304 * by the workqueue.
2305 *
2306 * @note Can be called by ISRs.
2307 *
2308 * @param work_q Address of workqueue.
2309 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002310 *
2311 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002312 */
2313static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2314 struct k_work *work)
2315{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002316 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002317 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002318 }
2319}
2320
2321/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002322 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002323 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002324 * This routine indicates if work item @a work is pending in a workqueue's
2325 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002326 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002327 * @note Can be called by ISRs.
2328 *
2329 * @param work Address of work item.
2330 *
2331 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002332 */
2333static inline int k_work_pending(struct k_work *work)
2334{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002335 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002336}
2337
2338/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002339 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002340 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002341 * This routine starts workqueue @a work_q. The workqueue spawns its work
2342 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002343 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002344 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002345 * @param stack Pointer to work queue thread's stack space, as defined by
2346 * K_THREAD_STACK_DEFINE()
2347 * @param stack_size Size of the work queue thread's stack (in bytes), which
2348 * should either be the same constant passed to
2349 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002350 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002351 *
2352 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002353 */
Andrew Boie507852a2017-07-25 18:47:07 -07002354extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002355 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002356 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002357
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002358/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002359 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002360 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002361 * This routine initializes a workqueue delayed work item, prior to
2362 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002363 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002364 * @param work Address of delayed work item.
2365 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002366 *
2367 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002368 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002369extern void k_delayed_work_init(struct k_delayed_work *work,
2370 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002371
2372/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002373 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002374 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002375 * This routine schedules work item @a work to be processed by workqueue
2376 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002377 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002378 * Only when the countdown completes is the work item actually submitted to
2379 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002380 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002381 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002382 * counting down cancels the existing submission and restarts the
2383 * countdown using the new delay. Note that this behavior is
2384 * inherently subject to race conditions with the pre-existing
2385 * timeouts and work queue, so care must be taken to synchronize such
2386 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002387 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002388 * @warning
2389 * A delayed work item must not be modified until it has been processed
2390 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002391 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002392 * @note Can be called by ISRs.
2393 *
2394 * @param work_q Address of workqueue.
2395 * @param work Address of delayed work item.
2396 * @param delay Delay before submitting the work item (in milliseconds).
2397 *
2398 * @retval 0 Work item countdown started.
2399 * @retval -EINPROGRESS Work item is already pending.
2400 * @retval -EINVAL Work item is being processed or has completed its work.
2401 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002402 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002403extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2404 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002405 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002406
2407/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002408 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002409 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002410 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002411 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002412 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002413 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002414 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002415 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002416 * @param work Address of delayed work item.
2417 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002418 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002419 * @retval -EINPROGRESS Work item is already pending.
2420 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002421 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002422extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002423
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002424/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002425 * @brief Submit a work item to the system workqueue.
2426 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002427 * This routine submits work item @a work to be processed by the system
2428 * workqueue. If the work item is already pending in the workqueue's queue
2429 * as a result of an earlier submission, this routine has no effect on the
2430 * work item. If the work item has already been processed, or is currently
2431 * being processed, its work is considered complete and the work item can be
2432 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002433 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002434 * @warning
2435 * Work items submitted to the system workqueue should avoid using handlers
2436 * that block or yield since this may prevent the system workqueue from
2437 * processing other work items in a timely manner.
2438 *
2439 * @note Can be called by ISRs.
2440 *
2441 * @param work Address of work item.
2442 *
2443 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002444 */
2445static inline void k_work_submit(struct k_work *work)
2446{
2447 k_work_submit_to_queue(&k_sys_work_q, work);
2448}
2449
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002450/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002451 * @brief Submit a delayed work item to the system workqueue.
2452 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002453 * This routine schedules work item @a work to be processed by the system
2454 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002455 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002456 * Only when the countdown completes is the work item actually submitted to
2457 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002458 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002459 * Submitting a previously submitted delayed work item that is still
2460 * counting down cancels the existing submission and restarts the countdown
2461 * using the new delay. If the work item is currently pending on the
2462 * workqueue's queue because the countdown has completed it is too late to
2463 * resubmit the item, and resubmission fails without impacting the work item.
2464 * If the work item has already been processed, or is currently being processed,
2465 * its work is considered complete and the work item can be resubmitted.
2466 *
2467 * @warning
2468 * Work items submitted to the system workqueue should avoid using handlers
2469 * that block or yield since this may prevent the system workqueue from
2470 * processing other work items in a timely manner.
2471 *
2472 * @note Can be called by ISRs.
2473 *
2474 * @param work Address of delayed work item.
2475 * @param delay Delay before submitting the work item (in milliseconds).
2476 *
2477 * @retval 0 Work item countdown started.
2478 * @retval -EINPROGRESS Work item is already pending.
2479 * @retval -EINVAL Work item is being processed or has completed its work.
2480 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002481 */
2482static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002483 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002484{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002485 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002486}
2487
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002488/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002489 * @brief Get time remaining before a delayed work gets scheduled.
2490 *
2491 * This routine computes the (approximate) time remaining before a
2492 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002493 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002494 *
2495 * @param work Delayed work item.
2496 *
2497 * @return Remaining time (in milliseconds).
2498 */
Kumar Galacc334c72017-04-21 10:55:34 -05002499static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002500{
2501 return _timeout_remaining_get(&work->timeout);
2502}
2503
Anas Nashif166f5192018-02-25 08:02:36 -06002504/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002505
Allan Stephensc98da842016-11-11 15:45:03 -05002506/**
2507 * @cond INTERNAL_HIDDEN
2508 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002509
2510struct k_mutex {
2511 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002512 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002513 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002514 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002515
Anas Nashif2f203c22016-12-18 06:57:45 -05002516 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002517};
2518
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002519#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002520 { \
2521 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2522 .owner = NULL, \
2523 .lock_count = 0, \
2524 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002525 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002526 }
2527
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002528#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2529
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002530/**
Allan Stephensc98da842016-11-11 15:45:03 -05002531 * INTERNAL_HIDDEN @endcond
2532 */
2533
2534/**
2535 * @defgroup mutex_apis Mutex APIs
2536 * @ingroup kernel_apis
2537 * @{
2538 */
2539
2540/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002541 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002542 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002543 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002544 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002545 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002546 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002547 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002548 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002549#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002550 struct k_mutex name \
2551 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002552 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002553
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002554/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002555 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002556 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002557 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002558 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002559 * Upon completion, the mutex is available and does not have an owner.
2560 *
2561 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002562 *
2563 * @return N/A
2564 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002565__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002566
2567/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002568 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002569 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002570 * This routine locks @a mutex. If the mutex is locked by another thread,
2571 * the calling thread waits until the mutex becomes available or until
2572 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002573 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002574 * A thread is permitted to lock a mutex it has already locked. The operation
2575 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002576 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002577 * @param mutex Address of the mutex.
2578 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002579 * or one of the special values K_NO_WAIT and K_FOREVER.
2580 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002581 * @retval 0 Mutex locked.
2582 * @retval -EBUSY Returned without waiting.
2583 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002584 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002585__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002586
2587/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002588 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002589 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002590 * This routine unlocks @a mutex. The mutex must already be locked by the
2591 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002592 *
2593 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002594 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002595 * thread.
2596 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002597 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002598 *
2599 * @return N/A
2600 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002601__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002602
Allan Stephensc98da842016-11-11 15:45:03 -05002603/**
Anas Nashif166f5192018-02-25 08:02:36 -06002604 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002605 */
2606
2607/**
2608 * @cond INTERNAL_HIDDEN
2609 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002610
2611struct k_sem {
2612 _wait_q_t wait_q;
2613 unsigned int count;
2614 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002615 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002616
Anas Nashif2f203c22016-12-18 06:57:45 -05002617 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002618};
2619
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002620#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002621 { \
2622 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2623 .count = initial_count, \
2624 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002625 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002626 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002627 }
2628
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002629#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2630
Allan Stephensc98da842016-11-11 15:45:03 -05002631/**
2632 * INTERNAL_HIDDEN @endcond
2633 */
2634
2635/**
2636 * @defgroup semaphore_apis Semaphore APIs
2637 * @ingroup kernel_apis
2638 * @{
2639 */
2640
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002641/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002642 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002643 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002644 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002645 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002646 * @param sem Address of the semaphore.
2647 * @param initial_count Initial semaphore count.
2648 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002649 *
2650 * @return N/A
2651 */
Andrew Boie99280232017-09-29 14:17:47 -07002652__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2653 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002654
2655/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002656 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002657 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002658 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002659 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002660 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2661 *
2662 * @param sem Address of the semaphore.
2663 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002664 * or one of the special values K_NO_WAIT and K_FOREVER.
2665 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002666 * @note When porting code from the nanokernel legacy API to the new API, be
2667 * careful with the return value of this function. The return value is the
2668 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2669 * non-zero means failure, while the nano_sem_take family returns 1 for success
2670 * and 0 for failure.
2671 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002672 * @retval 0 Semaphore taken.
2673 * @retval -EBUSY Returned without waiting.
2674 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002675 */
Andrew Boie99280232017-09-29 14:17:47 -07002676__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002677
2678/**
2679 * @brief Give a semaphore.
2680 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002681 * This routine gives @a sem, unless the semaphore is already at its maximum
2682 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002683 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002684 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002685 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002686 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002687 *
2688 * @return N/A
2689 */
Andrew Boie99280232017-09-29 14:17:47 -07002690__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002691
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002692/**
2693 * @brief Reset a semaphore's count to zero.
2694 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002695 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002696 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002697 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002698 *
2699 * @return N/A
2700 */
Andrew Boie990bf162017-10-03 12:36:49 -07002701__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002702
Anas Nashif954d5502018-02-25 08:37:28 -06002703/**
2704 * @internal
2705 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002706static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002707{
2708 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002709}
2710
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002711/**
2712 * @brief Get a semaphore's count.
2713 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002714 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002716 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002717 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002718 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002719 */
Andrew Boie990bf162017-10-03 12:36:49 -07002720__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002721
Anas Nashif954d5502018-02-25 08:37:28 -06002722/**
2723 * @internal
2724 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002725static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002726{
2727 return sem->count;
2728}
2729
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002730/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002731 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002732 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002733 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002734 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002735 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002736 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002737 * @param name Name of the semaphore.
2738 * @param initial_count Initial semaphore count.
2739 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002740 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002741#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002742 struct k_sem name \
2743 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07002744 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05302745 BUILD_ASSERT(((count_limit) != 0) && \
2746 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002747
Anas Nashif166f5192018-02-25 08:02:36 -06002748/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002749
2750/**
2751 * @defgroup alert_apis Alert APIs
2752 * @ingroup kernel_apis
2753 * @{
2754 */
2755
Allan Stephens5eceb852016-11-16 10:16:30 -05002756/**
2757 * @typedef k_alert_handler_t
2758 * @brief Alert handler function type.
2759 *
2760 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002761 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002762 * and is only invoked if the alert has been initialized with one.
2763 *
2764 * @param alert Address of the alert.
2765 *
2766 * @return 0 if alert has been consumed; non-zero if alert should pend.
2767 */
2768typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002769
Anas Nashif166f5192018-02-25 08:02:36 -06002770/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002771
2772/**
2773 * @cond INTERNAL_HIDDEN
2774 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002775
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002776#define K_ALERT_DEFAULT NULL
2777#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002778
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002779struct k_alert {
2780 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002781 atomic_t send_count;
2782 struct k_work work_item;
2783 struct k_sem sem;
2784
Anas Nashif2f203c22016-12-18 06:57:45 -05002785 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002786};
2787
Anas Nashif954d5502018-02-25 08:37:28 -06002788/**
2789 * @internal
2790 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002791extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002792
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002793#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002794 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002795 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002796 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002797 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2798 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002799 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002800 }
2801
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002802#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2803
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002804/**
Allan Stephensc98da842016-11-11 15:45:03 -05002805 * INTERNAL_HIDDEN @endcond
2806 */
2807
2808/**
2809 * @addtogroup alert_apis
2810 * @{
2811 */
2812
2813/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002814 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002815 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002816 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002817 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002818 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002820 * @param name Name of the alert.
2821 * @param alert_handler Action to take when alert is sent. Specify either
2822 * the address of a function to be invoked by the system workqueue
2823 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2824 * K_ALERT_DEFAULT (which causes the alert to pend).
2825 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002826 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002827#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002828 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002829 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002830 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002831 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002832
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002833/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002834 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002835 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002836 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002837 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002838 * @param alert Address of the alert.
2839 * @param handler Action to take when alert is sent. Specify either the address
2840 * of a function to be invoked by the system workqueue thread,
2841 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2842 * K_ALERT_DEFAULT (which causes the alert to pend).
2843 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002844 *
2845 * @return N/A
2846 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002847extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2848 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002849
2850/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002851 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002852 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002853 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002854 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002855 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2856 *
2857 * @param alert Address of the alert.
2858 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002859 * or one of the special values K_NO_WAIT and K_FOREVER.
2860 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002861 * @retval 0 Alert received.
2862 * @retval -EBUSY Returned without waiting.
2863 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002864 */
Andrew Boie310e9872017-09-29 04:41:15 -07002865__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002866
2867/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002868 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002869 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002870 * This routine signals @a alert. The action specified for @a alert will
2871 * be taken, which may trigger the execution of an alert handler function
2872 * and/or cause the alert to pend (assuming the alert has not reached its
2873 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002874 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002875 * @note Can be called by ISRs.
2876 *
2877 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002878 *
2879 * @return N/A
2880 */
Andrew Boie310e9872017-09-29 04:41:15 -07002881__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002882
2883/**
Anas Nashif166f5192018-02-25 08:02:36 -06002884 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002885 */
2886
Allan Stephensc98da842016-11-11 15:45:03 -05002887/**
2888 * @cond INTERNAL_HIDDEN
2889 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002890
2891struct k_msgq {
2892 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002893 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002894 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002895 char *buffer_start;
2896 char *buffer_end;
2897 char *read_ptr;
2898 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002899 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002900
Anas Nashif2f203c22016-12-18 06:57:45 -05002901 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002902};
2903
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002904#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002905 { \
2906 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002907 .max_msgs = q_max_msgs, \
2908 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002909 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002910 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002911 .read_ptr = q_buffer, \
2912 .write_ptr = q_buffer, \
2913 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002914 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002915 }
2916
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002917#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2918
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05302919struct k_msgq_attrs {
2920 size_t msg_size;
2921 u32_t max_msgs;
2922 u32_t used_msgs;
2923};
2924
Peter Mitsis1da807e2016-10-06 11:36:59 -04002925/**
Allan Stephensc98da842016-11-11 15:45:03 -05002926 * INTERNAL_HIDDEN @endcond
2927 */
2928
2929/**
2930 * @defgroup msgq_apis Message Queue APIs
2931 * @ingroup kernel_apis
2932 * @{
2933 */
2934
2935/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002936 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002937 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002938 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2939 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002940 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2941 * message is similarly aligned to this boundary, @a q_msg_size must also be
2942 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002943 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002944 * The message queue can be accessed outside the module where it is defined
2945 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002946 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002947 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002948 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002949 * @param q_name Name of the message queue.
2950 * @param q_msg_size Message size (in bytes).
2951 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002952 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002953 */
2954#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2955 static char __noinit __aligned(q_align) \
2956 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002957 struct k_msgq q_name \
2958 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002959 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002960 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002961
Peter Mitsisd7a37502016-10-13 11:37:40 -04002962/**
2963 * @brief Initialize a message queue.
2964 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002965 * This routine initializes a message queue object, prior to its first use.
2966 *
Allan Stephensda827222016-11-09 14:23:58 -06002967 * The message queue's ring buffer must contain space for @a max_msgs messages,
2968 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2969 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2970 * that each message is similarly aligned to this boundary, @a q_msg_size
2971 * must also be a multiple of N.
2972 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002973 * @param q Address of the message queue.
2974 * @param buffer Pointer to ring buffer that holds queued messages.
2975 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002976 * @param max_msgs Maximum number of messages that can be queued.
2977 *
2978 * @return N/A
2979 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002980__syscall void k_msgq_init(struct k_msgq *q, char *buffer,
2981 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002982
2983/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002984 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002986 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002987 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002988 * @note Can be called by ISRs.
2989 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002990 * @param q Address of the message queue.
2991 * @param data Pointer to the message.
2992 * @param timeout Waiting period to add the message (in milliseconds),
2993 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002994 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002995 * @retval 0 Message sent.
2996 * @retval -ENOMSG Returned without waiting or queue purged.
2997 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002998 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002999__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003000
3001/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003002 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003003 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003004 * This routine receives a message from message queue @a q in a "first in,
3005 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003006 *
Allan Stephensc98da842016-11-11 15:45:03 -05003007 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003008 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003009 * @param q Address of the message queue.
3010 * @param data Address of area to hold the received message.
3011 * @param timeout Waiting period to receive the message (in milliseconds),
3012 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003013 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003014 * @retval 0 Message received.
3015 * @retval -ENOMSG Returned without waiting.
3016 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003017 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003018__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003019
3020/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003021 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003022 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003023 * This routine discards all unreceived messages in a message queue's ring
3024 * buffer. Any threads that are blocked waiting to send a message to the
3025 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003026 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003027 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003028 *
3029 * @return N/A
3030 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003031__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003032
Peter Mitsis67be2492016-10-07 11:44:34 -04003033/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003034 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003035 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003036 * This routine returns the number of unused entries in a message queue's
3037 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003038 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003039 * @param q Address of the message queue.
3040 *
3041 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04003042 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003043__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3044
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303045/**
3046 * @brief Get basic attributes of a message queue.
3047 *
3048 * This routine fetches basic attributes of message queue into attr argument.
3049 *
3050 * @param q Address of the message queue.
3051 * @param attrs pointer to message queue attribute structure.
3052 *
3053 * @return N/A
3054 */
3055__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3056
3057
Andrew Boie82edb6e2017-10-02 10:53:06 -07003058static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003059{
3060 return q->max_msgs - q->used_msgs;
3061}
3062
Peter Mitsisd7a37502016-10-13 11:37:40 -04003063/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003064 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003065 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003066 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003067 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003068 * @param q Address of the message queue.
3069 *
3070 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003071 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003072__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3073
3074static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003075{
3076 return q->used_msgs;
3077}
3078
Anas Nashif166f5192018-02-25 08:02:36 -06003079/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003080
3081/**
3082 * @defgroup mem_pool_apis Memory Pool APIs
3083 * @ingroup kernel_apis
3084 * @{
3085 */
3086
Andy Ross73cb9582017-05-09 10:42:39 -07003087/* Note on sizing: the use of a 20 bit field for block means that,
3088 * assuming a reasonable minimum block size of 16 bytes, we're limited
3089 * to 16M of memory managed by a single pool. Long term it would be
3090 * good to move to a variable bit size based on configuration.
3091 */
3092struct k_mem_block_id {
3093 u32_t pool : 8;
3094 u32_t level : 4;
3095 u32_t block : 20;
3096};
3097
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003098struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003099 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003100 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003101};
3102
Anas Nashif166f5192018-02-25 08:02:36 -06003103/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003104
3105/**
3106 * @defgroup mailbox_apis Mailbox APIs
3107 * @ingroup kernel_apis
3108 * @{
3109 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003110
3111struct k_mbox_msg {
3112 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003113 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003114 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003115 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003116 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003117 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003118 /** sender's message data buffer */
3119 void *tx_data;
3120 /** internal use only - needed for legacy API support */
3121 void *_rx_data;
3122 /** message data block descriptor */
3123 struct k_mem_block tx_block;
3124 /** source thread id */
3125 k_tid_t rx_source_thread;
3126 /** target thread id */
3127 k_tid_t tx_target_thread;
3128 /** internal use only - thread waiting on send (may be a dummy) */
3129 k_tid_t _syncing_thread;
3130#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3131 /** internal use only - semaphore used during asynchronous send */
3132 struct k_sem *_async_sem;
3133#endif
3134};
3135
Allan Stephensc98da842016-11-11 15:45:03 -05003136/**
3137 * @cond INTERNAL_HIDDEN
3138 */
3139
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003140struct k_mbox {
3141 _wait_q_t tx_msg_queue;
3142 _wait_q_t rx_msg_queue;
3143
Anas Nashif2f203c22016-12-18 06:57:45 -05003144 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003145};
3146
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003147#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003148 { \
3149 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3150 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003151 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003152 }
3153
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003154#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3155
Peter Mitsis12092702016-10-14 12:57:23 -04003156/**
Allan Stephensc98da842016-11-11 15:45:03 -05003157 * INTERNAL_HIDDEN @endcond
3158 */
3159
3160/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003161 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003162 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003163 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003164 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003165 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003166 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003167 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003168 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003169#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003170 struct k_mbox name \
3171 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003172 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003173
Peter Mitsis12092702016-10-14 12:57:23 -04003174/**
3175 * @brief Initialize a mailbox.
3176 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003177 * This routine initializes a mailbox object, prior to its first use.
3178 *
3179 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003180 *
3181 * @return N/A
3182 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003183extern void k_mbox_init(struct k_mbox *mbox);
3184
Peter Mitsis12092702016-10-14 12:57:23 -04003185/**
3186 * @brief Send a mailbox message in a synchronous manner.
3187 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003188 * This routine sends a message to @a mbox and waits for a receiver to both
3189 * receive and process it. The message data may be in a buffer, in a memory
3190 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003191 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003192 * @param mbox Address of the mailbox.
3193 * @param tx_msg Address of the transmit message descriptor.
3194 * @param timeout Waiting period for the message to be received (in
3195 * milliseconds), or one of the special values K_NO_WAIT
3196 * and K_FOREVER. Once the message has been received,
3197 * this routine waits as long as necessary for the message
3198 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003199 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003200 * @retval 0 Message sent.
3201 * @retval -ENOMSG Returned without waiting.
3202 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003203 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003204extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003205 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003206
Peter Mitsis12092702016-10-14 12:57:23 -04003207/**
3208 * @brief Send a mailbox message in an asynchronous manner.
3209 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003210 * This routine sends a message to @a mbox without waiting for a receiver
3211 * to process it. The message data may be in a buffer, in a memory pool block,
3212 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3213 * will be given when the message has been both received and completely
3214 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003215 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003216 * @param mbox Address of the mailbox.
3217 * @param tx_msg Address of the transmit message descriptor.
3218 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003219 *
3220 * @return N/A
3221 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003222extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003223 struct k_sem *sem);
3224
Peter Mitsis12092702016-10-14 12:57:23 -04003225/**
3226 * @brief Receive a mailbox message.
3227 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003228 * This routine receives a message from @a mbox, then optionally retrieves
3229 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003230 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003231 * @param mbox Address of the mailbox.
3232 * @param rx_msg Address of the receive message descriptor.
3233 * @param buffer Address of the buffer to receive data, or NULL to defer data
3234 * retrieval and message disposal until later.
3235 * @param timeout Waiting period for a message to be received (in
3236 * milliseconds), or one of the special values K_NO_WAIT
3237 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003238 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003239 * @retval 0 Message received.
3240 * @retval -ENOMSG Returned without waiting.
3241 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003242 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003243extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003244 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003245
3246/**
3247 * @brief Retrieve mailbox message data into a buffer.
3248 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003249 * This routine completes the processing of a received message by retrieving
3250 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003251 *
3252 * Alternatively, this routine can be used to dispose of a received message
3253 * without retrieving its data.
3254 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003255 * @param rx_msg Address of the receive message descriptor.
3256 * @param buffer Address of the buffer to receive data, or NULL to discard
3257 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003258 *
3259 * @return N/A
3260 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003261extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003262
3263/**
3264 * @brief Retrieve mailbox message data into a memory pool block.
3265 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003266 * This routine completes the processing of a received message by retrieving
3267 * its data into a memory pool block, then disposing of the message.
3268 * The memory pool block that results from successful retrieval must be
3269 * returned to the pool once the data has been processed, even in cases
3270 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003271 *
3272 * Alternatively, this routine can be used to dispose of a received message
3273 * without retrieving its data. In this case there is no need to return a
3274 * memory pool block to the pool.
3275 *
3276 * This routine allocates a new memory pool block for the data only if the
3277 * data is not already in one. If a new block cannot be allocated, the routine
3278 * returns a failure code and the received message is left unchanged. This
3279 * permits the caller to reattempt data retrieval at a later time or to dispose
3280 * of the received message without retrieving its data.
3281 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003282 * @param rx_msg Address of a receive message descriptor.
3283 * @param pool Address of memory pool, or NULL to discard data.
3284 * @param block Address of the area to hold memory pool block info.
3285 * @param timeout Waiting period to wait for a memory pool block (in
3286 * milliseconds), or one of the special values K_NO_WAIT
3287 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003288 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003289 * @retval 0 Data retrieved.
3290 * @retval -ENOMEM Returned without waiting.
3291 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003292 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003293extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003294 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003295 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003296
Anas Nashif166f5192018-02-25 08:02:36 -06003297/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003298
3299/**
3300 * @cond INTERNAL_HIDDEN
3301 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003302
3303struct k_pipe {
3304 unsigned char *buffer; /* Pipe buffer: may be NULL */
3305 size_t size; /* Buffer size */
3306 size_t bytes_used; /* # bytes used in buffer */
3307 size_t read_index; /* Where in buffer to read from */
3308 size_t write_index; /* Where in buffer to write */
3309
3310 struct {
3311 _wait_q_t readers; /* Reader wait queue */
3312 _wait_q_t writers; /* Writer wait queue */
3313 } wait_q;
3314
Anas Nashif2f203c22016-12-18 06:57:45 -05003315 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003316};
3317
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003318#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003319 { \
3320 .buffer = pipe_buffer, \
3321 .size = pipe_buffer_size, \
3322 .bytes_used = 0, \
3323 .read_index = 0, \
3324 .write_index = 0, \
3325 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3326 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003327 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003328 }
3329
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003330#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3331
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003332/**
Allan Stephensc98da842016-11-11 15:45:03 -05003333 * INTERNAL_HIDDEN @endcond
3334 */
3335
3336/**
3337 * @defgroup pipe_apis Pipe APIs
3338 * @ingroup kernel_apis
3339 * @{
3340 */
3341
3342/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003343 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003344 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003345 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003346 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003347 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003348 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003349 * @param name Name of the pipe.
3350 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3351 * or zero if no ring buffer is used.
3352 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003353 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003354#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3355 static unsigned char __noinit __aligned(pipe_align) \
3356 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003357 struct k_pipe name \
3358 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003359 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003360
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003361/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003362 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003363 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003364 * This routine initializes a pipe object, prior to its first use.
3365 *
3366 * @param pipe Address of the pipe.
3367 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3368 * is used.
3369 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3370 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003371 *
3372 * @return N/A
3373 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003374__syscall void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3375 size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003376
3377/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003378 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003379 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003380 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003381 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003382 * @param pipe Address of the pipe.
3383 * @param data Address of data to write.
3384 * @param bytes_to_write Size of data (in bytes).
3385 * @param bytes_written Address of area to hold the number of bytes written.
3386 * @param min_xfer Minimum number of bytes to write.
3387 * @param timeout Waiting period to wait for the data to be written (in
3388 * milliseconds), or one of the special values K_NO_WAIT
3389 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003390 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003391 * @retval 0 At least @a min_xfer bytes of data were written.
3392 * @retval -EIO Returned without waiting; zero data bytes were written.
3393 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003394 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003395 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003396__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3397 size_t bytes_to_write, size_t *bytes_written,
3398 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003399
3400/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003401 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003402 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003403 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003404 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003405 * @param pipe Address of the pipe.
3406 * @param data Address to place the data read from pipe.
3407 * @param bytes_to_read Maximum number of data bytes to read.
3408 * @param bytes_read Address of area to hold the number of bytes read.
3409 * @param min_xfer Minimum number of data bytes to read.
3410 * @param timeout Waiting period to wait for the data to be read (in
3411 * milliseconds), or one of the special values K_NO_WAIT
3412 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003413 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003414 * @retval 0 At least @a min_xfer bytes of data were read.
3415 * @retval -EIO Returned without waiting; zero data bytes were read.
3416 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003417 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003418 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003419__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3420 size_t bytes_to_read, size_t *bytes_read,
3421 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003422
3423/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003424 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003425 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003426 * This routine writes the data contained in a memory block to @a pipe.
3427 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003428 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003429 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003430 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003431 * @param block Memory block containing data to send
3432 * @param size Number of data bytes in memory block to send
3433 * @param sem Semaphore to signal upon completion (else NULL)
3434 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003435 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003436 */
3437extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3438 size_t size, struct k_sem *sem);
3439
Anas Nashif166f5192018-02-25 08:02:36 -06003440/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003441
Allan Stephensc98da842016-11-11 15:45:03 -05003442/**
3443 * @cond INTERNAL_HIDDEN
3444 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003445
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003446struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003447 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003448 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003449 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003450 char *buffer;
3451 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003452 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003453
Anas Nashif2f203c22016-12-18 06:57:45 -05003454 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003455};
3456
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003457#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003458 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003459 { \
3460 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003461 .num_blocks = slab_num_blocks, \
3462 .block_size = slab_block_size, \
3463 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003464 .free_list = NULL, \
3465 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003466 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003467 }
3468
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003469#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3470
3471
Peter Mitsis578f9112016-10-07 13:50:31 -04003472/**
Allan Stephensc98da842016-11-11 15:45:03 -05003473 * INTERNAL_HIDDEN @endcond
3474 */
3475
3476/**
3477 * @defgroup mem_slab_apis Memory Slab APIs
3478 * @ingroup kernel_apis
3479 * @{
3480 */
3481
3482/**
Allan Stephensda827222016-11-09 14:23:58 -06003483 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003484 *
Allan Stephensda827222016-11-09 14:23:58 -06003485 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003486 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003487 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3488 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003489 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003490 *
Allan Stephensda827222016-11-09 14:23:58 -06003491 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003492 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003493 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003494 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003495 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003496 * @param name Name of the memory slab.
3497 * @param slab_block_size Size of each memory block (in bytes).
3498 * @param slab_num_blocks Number memory blocks.
3499 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003500 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003501#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3502 char __noinit __aligned(slab_align) \
3503 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3504 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003505 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003506 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003507 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003508
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003509/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003510 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003511 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003512 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003513 *
Allan Stephensda827222016-11-09 14:23:58 -06003514 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3515 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3516 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3517 * To ensure that each memory block is similarly aligned to this boundary,
3518 * @a slab_block_size must also be a multiple of N.
3519 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003520 * @param slab Address of the memory slab.
3521 * @param buffer Pointer to buffer used for the memory blocks.
3522 * @param block_size Size of each memory block (in bytes).
3523 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003524 *
3525 * @return N/A
3526 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003527extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003528 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003529
3530/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003531 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003532 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003533 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003534 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003535 * @param slab Address of the memory slab.
3536 * @param mem Pointer to block address area.
3537 * @param timeout Maximum time to wait for operation to complete
3538 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3539 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003540 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003541 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003542 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003543 * @retval -ENOMEM Returned without waiting.
3544 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003545 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003546extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003547 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003548
3549/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003550 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003551 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003552 * This routine releases a previously allocated memory block back to its
3553 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003554 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003555 * @param slab Address of the memory slab.
3556 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003557 *
3558 * @return N/A
3559 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003560extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003561
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003562/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003563 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003564 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003565 * This routine gets the number of memory blocks that are currently
3566 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003567 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003568 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003569 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003570 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003571 */
Kumar Galacc334c72017-04-21 10:55:34 -05003572static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003573{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003574 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003575}
3576
Peter Mitsisc001aa82016-10-13 13:53:37 -04003577/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003578 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003579 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003580 * This routine gets the number of memory blocks that are currently
3581 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003582 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003583 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003584 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003585 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003586 */
Kumar Galacc334c72017-04-21 10:55:34 -05003587static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003588{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003589 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003590}
3591
Anas Nashif166f5192018-02-25 08:02:36 -06003592/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003593
3594/**
3595 * @cond INTERNAL_HIDDEN
3596 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003597
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003598struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003599 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003600 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003601};
3602
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003603/**
Allan Stephensc98da842016-11-11 15:45:03 -05003604 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003605 */
3606
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003607/**
Allan Stephensc98da842016-11-11 15:45:03 -05003608 * @addtogroup mem_pool_apis
3609 * @{
3610 */
3611
3612/**
3613 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003614 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003615 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3616 * long. The memory pool allows blocks to be repeatedly partitioned into
3617 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003618 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003619 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003620 * If the pool is to be accessed outside the module where it is defined, it
3621 * can be declared via
3622 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003623 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003624 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003625 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003626 * @param minsz Size of the smallest blocks in the pool (in bytes).
3627 * @param maxsz Size of the largest blocks in the pool (in bytes).
3628 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003629 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003630 */
Andy Ross73cb9582017-05-09 10:42:39 -07003631#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3632 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3633 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08003634 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07003635 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08003636 .base = { \
3637 .buf = _mpool_buf_##name, \
3638 .max_sz = maxsz, \
3639 .n_max = nmax, \
3640 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3641 .levels = _mpool_lvls_##name, \
3642 .flags = SYS_MEM_POOL_KERNEL \
3643 } \
Andy Ross73cb9582017-05-09 10:42:39 -07003644 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003645
Peter Mitsis937042c2016-10-13 13:18:26 -04003646/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003647 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003648 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003649 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003650 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003651 * @param pool Address of the memory pool.
3652 * @param block Pointer to block descriptor for the allocated memory.
3653 * @param size Amount of memory to allocate (in bytes).
3654 * @param timeout Maximum time to wait for operation to complete
3655 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3656 * or K_FOREVER to wait as long as necessary.
3657 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003658 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003659 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003660 * @retval -ENOMEM Returned without waiting.
3661 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003662 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003663extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003664 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003665
3666/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07003667 * @brief Allocate memory from a memory pool with malloc() semantics
3668 *
3669 * Such memory must be released using k_free().
3670 *
3671 * @param pool Address of the memory pool.
3672 * @param size Amount of memory to allocate (in bytes).
3673 * @return Address of the allocated memory if successful, otherwise NULL
3674 */
3675extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
3676
3677/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003678 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003679 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003680 * This routine releases a previously allocated memory block back to its
3681 * memory pool.
3682 *
3683 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003684 *
3685 * @return N/A
3686 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003687extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003688
3689/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003690 * @brief Free memory allocated from a memory pool.
3691 *
3692 * This routine releases a previously allocated memory block back to its
3693 * memory pool
3694 *
3695 * @param id Memory block identifier.
3696 *
3697 * @return N/A
3698 */
3699extern void k_mem_pool_free_id(struct k_mem_block_id *id);
3700
3701/**
Anas Nashif166f5192018-02-25 08:02:36 -06003702 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003703 */
3704
3705/**
3706 * @defgroup heap_apis Heap Memory Pool APIs
3707 * @ingroup kernel_apis
3708 * @{
3709 */
3710
3711/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003712 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003713 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003714 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003715 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003716 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003717 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003718 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003719 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003720 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003721extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003722
3723/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003724 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003725 *
3726 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07003727 * returned must have been allocated from the heap memory pool or
3728 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04003729 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003730 * If @a ptr is NULL, no operation is performed.
3731 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003732 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003733 *
3734 * @return N/A
3735 */
3736extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003737
Allan Stephensc98da842016-11-11 15:45:03 -05003738/**
Andrew Boie7f95e832017-11-08 14:40:01 -08003739 * @brief Allocate memory from heap, array style
3740 *
3741 * This routine provides traditional calloc() semantics. Memory is
3742 * allocated from the heap memory pool and zeroed.
3743 *
3744 * @param nmemb Number of elements in the requested array
3745 * @param size Size of each array element (in bytes).
3746 *
3747 * @return Address of the allocated memory if successful; otherwise NULL.
3748 */
3749extern void *k_calloc(size_t nmemb, size_t size);
3750
Anas Nashif166f5192018-02-25 08:02:36 -06003751/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003752
Benjamin Walshacc68c12017-01-29 18:57:45 -05003753/* polling API - PRIVATE */
3754
Benjamin Walshb0179862017-02-02 16:39:57 -05003755#ifdef CONFIG_POLL
3756#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3757#else
3758#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3759#endif
3760
Benjamin Walshacc68c12017-01-29 18:57:45 -05003761/* private - implementation data created as needed, per-type */
3762struct _poller {
3763 struct k_thread *thread;
3764};
3765
3766/* private - types bit positions */
3767enum _poll_types_bits {
3768 /* can be used to ignore an event */
3769 _POLL_TYPE_IGNORE,
3770
3771 /* to be signaled by k_poll_signal() */
3772 _POLL_TYPE_SIGNAL,
3773
3774 /* semaphore availability */
3775 _POLL_TYPE_SEM_AVAILABLE,
3776
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003777 /* queue/fifo/lifo data availability */
3778 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003779
3780 _POLL_NUM_TYPES
3781};
3782
3783#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3784
3785/* private - states bit positions */
3786enum _poll_states_bits {
3787 /* default state when creating event */
3788 _POLL_STATE_NOT_READY,
3789
Benjamin Walshacc68c12017-01-29 18:57:45 -05003790 /* signaled by k_poll_signal() */
3791 _POLL_STATE_SIGNALED,
3792
3793 /* semaphore is available */
3794 _POLL_STATE_SEM_AVAILABLE,
3795
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003796 /* data is available to read on queue/fifo/lifo */
3797 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003798
3799 _POLL_NUM_STATES
3800};
3801
3802#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3803
3804#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003805 (32 - (0 \
3806 + 8 /* tag */ \
3807 + _POLL_NUM_TYPES \
3808 + _POLL_NUM_STATES \
3809 + 1 /* modes */ \
3810 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003811
Benjamin Walshacc68c12017-01-29 18:57:45 -05003812/* end of polling API - PRIVATE */
3813
3814
3815/**
3816 * @defgroup poll_apis Async polling APIs
3817 * @ingroup kernel_apis
3818 * @{
3819 */
3820
3821/* Public polling API */
3822
3823/* public - values for k_poll_event.type bitfield */
3824#define K_POLL_TYPE_IGNORE 0
3825#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3826#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003827#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3828#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003829
3830/* public - polling modes */
3831enum k_poll_modes {
3832 /* polling thread does not take ownership of objects when available */
3833 K_POLL_MODE_NOTIFY_ONLY = 0,
3834
3835 K_POLL_NUM_MODES
3836};
3837
3838/* public - values for k_poll_event.state bitfield */
3839#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003840#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3841#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003842#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3843#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003844
3845/* public - poll signal object */
3846struct k_poll_signal {
3847 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003848 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003849
3850 /*
3851 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3852 * user resets it to 0.
3853 */
3854 unsigned int signaled;
3855
3856 /* custom result value passed to k_poll_signal() if needed */
3857 int result;
3858};
3859
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003860#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003861 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003862 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003863 .signaled = 0, \
3864 .result = 0, \
3865 }
3866
3867struct k_poll_event {
3868 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003869 sys_dnode_t _node;
3870
3871 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003872 struct _poller *poller;
3873
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003874 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003875 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003876
Benjamin Walshacc68c12017-01-29 18:57:45 -05003877 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003878 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003879
3880 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003881 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003882
3883 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003884 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003885
3886 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003887 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003888
3889 /* per-type data */
3890 union {
3891 void *obj;
3892 struct k_poll_signal *signal;
3893 struct k_sem *sem;
3894 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003895 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003896 };
3897};
3898
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003899#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003900 { \
3901 .poller = NULL, \
3902 .type = event_type, \
3903 .state = K_POLL_STATE_NOT_READY, \
3904 .mode = event_mode, \
3905 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003906 { .obj = event_obj }, \
3907 }
3908
3909#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3910 event_tag) \
3911 { \
3912 .type = event_type, \
3913 .tag = event_tag, \
3914 .state = K_POLL_STATE_NOT_READY, \
3915 .mode = event_mode, \
3916 .unused = 0, \
3917 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003918 }
3919
3920/**
3921 * @brief Initialize one struct k_poll_event instance
3922 *
3923 * After this routine is called on a poll event, the event it ready to be
3924 * placed in an event array to be passed to k_poll().
3925 *
3926 * @param event The event to initialize.
3927 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3928 * values. Only values that apply to the same object being polled
3929 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3930 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003931 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003932 * @param obj Kernel object or poll signal.
3933 *
3934 * @return N/A
3935 */
3936
Kumar Galacc334c72017-04-21 10:55:34 -05003937extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003938 int mode, void *obj);
3939
3940/**
3941 * @brief Wait for one or many of multiple poll events to occur
3942 *
3943 * This routine allows a thread to wait concurrently for one or many of
3944 * multiple poll events to have occurred. Such events can be a kernel object
3945 * being available, like a semaphore, or a poll signal event.
3946 *
3947 * When an event notifies that a kernel object is available, the kernel object
3948 * is not "given" to the thread calling k_poll(): it merely signals the fact
3949 * that the object was available when the k_poll() call was in effect. Also,
3950 * all threads trying to acquire an object the regular way, i.e. by pending on
3951 * the object, have precedence over the thread polling on the object. This
3952 * means that the polling thread will never get the poll event on an object
3953 * until the object becomes available and its pend queue is empty. For this
3954 * reason, the k_poll() call is more effective when the objects being polled
3955 * only have one thread, the polling thread, trying to acquire them.
3956 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003957 * When k_poll() returns 0, the caller should loop on all the events that were
3958 * passed to k_poll() and check the state field for the values that were
3959 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003960 *
3961 * Before being reused for another call to k_poll(), the user has to reset the
3962 * state field to K_POLL_STATE_NOT_READY.
3963 *
3964 * @param events An array of pointers to events to be polled for.
3965 * @param num_events The number of events in the array.
3966 * @param timeout Waiting period for an event to be ready (in milliseconds),
3967 * or one of the special values K_NO_WAIT and K_FOREVER.
3968 *
3969 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003970 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02003971 * @retval -EINTR Poller thread has been interrupted.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003972 */
3973
3974extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003975 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003976
3977/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003978 * @brief Initialize a poll signal object.
3979 *
3980 * Ready a poll signal object to be signaled via k_poll_signal().
3981 *
3982 * @param signal A poll signal.
3983 *
3984 * @return N/A
3985 */
3986
3987extern void k_poll_signal_init(struct k_poll_signal *signal);
3988
3989/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003990 * @brief Signal a poll signal object.
3991 *
3992 * This routine makes ready a poll signal, which is basically a poll event of
3993 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3994 * made ready to run. A @a result value can be specified.
3995 *
3996 * The poll signal contains a 'signaled' field that, when set by
3997 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3998 * be reset by the user before being passed again to k_poll() or k_poll() will
3999 * consider it being signaled, and will return immediately.
4000 *
4001 * @param signal A poll signal.
4002 * @param result The value to store in the result field of the signal.
4003 *
4004 * @retval 0 The signal was delivered successfully.
4005 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
4006 */
4007
4008extern int k_poll_signal(struct k_poll_signal *signal, int result);
4009
Anas Nashif954d5502018-02-25 08:37:28 -06004010/**
4011 * @internal
4012 */
Andy Ross8606fab2018-03-26 10:54:40 -07004013extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004014
Anas Nashif166f5192018-02-25 08:02:36 -06004015/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004016
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004017/**
4018 * @brief Make the CPU idle.
4019 *
4020 * This function makes the CPU idle until an event wakes it up.
4021 *
4022 * In a regular system, the idle thread should be the only thread responsible
4023 * for making the CPU idle and triggering any type of power management.
4024 * However, in some more constrained systems, such as a single-threaded system,
4025 * the only thread would be responsible for this if needed.
4026 *
4027 * @return N/A
4028 */
4029extern void k_cpu_idle(void);
4030
4031/**
4032 * @brief Make the CPU idle in an atomic fashion.
4033 *
4034 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4035 * must be done atomically before making the CPU idle.
4036 *
4037 * @param key Interrupt locking key obtained from irq_lock().
4038 *
4039 * @return N/A
4040 */
4041extern void k_cpu_atomic_idle(unsigned int key);
4042
Anas Nashif954d5502018-02-25 08:37:28 -06004043
4044/**
4045 * @internal
4046 */
Kumar Galacc334c72017-04-21 10:55:34 -05004047extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004048
Andrew Boiecdb94d62017-04-18 15:22:05 -07004049#ifdef _ARCH_EXCEPT
4050/* This archtecture has direct support for triggering a CPU exception */
4051#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4052#else
4053
Andrew Boiecdb94d62017-04-18 15:22:05 -07004054/* NOTE: This is the implementation for arches that do not implement
4055 * _ARCH_EXCEPT() to generate a real CPU exception.
4056 *
4057 * We won't have a real exception frame to determine the PC value when
4058 * the oops occurred, so print file and line number before we jump into
4059 * the fatal error handler.
4060 */
4061#define _k_except_reason(reason) do { \
4062 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4063 _NanoFatalErrorHandler(reason, &_default_esf); \
4064 CODE_UNREACHABLE; \
4065 } while (0)
4066
4067#endif /* _ARCH__EXCEPT */
4068
4069/**
4070 * @brief Fatally terminate a thread
4071 *
4072 * This should be called when a thread has encountered an unrecoverable
4073 * runtime condition and needs to terminate. What this ultimately
4074 * means is determined by the _fatal_error_handler() implementation, which
4075 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4076 *
4077 * If this is called from ISR context, the default system fatal error handler
4078 * will treat it as an unrecoverable system error, just like k_panic().
4079 */
4080#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4081
4082/**
4083 * @brief Fatally terminate the system
4084 *
4085 * This should be called when the Zephyr kernel has encountered an
4086 * unrecoverable runtime condition and needs to terminate. What this ultimately
4087 * means is determined by the _fatal_error_handler() implementation, which
4088 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4089 */
4090#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4091
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004092/*
4093 * private APIs that are utilized by one or more public APIs
4094 */
4095
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004096#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004097/**
4098 * @internal
4099 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004100extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004101#else
Anas Nashif954d5502018-02-25 08:37:28 -06004102/**
4103 * @internal
4104 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004105#define _init_static_threads() do { } while ((0))
4106#endif
4107
Anas Nashif954d5502018-02-25 08:37:28 -06004108/**
4109 * @internal
4110 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004111extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004112/**
4113 * @internal
4114 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004115extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004116
Andrew Boiedc5d9352017-06-02 12:56:47 -07004117/* arch/cpu.h may declare an architecture or platform-specific macro
4118 * for properly declaring stacks, compatible with MMU/MPU constraints if
4119 * enabled
4120 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004121
4122/**
4123 * @brief Obtain an extern reference to a stack
4124 *
4125 * This macro properly brings the symbol of a thread stack declared
4126 * elsewhere into scope.
4127 *
4128 * @param sym Thread stack symbol name
4129 */
4130#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4131
Andrew Boiedc5d9352017-06-02 12:56:47 -07004132#ifdef _ARCH_THREAD_STACK_DEFINE
4133#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4134#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4135 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4136#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4137#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004138static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004139{
4140 return _ARCH_THREAD_STACK_BUFFER(sym);
4141}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004142#else
4143/**
4144 * @brief Declare a toplevel thread stack memory region
4145 *
4146 * This declares a region of memory suitable for use as a thread's stack.
4147 *
4148 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4149 * 'noinit' section so that it isn't zeroed at boot
4150 *
Andrew Boie507852a2017-07-25 18:47:07 -07004151 * The declared symbol will always be a k_thread_stack_t which can be passed to
4152 * k_thread_create, but should otherwise not be manipulated. If the buffer
4153 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004154 *
4155 * It is legal to precede this definition with the 'static' keyword.
4156 *
4157 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4158 * parameter of k_thread_create(), it may not be the same as the
4159 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4160 *
4161 * @param sym Thread stack symbol name
4162 * @param size Size of the stack memory region
4163 */
4164#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004165 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004166
4167/**
4168 * @brief Declare a toplevel array of thread stack memory regions
4169 *
4170 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4171 * definition for additional details and constraints.
4172 *
4173 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4174 * 'noinit' section so that it isn't zeroed at boot
4175 *
4176 * @param sym Thread stack symbol name
4177 * @param nmemb Number of stacks to declare
4178 * @param size Size of the stack memory region
4179 */
4180
4181#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004182 struct _k_thread_stack_element __noinit \
4183 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004184
4185/**
4186 * @brief Declare an embedded stack memory region
4187 *
4188 * Used for stacks embedded within other data structures. Use is highly
4189 * discouraged but in some cases necessary. For memory protection scenarios,
4190 * it is very important that any RAM preceding this member not be writable
4191 * by threads else a stack overflow will lead to silent corruption. In other
4192 * words, the containing data structure should live in RAM owned by the kernel.
4193 *
4194 * @param sym Thread stack symbol name
4195 * @param size Size of the stack memory region
4196 */
4197#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004198 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004199
4200/**
4201 * @brief Return the size in bytes of a stack memory region
4202 *
4203 * Convenience macro for passing the desired stack size to k_thread_create()
4204 * since the underlying implementation may actually create something larger
4205 * (for instance a guard area).
4206 *
4207 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004208 * passed to K_THREAD_STACK_DEFINE.
4209 *
4210 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4211 * it is not guaranteed to return the original value since each array
4212 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004213 *
4214 * @param sym Stack memory symbol
4215 * @return Size of the stack
4216 */
4217#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4218
4219/**
4220 * @brief Get a pointer to the physical stack buffer
4221 *
4222 * Convenience macro to get at the real underlying stack buffer used by
4223 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4224 * This is really only intended for diagnostic tools which want to examine
4225 * stack memory contents.
4226 *
4227 * @param sym Declared stack symbol name
4228 * @return The buffer itself, a char *
4229 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004230static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004231{
4232 return (char *)sym;
4233}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004234
4235#endif /* _ARCH_DECLARE_STACK */
4236
Chunlin Hane9c97022017-07-07 20:29:30 +08004237/**
4238 * @defgroup mem_domain_apis Memory domain APIs
4239 * @ingroup kernel_apis
4240 * @{
4241 */
4242
4243/** @def MEM_PARTITION_ENTRY
4244 * @brief Used to declare a memory partition entry
4245 */
4246#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4247 {\
4248 .start = _start, \
4249 .size = _size, \
4250 .attr = _attr, \
4251 }
4252
4253/** @def K_MEM_PARTITION_DEFINE
4254 * @brief Used to declare a memory partition
4255 */
4256#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4257#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4258 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304259 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004260 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4261#else
4262#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304263 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004264 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4265#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4266
Chunlin Hane9c97022017-07-07 20:29:30 +08004267/* memory partition */
4268struct k_mem_partition {
4269 /* start address of memory partition */
4270 u32_t start;
4271 /* size of memory partition */
4272 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304273#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004274 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304275 k_mem_partition_attr_t attr;
4276#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004277};
4278
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304279/* memory domian
4280 * Note: Always declare this structure with __kernel prefix
4281 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004282struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304283#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004284 /* partitions in the domain */
4285 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304286#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004287 /* domain q */
4288 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004289 /* number of partitions in the domain */
4290 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004291};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304292
Chunlin Hane9c97022017-07-07 20:29:30 +08004293
4294/**
4295 * @brief Initialize a memory domain.
4296 *
4297 * Initialize a memory domain with given name and memory partitions.
4298 *
4299 * @param domain The memory domain to be initialized.
4300 * @param num_parts The number of array items of "parts" parameter.
4301 * @param parts An array of pointers to the memory partitions. Can be NULL
4302 * if num_parts is zero.
4303 */
4304
Leandro Pereira08de6582018-02-28 14:22:57 -08004305extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004306 struct k_mem_partition *parts[]);
4307/**
4308 * @brief Destroy a memory domain.
4309 *
4310 * Destroy a memory domain.
4311 *
4312 * @param domain The memory domain to be destroyed.
4313 */
4314
4315extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4316
4317/**
4318 * @brief Add a memory partition into a memory domain.
4319 *
4320 * Add a memory partition into a memory domain.
4321 *
4322 * @param domain The memory domain to be added a memory partition.
4323 * @param part The memory partition to be added
4324 */
4325
4326extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4327 struct k_mem_partition *part);
4328
4329/**
4330 * @brief Remove a memory partition from a memory domain.
4331 *
4332 * Remove a memory partition from a memory domain.
4333 *
4334 * @param domain The memory domain to be removed a memory partition.
4335 * @param part The memory partition to be removed
4336 */
4337
4338extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4339 struct k_mem_partition *part);
4340
4341/**
4342 * @brief Add a thread into a memory domain.
4343 *
4344 * Add a thread into a memory domain.
4345 *
4346 * @param domain The memory domain that the thread is going to be added into.
4347 * @param thread ID of thread going to be added into the memory domain.
4348 */
4349
4350extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4351 k_tid_t thread);
4352
4353/**
4354 * @brief Remove a thread from its memory domain.
4355 *
4356 * Remove a thread from its memory domain.
4357 *
4358 * @param thread ID of thread going to be removed from its memory domain.
4359 */
4360
4361extern void k_mem_domain_remove_thread(k_tid_t thread);
4362
Anas Nashif166f5192018-02-25 08:02:36 -06004363/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004364
Andrew Boie756f9072017-10-10 16:01:49 -07004365/**
4366 * @brief Emit a character buffer to the console device
4367 *
4368 * @param c String of characters to print
4369 * @param n The length of the string
4370 */
4371__syscall void k_str_out(char *c, size_t n);
4372
Andy Rosse7172672018-01-24 15:48:32 -08004373/**
4374 * @brief Start a numbered CPU on a MP-capable system
4375
4376 * This starts and initializes a specific CPU. The main thread on
4377 * startup is running on CPU zero, other processors are numbered
4378 * sequentially. On return from this function, the CPU is known to
4379 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004380 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004381 * with the provided key will work to enable them.
4382 *
4383 * Normally, in SMP mode this function will be called by the kernel
4384 * initialization and should not be used as a user API. But it is
4385 * defined here for special-purpose apps which want Zephyr running on
4386 * one core and to use others for design-specific processing.
4387 *
4388 * @param cpu_num Integer number of the CPU
4389 * @param stack Stack memory for the CPU
4390 * @param sz Stack buffer size, in bytes
4391 * @param fn Function to begin running on the CPU. First argument is
4392 * an irq_unlock() key.
4393 * @param arg Untyped argument to be passed to "fn"
4394 */
4395extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4396 void (*fn)(int, void *), void *arg);
4397
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004398#ifdef __cplusplus
4399}
4400#endif
4401
Andrew Boiee004dec2016-11-07 09:01:19 -08004402#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4403/*
4404 * Define new and delete operators.
4405 * At this moment, the operators do nothing since objects are supposed
4406 * to be statically allocated.
4407 */
4408inline void operator delete(void *ptr)
4409{
4410 (void)ptr;
4411}
4412
4413inline void operator delete[](void *ptr)
4414{
4415 (void)ptr;
4416}
4417
4418inline void *operator new(size_t size)
4419{
4420 (void)size;
4421 return NULL;
4422}
4423
4424inline void *operator new[](size_t size)
4425{
4426 (void)size;
4427 return NULL;
4428}
4429
4430/* Placement versions of operator new and delete */
4431inline void operator delete(void *ptr1, void *ptr2)
4432{
4433 (void)ptr1;
4434 (void)ptr2;
4435}
4436
4437inline void operator delete[](void *ptr1, void *ptr2)
4438{
4439 (void)ptr1;
4440 (void)ptr2;
4441}
4442
4443inline void *operator new(size_t size, void *ptr)
4444{
4445 (void)size;
4446 return ptr;
4447}
4448
4449inline void *operator new[](size_t size, void *ptr)
4450{
4451 (void)size;
4452 return ptr;
4453}
4454
4455#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4456
Andrew Boiefa94ee72017-09-28 16:54:35 -07004457#include <syscalls/kernel.h>
4458
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004459#endif /* !_ASMLANGUAGE */
4460
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004461#endif /* _kernel__h_ */