blob: d23f230703e01786957e17b5afe318e3cbb6a6d1 [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
Andrew Boie5bd891d2017-09-27 12:59:28 -0700138 /* Core kernel objects */
Andrew Boie945af952017-08-22 13:15:23 -0700139 K_OBJ_ALERT,
Andrew Boie945af952017-08-22 13:15:23 -0700140 K_OBJ_MSGQ,
141 K_OBJ_MUTEX,
142 K_OBJ_PIPE,
143 K_OBJ_SEM,
144 K_OBJ_STACK,
145 K_OBJ_THREAD,
146 K_OBJ_TIMER,
Andrew Boiebca15da2017-10-15 14:17:48 -0700147 K_OBJ__THREAD_STACK_ELEMENT,
Andrew Boie945af952017-08-22 13:15:23 -0700148
Andrew Boie5bd891d2017-09-27 12:59:28 -0700149 /* Driver subsystems */
150 K_OBJ_DRIVER_ADC,
151 K_OBJ_DRIVER_AIO_CMP,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700152 K_OBJ_DRIVER_COUNTER,
153 K_OBJ_DRIVER_CRYPTO,
Andrew Boiece6c8f32018-02-09 13:58:37 -0800154 K_OBJ_DRIVER_DMA,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700155 K_OBJ_DRIVER_FLASH,
156 K_OBJ_DRIVER_GPIO,
157 K_OBJ_DRIVER_I2C,
158 K_OBJ_DRIVER_I2S,
159 K_OBJ_DRIVER_IPM,
160 K_OBJ_DRIVER_PINMUX,
161 K_OBJ_DRIVER_PWM,
Leandro Pereirada9b0dd2017-10-13 16:30:55 -0700162 K_OBJ_DRIVER_ENTROPY,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700163 K_OBJ_DRIVER_RTC,
164 K_OBJ_DRIVER_SENSOR,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700165 K_OBJ_DRIVER_SPI,
166 K_OBJ_DRIVER_UART,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700167
Andrew Boie945af952017-08-22 13:15:23 -0700168 K_OBJ_LAST
169};
170
171#ifdef CONFIG_USERSPACE
172/* Table generated by gperf, these objects are retrieved via
173 * _k_object_find() */
174struct _k_object {
175 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700176 u8_t perms[CONFIG_MAX_THREAD_BYTES];
177 u8_t type;
178 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700179 u32_t data;
Andrew Boie945af952017-08-22 13:15:23 -0700180} __packed;
181
Andrew Boie877f82e2017-10-17 11:20:22 -0700182struct _k_object_assignment {
183 struct k_thread *thread;
184 void * const *objects;
185};
186
187/**
188 * @brief Grant a static thread access to a list of kernel objects
189 *
190 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
191 * a set of kernel objects. These objects do not need to be in an initialized
192 * state. The permissions will be granted when the threads are initialized
193 * in the early boot sequence.
194 *
195 * All arguments beyond the first must be pointers to kernel objects.
196 *
197 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
198 */
199#define K_THREAD_ACCESS_GRANT(name_, ...) \
200 static void * const _CONCAT(_object_list_, name_)[] = \
201 { __VA_ARGS__, NULL }; \
202 static __used __in_section_unique(object_access) \
203 const struct _k_object_assignment \
204 _CONCAT(_object_access_, name_) = \
205 { (&_k_thread_obj_ ## name_), \
206 (_CONCAT(_object_list_, name_)) }
207
Andrew Boie945af952017-08-22 13:15:23 -0700208#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700209#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie945af952017-08-22 13:15:23 -0700210
211/**
212 * Lookup a kernel object and init its metadata if it exists
213 *
214 * Calling this on an object will make it usable from userspace.
215 * Intended to be called as the last statement in kernel object init
216 * functions.
217 *
218 * @param object Address of the kernel object
219 */
220void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700221#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700222
223#define K_THREAD_ACCESS_GRANT(thread, ...)
224
Anas Nashif954d5502018-02-25 08:37:28 -0600225/**
226 * @internal
227 */
Andrew Boie743e4682017-10-04 12:25:50 -0700228static inline void _k_object_init(void *obj)
229{
230 ARG_UNUSED(obj);
231}
232
Anas Nashif954d5502018-02-25 08:37:28 -0600233/**
234 * @internal
235 */
Andrew Boie743e4682017-10-04 12:25:50 -0700236static inline void _impl_k_object_access_grant(void *object,
237 struct k_thread *thread)
238{
239 ARG_UNUSED(object);
240 ARG_UNUSED(thread);
241}
242
Anas Nashif954d5502018-02-25 08:37:28 -0600243/**
244 * @internal
245 */
Andrew Boiea89bf012017-10-09 14:47:55 -0700246static inline void _impl_k_object_access_revoke(void *object,
247 struct k_thread *thread)
248{
249 ARG_UNUSED(object);
250 ARG_UNUSED(thread);
251}
252
Andrew Boie41bab6e2017-10-14 14:42:23 -0700253static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700254{
255 ARG_UNUSED(object);
256}
257#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700258
259/**
260 * grant a thread access to a kernel object
261 *
262 * The thread will be granted access to the object if the caller is from
263 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700264 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700265 *
266 * @param object Address of kernel object
267 * @param thread Thread to grant access to the object
268 */
Andrew Boie743e4682017-10-04 12:25:50 -0700269__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700270
Andrew Boiea89bf012017-10-09 14:47:55 -0700271/**
272 * grant a thread access to a kernel object
273 *
274 * The thread will lose access to the object if the caller is from
275 * supervisor mode, or the caller is from user mode AND has permissions
276 * on both the object and the thread whose access is being revoked.
277 *
278 * @param object Address of kernel object
279 * @param thread Thread to remove access to the object
280 */
281__syscall void k_object_access_revoke(void *object, struct k_thread *thread);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700282
283/**
284 * grant all present and future threads access to an object
285 *
286 * If the caller is from supervisor mode, or the caller is from user mode and
287 * have sufficient permissions on the object, then that object will have
288 * permissions granted to it for *all* current and future threads running in
289 * the system, effectively becoming a public kernel object.
290 *
291 * Use of this API should be avoided on systems that are running untrusted code
292 * as it is possible for such code to derive the addresses of kernel objects
293 * and perform unwanted operations on them.
294 *
Andrew Boie04caa672017-10-13 13:57:07 -0700295 * It is not possible to revoke permissions on public objects; once public,
296 * any thread may use it.
297 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700298 * @param object Address of kernel object
299 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700300void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700301
Andrew Boiebca15da2017-10-15 14:17:48 -0700302/* Using typedef deliberately here, this is quite intended to be an opaque
303 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
304 *
305 * The purpose of this data type is to clearly distinguish between the
306 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
307 * buffer which composes the stack data actually used by the underlying
308 * thread; they cannot be used interchangably as some arches precede the
309 * stack buffer region with guard areas that trigger a MPU or MMU fault
310 * if written to.
311 *
312 * APIs that want to work with the buffer inside should continue to use
313 * char *.
314 *
315 * Stacks should always be created with K_THREAD_STACK_DEFINE().
316 */
317struct __packed _k_thread_stack_element {
318 char data;
319};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700320typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700321
Andrew Boie73abd322017-04-04 13:19:13 -0700322/* timeouts */
323
324struct _timeout;
325typedef void (*_timeout_func_t)(struct _timeout *t);
326
327struct _timeout {
328 sys_dnode_t node;
329 struct k_thread *thread;
330 sys_dlist_t *wait_q;
331 s32_t delta_ticks_from_prev;
332 _timeout_func_t func;
333};
334
335extern s32_t _timeout_remaining_get(struct _timeout *timeout);
336
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700337/**
338 * @typedef k_thread_entry_t
339 * @brief Thread entry point function type.
340 *
341 * A thread's entry point function is invoked when the thread starts executing.
342 * Up to 3 argument values can be passed to the function.
343 *
344 * The thread terminates execution permanently if the entry point function
345 * returns. The thread is responsible for releasing any shared resources
346 * it may own (such as mutexes and dynamically allocated memory), prior to
347 * returning.
348 *
349 * @param p1 First argument.
350 * @param p2 Second argument.
351 * @param p3 Third argument.
352 *
353 * @return N/A
354 */
355typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700356
357#ifdef CONFIG_THREAD_MONITOR
358struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700359 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700360 void *parameter1;
361 void *parameter2;
362 void *parameter3;
363};
364#endif
365
366/* can be used for creating 'dummy' threads, e.g. for pending on objects */
367struct _thread_base {
368
369 /* this thread's entry in a ready/wait queue */
370 sys_dnode_t k_q_node;
371
372 /* user facing 'thread options'; values defined in include/kernel.h */
373 u8_t user_options;
374
375 /* thread state */
376 u8_t thread_state;
377
378 /*
379 * scheduler lock count and thread priority
380 *
381 * These two fields control the preemptibility of a thread.
382 *
383 * When the scheduler is locked, sched_locked is decremented, which
384 * means that the scheduler is locked for values from 0xff to 0x01. A
385 * thread is coop if its prio is negative, thus 0x80 to 0xff when
386 * looked at the value as unsigned.
387 *
388 * By putting them end-to-end, this means that a thread is
389 * non-preemptible if the bundled value is greater than or equal to
390 * 0x0080.
391 */
392 union {
393 struct {
394#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
395 u8_t sched_locked;
396 s8_t prio;
397#else /* LITTLE and PDP */
398 s8_t prio;
399 u8_t sched_locked;
400#endif
401 };
402 u16_t preempt;
403 };
404
Andy Ross2724fd12018-01-29 14:55:20 -0800405#ifdef CONFIG_SMP
406 /* True for the per-CPU idle threads */
407 u8_t is_idle;
408
409 /* Non-zero when actively running on a CPU */
410 u8_t active;
411
412 /* CPU index on which thread was last run */
413 u8_t cpu;
414#endif
415
Andrew Boie73abd322017-04-04 13:19:13 -0700416 /* data returned by APIs */
417 void *swap_data;
418
419#ifdef CONFIG_SYS_CLOCK_EXISTS
420 /* this thread's entry in a timeout queue */
421 struct _timeout timeout;
422#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700423};
424
425typedef struct _thread_base _thread_base_t;
426
427#if defined(CONFIG_THREAD_STACK_INFO)
428/* Contains the stack information of a thread */
429struct _thread_stack_info {
430 /* Stack Start */
431 u32_t start;
432 /* Stack Size */
433 u32_t size;
434};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700435
436typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700437#endif /* CONFIG_THREAD_STACK_INFO */
438
Chunlin Hane9c97022017-07-07 20:29:30 +0800439#if defined(CONFIG_USERSPACE)
440struct _mem_domain_info {
441 /* memory domain queue node */
442 sys_dnode_t mem_domain_q_node;
443 /* memory domain of the thread */
444 struct k_mem_domain *mem_domain;
445};
446
447#endif /* CONFIG_USERSPACE */
448
Andrew Boie73abd322017-04-04 13:19:13 -0700449struct k_thread {
450
451 struct _thread_base base;
452
453 /* defined by the architecture, but all archs need these */
454 struct _caller_saved caller_saved;
455 struct _callee_saved callee_saved;
456
457 /* static thread init data */
458 void *init_data;
459
460 /* abort function */
461 void (*fn_abort)(void);
462
463#if defined(CONFIG_THREAD_MONITOR)
464 /* thread entry and parameters description */
465 struct __thread_entry *entry;
466
467 /* next item in list of all threads */
468 struct k_thread *next_thread;
469#endif
470
471#ifdef CONFIG_THREAD_CUSTOM_DATA
472 /* crude thread-local storage */
473 void *custom_data;
474#endif
475
476#ifdef CONFIG_ERRNO
477 /* per-thread errno variable */
478 int errno_var;
479#endif
480
481#if defined(CONFIG_THREAD_STACK_INFO)
482 /* Stack Info */
483 struct _thread_stack_info stack_info;
484#endif /* CONFIG_THREAD_STACK_INFO */
485
Chunlin Hane9c97022017-07-07 20:29:30 +0800486#if defined(CONFIG_USERSPACE)
487 /* memory domain info of the thread */
488 struct _mem_domain_info mem_domain_info;
Andrew Boiebca15da2017-10-15 14:17:48 -0700489 /* Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700490 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800491#endif /* CONFIG_USERSPACE */
492
Andy Ross042d8ec2017-12-09 08:37:20 -0800493#if defined(CONFIG_USE_SWITCH)
494 /* When using __switch() a few previously arch-specific items
495 * become part of the core OS
496 */
497
498 /* _Swap() return value */
499 int swap_retval;
500
501 /* Context handle returned via _arch_switch() */
502 void *switch_handle;
503#endif
504
Andrew Boie73abd322017-04-04 13:19:13 -0700505 /* arch-specifics: must always be at the end */
506 struct _thread_arch arch;
507};
508
509typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400510typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700511#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400512
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400513enum execution_context_types {
514 K_ISR = 0,
515 K_COOP_THREAD,
516 K_PREEMPT_THREAD,
517};
518
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400519/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100520 * @defgroup profiling_apis Profiling APIs
521 * @ingroup kernel_apis
522 * @{
523 */
524
525/**
526 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
527 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700528 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100529 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
530 *
531 * CONFIG_MAIN_STACK_SIZE
532 * CONFIG_IDLE_STACK_SIZE
533 * CONFIG_ISR_STACK_SIZE
534 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
535 *
536 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
537 * produce output.
538 *
539 * @return N/A
540 */
541extern void k_call_stacks_analyze(void);
542
Anas Nashif166f5192018-02-25 08:02:36 -0600543/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100544
545/**
Allan Stephensc98da842016-11-11 15:45:03 -0500546 * @defgroup thread_apis Thread APIs
547 * @ingroup kernel_apis
548 * @{
549 */
550
Benjamin Walshed240f22017-01-22 13:05:08 -0500551#endif /* !_ASMLANGUAGE */
552
553
554/*
555 * Thread user options. May be needed by assembly code. Common part uses low
556 * bits, arch-specific use high bits.
557 */
558
559/* system thread that must not abort */
560#define K_ESSENTIAL (1 << 0)
561
562#if defined(CONFIG_FP_SHARING)
563/* thread uses floating point registers */
564#define K_FP_REGS (1 << 1)
565#endif
566
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700567/* This thread has dropped from supervisor mode to user mode and consequently
568 * has additional restrictions
569 */
570#define K_USER (1 << 2)
571
Andrew Boie47f8fd12017-10-05 11:11:02 -0700572/* Indicates that the thread being created should inherit all kernel object
573 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
574 * is not enabled.
575 */
576#define K_INHERIT_PERMS (1 << 3)
577
Benjamin Walshed240f22017-01-22 13:05:08 -0500578#ifdef CONFIG_X86
579/* x86 Bitmask definitions for threads user options */
580
581#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
582/* thread uses SSEx (and also FP) registers */
583#define K_SSE_REGS (1 << 7)
584#endif
585#endif
586
587/* end - thread options */
588
589#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400590/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700591 * @brief Create a thread.
592 *
593 * This routine initializes a thread, then schedules it for execution.
594 *
595 * The new thread may be scheduled for immediate execution or a delayed start.
596 * If the newly spawned thread does not have a delayed start the kernel
597 * scheduler may preempt the current thread to allow the new thread to
598 * execute.
599 *
600 * Thread options are architecture-specific, and can include K_ESSENTIAL,
601 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
602 * them using "|" (the logical OR operator).
603 *
604 * Historically, users often would use the beginning of the stack memory region
605 * to store the struct k_thread data, although corruption will occur if the
606 * stack overflows this region and stack protection features may not detect this
607 * situation.
608 *
609 * @param new_thread Pointer to uninitialized struct k_thread
610 * @param stack Pointer to the stack space.
611 * @param stack_size Stack size in bytes.
612 * @param entry Thread entry function.
613 * @param p1 1st entry point parameter.
614 * @param p2 2nd entry point parameter.
615 * @param p3 3rd entry point parameter.
616 * @param prio Thread priority.
617 * @param options Thread options.
618 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
619 *
620 * @return ID of new thread.
621 */
Andrew Boie662c3452017-10-02 10:51:18 -0700622__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700623 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700624 size_t stack_size,
625 k_thread_entry_t entry,
626 void *p1, void *p2, void *p3,
627 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700628
Andrew Boie3f091b52017-08-30 14:34:14 -0700629/**
630 * @brief Drop a thread's privileges permanently to user mode
631 *
632 * @param entry Function to start executing from
633 * @param p1 1st entry point parameter
634 * @param p2 2nd entry point parameter
635 * @param p3 3rd entry point parameter
636 */
637extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
638 void *p1, void *p2,
639 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700640
Andrew Boied26cf2d2017-03-30 13:07:02 -0700641/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700642 * @brief Grant a thread access to a NULL-terminated set of kernel objects
643 *
644 * This is a convenience function. For the provided thread, grant access to
645 * the remaining arguments, which must be pointers to kernel objects.
646 * The final argument must be a NULL.
647 *
648 * The thread object must be initialized (i.e. running). The objects don't
649 * need to be.
650 *
651 * @param thread Thread to grant access to objects
652 * @param ... NULL-terminated list of kernel object pointers
653 */
654extern void __attribute__((sentinel))
655 k_thread_access_grant(struct k_thread *thread, ...);
656
657/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500658 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400659 *
Allan Stephensc98da842016-11-11 15:45:03 -0500660 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500661 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400662 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500663 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400664 *
665 * @return N/A
666 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700667__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400668
669/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500670 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400671 *
672 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500673 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400674 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400675 * @return N/A
676 */
Kumar Galacc334c72017-04-21 10:55:34 -0500677extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400678
679/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500680 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400681 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500682 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400683 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500684 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400685 *
686 * @return N/A
687 */
Andrew Boie468190a2017-09-29 14:00:48 -0700688__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400689
690/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500691 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400692 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500693 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400694 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500695 * If @a thread is not currently sleeping, the routine has no effect.
696 *
697 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400698 *
699 * @return N/A
700 */
Andrew Boie468190a2017-09-29 14:00:48 -0700701__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400702
703/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500704 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400705 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500706 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400707 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700708__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400709
710/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500711 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400712 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500713 * This routine prevents @a thread from executing if it has not yet started
714 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500716 * @param thread ID of thread to cancel.
717 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700718 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500719 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700720 *
721 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400722 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700723__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400724
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400725/**
Allan Stephensc98da842016-11-11 15:45:03 -0500726 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400727 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500728 * This routine permanently stops execution of @a thread. The thread is taken
729 * off all kernel queues it is part of (i.e. the ready queue, the timeout
730 * queue, or a kernel object wait queue). However, any kernel resources the
731 * thread might currently own (such as mutexes or memory blocks) are not
732 * released. It is the responsibility of the caller of this routine to ensure
733 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400734 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500735 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400736 *
737 * @return N/A
738 */
Andrew Boie468190a2017-09-29 14:00:48 -0700739__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400740
Andrew Boie7d627c52017-08-30 11:01:56 -0700741
742/**
743 * @brief Start an inactive thread
744 *
745 * If a thread was created with K_FOREVER in the delay parameter, it will
746 * not be added to the scheduling queue until this function is called
747 * on it.
748 *
749 * @param thread thread to start
750 */
Andrew Boie468190a2017-09-29 14:00:48 -0700751__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700752
Allan Stephensc98da842016-11-11 15:45:03 -0500753/**
754 * @cond INTERNAL_HIDDEN
755 */
756
Benjamin Walshd211a522016-12-06 11:44:01 -0500757/* timeout has timed out and is not on _timeout_q anymore */
758#define _EXPIRED (-2)
759
760/* timeout is not in use */
761#define _INACTIVE (-1)
762
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400763struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700764 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700765 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400766 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700767 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500768 void *init_p1;
769 void *init_p2;
770 void *init_p3;
771 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500772 u32_t init_options;
773 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500774 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400775};
776
Andrew Boied26cf2d2017-03-30 13:07:02 -0700777#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400778 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500779 prio, options, delay, abort, groups) \
780 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700781 .init_thread = (thread), \
782 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500783 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700784 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400785 .init_p1 = (void *)p1, \
786 .init_p2 = (void *)p2, \
787 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500788 .init_prio = (prio), \
789 .init_options = (options), \
790 .init_delay = (delay), \
791 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400792 }
793
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400794/**
Allan Stephensc98da842016-11-11 15:45:03 -0500795 * INTERNAL_HIDDEN @endcond
796 */
797
798/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500799 * @brief Statically define and initialize a thread.
800 *
801 * The thread may be scheduled for immediate execution or a delayed start.
802 *
803 * Thread options are architecture-specific, and can include K_ESSENTIAL,
804 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
805 * them using "|" (the logical OR operator).
806 *
807 * The ID of the thread can be accessed using:
808 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500809 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500810 *
811 * @param name Name of the thread.
812 * @param stack_size Stack size in bytes.
813 * @param entry Thread entry function.
814 * @param p1 1st entry point parameter.
815 * @param p2 2nd entry point parameter.
816 * @param p3 3rd entry point parameter.
817 * @param prio Thread priority.
818 * @param options Thread options.
819 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400820 *
821 * @internal It has been observed that the x86 compiler by default aligns
822 * these _static_thread_data structures to 32-byte boundaries, thereby
823 * wasting space. To work around this, force a 4-byte alignment.
824 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500825#define K_THREAD_DEFINE(name, stack_size, \
826 entry, p1, p2, p3, \
827 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700828 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700829 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500830 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500831 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700832 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
833 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500834 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700835 NULL, 0); \
836 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400837
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400838/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500839 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400840 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500841 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400842 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500843 * @param thread ID of thread whose priority is needed.
844 *
845 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400846 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700847__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400848
849/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500850 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500852 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400853 *
854 * Rescheduling can occur immediately depending on the priority @a thread is
855 * set to:
856 *
857 * - If its priority is raised above the priority of the caller of this
858 * function, and the caller is preemptible, @a thread will be scheduled in.
859 *
860 * - If the caller operates on itself, it lowers its priority below that of
861 * other threads in the system, and the caller is preemptible, the thread of
862 * highest priority will be scheduled in.
863 *
864 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
865 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
866 * highest priority.
867 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500868 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400869 * @param prio New priority.
870 *
871 * @warning Changing the priority of a thread currently involved in mutex
872 * priority inheritance may result in undefined behavior.
873 *
874 * @return N/A
875 */
Andrew Boie468190a2017-09-29 14:00:48 -0700876__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400877
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400878/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500879 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500881 * This routine prevents the kernel scheduler from making @a thread the
882 * current thread. All other internal operations on @a thread are still
883 * performed; for example, any timeout it is waiting on keeps ticking,
884 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400885 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500886 * If @a thread is already suspended, the routine has no effect.
887 *
888 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400889 *
890 * @return N/A
891 */
Andrew Boie468190a2017-09-29 14:00:48 -0700892__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400893
894/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500895 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500897 * This routine allows the kernel scheduler to make @a thread the current
898 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400899 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500900 * If @a thread is not currently suspended, the routine has no effect.
901 *
902 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400903 *
904 * @return N/A
905 */
Andrew Boie468190a2017-09-29 14:00:48 -0700906__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400907
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400908/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500909 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400910 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500911 * This routine specifies how the scheduler will perform time slicing of
912 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400913 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500914 * To enable time slicing, @a slice must be non-zero. The scheduler
915 * ensures that no thread runs for more than the specified time limit
916 * before other threads of that priority are given a chance to execute.
917 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700918 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400919 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500920 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400921 * execute. Once the scheduler selects a thread for execution, there is no
922 * minimum guaranteed time the thread will execute before threads of greater or
923 * equal priority are scheduled.
924 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500925 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400926 * for execution, this routine has no effect; the thread is immediately
927 * rescheduled after the slice period expires.
928 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500929 * To disable timeslicing, set both @a slice and @a prio to zero.
930 *
931 * @param slice Maximum time slice length (in milliseconds).
932 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400933 *
934 * @return N/A
935 */
Kumar Galacc334c72017-04-21 10:55:34 -0500936extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400937
Anas Nashif166f5192018-02-25 08:02:36 -0600938/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -0500939
940/**
941 * @addtogroup isr_apis
942 * @{
943 */
944
945/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500946 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400947 *
Allan Stephensc98da842016-11-11 15:45:03 -0500948 * This routine allows the caller to customize its actions, depending on
949 * whether it is a thread or an ISR.
950 *
951 * @note Can be called by ISRs.
952 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500953 * @return 0 if invoked by a thread.
954 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400955 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500956extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400957
Benjamin Walsh445830d2016-11-10 15:54:27 -0500958/**
959 * @brief Determine if code is running in a preemptible thread.
960 *
Allan Stephensc98da842016-11-11 15:45:03 -0500961 * This routine allows the caller to customize its actions, depending on
962 * whether it can be preempted by another thread. The routine returns a 'true'
963 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500964 *
Allan Stephensc98da842016-11-11 15:45:03 -0500965 * - The code is running in a thread, not at ISR.
966 * - The thread's priority is in the preemptible range.
967 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500968 *
Allan Stephensc98da842016-11-11 15:45:03 -0500969 * @note Can be called by ISRs.
970 *
971 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500972 * @return Non-zero if invoked by a preemptible thread.
973 */
Andrew Boie468190a2017-09-29 14:00:48 -0700974__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -0500975
Allan Stephensc98da842016-11-11 15:45:03 -0500976/**
Anas Nashif166f5192018-02-25 08:02:36 -0600977 * @}
Allan Stephensc98da842016-11-11 15:45:03 -0500978 */
979
980/**
981 * @addtogroup thread_apis
982 * @{
983 */
984
985/**
986 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500987 *
Allan Stephensc98da842016-11-11 15:45:03 -0500988 * This routine prevents the current thread from being preempted by another
989 * thread by instructing the scheduler to treat it as a cooperative thread.
990 * If the thread subsequently performs an operation that makes it unready,
991 * it will be context switched out in the normal manner. When the thread
992 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500993 *
Allan Stephensc98da842016-11-11 15:45:03 -0500994 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500995 *
Allan Stephensc98da842016-11-11 15:45:03 -0500996 * @note k_sched_lock() and k_sched_unlock() should normally be used
997 * when the operation being performed can be safely interrupted by ISRs.
998 * However, if the amount of processing involved is very small, better
999 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001000 *
1001 * @return N/A
1002 */
1003extern void k_sched_lock(void);
1004
Allan Stephensc98da842016-11-11 15:45:03 -05001005/**
1006 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001007 *
Allan Stephensc98da842016-11-11 15:45:03 -05001008 * This routine reverses the effect of a previous call to k_sched_lock().
1009 * A thread must call the routine once for each time it called k_sched_lock()
1010 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001011 *
1012 * @return N/A
1013 */
1014extern void k_sched_unlock(void);
1015
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001016/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001017 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001018 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001019 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001020 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001021 * Custom data is not used by the kernel itself, and is freely available
1022 * for a thread to use as it sees fit. It can be used as a framework
1023 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001024 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001025 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001026 *
1027 * @return N/A
1028 */
Andrew Boie468190a2017-09-29 14:00:48 -07001029__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001030
1031/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001032 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001033 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001034 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001035 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001036 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001037 */
Andrew Boie468190a2017-09-29 14:00:48 -07001038__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001039
1040/**
Anas Nashif166f5192018-02-25 08:02:36 -06001041 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001042 */
1043
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001044#include <sys_clock.h>
1045
Allan Stephensc2f15a42016-11-17 12:24:22 -05001046/**
1047 * @addtogroup clock_apis
1048 * @{
1049 */
1050
1051/**
1052 * @brief Generate null timeout delay.
1053 *
1054 * This macro generates a timeout delay that that instructs a kernel API
1055 * not to wait if the requested operation cannot be performed immediately.
1056 *
1057 * @return Timeout delay value.
1058 */
1059#define K_NO_WAIT 0
1060
1061/**
1062 * @brief Generate timeout delay from milliseconds.
1063 *
1064 * This macro generates a timeout delay that that instructs a kernel API
1065 * to wait up to @a ms milliseconds to perform the requested operation.
1066 *
1067 * @param ms Duration in milliseconds.
1068 *
1069 * @return Timeout delay value.
1070 */
Johan Hedberg14471692016-11-13 10:52:15 +02001071#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001072
1073/**
1074 * @brief Generate timeout delay from seconds.
1075 *
1076 * This macro generates a timeout delay that that instructs a kernel API
1077 * to wait up to @a s seconds to perform the requested operation.
1078 *
1079 * @param s Duration in seconds.
1080 *
1081 * @return Timeout delay value.
1082 */
Johan Hedberg14471692016-11-13 10:52:15 +02001083#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001084
1085/**
1086 * @brief Generate timeout delay from minutes.
1087 *
1088 * This macro generates a timeout delay that that instructs a kernel API
1089 * to wait up to @a m minutes to perform the requested operation.
1090 *
1091 * @param m Duration in minutes.
1092 *
1093 * @return Timeout delay value.
1094 */
Johan Hedberg14471692016-11-13 10:52:15 +02001095#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001096
1097/**
1098 * @brief Generate timeout delay from hours.
1099 *
1100 * This macro generates a timeout delay that that instructs a kernel API
1101 * to wait up to @a h hours to perform the requested operation.
1102 *
1103 * @param h Duration in hours.
1104 *
1105 * @return Timeout delay value.
1106 */
Johan Hedberg14471692016-11-13 10:52:15 +02001107#define K_HOURS(h) K_MINUTES((h) * 60)
1108
Allan Stephensc98da842016-11-11 15:45:03 -05001109/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001110 * @brief Generate infinite timeout delay.
1111 *
1112 * This macro generates a timeout delay that that instructs a kernel API
1113 * to wait as long as necessary to perform the requested operation.
1114 *
1115 * @return Timeout delay value.
1116 */
1117#define K_FOREVER (-1)
1118
1119/**
Anas Nashif166f5192018-02-25 08:02:36 -06001120 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001121 */
1122
1123/**
Allan Stephensc98da842016-11-11 15:45:03 -05001124 * @cond INTERNAL_HIDDEN
1125 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001126
Benjamin Walsh62092182016-12-20 14:39:08 -05001127/* kernel clocks */
1128
1129#if (sys_clock_ticks_per_sec == 1000) || \
1130 (sys_clock_ticks_per_sec == 500) || \
1131 (sys_clock_ticks_per_sec == 250) || \
1132 (sys_clock_ticks_per_sec == 125) || \
1133 (sys_clock_ticks_per_sec == 100) || \
1134 (sys_clock_ticks_per_sec == 50) || \
1135 (sys_clock_ticks_per_sec == 25) || \
1136 (sys_clock_ticks_per_sec == 20) || \
1137 (sys_clock_ticks_per_sec == 10) || \
1138 (sys_clock_ticks_per_sec == 1)
1139
1140 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1141#else
1142 /* yields horrible 64-bit math on many architectures: try to avoid */
1143 #define _NON_OPTIMIZED_TICKS_PER_SEC
1144#endif
1145
1146#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001147extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001148#else
Kumar Galacc334c72017-04-21 10:55:34 -05001149static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001150{
Kumar Galacc334c72017-04-21 10:55:34 -05001151 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001152}
1153#endif
1154
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001155/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001156#ifdef CONFIG_TICKLESS_KERNEL
1157#define _TICK_ALIGN 0
1158#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001159#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001160#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001161
Kumar Galacc334c72017-04-21 10:55:34 -05001162static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001163{
Benjamin Walsh62092182016-12-20 14:39:08 -05001164#ifdef CONFIG_SYS_CLOCK_EXISTS
1165
1166#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001167 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001168#else
Kumar Galacc334c72017-04-21 10:55:34 -05001169 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001170#endif
1171
1172#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001173 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001174 return 0;
1175#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001176}
1177
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001178struct k_timer {
1179 /*
1180 * _timeout structure must be first here if we want to use
1181 * dynamic timer allocation. timeout.node is used in the double-linked
1182 * list of free timers
1183 */
1184 struct _timeout timeout;
1185
Allan Stephens45bfa372016-10-12 12:39:42 -05001186 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001187 _wait_q_t wait_q;
1188
1189 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001190 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001191
1192 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001193 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001194
1195 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001196 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001197
Allan Stephens45bfa372016-10-12 12:39:42 -05001198 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001199 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001200
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001201 /* user-specific data, also used to support legacy features */
1202 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001203
Anas Nashif2f203c22016-12-18 06:57:45 -05001204 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001205};
1206
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001207#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001208 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001209 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001210 .timeout.wait_q = NULL, \
1211 .timeout.thread = NULL, \
1212 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001213 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001214 .expiry_fn = expiry, \
1215 .stop_fn = stop, \
1216 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001217 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001218 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001219 }
1220
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001221#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1222
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001223/**
Allan Stephensc98da842016-11-11 15:45:03 -05001224 * INTERNAL_HIDDEN @endcond
1225 */
1226
1227/**
1228 * @defgroup timer_apis Timer APIs
1229 * @ingroup kernel_apis
1230 * @{
1231 */
1232
1233/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001234 * @typedef k_timer_expiry_t
1235 * @brief Timer expiry function type.
1236 *
1237 * A timer's expiry function is executed by the system clock interrupt handler
1238 * each time the timer expires. The expiry function is optional, and is only
1239 * invoked if the timer has been initialized with one.
1240 *
1241 * @param timer Address of timer.
1242 *
1243 * @return N/A
1244 */
1245typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1246
1247/**
1248 * @typedef k_timer_stop_t
1249 * @brief Timer stop function type.
1250 *
1251 * A timer's stop function is executed if the timer is stopped prematurely.
1252 * The function runs in the context of the thread that stops the timer.
1253 * The stop function is optional, and is only invoked if the timer has been
1254 * initialized with one.
1255 *
1256 * @param timer Address of timer.
1257 *
1258 * @return N/A
1259 */
1260typedef void (*k_timer_stop_t)(struct k_timer *timer);
1261
1262/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001263 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001264 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001265 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001266 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001267 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001268 *
1269 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001270 * @param expiry_fn Function to invoke each time the timer expires.
1271 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001272 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001273#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001274 struct k_timer name \
1275 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001276 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001277
Allan Stephens45bfa372016-10-12 12:39:42 -05001278/**
1279 * @brief Initialize a timer.
1280 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001281 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001282 *
1283 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001284 * @param expiry_fn Function to invoke each time the timer expires.
1285 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001286 *
1287 * @return N/A
1288 */
1289extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001290 k_timer_expiry_t expiry_fn,
1291 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001292
Allan Stephens45bfa372016-10-12 12:39:42 -05001293/**
1294 * @brief Start a timer.
1295 *
1296 * This routine starts a timer, and resets its status to zero. The timer
1297 * begins counting down using the specified duration and period values.
1298 *
1299 * Attempting to start a timer that is already running is permitted.
1300 * The timer's status is reset to zero and the timer begins counting down
1301 * using the new duration and period values.
1302 *
1303 * @param timer Address of timer.
1304 * @param duration Initial timer duration (in milliseconds).
1305 * @param period Timer period (in milliseconds).
1306 *
1307 * @return N/A
1308 */
Andrew Boiea354d492017-09-29 16:22:28 -07001309__syscall void k_timer_start(struct k_timer *timer,
1310 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001311
1312/**
1313 * @brief Stop a timer.
1314 *
1315 * This routine stops a running timer prematurely. The timer's stop function,
1316 * if one exists, is invoked by the caller.
1317 *
1318 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001319 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001320 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001321 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1322 * if @a k_timer_stop is to be called from ISRs.
1323 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001324 * @param timer Address of timer.
1325 *
1326 * @return N/A
1327 */
Andrew Boiea354d492017-09-29 16:22:28 -07001328__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001329
1330/**
1331 * @brief Read timer status.
1332 *
1333 * This routine reads the timer's status, which indicates the number of times
1334 * it has expired since its status was last read.
1335 *
1336 * Calling this routine resets the timer's status to zero.
1337 *
1338 * @param timer Address of timer.
1339 *
1340 * @return Timer status.
1341 */
Andrew Boiea354d492017-09-29 16:22:28 -07001342__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001343
1344/**
1345 * @brief Synchronize thread to timer expiration.
1346 *
1347 * This routine blocks the calling thread until the timer's status is non-zero
1348 * (indicating that it has expired at least once since it was last examined)
1349 * or the timer is stopped. If the timer status is already non-zero,
1350 * or the timer is already stopped, the caller continues without waiting.
1351 *
1352 * Calling this routine resets the timer's status to zero.
1353 *
1354 * This routine must not be used by interrupt handlers, since they are not
1355 * allowed to block.
1356 *
1357 * @param timer Address of timer.
1358 *
1359 * @return Timer status.
1360 */
Andrew Boiea354d492017-09-29 16:22:28 -07001361__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001362
1363/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001364 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001365 *
1366 * This routine computes the (approximate) time remaining before a running
1367 * timer next expires. If the timer is not running, it returns zero.
1368 *
1369 * @param timer Address of timer.
1370 *
1371 * @return Remaining time (in milliseconds).
1372 */
Andrew Boiea354d492017-09-29 16:22:28 -07001373__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1374
1375static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001376{
1377 return _timeout_remaining_get(&timer->timeout);
1378}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001379
Allan Stephensc98da842016-11-11 15:45:03 -05001380/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001381 * @brief Associate user-specific data with a timer.
1382 *
1383 * This routine records the @a user_data with the @a timer, to be retrieved
1384 * later.
1385 *
1386 * It can be used e.g. in a timer handler shared across multiple subsystems to
1387 * retrieve data specific to the subsystem this timer is associated with.
1388 *
1389 * @param timer Address of timer.
1390 * @param user_data User data to associate with the timer.
1391 *
1392 * @return N/A
1393 */
Andrew Boiea354d492017-09-29 16:22:28 -07001394__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1395
Anas Nashif954d5502018-02-25 08:37:28 -06001396/**
1397 * @internal
1398 */
Andrew Boiea354d492017-09-29 16:22:28 -07001399static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1400 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001401{
1402 timer->user_data = user_data;
1403}
1404
1405/**
1406 * @brief Retrieve the user-specific data from a timer.
1407 *
1408 * @param timer Address of timer.
1409 *
1410 * @return The user data.
1411 */
Andrew Boiea354d492017-09-29 16:22:28 -07001412__syscall void *k_timer_user_data_get(struct k_timer *timer);
1413
1414static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001415{
1416 return timer->user_data;
1417}
1418
Anas Nashif166f5192018-02-25 08:02:36 -06001419/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001420
Allan Stephensc98da842016-11-11 15:45:03 -05001421/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001422 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001423 * @{
1424 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001425
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001426/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001427 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001428 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001429 * This routine returns the elapsed time since the system booted,
1430 * in milliseconds.
1431 *
1432 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001433 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001434__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001435
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001436/**
1437 * @brief Enable clock always on in tickless kernel
1438 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001439 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001440 * there are no timer events programmed in tickless kernel
1441 * scheduling. This is necessary if the clock is used to track
1442 * passage of time.
1443 *
1444 * @retval prev_status Previous status of always on flag
1445 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301446#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001447static inline int k_enable_sys_clock_always_on(void)
1448{
1449 int prev_status = _sys_clock_always_on;
1450
1451 _sys_clock_always_on = 1;
1452 _enable_sys_clock();
1453
1454 return prev_status;
1455}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301456#else
1457#define k_enable_sys_clock_always_on() do { } while ((0))
1458#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001459
1460/**
1461 * @brief Disable clock always on in tickless kernel
1462 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001463 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001464 * there are no timer events programmed in tickless kernel
1465 * scheduling. To save power, this routine should be called
1466 * immediately when clock is not used to track time.
1467 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301468#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001469static inline void k_disable_sys_clock_always_on(void)
1470{
1471 _sys_clock_always_on = 0;
1472}
1473#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001474#define k_disable_sys_clock_always_on() do { } while ((0))
1475#endif
1476
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001477/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001478 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001479 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001480 * This routine returns the lower 32-bits of the elapsed time since the system
1481 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001482 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001483 * This routine can be more efficient than k_uptime_get(), as it reduces the
1484 * need for interrupt locking and 64-bit math. However, the 32-bit result
1485 * cannot hold a system uptime time larger than approximately 50 days, so the
1486 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001487 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001488 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001489 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001490__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001491
1492/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001493 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001494 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001495 * This routine computes the elapsed time between the current system uptime
1496 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001497 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001498 * @param reftime Pointer to a reference time, which is updated to the current
1499 * uptime upon return.
1500 *
1501 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001502 */
Kumar Galacc334c72017-04-21 10:55:34 -05001503extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001504
1505/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001506 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001507 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001508 * This routine computes the elapsed time between the current system uptime
1509 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001510 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001511 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1512 * need for interrupt locking and 64-bit math. However, the 32-bit result
1513 * cannot hold an elapsed time larger than approximately 50 days, so the
1514 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001515 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001516 * @param reftime Pointer to a reference time, which is updated to the current
1517 * uptime upon return.
1518 *
1519 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001520 */
Kumar Galacc334c72017-04-21 10:55:34 -05001521extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001522
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001523/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001524 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001525 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001526 * This routine returns the current time, as measured by the system's hardware
1527 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001528 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001529 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001530 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001531#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001532
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001533/**
Anas Nashif166f5192018-02-25 08:02:36 -06001534 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001535 */
1536
Allan Stephensc98da842016-11-11 15:45:03 -05001537/**
1538 * @cond INTERNAL_HIDDEN
1539 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001540
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001541struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001542 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001543 union {
1544 _wait_q_t wait_q;
1545
1546 _POLL_EVENT;
1547 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001548
1549 _OBJECT_TRACING_NEXT_PTR(k_queue);
1550};
1551
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001552#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001553 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001554 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Michael Hope5f67a612018-03-17 12:44:40 +01001555 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001556 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001557 _OBJECT_TRACING_INIT \
1558 }
1559
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001560#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1561
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001562/**
1563 * INTERNAL_HIDDEN @endcond
1564 */
1565
1566/**
1567 * @defgroup queue_apis Queue APIs
1568 * @ingroup kernel_apis
1569 * @{
1570 */
1571
1572/**
1573 * @brief Initialize a queue.
1574 *
1575 * This routine initializes a queue object, prior to its first use.
1576 *
1577 * @param queue Address of the queue.
1578 *
1579 * @return N/A
1580 */
1581extern void k_queue_init(struct k_queue *queue);
1582
1583/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001584 * @brief Cancel waiting on a queue.
1585 *
1586 * This routine causes first thread pending on @a queue, if any, to
1587 * return from k_queue_get() call with NULL value (as if timeout expired).
1588 *
1589 * @note Can be called by ISRs.
1590 *
1591 * @param queue Address of the queue.
1592 *
1593 * @return N/A
1594 */
1595extern void k_queue_cancel_wait(struct k_queue *queue);
1596
1597/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001598 * @brief Append an element to the end of a queue.
1599 *
1600 * This routine appends a data item to @a queue. A queue data item must be
1601 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1602 * reserved for the kernel's use.
1603 *
1604 * @note Can be called by ISRs.
1605 *
1606 * @param queue Address of the queue.
1607 * @param data Address of the data item.
1608 *
1609 * @return N/A
1610 */
1611extern void k_queue_append(struct k_queue *queue, void *data);
1612
1613/**
1614 * @brief Prepend an element to a queue.
1615 *
1616 * This routine prepends a data item to @a queue. A queue data item must be
1617 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1618 * reserved for the kernel's use.
1619 *
1620 * @note Can be called by ISRs.
1621 *
1622 * @param queue Address of the queue.
1623 * @param data Address of the data item.
1624 *
1625 * @return N/A
1626 */
1627extern void k_queue_prepend(struct k_queue *queue, void *data);
1628
1629/**
1630 * @brief Inserts an element to a queue.
1631 *
1632 * This routine inserts a data item to @a queue after previous item. A queue
1633 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1634 * item are reserved for the kernel's use.
1635 *
1636 * @note Can be called by ISRs.
1637 *
1638 * @param queue Address of the queue.
1639 * @param prev Address of the previous data item.
1640 * @param data Address of the data item.
1641 *
1642 * @return N/A
1643 */
1644extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1645
1646/**
1647 * @brief Atomically append a list of elements to a queue.
1648 *
1649 * This routine adds a list of data items to @a queue in one operation.
1650 * The data items must be in a singly-linked list, with the first 32 bits
1651 * in each data item pointing to the next data item; the list must be
1652 * NULL-terminated.
1653 *
1654 * @note Can be called by ISRs.
1655 *
1656 * @param queue Address of the queue.
1657 * @param head Pointer to first node in singly-linked list.
1658 * @param tail Pointer to last node in singly-linked list.
1659 *
1660 * @return N/A
1661 */
1662extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1663
1664/**
1665 * @brief Atomically add a list of elements to a queue.
1666 *
1667 * This routine adds a list of data items to @a queue in one operation.
1668 * The data items must be in a singly-linked list implemented using a
1669 * sys_slist_t object. Upon completion, the original list is empty.
1670 *
1671 * @note Can be called by ISRs.
1672 *
1673 * @param queue Address of the queue.
1674 * @param list Pointer to sys_slist_t object.
1675 *
1676 * @return N/A
1677 */
1678extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1679
1680/**
1681 * @brief Get an element from a queue.
1682 *
1683 * This routine removes first data item from @a queue. The first 32 bits of the
1684 * data item are reserved for the kernel's use.
1685 *
1686 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1687 *
1688 * @param queue Address of the queue.
1689 * @param timeout Waiting period to obtain a data item (in milliseconds),
1690 * or one of the special values K_NO_WAIT and K_FOREVER.
1691 *
1692 * @return Address of the data item if successful; NULL if returned
1693 * without waiting, or waiting period timed out.
1694 */
Kumar Galacc334c72017-04-21 10:55:34 -05001695extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001696
1697/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001698 * @brief Remove an element from a queue.
1699 *
1700 * This routine removes data item from @a queue. The first 32 bits of the
1701 * data item are reserved for the kernel's use. Removing elements from k_queue
1702 * rely on sys_slist_find_and_remove which is not a constant time operation.
1703 *
1704 * @note Can be called by ISRs
1705 *
1706 * @param queue Address of the queue.
1707 * @param data Address of the data item.
1708 *
1709 * @return true if data item was removed
1710 */
1711static inline bool k_queue_remove(struct k_queue *queue, void *data)
1712{
1713 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1714}
1715
1716/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001717 * @brief Query a queue to see if it has data available.
1718 *
1719 * Note that the data might be already gone by the time this function returns
1720 * if other threads are also trying to read from the queue.
1721 *
1722 * @note Can be called by ISRs.
1723 *
1724 * @param queue Address of the queue.
1725 *
1726 * @return Non-zero if the queue is empty.
1727 * @return 0 if data is available.
1728 */
1729static inline int k_queue_is_empty(struct k_queue *queue)
1730{
1731 return (int)sys_slist_is_empty(&queue->data_q);
1732}
1733
1734/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001735 * @brief Peek element at the head of queue.
1736 *
1737 * Return element from the head of queue without removing it.
1738 *
1739 * @param queue Address of the queue.
1740 *
1741 * @return Head element, or NULL if queue is empty.
1742 */
1743static inline void *k_queue_peek_head(struct k_queue *queue)
1744{
1745 return sys_slist_peek_head(&queue->data_q);
1746}
1747
1748/**
1749 * @brief Peek element at the tail of queue.
1750 *
1751 * Return element from the tail of queue without removing it.
1752 *
1753 * @param queue Address of the queue.
1754 *
1755 * @return Tail element, or NULL if queue is empty.
1756 */
1757static inline void *k_queue_peek_tail(struct k_queue *queue)
1758{
1759 return sys_slist_peek_tail(&queue->data_q);
1760}
1761
1762/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001763 * @brief Statically define and initialize a queue.
1764 *
1765 * The queue can be accessed outside the module where it is defined using:
1766 *
1767 * @code extern struct k_queue <name>; @endcode
1768 *
1769 * @param name Name of the queue.
1770 */
1771#define K_QUEUE_DEFINE(name) \
1772 struct k_queue name \
1773 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001774 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001775
Anas Nashif166f5192018-02-25 08:02:36 -06001776/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001777
1778/**
1779 * @cond INTERNAL_HIDDEN
1780 */
1781
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001782struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001783 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001784};
1785
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001786#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001787 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001788 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001789 }
1790
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001791#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1792
Allan Stephensc98da842016-11-11 15:45:03 -05001793/**
1794 * INTERNAL_HIDDEN @endcond
1795 */
1796
1797/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001798 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05001799 * @ingroup kernel_apis
1800 * @{
1801 */
1802
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001803/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001804 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001805 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001806 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001807 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001808 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001809 *
1810 * @return N/A
1811 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001812#define k_fifo_init(fifo) \
1813 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001814
1815/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001816 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001817 *
1818 * This routine causes first thread pending on @a fifo, if any, to
1819 * return from k_fifo_get() call with NULL value (as if timeout
1820 * expired).
1821 *
1822 * @note Can be called by ISRs.
1823 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001824 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001825 *
1826 * @return N/A
1827 */
1828#define k_fifo_cancel_wait(fifo) \
1829 k_queue_cancel_wait((struct k_queue *) fifo)
1830
1831/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001832 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001833 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001834 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001835 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1836 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001837 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001838 * @note Can be called by ISRs.
1839 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001840 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001841 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001842 *
1843 * @return N/A
1844 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001845#define k_fifo_put(fifo, data) \
1846 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001847
1848/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001849 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001850 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001851 * This routine adds a list of data items to @a fifo in one operation.
1852 * The data items must be in a singly-linked list, with the first 32 bits
1853 * each data item pointing to the next data item; the list must be
1854 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001856 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001857 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001858 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001859 * @param head Pointer to first node in singly-linked list.
1860 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001861 *
1862 * @return N/A
1863 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001864#define k_fifo_put_list(fifo, head, tail) \
1865 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001866
1867/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001868 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001869 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001870 * This routine adds a list of data items to @a fifo in one operation.
1871 * The data items must be in a singly-linked list implemented using a
1872 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001873 * and must be re-initialized via sys_slist_init().
1874 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001875 * @note Can be called by ISRs.
1876 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001877 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001878 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001879 *
1880 * @return N/A
1881 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001882#define k_fifo_put_slist(fifo, list) \
1883 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001884
1885/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001886 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001887 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001888 * This routine removes a data item from @a fifo in a "first in, first out"
1889 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001890 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001891 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1892 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001893 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001894 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001895 * or one of the special values K_NO_WAIT and K_FOREVER.
1896 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001897 * @return Address of the data item if successful; NULL if returned
1898 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001899 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001900#define k_fifo_get(fifo, timeout) \
1901 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001902
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001903/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001904 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001905 *
1906 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06001907 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001908 *
1909 * @note Can be called by ISRs.
1910 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001911 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001912 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001913 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001914 * @return 0 if data is available.
1915 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001916#define k_fifo_is_empty(fifo) \
1917 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001918
1919/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001920 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001921 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001922 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05301923 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001924 * on each iteration of processing, a head container will be peeked,
1925 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06001926 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001927 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001928 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001929 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001930 * @return Head element, or NULL if the FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001931 */
1932#define k_fifo_peek_head(fifo) \
1933 k_queue_peek_head((struct k_queue *) fifo)
1934
1935/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001936 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001937 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001938 * Return element from the tail of FIFO queue (without removing it). A usecase
1939 * for this is if elements of the FIFO queue are themselves containers. Then
1940 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001941 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001942 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001943 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001944 * @return Tail element, or NULL if a FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001945 */
1946#define k_fifo_peek_tail(fifo) \
1947 k_queue_peek_tail((struct k_queue *) fifo)
1948
1949/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001950 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001951 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001952 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001953 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001954 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001955 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001956 * @param name Name of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001957 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001958#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001959 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001960 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001961 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001962
Anas Nashif166f5192018-02-25 08:02:36 -06001963/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001964
1965/**
1966 * @cond INTERNAL_HIDDEN
1967 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001968
1969struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001970 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001971};
1972
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001973#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001974 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001975 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001976 }
1977
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001978#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1979
Allan Stephensc98da842016-11-11 15:45:03 -05001980/**
1981 * INTERNAL_HIDDEN @endcond
1982 */
1983
1984/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001985 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05001986 * @ingroup kernel_apis
1987 * @{
1988 */
1989
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001990/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001991 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001992 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001993 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001994 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001995 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001996 *
1997 * @return N/A
1998 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001999#define k_lifo_init(lifo) \
2000 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002001
2002/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002003 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002004 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002005 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002006 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2007 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002008 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002009 * @note Can be called by ISRs.
2010 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002011 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002012 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002013 *
2014 * @return N/A
2015 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002016#define k_lifo_put(lifo, data) \
2017 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002018
2019/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002020 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002021 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002022 * This routine removes a data item from @a lifo in a "last in, first out"
2023 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002024 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002025 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2026 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002027 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002028 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002029 * or one of the special values K_NO_WAIT and K_FOREVER.
2030 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002031 * @return Address of the data item if successful; NULL if returned
2032 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002033 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002034#define k_lifo_get(lifo, timeout) \
2035 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002036
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002037/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002038 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002039 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002040 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002041 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002042 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002043 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002044 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002045 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002046#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002047 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002048 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002049 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002050
Anas Nashif166f5192018-02-25 08:02:36 -06002051/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002052
2053/**
2054 * @cond INTERNAL_HIDDEN
2055 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002056
2057struct k_stack {
2058 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002059 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002060
Anas Nashif2f203c22016-12-18 06:57:45 -05002061 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002062};
2063
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002064#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002065 { \
2066 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2067 .base = stack_buffer, \
2068 .next = stack_buffer, \
2069 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002070 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002071 }
2072
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002073#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2074
Allan Stephensc98da842016-11-11 15:45:03 -05002075/**
2076 * INTERNAL_HIDDEN @endcond
2077 */
2078
2079/**
2080 * @defgroup stack_apis Stack APIs
2081 * @ingroup kernel_apis
2082 * @{
2083 */
2084
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002085/**
2086 * @brief Initialize a stack.
2087 *
2088 * This routine initializes a stack object, prior to its first use.
2089 *
2090 * @param stack Address of the stack.
2091 * @param buffer Address of array used to hold stacked values.
2092 * @param num_entries Maximum number of values that can be stacked.
2093 *
2094 * @return N/A
2095 */
Andrew Boiee8734462017-09-29 16:42:07 -07002096__syscall void k_stack_init(struct k_stack *stack,
Andrew Boie8e3e6d02017-10-12 14:01:25 -07002097 u32_t *buffer, unsigned int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002098
2099/**
2100 * @brief Push an element onto a stack.
2101 *
2102 * This routine adds a 32-bit value @a data to @a stack.
2103 *
2104 * @note Can be called by ISRs.
2105 *
2106 * @param stack Address of the stack.
2107 * @param data Value to push onto the stack.
2108 *
2109 * @return N/A
2110 */
Andrew Boiee8734462017-09-29 16:42:07 -07002111__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002112
2113/**
2114 * @brief Pop an element from a stack.
2115 *
2116 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2117 * manner and stores the value in @a data.
2118 *
2119 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2120 *
2121 * @param stack Address of the stack.
2122 * @param data Address of area to hold the value popped from the stack.
2123 * @param timeout Waiting period to obtain a value (in milliseconds),
2124 * or one of the special values K_NO_WAIT and K_FOREVER.
2125 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002126 * @retval 0 Element popped from stack.
2127 * @retval -EBUSY Returned without waiting.
2128 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002129 */
Andrew Boiee8734462017-09-29 16:42:07 -07002130__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002131
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002132/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002133 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002135 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002136 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002137 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002139 * @param name Name of the stack.
2140 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002141 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002142#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002143 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002144 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002145 struct k_stack name \
2146 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002147 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002148 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002149
Anas Nashif166f5192018-02-25 08:02:36 -06002150/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002151
Allan Stephens6bba9b02016-11-16 14:56:54 -05002152struct k_work;
2153
Allan Stephensc98da842016-11-11 15:45:03 -05002154/**
2155 * @defgroup workqueue_apis Workqueue Thread APIs
2156 * @ingroup kernel_apis
2157 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002158 */
2159
Allan Stephens6bba9b02016-11-16 14:56:54 -05002160/**
2161 * @typedef k_work_handler_t
2162 * @brief Work item handler function type.
2163 *
2164 * A work item's handler function is executed by a workqueue's thread
2165 * when the work item is processed by the workqueue.
2166 *
2167 * @param work Address of the work item.
2168 *
2169 * @return N/A
2170 */
2171typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002172
2173/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002174 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002175 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002176
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002177struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002178 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002179 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002180};
2181
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002182enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002183 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002184};
2185
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002186struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002187 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002188 k_work_handler_t handler;
2189 atomic_t flags[1];
2190};
2191
Allan Stephens6bba9b02016-11-16 14:56:54 -05002192struct k_delayed_work {
2193 struct k_work work;
2194 struct _timeout timeout;
2195 struct k_work_q *work_q;
2196};
2197
2198extern struct k_work_q k_sys_work_q;
2199
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002200/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002201 * INTERNAL_HIDDEN @endcond
2202 */
2203
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002204#define _K_WORK_INITIALIZER(work_handler) \
2205 { \
2206 ._reserved = NULL, \
2207 .handler = work_handler, \
2208 .flags = { 0 } \
2209 }
2210
2211#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2212
Allan Stephens6bba9b02016-11-16 14:56:54 -05002213/**
2214 * @brief Initialize a statically-defined work item.
2215 *
2216 * This macro can be used to initialize a statically-defined workqueue work
2217 * item, prior to its first use. For example,
2218 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002219 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002220 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002221 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002222 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002223 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002224#define K_WORK_DEFINE(work, work_handler) \
2225 struct k_work work \
2226 __in_section(_k_work, static, work) = \
2227 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002228
2229/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002230 * @brief Initialize a work item.
2231 *
2232 * This routine initializes a workqueue work item, prior to its first use.
2233 *
2234 * @param work Address of work item.
2235 * @param handler Function to invoke each time work item is processed.
2236 *
2237 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002238 */
2239static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2240{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002241 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002242 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002243 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002244}
2245
2246/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002247 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002248 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002249 * This routine submits work item @a work to be processed by workqueue
2250 * @a work_q. If the work item is already pending in the workqueue's queue
2251 * as a result of an earlier submission, this routine has no effect on the
2252 * work item. If the work item has already been processed, or is currently
2253 * being processed, its work is considered complete and the work item can be
2254 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002255 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002256 * @warning
2257 * A submitted work item must not be modified until it has been processed
2258 * by the workqueue.
2259 *
2260 * @note Can be called by ISRs.
2261 *
2262 * @param work_q Address of workqueue.
2263 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002264 *
2265 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002266 */
2267static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2268 struct k_work *work)
2269{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002270 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002271 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002272 }
2273}
2274
2275/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002276 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002277 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002278 * This routine indicates if work item @a work is pending in a workqueue's
2279 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002280 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002281 * @note Can be called by ISRs.
2282 *
2283 * @param work Address of work item.
2284 *
2285 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002286 */
2287static inline int k_work_pending(struct k_work *work)
2288{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002289 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002290}
2291
2292/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002293 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002294 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002295 * This routine starts workqueue @a work_q. The workqueue spawns its work
2296 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002297 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002298 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002299 * @param stack Pointer to work queue thread's stack space, as defined by
2300 * K_THREAD_STACK_DEFINE()
2301 * @param stack_size Size of the work queue thread's stack (in bytes), which
2302 * should either be the same constant passed to
2303 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002304 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002305 *
2306 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002307 */
Andrew Boie507852a2017-07-25 18:47:07 -07002308extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002309 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002310 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002311
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002312/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002313 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002314 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002315 * This routine initializes a workqueue delayed work item, prior to
2316 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002317 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002318 * @param work Address of delayed work item.
2319 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002320 *
2321 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002322 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002323extern void k_delayed_work_init(struct k_delayed_work *work,
2324 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002325
2326/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002327 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002328 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002329 * This routine schedules work item @a work to be processed by workqueue
2330 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002331 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002332 * Only when the countdown completes is the work item actually submitted to
2333 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002334 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002335 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002336 * counting down cancels the existing submission and restarts the
2337 * countdown using the new delay. Note that this behavior is
2338 * inherently subject to race conditions with the pre-existing
2339 * timeouts and work queue, so care must be taken to synchronize such
2340 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002341 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002342 * @warning
2343 * A delayed work item must not be modified until it has been processed
2344 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002345 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002346 * @note Can be called by ISRs.
2347 *
2348 * @param work_q Address of workqueue.
2349 * @param work Address of delayed work item.
2350 * @param delay Delay before submitting the work item (in milliseconds).
2351 *
2352 * @retval 0 Work item countdown started.
2353 * @retval -EINPROGRESS Work item is already pending.
2354 * @retval -EINVAL Work item is being processed or has completed its work.
2355 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002356 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002357extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2358 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002359 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002360
2361/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002362 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002363 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002364 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002365 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002366 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002367 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002368 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002369 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002370 * @param work Address of delayed work item.
2371 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002372 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002373 * @retval -EINPROGRESS Work item is already pending.
2374 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002375 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002376extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002377
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002378/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002379 * @brief Submit a work item to the system workqueue.
2380 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002381 * This routine submits work item @a work to be processed by the system
2382 * workqueue. If the work item is already pending in the workqueue's queue
2383 * as a result of an earlier submission, this routine has no effect on the
2384 * work item. If the work item has already been processed, or is currently
2385 * being processed, its work is considered complete and the work item can be
2386 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002387 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002388 * @warning
2389 * Work items submitted to the system workqueue should avoid using handlers
2390 * that block or yield since this may prevent the system workqueue from
2391 * processing other work items in a timely manner.
2392 *
2393 * @note Can be called by ISRs.
2394 *
2395 * @param work Address of work item.
2396 *
2397 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002398 */
2399static inline void k_work_submit(struct k_work *work)
2400{
2401 k_work_submit_to_queue(&k_sys_work_q, work);
2402}
2403
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002404/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002405 * @brief Submit a delayed work item to the system workqueue.
2406 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002407 * This routine schedules work item @a work to be processed by the system
2408 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002409 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002410 * Only when the countdown completes is the work item actually submitted to
2411 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002412 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002413 * Submitting a previously submitted delayed work item that is still
2414 * counting down cancels the existing submission and restarts the countdown
2415 * using the new delay. If the work item is currently pending on the
2416 * workqueue's queue because the countdown has completed it is too late to
2417 * resubmit the item, and resubmission fails without impacting the work item.
2418 * If the work item has already been processed, or is currently being processed,
2419 * its work is considered complete and the work item can be resubmitted.
2420 *
2421 * @warning
2422 * Work items submitted to the system workqueue should avoid using handlers
2423 * that block or yield since this may prevent the system workqueue from
2424 * processing other work items in a timely manner.
2425 *
2426 * @note Can be called by ISRs.
2427 *
2428 * @param work Address of delayed work item.
2429 * @param delay Delay before submitting the work item (in milliseconds).
2430 *
2431 * @retval 0 Work item countdown started.
2432 * @retval -EINPROGRESS Work item is already pending.
2433 * @retval -EINVAL Work item is being processed or has completed its work.
2434 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002435 */
2436static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002437 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002438{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002439 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002440}
2441
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002442/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002443 * @brief Get time remaining before a delayed work gets scheduled.
2444 *
2445 * This routine computes the (approximate) time remaining before a
2446 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002447 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002448 *
2449 * @param work Delayed work item.
2450 *
2451 * @return Remaining time (in milliseconds).
2452 */
Kumar Galacc334c72017-04-21 10:55:34 -05002453static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002454{
2455 return _timeout_remaining_get(&work->timeout);
2456}
2457
Anas Nashif166f5192018-02-25 08:02:36 -06002458/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002459
Allan Stephensc98da842016-11-11 15:45:03 -05002460/**
2461 * @cond INTERNAL_HIDDEN
2462 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002463
2464struct k_mutex {
2465 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002466 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002467 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002468 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002469
Anas Nashif2f203c22016-12-18 06:57:45 -05002470 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002471};
2472
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002473#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002474 { \
2475 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2476 .owner = NULL, \
2477 .lock_count = 0, \
2478 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002479 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002480 }
2481
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002482#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2483
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002484/**
Allan Stephensc98da842016-11-11 15:45:03 -05002485 * INTERNAL_HIDDEN @endcond
2486 */
2487
2488/**
2489 * @defgroup mutex_apis Mutex APIs
2490 * @ingroup kernel_apis
2491 * @{
2492 */
2493
2494/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002495 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002496 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002497 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002498 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002499 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002500 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002501 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002502 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002503#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002504 struct k_mutex name \
2505 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002506 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002507
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002508/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002509 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002510 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002511 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002512 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002513 * Upon completion, the mutex is available and does not have an owner.
2514 *
2515 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002516 *
2517 * @return N/A
2518 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002519__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002520
2521/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002522 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002523 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002524 * This routine locks @a mutex. If the mutex is locked by another thread,
2525 * the calling thread waits until the mutex becomes available or until
2526 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002527 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002528 * A thread is permitted to lock a mutex it has already locked. The operation
2529 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002530 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002531 * @param mutex Address of the mutex.
2532 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002533 * or one of the special values K_NO_WAIT and K_FOREVER.
2534 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002535 * @retval 0 Mutex locked.
2536 * @retval -EBUSY Returned without waiting.
2537 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002538 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002539__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002540
2541/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002542 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002543 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002544 * This routine unlocks @a mutex. The mutex must already be locked by the
2545 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002546 *
2547 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002548 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002549 * thread.
2550 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002551 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002552 *
2553 * @return N/A
2554 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002555__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002556
Allan Stephensc98da842016-11-11 15:45:03 -05002557/**
Anas Nashif166f5192018-02-25 08:02:36 -06002558 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002559 */
2560
2561/**
2562 * @cond INTERNAL_HIDDEN
2563 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002564
2565struct k_sem {
2566 _wait_q_t wait_q;
2567 unsigned int count;
2568 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002569 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002570
Anas Nashif2f203c22016-12-18 06:57:45 -05002571 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002572};
2573
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002574#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002575 { \
2576 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2577 .count = initial_count, \
2578 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002579 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002580 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002581 }
2582
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002583#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2584
Allan Stephensc98da842016-11-11 15:45:03 -05002585/**
2586 * INTERNAL_HIDDEN @endcond
2587 */
2588
2589/**
2590 * @defgroup semaphore_apis Semaphore APIs
2591 * @ingroup kernel_apis
2592 * @{
2593 */
2594
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002595/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002596 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002597 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002598 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002599 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002600 * @param sem Address of the semaphore.
2601 * @param initial_count Initial semaphore count.
2602 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002603 *
2604 * @return N/A
2605 */
Andrew Boie99280232017-09-29 14:17:47 -07002606__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2607 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002608
2609/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002610 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002611 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002612 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002613 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002614 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2615 *
2616 * @param sem Address of the semaphore.
2617 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002618 * or one of the special values K_NO_WAIT and K_FOREVER.
2619 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002620 * @note When porting code from the nanokernel legacy API to the new API, be
2621 * careful with the return value of this function. The return value is the
2622 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2623 * non-zero means failure, while the nano_sem_take family returns 1 for success
2624 * and 0 for failure.
2625 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002626 * @retval 0 Semaphore taken.
2627 * @retval -EBUSY Returned without waiting.
2628 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002629 */
Andrew Boie99280232017-09-29 14:17:47 -07002630__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002631
2632/**
2633 * @brief Give a semaphore.
2634 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002635 * This routine gives @a sem, unless the semaphore is already at its maximum
2636 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002637 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002638 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002639 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002640 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002641 *
2642 * @return N/A
2643 */
Andrew Boie99280232017-09-29 14:17:47 -07002644__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002645
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002646/**
2647 * @brief Reset a semaphore's count to zero.
2648 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002649 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002650 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002651 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002652 *
2653 * @return N/A
2654 */
Andrew Boie990bf162017-10-03 12:36:49 -07002655__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002656
Anas Nashif954d5502018-02-25 08:37:28 -06002657/**
2658 * @internal
2659 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002660static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002661{
2662 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002663}
2664
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002665/**
2666 * @brief Get a semaphore's count.
2667 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002668 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002669 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002670 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002671 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002672 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002673 */
Andrew Boie990bf162017-10-03 12:36:49 -07002674__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002675
Anas Nashif954d5502018-02-25 08:37:28 -06002676/**
2677 * @internal
2678 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002679static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002680{
2681 return sem->count;
2682}
2683
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002684/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002685 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002686 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002687 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002688 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002689 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002690 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002691 * @param name Name of the semaphore.
2692 * @param initial_count Initial semaphore count.
2693 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002694 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002695#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002696 struct k_sem name \
2697 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07002698 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
2699 BUILD_ASSERT((count_limit) != 0); \
2700 BUILD_ASSERT((initial_count) <= (count_limit));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002701
Anas Nashif166f5192018-02-25 08:02:36 -06002702/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002703
2704/**
2705 * @defgroup alert_apis Alert APIs
2706 * @ingroup kernel_apis
2707 * @{
2708 */
2709
Allan Stephens5eceb852016-11-16 10:16:30 -05002710/**
2711 * @typedef k_alert_handler_t
2712 * @brief Alert handler function type.
2713 *
2714 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002715 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002716 * and is only invoked if the alert has been initialized with one.
2717 *
2718 * @param alert Address of the alert.
2719 *
2720 * @return 0 if alert has been consumed; non-zero if alert should pend.
2721 */
2722typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002723
Anas Nashif166f5192018-02-25 08:02:36 -06002724/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002725
2726/**
2727 * @cond INTERNAL_HIDDEN
2728 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002729
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002730#define K_ALERT_DEFAULT NULL
2731#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002732
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002733struct k_alert {
2734 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002735 atomic_t send_count;
2736 struct k_work work_item;
2737 struct k_sem sem;
2738
Anas Nashif2f203c22016-12-18 06:57:45 -05002739 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002740};
2741
Anas Nashif954d5502018-02-25 08:37:28 -06002742/**
2743 * @internal
2744 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002745extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002746
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002747#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002748 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002749 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002750 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002751 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2752 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002753 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002754 }
2755
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002756#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2757
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002758/**
Allan Stephensc98da842016-11-11 15:45:03 -05002759 * INTERNAL_HIDDEN @endcond
2760 */
2761
2762/**
2763 * @addtogroup alert_apis
2764 * @{
2765 */
2766
2767/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002768 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002769 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002770 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002771 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002772 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002773 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002774 * @param name Name of the alert.
2775 * @param alert_handler Action to take when alert is sent. Specify either
2776 * the address of a function to be invoked by the system workqueue
2777 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2778 * K_ALERT_DEFAULT (which causes the alert to pend).
2779 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002780 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002781#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002782 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002783 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002784 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002785 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002786
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002787/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002788 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002789 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002790 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002791 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002792 * @param alert Address of the alert.
2793 * @param handler Action to take when alert is sent. Specify either the address
2794 * of a function to be invoked by the system workqueue thread,
2795 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2796 * K_ALERT_DEFAULT (which causes the alert to pend).
2797 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002798 *
2799 * @return N/A
2800 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002801extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2802 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002803
2804/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002805 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002806 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002807 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002808 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002809 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2810 *
2811 * @param alert Address of the alert.
2812 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002813 * or one of the special values K_NO_WAIT and K_FOREVER.
2814 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002815 * @retval 0 Alert received.
2816 * @retval -EBUSY Returned without waiting.
2817 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002818 */
Andrew Boie310e9872017-09-29 04:41:15 -07002819__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002820
2821/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002822 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002823 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002824 * This routine signals @a alert. The action specified for @a alert will
2825 * be taken, which may trigger the execution of an alert handler function
2826 * and/or cause the alert to pend (assuming the alert has not reached its
2827 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002828 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002829 * @note Can be called by ISRs.
2830 *
2831 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002832 *
2833 * @return N/A
2834 */
Andrew Boie310e9872017-09-29 04:41:15 -07002835__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002836
2837/**
Anas Nashif166f5192018-02-25 08:02:36 -06002838 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002839 */
2840
Allan Stephensc98da842016-11-11 15:45:03 -05002841/**
2842 * @cond INTERNAL_HIDDEN
2843 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002844
2845struct k_msgq {
2846 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002847 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002848 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002849 char *buffer_start;
2850 char *buffer_end;
2851 char *read_ptr;
2852 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002853 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002854
Anas Nashif2f203c22016-12-18 06:57:45 -05002855 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002856};
2857
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002858#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002859 { \
2860 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002861 .max_msgs = q_max_msgs, \
2862 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002863 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002864 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002865 .read_ptr = q_buffer, \
2866 .write_ptr = q_buffer, \
2867 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002868 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002869 }
2870
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002871#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2872
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05302873struct k_msgq_attrs {
2874 size_t msg_size;
2875 u32_t max_msgs;
2876 u32_t used_msgs;
2877};
2878
Peter Mitsis1da807e2016-10-06 11:36:59 -04002879/**
Allan Stephensc98da842016-11-11 15:45:03 -05002880 * INTERNAL_HIDDEN @endcond
2881 */
2882
2883/**
2884 * @defgroup msgq_apis Message Queue APIs
2885 * @ingroup kernel_apis
2886 * @{
2887 */
2888
2889/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002890 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002891 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002892 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2893 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002894 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2895 * message is similarly aligned to this boundary, @a q_msg_size must also be
2896 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002897 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002898 * The message queue can be accessed outside the module where it is defined
2899 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002900 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002901 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002902 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002903 * @param q_name Name of the message queue.
2904 * @param q_msg_size Message size (in bytes).
2905 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002906 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002907 */
2908#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2909 static char __noinit __aligned(q_align) \
2910 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002911 struct k_msgq q_name \
2912 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002913 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002914 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002915
Peter Mitsisd7a37502016-10-13 11:37:40 -04002916/**
2917 * @brief Initialize a message queue.
2918 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002919 * This routine initializes a message queue object, prior to its first use.
2920 *
Allan Stephensda827222016-11-09 14:23:58 -06002921 * The message queue's ring buffer must contain space for @a max_msgs messages,
2922 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2923 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2924 * that each message is similarly aligned to this boundary, @a q_msg_size
2925 * must also be a multiple of N.
2926 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002927 * @param q Address of the message queue.
2928 * @param buffer Pointer to ring buffer that holds queued messages.
2929 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002930 * @param max_msgs Maximum number of messages that can be queued.
2931 *
2932 * @return N/A
2933 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002934__syscall void k_msgq_init(struct k_msgq *q, char *buffer,
2935 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002936
2937/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002938 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002939 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002940 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002941 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002942 * @note Can be called by ISRs.
2943 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002944 * @param q Address of the message queue.
2945 * @param data Pointer to the message.
2946 * @param timeout Waiting period to add the message (in milliseconds),
2947 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002948 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002949 * @retval 0 Message sent.
2950 * @retval -ENOMSG Returned without waiting or queue purged.
2951 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002952 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002953__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002954
2955/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002956 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002957 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002958 * This routine receives a message from message queue @a q in a "first in,
2959 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002960 *
Allan Stephensc98da842016-11-11 15:45:03 -05002961 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002962 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002963 * @param q Address of the message queue.
2964 * @param data Address of area to hold the received message.
2965 * @param timeout Waiting period to receive the message (in milliseconds),
2966 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002967 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002968 * @retval 0 Message received.
2969 * @retval -ENOMSG Returned without waiting.
2970 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002971 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002972__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002973
2974/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002975 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002976 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002977 * This routine discards all unreceived messages in a message queue's ring
2978 * buffer. Any threads that are blocked waiting to send a message to the
2979 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002980 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002981 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002982 *
2983 * @return N/A
2984 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002985__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002986
Peter Mitsis67be2492016-10-07 11:44:34 -04002987/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002988 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002989 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002990 * This routine returns the number of unused entries in a message queue's
2991 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002993 * @param q Address of the message queue.
2994 *
2995 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002996 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002997__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
2998
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05302999/**
3000 * @brief Get basic attributes of a message queue.
3001 *
3002 * This routine fetches basic attributes of message queue into attr argument.
3003 *
3004 * @param q Address of the message queue.
3005 * @param attrs pointer to message queue attribute structure.
3006 *
3007 * @return N/A
3008 */
3009__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3010
3011
Andrew Boie82edb6e2017-10-02 10:53:06 -07003012static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003013{
3014 return q->max_msgs - q->used_msgs;
3015}
3016
Peter Mitsisd7a37502016-10-13 11:37:40 -04003017/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003018 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003019 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003020 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003021 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003022 * @param q Address of the message queue.
3023 *
3024 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003025 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003026__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3027
3028static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003029{
3030 return q->used_msgs;
3031}
3032
Anas Nashif166f5192018-02-25 08:02:36 -06003033/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003034
3035/**
3036 * @defgroup mem_pool_apis Memory Pool APIs
3037 * @ingroup kernel_apis
3038 * @{
3039 */
3040
Andy Ross73cb9582017-05-09 10:42:39 -07003041/* Note on sizing: the use of a 20 bit field for block means that,
3042 * assuming a reasonable minimum block size of 16 bytes, we're limited
3043 * to 16M of memory managed by a single pool. Long term it would be
3044 * good to move to a variable bit size based on configuration.
3045 */
3046struct k_mem_block_id {
3047 u32_t pool : 8;
3048 u32_t level : 4;
3049 u32_t block : 20;
3050};
3051
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003052struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003053 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003054 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003055};
3056
Anas Nashif166f5192018-02-25 08:02:36 -06003057/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003058
3059/**
3060 * @defgroup mailbox_apis Mailbox APIs
3061 * @ingroup kernel_apis
3062 * @{
3063 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003064
3065struct k_mbox_msg {
3066 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003067 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003068 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003069 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003070 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003071 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003072 /** sender's message data buffer */
3073 void *tx_data;
3074 /** internal use only - needed for legacy API support */
3075 void *_rx_data;
3076 /** message data block descriptor */
3077 struct k_mem_block tx_block;
3078 /** source thread id */
3079 k_tid_t rx_source_thread;
3080 /** target thread id */
3081 k_tid_t tx_target_thread;
3082 /** internal use only - thread waiting on send (may be a dummy) */
3083 k_tid_t _syncing_thread;
3084#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3085 /** internal use only - semaphore used during asynchronous send */
3086 struct k_sem *_async_sem;
3087#endif
3088};
3089
Allan Stephensc98da842016-11-11 15:45:03 -05003090/**
3091 * @cond INTERNAL_HIDDEN
3092 */
3093
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003094struct k_mbox {
3095 _wait_q_t tx_msg_queue;
3096 _wait_q_t rx_msg_queue;
3097
Anas Nashif2f203c22016-12-18 06:57:45 -05003098 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003099};
3100
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003101#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003102 { \
3103 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3104 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003105 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003106 }
3107
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003108#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3109
Peter Mitsis12092702016-10-14 12:57:23 -04003110/**
Allan Stephensc98da842016-11-11 15:45:03 -05003111 * INTERNAL_HIDDEN @endcond
3112 */
3113
3114/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003115 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003116 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003117 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003118 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003119 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003120 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003121 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003122 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003123#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003124 struct k_mbox name \
3125 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003126 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003127
Peter Mitsis12092702016-10-14 12:57:23 -04003128/**
3129 * @brief Initialize a mailbox.
3130 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003131 * This routine initializes a mailbox object, prior to its first use.
3132 *
3133 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003134 *
3135 * @return N/A
3136 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003137extern void k_mbox_init(struct k_mbox *mbox);
3138
Peter Mitsis12092702016-10-14 12:57:23 -04003139/**
3140 * @brief Send a mailbox message in a synchronous manner.
3141 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003142 * This routine sends a message to @a mbox and waits for a receiver to both
3143 * receive and process it. The message data may be in a buffer, in a memory
3144 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003145 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003146 * @param mbox Address of the mailbox.
3147 * @param tx_msg Address of the transmit message descriptor.
3148 * @param timeout Waiting period for the message to be received (in
3149 * milliseconds), or one of the special values K_NO_WAIT
3150 * and K_FOREVER. Once the message has been received,
3151 * this routine waits as long as necessary for the message
3152 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003153 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003154 * @retval 0 Message sent.
3155 * @retval -ENOMSG Returned without waiting.
3156 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003157 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003158extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003159 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003160
Peter Mitsis12092702016-10-14 12:57:23 -04003161/**
3162 * @brief Send a mailbox message in an asynchronous manner.
3163 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003164 * This routine sends a message to @a mbox without waiting for a receiver
3165 * to process it. The message data may be in a buffer, in a memory pool block,
3166 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3167 * will be given when the message has been both received and completely
3168 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003169 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003170 * @param mbox Address of the mailbox.
3171 * @param tx_msg Address of the transmit message descriptor.
3172 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003173 *
3174 * @return N/A
3175 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003176extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003177 struct k_sem *sem);
3178
Peter Mitsis12092702016-10-14 12:57:23 -04003179/**
3180 * @brief Receive a mailbox message.
3181 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003182 * This routine receives a message from @a mbox, then optionally retrieves
3183 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003184 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003185 * @param mbox Address of the mailbox.
3186 * @param rx_msg Address of the receive message descriptor.
3187 * @param buffer Address of the buffer to receive data, or NULL to defer data
3188 * retrieval and message disposal until later.
3189 * @param timeout Waiting period for a message to be received (in
3190 * milliseconds), or one of the special values K_NO_WAIT
3191 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003192 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003193 * @retval 0 Message received.
3194 * @retval -ENOMSG Returned without waiting.
3195 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003196 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003197extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003198 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003199
3200/**
3201 * @brief Retrieve mailbox message data into a buffer.
3202 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003203 * This routine completes the processing of a received message by retrieving
3204 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003205 *
3206 * Alternatively, this routine can be used to dispose of a received message
3207 * without retrieving its data.
3208 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003209 * @param rx_msg Address of the receive message descriptor.
3210 * @param buffer Address of the buffer to receive data, or NULL to discard
3211 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003212 *
3213 * @return N/A
3214 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003215extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003216
3217/**
3218 * @brief Retrieve mailbox message data into a memory pool block.
3219 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003220 * This routine completes the processing of a received message by retrieving
3221 * its data into a memory pool block, then disposing of the message.
3222 * The memory pool block that results from successful retrieval must be
3223 * returned to the pool once the data has been processed, even in cases
3224 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003225 *
3226 * Alternatively, this routine can be used to dispose of a received message
3227 * without retrieving its data. In this case there is no need to return a
3228 * memory pool block to the pool.
3229 *
3230 * This routine allocates a new memory pool block for the data only if the
3231 * data is not already in one. If a new block cannot be allocated, the routine
3232 * returns a failure code and the received message is left unchanged. This
3233 * permits the caller to reattempt data retrieval at a later time or to dispose
3234 * of the received message without retrieving its data.
3235 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003236 * @param rx_msg Address of a receive message descriptor.
3237 * @param pool Address of memory pool, or NULL to discard data.
3238 * @param block Address of the area to hold memory pool block info.
3239 * @param timeout Waiting period to wait for a memory pool block (in
3240 * milliseconds), or one of the special values K_NO_WAIT
3241 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003242 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003243 * @retval 0 Data retrieved.
3244 * @retval -ENOMEM Returned without waiting.
3245 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003246 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003247extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003248 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003249 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003250
Anas Nashif166f5192018-02-25 08:02:36 -06003251/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003252
3253/**
3254 * @cond INTERNAL_HIDDEN
3255 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003256
3257struct k_pipe {
3258 unsigned char *buffer; /* Pipe buffer: may be NULL */
3259 size_t size; /* Buffer size */
3260 size_t bytes_used; /* # bytes used in buffer */
3261 size_t read_index; /* Where in buffer to read from */
3262 size_t write_index; /* Where in buffer to write */
3263
3264 struct {
3265 _wait_q_t readers; /* Reader wait queue */
3266 _wait_q_t writers; /* Writer wait queue */
3267 } wait_q;
3268
Anas Nashif2f203c22016-12-18 06:57:45 -05003269 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003270};
3271
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003272#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003273 { \
3274 .buffer = pipe_buffer, \
3275 .size = pipe_buffer_size, \
3276 .bytes_used = 0, \
3277 .read_index = 0, \
3278 .write_index = 0, \
3279 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3280 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003281 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003282 }
3283
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003284#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3285
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003286/**
Allan Stephensc98da842016-11-11 15:45:03 -05003287 * INTERNAL_HIDDEN @endcond
3288 */
3289
3290/**
3291 * @defgroup pipe_apis Pipe APIs
3292 * @ingroup kernel_apis
3293 * @{
3294 */
3295
3296/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003297 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003298 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003299 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003300 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003301 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003302 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003303 * @param name Name of the pipe.
3304 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3305 * or zero if no ring buffer is used.
3306 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003307 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003308#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3309 static unsigned char __noinit __aligned(pipe_align) \
3310 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003311 struct k_pipe name \
3312 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003313 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003314
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003315/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003316 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003317 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003318 * This routine initializes a pipe object, prior to its first use.
3319 *
3320 * @param pipe Address of the pipe.
3321 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3322 * is used.
3323 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3324 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003325 *
3326 * @return N/A
3327 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003328__syscall void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3329 size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003330
3331/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003332 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003333 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003334 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003335 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003336 * @param pipe Address of the pipe.
3337 * @param data Address of data to write.
3338 * @param bytes_to_write Size of data (in bytes).
3339 * @param bytes_written Address of area to hold the number of bytes written.
3340 * @param min_xfer Minimum number of bytes to write.
3341 * @param timeout Waiting period to wait for the data to be written (in
3342 * milliseconds), or one of the special values K_NO_WAIT
3343 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003344 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003345 * @retval 0 At least @a min_xfer bytes of data were written.
3346 * @retval -EIO Returned without waiting; zero data bytes were written.
3347 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003348 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003349 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003350__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3351 size_t bytes_to_write, size_t *bytes_written,
3352 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003353
3354/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003355 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003356 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003357 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003358 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003359 * @param pipe Address of the pipe.
3360 * @param data Address to place the data read from pipe.
3361 * @param bytes_to_read Maximum number of data bytes to read.
3362 * @param bytes_read Address of area to hold the number of bytes read.
3363 * @param min_xfer Minimum number of data bytes to read.
3364 * @param timeout Waiting period to wait for the data to be read (in
3365 * milliseconds), or one of the special values K_NO_WAIT
3366 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003367 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003368 * @retval 0 At least @a min_xfer bytes of data were read.
3369 * @retval -EIO Returned without waiting; zero data bytes were read.
3370 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003371 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003372 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003373__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3374 size_t bytes_to_read, size_t *bytes_read,
3375 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003376
3377/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003378 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003379 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003380 * This routine writes the data contained in a memory block to @a pipe.
3381 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003382 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003383 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003384 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003385 * @param block Memory block containing data to send
3386 * @param size Number of data bytes in memory block to send
3387 * @param sem Semaphore to signal upon completion (else NULL)
3388 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003389 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003390 */
3391extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3392 size_t size, struct k_sem *sem);
3393
Anas Nashif166f5192018-02-25 08:02:36 -06003394/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003395
Allan Stephensc98da842016-11-11 15:45:03 -05003396/**
3397 * @cond INTERNAL_HIDDEN
3398 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003399
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003400struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003401 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003402 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003403 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003404 char *buffer;
3405 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003406 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003407
Anas Nashif2f203c22016-12-18 06:57:45 -05003408 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003409};
3410
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003411#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003412 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003413 { \
3414 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003415 .num_blocks = slab_num_blocks, \
3416 .block_size = slab_block_size, \
3417 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003418 .free_list = NULL, \
3419 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003420 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003421 }
3422
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003423#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3424
3425
Peter Mitsis578f9112016-10-07 13:50:31 -04003426/**
Allan Stephensc98da842016-11-11 15:45:03 -05003427 * INTERNAL_HIDDEN @endcond
3428 */
3429
3430/**
3431 * @defgroup mem_slab_apis Memory Slab APIs
3432 * @ingroup kernel_apis
3433 * @{
3434 */
3435
3436/**
Allan Stephensda827222016-11-09 14:23:58 -06003437 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003438 *
Allan Stephensda827222016-11-09 14:23:58 -06003439 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003440 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003441 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3442 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003443 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003444 *
Allan Stephensda827222016-11-09 14:23:58 -06003445 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003446 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003447 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003448 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003449 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003450 * @param name Name of the memory slab.
3451 * @param slab_block_size Size of each memory block (in bytes).
3452 * @param slab_num_blocks Number memory blocks.
3453 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003454 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003455#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3456 char __noinit __aligned(slab_align) \
3457 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3458 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003459 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003460 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003461 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003462
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003463/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003464 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003465 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003466 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003467 *
Allan Stephensda827222016-11-09 14:23:58 -06003468 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3469 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3470 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3471 * To ensure that each memory block is similarly aligned to this boundary,
3472 * @a slab_block_size must also be a multiple of N.
3473 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003474 * @param slab Address of the memory slab.
3475 * @param buffer Pointer to buffer used for the memory blocks.
3476 * @param block_size Size of each memory block (in bytes).
3477 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003478 *
3479 * @return N/A
3480 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003481extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003482 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003483
3484/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003485 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003486 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003487 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003488 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003489 * @param slab Address of the memory slab.
3490 * @param mem Pointer to block address area.
3491 * @param timeout Maximum time to wait for operation to complete
3492 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3493 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003494 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003495 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003496 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003497 * @retval -ENOMEM Returned without waiting.
3498 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003499 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003500extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003501 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003502
3503/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003504 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003505 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003506 * This routine releases a previously allocated memory block back to its
3507 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003508 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003509 * @param slab Address of the memory slab.
3510 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003511 *
3512 * @return N/A
3513 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003514extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003515
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003516/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003517 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003518 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003519 * This routine gets the number of memory blocks that are currently
3520 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003521 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003522 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003523 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003524 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003525 */
Kumar Galacc334c72017-04-21 10:55:34 -05003526static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003527{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003528 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003529}
3530
Peter Mitsisc001aa82016-10-13 13:53:37 -04003531/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003532 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003533 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003534 * This routine gets the number of memory blocks that are currently
3535 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003536 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003537 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003538 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003539 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003540 */
Kumar Galacc334c72017-04-21 10:55:34 -05003541static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003542{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003543 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003544}
3545
Anas Nashif166f5192018-02-25 08:02:36 -06003546/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003547
3548/**
3549 * @cond INTERNAL_HIDDEN
3550 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003551
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003552struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003553 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003554 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003555};
3556
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003557/**
Allan Stephensc98da842016-11-11 15:45:03 -05003558 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003559 */
3560
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003561/**
Allan Stephensc98da842016-11-11 15:45:03 -05003562 * @addtogroup mem_pool_apis
3563 * @{
3564 */
3565
3566/**
3567 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003568 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003569 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3570 * long. The memory pool allows blocks to be repeatedly partitioned into
3571 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003572 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003573 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003574 * If the pool is to be accessed outside the module where it is defined, it
3575 * can be declared via
3576 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003577 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003578 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003579 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003580 * @param minsz Size of the smallest blocks in the pool (in bytes).
3581 * @param maxsz Size of the largest blocks in the pool (in bytes).
3582 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003583 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003584 */
Andy Ross73cb9582017-05-09 10:42:39 -07003585#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3586 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3587 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08003588 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07003589 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08003590 .base = { \
3591 .buf = _mpool_buf_##name, \
3592 .max_sz = maxsz, \
3593 .n_max = nmax, \
3594 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3595 .levels = _mpool_lvls_##name, \
3596 .flags = SYS_MEM_POOL_KERNEL \
3597 } \
Andy Ross73cb9582017-05-09 10:42:39 -07003598 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003599
Peter Mitsis937042c2016-10-13 13:18:26 -04003600/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003601 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003602 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003603 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003604 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003605 * @param pool Address of the memory pool.
3606 * @param block Pointer to block descriptor for the allocated memory.
3607 * @param size Amount of memory to allocate (in bytes).
3608 * @param timeout Maximum time to wait for operation to complete
3609 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3610 * or K_FOREVER to wait as long as necessary.
3611 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003612 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003613 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003614 * @retval -ENOMEM Returned without waiting.
3615 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003616 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003617extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003618 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003619
3620/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003621 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003622 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003623 * This routine releases a previously allocated memory block back to its
3624 * memory pool.
3625 *
3626 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003627 *
3628 * @return N/A
3629 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003630extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003631
3632/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003633 * @brief Free memory allocated from a memory pool.
3634 *
3635 * This routine releases a previously allocated memory block back to its
3636 * memory pool
3637 *
3638 * @param id Memory block identifier.
3639 *
3640 * @return N/A
3641 */
3642extern void k_mem_pool_free_id(struct k_mem_block_id *id);
3643
3644/**
Anas Nashif166f5192018-02-25 08:02:36 -06003645 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003646 */
3647
3648/**
3649 * @defgroup heap_apis Heap Memory Pool APIs
3650 * @ingroup kernel_apis
3651 * @{
3652 */
3653
3654/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003655 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003656 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003657 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003658 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003659 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003660 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003661 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003662 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003663 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003664extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003665
3666/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003667 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003668 *
3669 * This routine provides traditional free() semantics. The memory being
3670 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003671 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003672 * If @a ptr is NULL, no operation is performed.
3673 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003674 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003675 *
3676 * @return N/A
3677 */
3678extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003679
Allan Stephensc98da842016-11-11 15:45:03 -05003680/**
Andrew Boie7f95e832017-11-08 14:40:01 -08003681 * @brief Allocate memory from heap, array style
3682 *
3683 * This routine provides traditional calloc() semantics. Memory is
3684 * allocated from the heap memory pool and zeroed.
3685 *
3686 * @param nmemb Number of elements in the requested array
3687 * @param size Size of each array element (in bytes).
3688 *
3689 * @return Address of the allocated memory if successful; otherwise NULL.
3690 */
3691extern void *k_calloc(size_t nmemb, size_t size);
3692
Anas Nashif166f5192018-02-25 08:02:36 -06003693/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003694
Benjamin Walshacc68c12017-01-29 18:57:45 -05003695/* polling API - PRIVATE */
3696
Benjamin Walshb0179862017-02-02 16:39:57 -05003697#ifdef CONFIG_POLL
3698#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3699#else
3700#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3701#endif
3702
Benjamin Walshacc68c12017-01-29 18:57:45 -05003703/* private - implementation data created as needed, per-type */
3704struct _poller {
3705 struct k_thread *thread;
3706};
3707
3708/* private - types bit positions */
3709enum _poll_types_bits {
3710 /* can be used to ignore an event */
3711 _POLL_TYPE_IGNORE,
3712
3713 /* to be signaled by k_poll_signal() */
3714 _POLL_TYPE_SIGNAL,
3715
3716 /* semaphore availability */
3717 _POLL_TYPE_SEM_AVAILABLE,
3718
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003719 /* queue/fifo/lifo data availability */
3720 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003721
3722 _POLL_NUM_TYPES
3723};
3724
3725#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3726
3727/* private - states bit positions */
3728enum _poll_states_bits {
3729 /* default state when creating event */
3730 _POLL_STATE_NOT_READY,
3731
Benjamin Walshacc68c12017-01-29 18:57:45 -05003732 /* signaled by k_poll_signal() */
3733 _POLL_STATE_SIGNALED,
3734
3735 /* semaphore is available */
3736 _POLL_STATE_SEM_AVAILABLE,
3737
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003738 /* data is available to read on queue/fifo/lifo */
3739 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003740
3741 _POLL_NUM_STATES
3742};
3743
3744#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3745
3746#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003747 (32 - (0 \
3748 + 8 /* tag */ \
3749 + _POLL_NUM_TYPES \
3750 + _POLL_NUM_STATES \
3751 + 1 /* modes */ \
3752 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003753
Benjamin Walshacc68c12017-01-29 18:57:45 -05003754/* end of polling API - PRIVATE */
3755
3756
3757/**
3758 * @defgroup poll_apis Async polling APIs
3759 * @ingroup kernel_apis
3760 * @{
3761 */
3762
3763/* Public polling API */
3764
3765/* public - values for k_poll_event.type bitfield */
3766#define K_POLL_TYPE_IGNORE 0
3767#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3768#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003769#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3770#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003771
3772/* public - polling modes */
3773enum k_poll_modes {
3774 /* polling thread does not take ownership of objects when available */
3775 K_POLL_MODE_NOTIFY_ONLY = 0,
3776
3777 K_POLL_NUM_MODES
3778};
3779
3780/* public - values for k_poll_event.state bitfield */
3781#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003782#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3783#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003784#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3785#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003786
3787/* public - poll signal object */
3788struct k_poll_signal {
3789 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003790 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003791
3792 /*
3793 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3794 * user resets it to 0.
3795 */
3796 unsigned int signaled;
3797
3798 /* custom result value passed to k_poll_signal() if needed */
3799 int result;
3800};
3801
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003802#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003803 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003804 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003805 .signaled = 0, \
3806 .result = 0, \
3807 }
3808
3809struct k_poll_event {
3810 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003811 sys_dnode_t _node;
3812
3813 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003814 struct _poller *poller;
3815
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003816 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003817 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003818
Benjamin Walshacc68c12017-01-29 18:57:45 -05003819 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003820 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003821
3822 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003823 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003824
3825 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003826 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003827
3828 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003829 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003830
3831 /* per-type data */
3832 union {
3833 void *obj;
3834 struct k_poll_signal *signal;
3835 struct k_sem *sem;
3836 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003837 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003838 };
3839};
3840
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003841#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003842 { \
3843 .poller = NULL, \
3844 .type = event_type, \
3845 .state = K_POLL_STATE_NOT_READY, \
3846 .mode = event_mode, \
3847 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003848 { .obj = event_obj }, \
3849 }
3850
3851#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3852 event_tag) \
3853 { \
3854 .type = event_type, \
3855 .tag = event_tag, \
3856 .state = K_POLL_STATE_NOT_READY, \
3857 .mode = event_mode, \
3858 .unused = 0, \
3859 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003860 }
3861
3862/**
3863 * @brief Initialize one struct k_poll_event instance
3864 *
3865 * After this routine is called on a poll event, the event it ready to be
3866 * placed in an event array to be passed to k_poll().
3867 *
3868 * @param event The event to initialize.
3869 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3870 * values. Only values that apply to the same object being polled
3871 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3872 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003873 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003874 * @param obj Kernel object or poll signal.
3875 *
3876 * @return N/A
3877 */
3878
Kumar Galacc334c72017-04-21 10:55:34 -05003879extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003880 int mode, void *obj);
3881
3882/**
3883 * @brief Wait for one or many of multiple poll events to occur
3884 *
3885 * This routine allows a thread to wait concurrently for one or many of
3886 * multiple poll events to have occurred. Such events can be a kernel object
3887 * being available, like a semaphore, or a poll signal event.
3888 *
3889 * When an event notifies that a kernel object is available, the kernel object
3890 * is not "given" to the thread calling k_poll(): it merely signals the fact
3891 * that the object was available when the k_poll() call was in effect. Also,
3892 * all threads trying to acquire an object the regular way, i.e. by pending on
3893 * the object, have precedence over the thread polling on the object. This
3894 * means that the polling thread will never get the poll event on an object
3895 * until the object becomes available and its pend queue is empty. For this
3896 * reason, the k_poll() call is more effective when the objects being polled
3897 * only have one thread, the polling thread, trying to acquire them.
3898 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003899 * When k_poll() returns 0, the caller should loop on all the events that were
3900 * passed to k_poll() and check the state field for the values that were
3901 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003902 *
3903 * Before being reused for another call to k_poll(), the user has to reset the
3904 * state field to K_POLL_STATE_NOT_READY.
3905 *
3906 * @param events An array of pointers to events to be polled for.
3907 * @param num_events The number of events in the array.
3908 * @param timeout Waiting period for an event to be ready (in milliseconds),
3909 * or one of the special values K_NO_WAIT and K_FOREVER.
3910 *
3911 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003912 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02003913 * @retval -EINTR Poller thread has been interrupted.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003914 */
3915
3916extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003917 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003918
3919/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003920 * @brief Initialize a poll signal object.
3921 *
3922 * Ready a poll signal object to be signaled via k_poll_signal().
3923 *
3924 * @param signal A poll signal.
3925 *
3926 * @return N/A
3927 */
3928
3929extern void k_poll_signal_init(struct k_poll_signal *signal);
3930
3931/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003932 * @brief Signal a poll signal object.
3933 *
3934 * This routine makes ready a poll signal, which is basically a poll event of
3935 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3936 * made ready to run. A @a result value can be specified.
3937 *
3938 * The poll signal contains a 'signaled' field that, when set by
3939 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3940 * be reset by the user before being passed again to k_poll() or k_poll() will
3941 * consider it being signaled, and will return immediately.
3942 *
3943 * @param signal A poll signal.
3944 * @param result The value to store in the result field of the signal.
3945 *
3946 * @retval 0 The signal was delivered successfully.
3947 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3948 */
3949
3950extern int k_poll_signal(struct k_poll_signal *signal, int result);
3951
Anas Nashif954d5502018-02-25 08:37:28 -06003952/**
3953 * @internal
3954 */
Andy Ross8606fab2018-03-26 10:54:40 -07003955extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003956
Anas Nashif166f5192018-02-25 08:02:36 -06003957/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003958
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003959/**
3960 * @brief Make the CPU idle.
3961 *
3962 * This function makes the CPU idle until an event wakes it up.
3963 *
3964 * In a regular system, the idle thread should be the only thread responsible
3965 * for making the CPU idle and triggering any type of power management.
3966 * However, in some more constrained systems, such as a single-threaded system,
3967 * the only thread would be responsible for this if needed.
3968 *
3969 * @return N/A
3970 */
3971extern void k_cpu_idle(void);
3972
3973/**
3974 * @brief Make the CPU idle in an atomic fashion.
3975 *
3976 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3977 * must be done atomically before making the CPU idle.
3978 *
3979 * @param key Interrupt locking key obtained from irq_lock().
3980 *
3981 * @return N/A
3982 */
3983extern void k_cpu_atomic_idle(unsigned int key);
3984
Anas Nashif954d5502018-02-25 08:37:28 -06003985
3986/**
3987 * @internal
3988 */
Kumar Galacc334c72017-04-21 10:55:34 -05003989extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003990
Andrew Boiecdb94d62017-04-18 15:22:05 -07003991#ifdef _ARCH_EXCEPT
3992/* This archtecture has direct support for triggering a CPU exception */
3993#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3994#else
3995
Andrew Boiecdb94d62017-04-18 15:22:05 -07003996/* NOTE: This is the implementation for arches that do not implement
3997 * _ARCH_EXCEPT() to generate a real CPU exception.
3998 *
3999 * We won't have a real exception frame to determine the PC value when
4000 * the oops occurred, so print file and line number before we jump into
4001 * the fatal error handler.
4002 */
4003#define _k_except_reason(reason) do { \
4004 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4005 _NanoFatalErrorHandler(reason, &_default_esf); \
4006 CODE_UNREACHABLE; \
4007 } while (0)
4008
4009#endif /* _ARCH__EXCEPT */
4010
4011/**
4012 * @brief Fatally terminate a thread
4013 *
4014 * This should be called when a thread has encountered an unrecoverable
4015 * runtime condition and needs to terminate. What this ultimately
4016 * means is determined by the _fatal_error_handler() implementation, which
4017 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4018 *
4019 * If this is called from ISR context, the default system fatal error handler
4020 * will treat it as an unrecoverable system error, just like k_panic().
4021 */
4022#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4023
4024/**
4025 * @brief Fatally terminate the system
4026 *
4027 * This should be called when the Zephyr kernel has encountered an
4028 * unrecoverable runtime condition and needs to terminate. What this ultimately
4029 * means is determined by the _fatal_error_handler() implementation, which
4030 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4031 */
4032#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4033
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004034/*
4035 * private APIs that are utilized by one or more public APIs
4036 */
4037
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004038#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004039/**
4040 * @internal
4041 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004042extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004043#else
Anas Nashif954d5502018-02-25 08:37:28 -06004044/**
4045 * @internal
4046 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004047#define _init_static_threads() do { } while ((0))
4048#endif
4049
Anas Nashif954d5502018-02-25 08:37:28 -06004050/**
4051 * @internal
4052 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004053extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004054/**
4055 * @internal
4056 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004057extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004058
Andrew Boiedc5d9352017-06-02 12:56:47 -07004059/* arch/cpu.h may declare an architecture or platform-specific macro
4060 * for properly declaring stacks, compatible with MMU/MPU constraints if
4061 * enabled
4062 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004063
4064/**
4065 * @brief Obtain an extern reference to a stack
4066 *
4067 * This macro properly brings the symbol of a thread stack declared
4068 * elsewhere into scope.
4069 *
4070 * @param sym Thread stack symbol name
4071 */
4072#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4073
Andrew Boiedc5d9352017-06-02 12:56:47 -07004074#ifdef _ARCH_THREAD_STACK_DEFINE
4075#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4076#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4077 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4078#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4079#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004080static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004081{
4082 return _ARCH_THREAD_STACK_BUFFER(sym);
4083}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004084#else
4085/**
4086 * @brief Declare a toplevel thread stack memory region
4087 *
4088 * This declares a region of memory suitable for use as a thread's stack.
4089 *
4090 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4091 * 'noinit' section so that it isn't zeroed at boot
4092 *
Andrew Boie507852a2017-07-25 18:47:07 -07004093 * The declared symbol will always be a k_thread_stack_t which can be passed to
4094 * k_thread_create, but should otherwise not be manipulated. If the buffer
4095 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004096 *
4097 * It is legal to precede this definition with the 'static' keyword.
4098 *
4099 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4100 * parameter of k_thread_create(), it may not be the same as the
4101 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4102 *
4103 * @param sym Thread stack symbol name
4104 * @param size Size of the stack memory region
4105 */
4106#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004107 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004108
4109/**
4110 * @brief Declare a toplevel array of thread stack memory regions
4111 *
4112 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4113 * definition for additional details and constraints.
4114 *
4115 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4116 * 'noinit' section so that it isn't zeroed at boot
4117 *
4118 * @param sym Thread stack symbol name
4119 * @param nmemb Number of stacks to declare
4120 * @param size Size of the stack memory region
4121 */
4122
4123#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004124 struct _k_thread_stack_element __noinit \
4125 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004126
4127/**
4128 * @brief Declare an embedded stack memory region
4129 *
4130 * Used for stacks embedded within other data structures. Use is highly
4131 * discouraged but in some cases necessary. For memory protection scenarios,
4132 * it is very important that any RAM preceding this member not be writable
4133 * by threads else a stack overflow will lead to silent corruption. In other
4134 * words, the containing data structure should live in RAM owned by the kernel.
4135 *
4136 * @param sym Thread stack symbol name
4137 * @param size Size of the stack memory region
4138 */
4139#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004140 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004141
4142/**
4143 * @brief Return the size in bytes of a stack memory region
4144 *
4145 * Convenience macro for passing the desired stack size to k_thread_create()
4146 * since the underlying implementation may actually create something larger
4147 * (for instance a guard area).
4148 *
4149 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004150 * passed to K_THREAD_STACK_DEFINE.
4151 *
4152 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4153 * it is not guaranteed to return the original value since each array
4154 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004155 *
4156 * @param sym Stack memory symbol
4157 * @return Size of the stack
4158 */
4159#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4160
4161/**
4162 * @brief Get a pointer to the physical stack buffer
4163 *
4164 * Convenience macro to get at the real underlying stack buffer used by
4165 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4166 * This is really only intended for diagnostic tools which want to examine
4167 * stack memory contents.
4168 *
4169 * @param sym Declared stack symbol name
4170 * @return The buffer itself, a char *
4171 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004172static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004173{
4174 return (char *)sym;
4175}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004176
4177#endif /* _ARCH_DECLARE_STACK */
4178
Chunlin Hane9c97022017-07-07 20:29:30 +08004179/**
4180 * @defgroup mem_domain_apis Memory domain APIs
4181 * @ingroup kernel_apis
4182 * @{
4183 */
4184
4185/** @def MEM_PARTITION_ENTRY
4186 * @brief Used to declare a memory partition entry
4187 */
4188#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4189 {\
4190 .start = _start, \
4191 .size = _size, \
4192 .attr = _attr, \
4193 }
4194
4195/** @def K_MEM_PARTITION_DEFINE
4196 * @brief Used to declare a memory partition
4197 */
4198#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4199#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4200 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304201 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004202 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4203#else
4204#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304205 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004206 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4207#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4208
Chunlin Hane9c97022017-07-07 20:29:30 +08004209/* memory partition */
4210struct k_mem_partition {
4211 /* start address of memory partition */
4212 u32_t start;
4213 /* size of memory partition */
4214 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304215#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004216 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304217 k_mem_partition_attr_t attr;
4218#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004219};
4220
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304221/* memory domian
4222 * Note: Always declare this structure with __kernel prefix
4223 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004224struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304225#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004226 /* partitions in the domain */
4227 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304228#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004229 /* domain q */
4230 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004231 /* number of partitions in the domain */
4232 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004233};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304234
Chunlin Hane9c97022017-07-07 20:29:30 +08004235
4236/**
4237 * @brief Initialize a memory domain.
4238 *
4239 * Initialize a memory domain with given name and memory partitions.
4240 *
4241 * @param domain The memory domain to be initialized.
4242 * @param num_parts The number of array items of "parts" parameter.
4243 * @param parts An array of pointers to the memory partitions. Can be NULL
4244 * if num_parts is zero.
4245 */
4246
Leandro Pereira08de6582018-02-28 14:22:57 -08004247extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004248 struct k_mem_partition *parts[]);
4249/**
4250 * @brief Destroy a memory domain.
4251 *
4252 * Destroy a memory domain.
4253 *
4254 * @param domain The memory domain to be destroyed.
4255 */
4256
4257extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4258
4259/**
4260 * @brief Add a memory partition into a memory domain.
4261 *
4262 * Add a memory partition into a memory domain.
4263 *
4264 * @param domain The memory domain to be added a memory partition.
4265 * @param part The memory partition to be added
4266 */
4267
4268extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4269 struct k_mem_partition *part);
4270
4271/**
4272 * @brief Remove a memory partition from a memory domain.
4273 *
4274 * Remove a memory partition from a memory domain.
4275 *
4276 * @param domain The memory domain to be removed a memory partition.
4277 * @param part The memory partition to be removed
4278 */
4279
4280extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4281 struct k_mem_partition *part);
4282
4283/**
4284 * @brief Add a thread into a memory domain.
4285 *
4286 * Add a thread into a memory domain.
4287 *
4288 * @param domain The memory domain that the thread is going to be added into.
4289 * @param thread ID of thread going to be added into the memory domain.
4290 */
4291
4292extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4293 k_tid_t thread);
4294
4295/**
4296 * @brief Remove a thread from its memory domain.
4297 *
4298 * Remove a thread from its memory domain.
4299 *
4300 * @param thread ID of thread going to be removed from its memory domain.
4301 */
4302
4303extern void k_mem_domain_remove_thread(k_tid_t thread);
4304
Anas Nashif166f5192018-02-25 08:02:36 -06004305/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004306
Andrew Boie756f9072017-10-10 16:01:49 -07004307/**
4308 * @brief Emit a character buffer to the console device
4309 *
4310 * @param c String of characters to print
4311 * @param n The length of the string
4312 */
4313__syscall void k_str_out(char *c, size_t n);
4314
Andy Rosse7172672018-01-24 15:48:32 -08004315/**
4316 * @brief Start a numbered CPU on a MP-capable system
4317
4318 * This starts and initializes a specific CPU. The main thread on
4319 * startup is running on CPU zero, other processors are numbered
4320 * sequentially. On return from this function, the CPU is known to
4321 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004322 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004323 * with the provided key will work to enable them.
4324 *
4325 * Normally, in SMP mode this function will be called by the kernel
4326 * initialization and should not be used as a user API. But it is
4327 * defined here for special-purpose apps which want Zephyr running on
4328 * one core and to use others for design-specific processing.
4329 *
4330 * @param cpu_num Integer number of the CPU
4331 * @param stack Stack memory for the CPU
4332 * @param sz Stack buffer size, in bytes
4333 * @param fn Function to begin running on the CPU. First argument is
4334 * an irq_unlock() key.
4335 * @param arg Untyped argument to be passed to "fn"
4336 */
4337extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4338 void (*fn)(int, void *), void *arg);
4339
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004340#ifdef __cplusplus
4341}
4342#endif
4343
Andrew Boiee004dec2016-11-07 09:01:19 -08004344#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4345/*
4346 * Define new and delete operators.
4347 * At this moment, the operators do nothing since objects are supposed
4348 * to be statically allocated.
4349 */
4350inline void operator delete(void *ptr)
4351{
4352 (void)ptr;
4353}
4354
4355inline void operator delete[](void *ptr)
4356{
4357 (void)ptr;
4358}
4359
4360inline void *operator new(size_t size)
4361{
4362 (void)size;
4363 return NULL;
4364}
4365
4366inline void *operator new[](size_t size)
4367{
4368 (void)size;
4369 return NULL;
4370}
4371
4372/* Placement versions of operator new and delete */
4373inline void operator delete(void *ptr1, void *ptr2)
4374{
4375 (void)ptr1;
4376 (void)ptr2;
4377}
4378
4379inline void operator delete[](void *ptr1, void *ptr2)
4380{
4381 (void)ptr1;
4382 (void)ptr2;
4383}
4384
4385inline void *operator new(size_t size, void *ptr)
4386{
4387 (void)size;
4388 return ptr;
4389}
4390
4391inline void *operator new[](size_t size, void *ptr)
4392{
4393 (void)size;
4394 return ptr;
4395}
4396
4397#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4398
Andrew Boiefa94ee72017-09-28 16:54:35 -07004399#include <syscalls/kernel.h>
4400
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004401#endif /* !_ASMLANGUAGE */
4402
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004403#endif /* _kernel__h_ */