blob: 92a0ceeb062c6dc375307d6d423d69b9e5ac0bec [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.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400720 */
Andrew Boie468190a2017-09-29 14:00:48 -0700721__syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400722
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400723/**
Allan Stephensc98da842016-11-11 15:45:03 -0500724 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400725 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500726 * This routine permanently stops execution of @a thread. The thread is taken
727 * off all kernel queues it is part of (i.e. the ready queue, the timeout
728 * queue, or a kernel object wait queue). However, any kernel resources the
729 * thread might currently own (such as mutexes or memory blocks) are not
730 * released. It is the responsibility of the caller of this routine to ensure
731 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400732 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500733 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400734 *
735 * @return N/A
736 */
Andrew Boie468190a2017-09-29 14:00:48 -0700737__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400738
Andrew Boie7d627c52017-08-30 11:01:56 -0700739
740/**
741 * @brief Start an inactive thread
742 *
743 * If a thread was created with K_FOREVER in the delay parameter, it will
744 * not be added to the scheduling queue until this function is called
745 * on it.
746 *
747 * @param thread thread to start
748 */
Andrew Boie468190a2017-09-29 14:00:48 -0700749__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700750
Allan Stephensc98da842016-11-11 15:45:03 -0500751/**
752 * @cond INTERNAL_HIDDEN
753 */
754
Benjamin Walshd211a522016-12-06 11:44:01 -0500755/* timeout has timed out and is not on _timeout_q anymore */
756#define _EXPIRED (-2)
757
758/* timeout is not in use */
759#define _INACTIVE (-1)
760
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400761struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700762 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700763 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400764 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700765 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500766 void *init_p1;
767 void *init_p2;
768 void *init_p3;
769 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500770 u32_t init_options;
771 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500772 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400773};
774
Andrew Boied26cf2d2017-03-30 13:07:02 -0700775#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400776 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500777 prio, options, delay, abort, groups) \
778 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700779 .init_thread = (thread), \
780 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500781 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700782 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400783 .init_p1 = (void *)p1, \
784 .init_p2 = (void *)p2, \
785 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500786 .init_prio = (prio), \
787 .init_options = (options), \
788 .init_delay = (delay), \
789 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400790 }
791
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400792/**
Allan Stephensc98da842016-11-11 15:45:03 -0500793 * INTERNAL_HIDDEN @endcond
794 */
795
796/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500797 * @brief Statically define and initialize a thread.
798 *
799 * The thread may be scheduled for immediate execution or a delayed start.
800 *
801 * Thread options are architecture-specific, and can include K_ESSENTIAL,
802 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
803 * them using "|" (the logical OR operator).
804 *
805 * The ID of the thread can be accessed using:
806 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500807 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500808 *
809 * @param name Name of the thread.
810 * @param stack_size Stack size in bytes.
811 * @param entry Thread entry function.
812 * @param p1 1st entry point parameter.
813 * @param p2 2nd entry point parameter.
814 * @param p3 3rd entry point parameter.
815 * @param prio Thread priority.
816 * @param options Thread options.
817 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400818 *
819 * @internal It has been observed that the x86 compiler by default aligns
820 * these _static_thread_data structures to 32-byte boundaries, thereby
821 * wasting space. To work around this, force a 4-byte alignment.
822 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500823#define K_THREAD_DEFINE(name, stack_size, \
824 entry, p1, p2, p3, \
825 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700826 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700827 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500828 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500829 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700830 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
831 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500832 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700833 NULL, 0); \
834 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400835
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400836/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500837 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400838 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500839 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400840 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500841 * @param thread ID of thread whose priority is needed.
842 *
843 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400844 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700845__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400846
847/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500848 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400849 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500850 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400851 *
852 * Rescheduling can occur immediately depending on the priority @a thread is
853 * set to:
854 *
855 * - If its priority is raised above the priority of the caller of this
856 * function, and the caller is preemptible, @a thread will be scheduled in.
857 *
858 * - If the caller operates on itself, it lowers its priority below that of
859 * other threads in the system, and the caller is preemptible, the thread of
860 * highest priority will be scheduled in.
861 *
862 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
863 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
864 * highest priority.
865 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500866 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400867 * @param prio New priority.
868 *
869 * @warning Changing the priority of a thread currently involved in mutex
870 * priority inheritance may result in undefined behavior.
871 *
872 * @return N/A
873 */
Andrew Boie468190a2017-09-29 14:00:48 -0700874__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400875
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400876/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500877 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400878 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500879 * This routine prevents the kernel scheduler from making @a thread the
880 * current thread. All other internal operations on @a thread are still
881 * performed; for example, any timeout it is waiting on keeps ticking,
882 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500884 * If @a thread is already suspended, the routine has no effect.
885 *
886 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400887 *
888 * @return N/A
889 */
Andrew Boie468190a2017-09-29 14:00:48 -0700890__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400891
892/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500893 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400894 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500895 * This routine allows the kernel scheduler to make @a thread the current
896 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400897 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500898 * If @a thread is not currently suspended, the routine has no effect.
899 *
900 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400901 *
902 * @return N/A
903 */
Andrew Boie468190a2017-09-29 14:00:48 -0700904__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400905
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400906/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500907 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400908 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500909 * This routine specifies how the scheduler will perform time slicing of
910 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400911 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500912 * To enable time slicing, @a slice must be non-zero. The scheduler
913 * ensures that no thread runs for more than the specified time limit
914 * before other threads of that priority are given a chance to execute.
915 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700916 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400917 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500918 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400919 * execute. Once the scheduler selects a thread for execution, there is no
920 * minimum guaranteed time the thread will execute before threads of greater or
921 * equal priority are scheduled.
922 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500923 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400924 * for execution, this routine has no effect; the thread is immediately
925 * rescheduled after the slice period expires.
926 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500927 * To disable timeslicing, set both @a slice and @a prio to zero.
928 *
929 * @param slice Maximum time slice length (in milliseconds).
930 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400931 *
932 * @return N/A
933 */
Kumar Galacc334c72017-04-21 10:55:34 -0500934extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400935
Anas Nashif166f5192018-02-25 08:02:36 -0600936/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -0500937
938/**
939 * @addtogroup isr_apis
940 * @{
941 */
942
943/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500944 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400945 *
Allan Stephensc98da842016-11-11 15:45:03 -0500946 * This routine allows the caller to customize its actions, depending on
947 * whether it is a thread or an ISR.
948 *
949 * @note Can be called by ISRs.
950 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500951 * @return 0 if invoked by a thread.
952 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400953 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500954extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400955
Benjamin Walsh445830d2016-11-10 15:54:27 -0500956/**
957 * @brief Determine if code is running in a preemptible thread.
958 *
Allan Stephensc98da842016-11-11 15:45:03 -0500959 * This routine allows the caller to customize its actions, depending on
960 * whether it can be preempted by another thread. The routine returns a 'true'
961 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500962 *
Allan Stephensc98da842016-11-11 15:45:03 -0500963 * - The code is running in a thread, not at ISR.
964 * - The thread's priority is in the preemptible range.
965 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500966 *
Allan Stephensc98da842016-11-11 15:45:03 -0500967 * @note Can be called by ISRs.
968 *
969 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500970 * @return Non-zero if invoked by a preemptible thread.
971 */
Andrew Boie468190a2017-09-29 14:00:48 -0700972__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -0500973
Allan Stephensc98da842016-11-11 15:45:03 -0500974/**
Anas Nashif166f5192018-02-25 08:02:36 -0600975 * @}
Allan Stephensc98da842016-11-11 15:45:03 -0500976 */
977
978/**
979 * @addtogroup thread_apis
980 * @{
981 */
982
983/**
984 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500985 *
Allan Stephensc98da842016-11-11 15:45:03 -0500986 * This routine prevents the current thread from being preempted by another
987 * thread by instructing the scheduler to treat it as a cooperative thread.
988 * If the thread subsequently performs an operation that makes it unready,
989 * it will be context switched out in the normal manner. When the thread
990 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500991 *
Allan Stephensc98da842016-11-11 15:45:03 -0500992 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500993 *
Allan Stephensc98da842016-11-11 15:45:03 -0500994 * @note k_sched_lock() and k_sched_unlock() should normally be used
995 * when the operation being performed can be safely interrupted by ISRs.
996 * However, if the amount of processing involved is very small, better
997 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500998 *
999 * @return N/A
1000 */
1001extern void k_sched_lock(void);
1002
Allan Stephensc98da842016-11-11 15:45:03 -05001003/**
1004 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001005 *
Allan Stephensc98da842016-11-11 15:45:03 -05001006 * This routine reverses the effect of a previous call to k_sched_lock().
1007 * A thread must call the routine once for each time it called k_sched_lock()
1008 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001009 *
1010 * @return N/A
1011 */
1012extern void k_sched_unlock(void);
1013
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001014/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001015 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001016 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001017 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001018 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001019 * Custom data is not used by the kernel itself, and is freely available
1020 * for a thread to use as it sees fit. It can be used as a framework
1021 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001022 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001023 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001024 *
1025 * @return N/A
1026 */
Andrew Boie468190a2017-09-29 14:00:48 -07001027__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001028
1029/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001030 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001031 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001032 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001033 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001034 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001035 */
Andrew Boie468190a2017-09-29 14:00:48 -07001036__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001037
1038/**
Anas Nashif166f5192018-02-25 08:02:36 -06001039 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001040 */
1041
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001042#include <sys_clock.h>
1043
Allan Stephensc2f15a42016-11-17 12:24:22 -05001044/**
1045 * @addtogroup clock_apis
1046 * @{
1047 */
1048
1049/**
1050 * @brief Generate null timeout delay.
1051 *
1052 * This macro generates a timeout delay that that instructs a kernel API
1053 * not to wait if the requested operation cannot be performed immediately.
1054 *
1055 * @return Timeout delay value.
1056 */
1057#define K_NO_WAIT 0
1058
1059/**
1060 * @brief Generate timeout delay from milliseconds.
1061 *
1062 * This macro generates a timeout delay that that instructs a kernel API
1063 * to wait up to @a ms milliseconds to perform the requested operation.
1064 *
1065 * @param ms Duration in milliseconds.
1066 *
1067 * @return Timeout delay value.
1068 */
Johan Hedberg14471692016-11-13 10:52:15 +02001069#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001070
1071/**
1072 * @brief Generate timeout delay from seconds.
1073 *
1074 * This macro generates a timeout delay that that instructs a kernel API
1075 * to wait up to @a s seconds to perform the requested operation.
1076 *
1077 * @param s Duration in seconds.
1078 *
1079 * @return Timeout delay value.
1080 */
Johan Hedberg14471692016-11-13 10:52:15 +02001081#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001082
1083/**
1084 * @brief Generate timeout delay from minutes.
1085 *
1086 * This macro generates a timeout delay that that instructs a kernel API
1087 * to wait up to @a m minutes to perform the requested operation.
1088 *
1089 * @param m Duration in minutes.
1090 *
1091 * @return Timeout delay value.
1092 */
Johan Hedberg14471692016-11-13 10:52:15 +02001093#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001094
1095/**
1096 * @brief Generate timeout delay from hours.
1097 *
1098 * This macro generates a timeout delay that that instructs a kernel API
1099 * to wait up to @a h hours to perform the requested operation.
1100 *
1101 * @param h Duration in hours.
1102 *
1103 * @return Timeout delay value.
1104 */
Johan Hedberg14471692016-11-13 10:52:15 +02001105#define K_HOURS(h) K_MINUTES((h) * 60)
1106
Allan Stephensc98da842016-11-11 15:45:03 -05001107/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001108 * @brief Generate infinite timeout delay.
1109 *
1110 * This macro generates a timeout delay that that instructs a kernel API
1111 * to wait as long as necessary to perform the requested operation.
1112 *
1113 * @return Timeout delay value.
1114 */
1115#define K_FOREVER (-1)
1116
1117/**
Anas Nashif166f5192018-02-25 08:02:36 -06001118 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001119 */
1120
1121/**
Allan Stephensc98da842016-11-11 15:45:03 -05001122 * @cond INTERNAL_HIDDEN
1123 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001124
Benjamin Walsh62092182016-12-20 14:39:08 -05001125/* kernel clocks */
1126
1127#if (sys_clock_ticks_per_sec == 1000) || \
1128 (sys_clock_ticks_per_sec == 500) || \
1129 (sys_clock_ticks_per_sec == 250) || \
1130 (sys_clock_ticks_per_sec == 125) || \
1131 (sys_clock_ticks_per_sec == 100) || \
1132 (sys_clock_ticks_per_sec == 50) || \
1133 (sys_clock_ticks_per_sec == 25) || \
1134 (sys_clock_ticks_per_sec == 20) || \
1135 (sys_clock_ticks_per_sec == 10) || \
1136 (sys_clock_ticks_per_sec == 1)
1137
1138 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1139#else
1140 /* yields horrible 64-bit math on many architectures: try to avoid */
1141 #define _NON_OPTIMIZED_TICKS_PER_SEC
1142#endif
1143
1144#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001145extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001146#else
Kumar Galacc334c72017-04-21 10:55:34 -05001147static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001148{
Kumar Galacc334c72017-04-21 10:55:34 -05001149 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001150}
1151#endif
1152
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001153/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001154#ifdef CONFIG_TICKLESS_KERNEL
1155#define _TICK_ALIGN 0
1156#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001157#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001158#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001159
Kumar Galacc334c72017-04-21 10:55:34 -05001160static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001161{
Benjamin Walsh62092182016-12-20 14:39:08 -05001162#ifdef CONFIG_SYS_CLOCK_EXISTS
1163
1164#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001165 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001166#else
Kumar Galacc334c72017-04-21 10:55:34 -05001167 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001168#endif
1169
1170#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001171 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001172 return 0;
1173#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001174}
1175
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001176struct k_timer {
1177 /*
1178 * _timeout structure must be first here if we want to use
1179 * dynamic timer allocation. timeout.node is used in the double-linked
1180 * list of free timers
1181 */
1182 struct _timeout timeout;
1183
Allan Stephens45bfa372016-10-12 12:39:42 -05001184 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001185 _wait_q_t wait_q;
1186
1187 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001188 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001189
1190 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001191 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001192
1193 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001194 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001195
Allan Stephens45bfa372016-10-12 12:39:42 -05001196 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001197 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001198
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001199 /* user-specific data, also used to support legacy features */
1200 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001201
Anas Nashif2f203c22016-12-18 06:57:45 -05001202 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001203};
1204
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001205#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001206 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001207 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001208 .timeout.wait_q = NULL, \
1209 .timeout.thread = NULL, \
1210 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001211 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001212 .expiry_fn = expiry, \
1213 .stop_fn = stop, \
1214 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001215 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001216 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001217 }
1218
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001219#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1220
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001221/**
Allan Stephensc98da842016-11-11 15:45:03 -05001222 * INTERNAL_HIDDEN @endcond
1223 */
1224
1225/**
1226 * @defgroup timer_apis Timer APIs
1227 * @ingroup kernel_apis
1228 * @{
1229 */
1230
1231/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001232 * @typedef k_timer_expiry_t
1233 * @brief Timer expiry function type.
1234 *
1235 * A timer's expiry function is executed by the system clock interrupt handler
1236 * each time the timer expires. The expiry function is optional, and is only
1237 * invoked if the timer has been initialized with one.
1238 *
1239 * @param timer Address of timer.
1240 *
1241 * @return N/A
1242 */
1243typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1244
1245/**
1246 * @typedef k_timer_stop_t
1247 * @brief Timer stop function type.
1248 *
1249 * A timer's stop function is executed if the timer is stopped prematurely.
1250 * The function runs in the context of the thread that stops the timer.
1251 * The stop function is optional, and is only invoked if the timer has been
1252 * initialized with one.
1253 *
1254 * @param timer Address of timer.
1255 *
1256 * @return N/A
1257 */
1258typedef void (*k_timer_stop_t)(struct k_timer *timer);
1259
1260/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001261 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001262 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001263 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001264 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001265 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001266 *
1267 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001268 * @param expiry_fn Function to invoke each time the timer expires.
1269 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001270 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001271#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001272 struct k_timer name \
1273 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001274 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001275
Allan Stephens45bfa372016-10-12 12:39:42 -05001276/**
1277 * @brief Initialize a timer.
1278 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001279 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001280 *
1281 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001282 * @param expiry_fn Function to invoke each time the timer expires.
1283 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001284 *
1285 * @return N/A
1286 */
1287extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001288 k_timer_expiry_t expiry_fn,
1289 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001290
Allan Stephens45bfa372016-10-12 12:39:42 -05001291/**
1292 * @brief Start a timer.
1293 *
1294 * This routine starts a timer, and resets its status to zero. The timer
1295 * begins counting down using the specified duration and period values.
1296 *
1297 * Attempting to start a timer that is already running is permitted.
1298 * The timer's status is reset to zero and the timer begins counting down
1299 * using the new duration and period values.
1300 *
1301 * @param timer Address of timer.
1302 * @param duration Initial timer duration (in milliseconds).
1303 * @param period Timer period (in milliseconds).
1304 *
1305 * @return N/A
1306 */
Andrew Boiea354d492017-09-29 16:22:28 -07001307__syscall void k_timer_start(struct k_timer *timer,
1308 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001309
1310/**
1311 * @brief Stop a timer.
1312 *
1313 * This routine stops a running timer prematurely. The timer's stop function,
1314 * if one exists, is invoked by the caller.
1315 *
1316 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001317 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001318 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001319 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1320 * if @a k_timer_stop is to be called from ISRs.
1321 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001322 * @param timer Address of timer.
1323 *
1324 * @return N/A
1325 */
Andrew Boiea354d492017-09-29 16:22:28 -07001326__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001327
1328/**
1329 * @brief Read timer status.
1330 *
1331 * This routine reads the timer's status, which indicates the number of times
1332 * it has expired since its status was last read.
1333 *
1334 * Calling this routine resets the timer's status to zero.
1335 *
1336 * @param timer Address of timer.
1337 *
1338 * @return Timer status.
1339 */
Andrew Boiea354d492017-09-29 16:22:28 -07001340__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001341
1342/**
1343 * @brief Synchronize thread to timer expiration.
1344 *
1345 * This routine blocks the calling thread until the timer's status is non-zero
1346 * (indicating that it has expired at least once since it was last examined)
1347 * or the timer is stopped. If the timer status is already non-zero,
1348 * or the timer is already stopped, the caller continues without waiting.
1349 *
1350 * Calling this routine resets the timer's status to zero.
1351 *
1352 * This routine must not be used by interrupt handlers, since they are not
1353 * allowed to block.
1354 *
1355 * @param timer Address of timer.
1356 *
1357 * @return Timer status.
1358 */
Andrew Boiea354d492017-09-29 16:22:28 -07001359__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001360
1361/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001362 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001363 *
1364 * This routine computes the (approximate) time remaining before a running
1365 * timer next expires. If the timer is not running, it returns zero.
1366 *
1367 * @param timer Address of timer.
1368 *
1369 * @return Remaining time (in milliseconds).
1370 */
Andrew Boiea354d492017-09-29 16:22:28 -07001371__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1372
1373static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001374{
1375 return _timeout_remaining_get(&timer->timeout);
1376}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001377
Allan Stephensc98da842016-11-11 15:45:03 -05001378/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001379 * @brief Associate user-specific data with a timer.
1380 *
1381 * This routine records the @a user_data with the @a timer, to be retrieved
1382 * later.
1383 *
1384 * It can be used e.g. in a timer handler shared across multiple subsystems to
1385 * retrieve data specific to the subsystem this timer is associated with.
1386 *
1387 * @param timer Address of timer.
1388 * @param user_data User data to associate with the timer.
1389 *
1390 * @return N/A
1391 */
Andrew Boiea354d492017-09-29 16:22:28 -07001392__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1393
Anas Nashif954d5502018-02-25 08:37:28 -06001394/**
1395 * @internal
1396 */
Andrew Boiea354d492017-09-29 16:22:28 -07001397static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1398 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001399{
1400 timer->user_data = user_data;
1401}
1402
1403/**
1404 * @brief Retrieve the user-specific data from a timer.
1405 *
1406 * @param timer Address of timer.
1407 *
1408 * @return The user data.
1409 */
Andrew Boiea354d492017-09-29 16:22:28 -07001410__syscall void *k_timer_user_data_get(struct k_timer *timer);
1411
1412static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001413{
1414 return timer->user_data;
1415}
1416
Anas Nashif166f5192018-02-25 08:02:36 -06001417/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001418
Allan Stephensc98da842016-11-11 15:45:03 -05001419/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001420 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001421 * @{
1422 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001423
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001424/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001425 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001426 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001427 * This routine returns the elapsed time since the system booted,
1428 * in milliseconds.
1429 *
1430 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001431 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001432__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001433
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001434/**
1435 * @brief Enable clock always on in tickless kernel
1436 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001437 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001438 * there are no timer events programmed in tickless kernel
1439 * scheduling. This is necessary if the clock is used to track
1440 * passage of time.
1441 *
1442 * @retval prev_status Previous status of always on flag
1443 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301444#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001445static inline int k_enable_sys_clock_always_on(void)
1446{
1447 int prev_status = _sys_clock_always_on;
1448
1449 _sys_clock_always_on = 1;
1450 _enable_sys_clock();
1451
1452 return prev_status;
1453}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301454#else
1455#define k_enable_sys_clock_always_on() do { } while ((0))
1456#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001457
1458/**
1459 * @brief Disable clock always on in tickless kernel
1460 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001461 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001462 * there are no timer events programmed in tickless kernel
1463 * scheduling. To save power, this routine should be called
1464 * immediately when clock is not used to track time.
1465 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301466#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001467static inline void k_disable_sys_clock_always_on(void)
1468{
1469 _sys_clock_always_on = 0;
1470}
1471#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001472#define k_disable_sys_clock_always_on() do { } while ((0))
1473#endif
1474
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001475/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001476 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001477 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001478 * This routine returns the lower 32-bits of the elapsed time since the system
1479 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001480 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001481 * This routine can be more efficient than k_uptime_get(), as it reduces the
1482 * need for interrupt locking and 64-bit math. However, the 32-bit result
1483 * cannot hold a system uptime time larger than approximately 50 days, so the
1484 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001485 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001486 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001487 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001488__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001489
1490/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001491 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001492 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001493 * This routine computes the elapsed time between the current system uptime
1494 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001495 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001496 * @param reftime Pointer to a reference time, which is updated to the current
1497 * uptime upon return.
1498 *
1499 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001500 */
Kumar Galacc334c72017-04-21 10:55:34 -05001501extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001502
1503/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001504 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001505 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001506 * This routine computes the elapsed time between the current system uptime
1507 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001508 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001509 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1510 * need for interrupt locking and 64-bit math. However, the 32-bit result
1511 * cannot hold an elapsed time larger than approximately 50 days, so the
1512 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001513 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001514 * @param reftime Pointer to a reference time, which is updated to the current
1515 * uptime upon return.
1516 *
1517 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001518 */
Kumar Galacc334c72017-04-21 10:55:34 -05001519extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001520
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001521/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001522 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001523 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001524 * This routine returns the current time, as measured by the system's hardware
1525 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001526 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001527 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001528 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001529#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001530
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001531/**
Anas Nashif166f5192018-02-25 08:02:36 -06001532 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001533 */
1534
Allan Stephensc98da842016-11-11 15:45:03 -05001535/**
1536 * @cond INTERNAL_HIDDEN
1537 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001538
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001539struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001540 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001541 union {
1542 _wait_q_t wait_q;
1543
1544 _POLL_EVENT;
1545 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001546
1547 _OBJECT_TRACING_NEXT_PTR(k_queue);
1548};
1549
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001550#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001551 { \
1552 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1553 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001554 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001555 _OBJECT_TRACING_INIT \
1556 }
1557
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001558#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1559
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001560/**
1561 * INTERNAL_HIDDEN @endcond
1562 */
1563
1564/**
1565 * @defgroup queue_apis Queue APIs
1566 * @ingroup kernel_apis
1567 * @{
1568 */
1569
1570/**
1571 * @brief Initialize a queue.
1572 *
1573 * This routine initializes a queue object, prior to its first use.
1574 *
1575 * @param queue Address of the queue.
1576 *
1577 * @return N/A
1578 */
1579extern void k_queue_init(struct k_queue *queue);
1580
1581/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001582 * @brief Cancel waiting on a queue.
1583 *
1584 * This routine causes first thread pending on @a queue, if any, to
1585 * return from k_queue_get() call with NULL value (as if timeout expired).
1586 *
1587 * @note Can be called by ISRs.
1588 *
1589 * @param queue Address of the queue.
1590 *
1591 * @return N/A
1592 */
1593extern void k_queue_cancel_wait(struct k_queue *queue);
1594
1595/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001596 * @brief Append an element to the end of a queue.
1597 *
1598 * This routine appends a data item to @a queue. A queue data item must be
1599 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1600 * reserved for the kernel's use.
1601 *
1602 * @note Can be called by ISRs.
1603 *
1604 * @param queue Address of the queue.
1605 * @param data Address of the data item.
1606 *
1607 * @return N/A
1608 */
1609extern void k_queue_append(struct k_queue *queue, void *data);
1610
1611/**
1612 * @brief Prepend an element to a queue.
1613 *
1614 * This routine prepends a data item to @a queue. A queue data item must be
1615 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1616 * reserved for the kernel's use.
1617 *
1618 * @note Can be called by ISRs.
1619 *
1620 * @param queue Address of the queue.
1621 * @param data Address of the data item.
1622 *
1623 * @return N/A
1624 */
1625extern void k_queue_prepend(struct k_queue *queue, void *data);
1626
1627/**
1628 * @brief Inserts an element to a queue.
1629 *
1630 * This routine inserts a data item to @a queue after previous item. A queue
1631 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1632 * item are reserved for the kernel's use.
1633 *
1634 * @note Can be called by ISRs.
1635 *
1636 * @param queue Address of the queue.
1637 * @param prev Address of the previous data item.
1638 * @param data Address of the data item.
1639 *
1640 * @return N/A
1641 */
1642extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1643
1644/**
1645 * @brief Atomically append a list of elements to a queue.
1646 *
1647 * This routine adds a list of data items to @a queue in one operation.
1648 * The data items must be in a singly-linked list, with the first 32 bits
1649 * in each data item pointing to the next data item; the list must be
1650 * NULL-terminated.
1651 *
1652 * @note Can be called by ISRs.
1653 *
1654 * @param queue Address of the queue.
1655 * @param head Pointer to first node in singly-linked list.
1656 * @param tail Pointer to last node in singly-linked list.
1657 *
1658 * @return N/A
1659 */
1660extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1661
1662/**
1663 * @brief Atomically add a list of elements to a queue.
1664 *
1665 * This routine adds a list of data items to @a queue in one operation.
1666 * The data items must be in a singly-linked list implemented using a
1667 * sys_slist_t object. Upon completion, the original list is empty.
1668 *
1669 * @note Can be called by ISRs.
1670 *
1671 * @param queue Address of the queue.
1672 * @param list Pointer to sys_slist_t object.
1673 *
1674 * @return N/A
1675 */
1676extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1677
1678/**
1679 * @brief Get an element from a queue.
1680 *
1681 * This routine removes first data item from @a queue. The first 32 bits of the
1682 * data item are reserved for the kernel's use.
1683 *
1684 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1685 *
1686 * @param queue Address of the queue.
1687 * @param timeout Waiting period to obtain a data item (in milliseconds),
1688 * or one of the special values K_NO_WAIT and K_FOREVER.
1689 *
1690 * @return Address of the data item if successful; NULL if returned
1691 * without waiting, or waiting period timed out.
1692 */
Kumar Galacc334c72017-04-21 10:55:34 -05001693extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001694
1695/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001696 * @brief Remove an element from a queue.
1697 *
1698 * This routine removes data item from @a queue. The first 32 bits of the
1699 * data item are reserved for the kernel's use. Removing elements from k_queue
1700 * rely on sys_slist_find_and_remove which is not a constant time operation.
1701 *
1702 * @note Can be called by ISRs
1703 *
1704 * @param queue Address of the queue.
1705 * @param data Address of the data item.
1706 *
1707 * @return true if data item was removed
1708 */
1709static inline bool k_queue_remove(struct k_queue *queue, void *data)
1710{
1711 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1712}
1713
1714/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001715 * @brief Query a queue to see if it has data available.
1716 *
1717 * Note that the data might be already gone by the time this function returns
1718 * if other threads are also trying to read from the queue.
1719 *
1720 * @note Can be called by ISRs.
1721 *
1722 * @param queue Address of the queue.
1723 *
1724 * @return Non-zero if the queue is empty.
1725 * @return 0 if data is available.
1726 */
1727static inline int k_queue_is_empty(struct k_queue *queue)
1728{
1729 return (int)sys_slist_is_empty(&queue->data_q);
1730}
1731
1732/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001733 * @brief Peek element at the head of queue.
1734 *
1735 * Return element from the head of queue without removing it.
1736 *
1737 * @param queue Address of the queue.
1738 *
1739 * @return Head element, or NULL if queue is empty.
1740 */
1741static inline void *k_queue_peek_head(struct k_queue *queue)
1742{
1743 return sys_slist_peek_head(&queue->data_q);
1744}
1745
1746/**
1747 * @brief Peek element at the tail of queue.
1748 *
1749 * Return element from the tail of queue without removing it.
1750 *
1751 * @param queue Address of the queue.
1752 *
1753 * @return Tail element, or NULL if queue is empty.
1754 */
1755static inline void *k_queue_peek_tail(struct k_queue *queue)
1756{
1757 return sys_slist_peek_tail(&queue->data_q);
1758}
1759
1760/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001761 * @brief Statically define and initialize a queue.
1762 *
1763 * The queue can be accessed outside the module where it is defined using:
1764 *
1765 * @code extern struct k_queue <name>; @endcode
1766 *
1767 * @param name Name of the queue.
1768 */
1769#define K_QUEUE_DEFINE(name) \
1770 struct k_queue name \
1771 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001772 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001773
Anas Nashif166f5192018-02-25 08:02:36 -06001774/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001775
1776/**
1777 * @cond INTERNAL_HIDDEN
1778 */
1779
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001780struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001781 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001782};
1783
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001784#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001785 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001786 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001787 }
1788
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001789#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1790
Allan Stephensc98da842016-11-11 15:45:03 -05001791/**
1792 * INTERNAL_HIDDEN @endcond
1793 */
1794
1795/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001796 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05001797 * @ingroup kernel_apis
1798 * @{
1799 */
1800
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001801/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001802 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001803 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001804 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001805 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001806 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001807 *
1808 * @return N/A
1809 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001810#define k_fifo_init(fifo) \
1811 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001812
1813/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001814 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001815 *
1816 * This routine causes first thread pending on @a fifo, if any, to
1817 * return from k_fifo_get() call with NULL value (as if timeout
1818 * expired).
1819 *
1820 * @note Can be called by ISRs.
1821 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001822 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001823 *
1824 * @return N/A
1825 */
1826#define k_fifo_cancel_wait(fifo) \
1827 k_queue_cancel_wait((struct k_queue *) fifo)
1828
1829/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001830 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001831 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001832 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001833 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1834 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001835 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001836 * @note Can be called by ISRs.
1837 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001838 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001839 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001840 *
1841 * @return N/A
1842 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001843#define k_fifo_put(fifo, data) \
1844 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001845
1846/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001847 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001848 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001849 * This routine adds a list of data items to @a fifo in one operation.
1850 * The data items must be in a singly-linked list, with the first 32 bits
1851 * each data item pointing to the next data item; the list must be
1852 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001853 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001854 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001855 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001856 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001857 * @param head Pointer to first node in singly-linked list.
1858 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001859 *
1860 * @return N/A
1861 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001862#define k_fifo_put_list(fifo, head, tail) \
1863 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001864
1865/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001866 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001867 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001868 * This routine adds a list of data items to @a fifo in one operation.
1869 * The data items must be in a singly-linked list implemented using a
1870 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001871 * and must be re-initialized via sys_slist_init().
1872 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001873 * @note Can be called by ISRs.
1874 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001875 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001876 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001877 *
1878 * @return N/A
1879 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001880#define k_fifo_put_slist(fifo, list) \
1881 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001882
1883/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001884 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001885 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001886 * This routine removes a data item from @a fifo in a "first in, first out"
1887 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001888 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001889 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1890 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001891 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001892 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001893 * or one of the special values K_NO_WAIT and K_FOREVER.
1894 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001895 * @return Address of the data item if successful; NULL if returned
1896 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001897 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001898#define k_fifo_get(fifo, timeout) \
1899 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001900
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001901/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001902 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001903 *
1904 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06001905 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001906 *
1907 * @note Can be called by ISRs.
1908 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001909 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001910 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001911 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001912 * @return 0 if data is available.
1913 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001914#define k_fifo_is_empty(fifo) \
1915 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001916
1917/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001918 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001919 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001920 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05301921 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001922 * on each iteration of processing, a head container will be peeked,
1923 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06001924 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001925 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001926 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001927 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001928 * @return Head element, or NULL if the FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001929 */
1930#define k_fifo_peek_head(fifo) \
1931 k_queue_peek_head((struct k_queue *) fifo)
1932
1933/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001934 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001935 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001936 * Return element from the tail of FIFO queue (without removing it). A usecase
1937 * for this is if elements of the FIFO queue are themselves containers. Then
1938 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001939 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001940 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001941 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001942 * @return Tail element, or NULL if a FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001943 */
1944#define k_fifo_peek_tail(fifo) \
1945 k_queue_peek_tail((struct k_queue *) fifo)
1946
1947/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001948 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001949 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001950 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001951 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001952 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001953 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001954 * @param name Name of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001955 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001956#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001957 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001958 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001959 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001960
Anas Nashif166f5192018-02-25 08:02:36 -06001961/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001962
1963/**
1964 * @cond INTERNAL_HIDDEN
1965 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001966
1967struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001968 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001969};
1970
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001971#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001972 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001973 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001974 }
1975
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001976#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1977
Allan Stephensc98da842016-11-11 15:45:03 -05001978/**
1979 * INTERNAL_HIDDEN @endcond
1980 */
1981
1982/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001983 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05001984 * @ingroup kernel_apis
1985 * @{
1986 */
1987
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001988/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001989 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001990 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001991 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001992 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001993 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001994 *
1995 * @return N/A
1996 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001997#define k_lifo_init(lifo) \
1998 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001999
2000/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002001 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002002 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002003 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002004 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2005 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002006 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002007 * @note Can be called by ISRs.
2008 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002009 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002010 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002011 *
2012 * @return N/A
2013 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002014#define k_lifo_put(lifo, data) \
2015 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002016
2017/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002018 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002019 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002020 * This routine removes a data item from @a lifo in a "last in, first out"
2021 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002022 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002023 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2024 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002025 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002026 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002027 * or one of the special values K_NO_WAIT and K_FOREVER.
2028 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002029 * @return Address of the data item if successful; NULL if returned
2030 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002031 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002032#define k_lifo_get(lifo, timeout) \
2033 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002034
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002035/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002036 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002037 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002038 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002039 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002040 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002041 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002042 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002043 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002044#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002045 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002046 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002047 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002048
Anas Nashif166f5192018-02-25 08:02:36 -06002049/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002050
2051/**
2052 * @cond INTERNAL_HIDDEN
2053 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002054
2055struct k_stack {
2056 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002057 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002058
Anas Nashif2f203c22016-12-18 06:57:45 -05002059 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002060};
2061
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002062#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002063 { \
2064 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2065 .base = stack_buffer, \
2066 .next = stack_buffer, \
2067 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002068 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002069 }
2070
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002071#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2072
Allan Stephensc98da842016-11-11 15:45:03 -05002073/**
2074 * INTERNAL_HIDDEN @endcond
2075 */
2076
2077/**
2078 * @defgroup stack_apis Stack APIs
2079 * @ingroup kernel_apis
2080 * @{
2081 */
2082
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002083/**
2084 * @brief Initialize a stack.
2085 *
2086 * This routine initializes a stack object, prior to its first use.
2087 *
2088 * @param stack Address of the stack.
2089 * @param buffer Address of array used to hold stacked values.
2090 * @param num_entries Maximum number of values that can be stacked.
2091 *
2092 * @return N/A
2093 */
Andrew Boiee8734462017-09-29 16:42:07 -07002094__syscall void k_stack_init(struct k_stack *stack,
Andrew Boie8e3e6d02017-10-12 14:01:25 -07002095 u32_t *buffer, unsigned int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002096
2097/**
2098 * @brief Push an element onto a stack.
2099 *
2100 * This routine adds a 32-bit value @a data to @a stack.
2101 *
2102 * @note Can be called by ISRs.
2103 *
2104 * @param stack Address of the stack.
2105 * @param data Value to push onto the stack.
2106 *
2107 * @return N/A
2108 */
Andrew Boiee8734462017-09-29 16:42:07 -07002109__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002110
2111/**
2112 * @brief Pop an element from a stack.
2113 *
2114 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2115 * manner and stores the value in @a data.
2116 *
2117 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2118 *
2119 * @param stack Address of the stack.
2120 * @param data Address of area to hold the value popped from the stack.
2121 * @param timeout Waiting period to obtain a value (in milliseconds),
2122 * or one of the special values K_NO_WAIT and K_FOREVER.
2123 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002124 * @retval 0 Element popped from stack.
2125 * @retval -EBUSY Returned without waiting.
2126 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002127 */
Andrew Boiee8734462017-09-29 16:42:07 -07002128__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002129
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002130/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002131 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002132 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002133 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002134 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002135 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002136 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002137 * @param name Name of the stack.
2138 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002139 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002140#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002141 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002142 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002143 struct k_stack name \
2144 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002145 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002146 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002147
Anas Nashif166f5192018-02-25 08:02:36 -06002148/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002149
Allan Stephens6bba9b02016-11-16 14:56:54 -05002150struct k_work;
2151
Allan Stephensc98da842016-11-11 15:45:03 -05002152/**
2153 * @defgroup workqueue_apis Workqueue Thread APIs
2154 * @ingroup kernel_apis
2155 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002156 */
2157
Allan Stephens6bba9b02016-11-16 14:56:54 -05002158/**
2159 * @typedef k_work_handler_t
2160 * @brief Work item handler function type.
2161 *
2162 * A work item's handler function is executed by a workqueue's thread
2163 * when the work item is processed by the workqueue.
2164 *
2165 * @param work Address of the work item.
2166 *
2167 * @return N/A
2168 */
2169typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002170
2171/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002172 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002173 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002174
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002175struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002176 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002177 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002178};
2179
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002180enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002181 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002182};
2183
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002184struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002185 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002186 k_work_handler_t handler;
2187 atomic_t flags[1];
2188};
2189
Allan Stephens6bba9b02016-11-16 14:56:54 -05002190struct k_delayed_work {
2191 struct k_work work;
2192 struct _timeout timeout;
2193 struct k_work_q *work_q;
2194};
2195
2196extern struct k_work_q k_sys_work_q;
2197
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002198/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002199 * INTERNAL_HIDDEN @endcond
2200 */
2201
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002202#define _K_WORK_INITIALIZER(work_handler) \
2203 { \
2204 ._reserved = NULL, \
2205 .handler = work_handler, \
2206 .flags = { 0 } \
2207 }
2208
2209#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2210
Allan Stephens6bba9b02016-11-16 14:56:54 -05002211/**
2212 * @brief Initialize a statically-defined work item.
2213 *
2214 * This macro can be used to initialize a statically-defined workqueue work
2215 * item, prior to its first use. For example,
2216 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002217 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002218 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002219 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002220 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002221 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002222#define K_WORK_DEFINE(work, work_handler) \
2223 struct k_work work \
2224 __in_section(_k_work, static, work) = \
2225 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002226
2227/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002228 * @brief Initialize a work item.
2229 *
2230 * This routine initializes a workqueue work item, prior to its first use.
2231 *
2232 * @param work Address of work item.
2233 * @param handler Function to invoke each time work item is processed.
2234 *
2235 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002236 */
2237static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2238{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002239 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002240 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002241 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002242}
2243
2244/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002245 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002246 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002247 * This routine submits work item @a work to be processed by workqueue
2248 * @a work_q. If the work item is already pending in the workqueue's queue
2249 * as a result of an earlier submission, this routine has no effect on the
2250 * work item. If the work item has already been processed, or is currently
2251 * being processed, its work is considered complete and the work item can be
2252 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002253 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002254 * @warning
2255 * A submitted work item must not be modified until it has been processed
2256 * by the workqueue.
2257 *
2258 * @note Can be called by ISRs.
2259 *
2260 * @param work_q Address of workqueue.
2261 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002262 *
2263 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002264 */
2265static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2266 struct k_work *work)
2267{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002268 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002269 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002270 }
2271}
2272
2273/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002274 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002275 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002276 * This routine indicates if work item @a work is pending in a workqueue's
2277 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002278 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002279 * @note Can be called by ISRs.
2280 *
2281 * @param work Address of work item.
2282 *
2283 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002284 */
2285static inline int k_work_pending(struct k_work *work)
2286{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002287 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002288}
2289
2290/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002291 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002292 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002293 * This routine starts workqueue @a work_q. The workqueue spawns its work
2294 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002295 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002296 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002297 * @param stack Pointer to work queue thread's stack space, as defined by
2298 * K_THREAD_STACK_DEFINE()
2299 * @param stack_size Size of the work queue thread's stack (in bytes), which
2300 * should either be the same constant passed to
2301 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002302 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002303 *
2304 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002305 */
Andrew Boie507852a2017-07-25 18:47:07 -07002306extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002307 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002308 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002309
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002310/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002311 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002312 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002313 * This routine initializes a workqueue delayed work item, prior to
2314 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002315 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002316 * @param work Address of delayed work item.
2317 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002318 *
2319 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002320 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002321extern void k_delayed_work_init(struct k_delayed_work *work,
2322 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002323
2324/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002325 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002326 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002327 * This routine schedules work item @a work to be processed by workqueue
2328 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002329 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002330 * Only when the countdown completes is the work item actually submitted to
2331 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002332 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002333 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002334 * counting down cancels the existing submission and restarts the
2335 * countdown using the new delay. Note that this behavior is
2336 * inherently subject to race conditions with the pre-existing
2337 * timeouts and work queue, so care must be taken to synchronize such
2338 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002339 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002340 * @warning
2341 * A delayed work item must not be modified until it has been processed
2342 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002343 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002344 * @note Can be called by ISRs.
2345 *
2346 * @param work_q Address of workqueue.
2347 * @param work Address of delayed work item.
2348 * @param delay Delay before submitting the work item (in milliseconds).
2349 *
2350 * @retval 0 Work item countdown started.
2351 * @retval -EINPROGRESS Work item is already pending.
2352 * @retval -EINVAL Work item is being processed or has completed its work.
2353 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002354 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002355extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2356 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002357 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002358
2359/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002360 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002361 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002362 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002363 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002364 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002365 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002366 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002367 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002368 * @param work Address of delayed work item.
2369 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002370 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002371 * @retval -EINPROGRESS Work item is already pending.
2372 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002373 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002374extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002375
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002376/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002377 * @brief Submit a work item to the system workqueue.
2378 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002379 * This routine submits work item @a work to be processed by the system
2380 * workqueue. If the work item is already pending in the workqueue's queue
2381 * as a result of an earlier submission, this routine has no effect on the
2382 * work item. If the work item has already been processed, or is currently
2383 * being processed, its work is considered complete and the work item can be
2384 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002385 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002386 * @warning
2387 * Work items submitted to the system workqueue should avoid using handlers
2388 * that block or yield since this may prevent the system workqueue from
2389 * processing other work items in a timely manner.
2390 *
2391 * @note Can be called by ISRs.
2392 *
2393 * @param work Address of work item.
2394 *
2395 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002396 */
2397static inline void k_work_submit(struct k_work *work)
2398{
2399 k_work_submit_to_queue(&k_sys_work_q, work);
2400}
2401
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002402/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002403 * @brief Submit a delayed work item to the system workqueue.
2404 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002405 * This routine schedules work item @a work to be processed by the system
2406 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002407 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002408 * Only when the countdown completes is the work item actually submitted to
2409 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002410 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002411 * Submitting a previously submitted delayed work item that is still
2412 * counting down cancels the existing submission and restarts the countdown
2413 * using the new delay. If the work item is currently pending on the
2414 * workqueue's queue because the countdown has completed it is too late to
2415 * resubmit the item, and resubmission fails without impacting the work item.
2416 * If the work item has already been processed, or is currently being processed,
2417 * its work is considered complete and the work item can be resubmitted.
2418 *
2419 * @warning
2420 * Work items submitted to the system workqueue should avoid using handlers
2421 * that block or yield since this may prevent the system workqueue from
2422 * processing other work items in a timely manner.
2423 *
2424 * @note Can be called by ISRs.
2425 *
2426 * @param work Address of delayed work item.
2427 * @param delay Delay before submitting the work item (in milliseconds).
2428 *
2429 * @retval 0 Work item countdown started.
2430 * @retval -EINPROGRESS Work item is already pending.
2431 * @retval -EINVAL Work item is being processed or has completed its work.
2432 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002433 */
2434static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002435 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002436{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002437 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002438}
2439
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002440/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002441 * @brief Get time remaining before a delayed work gets scheduled.
2442 *
2443 * This routine computes the (approximate) time remaining before a
2444 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002445 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002446 *
2447 * @param work Delayed work item.
2448 *
2449 * @return Remaining time (in milliseconds).
2450 */
Kumar Galacc334c72017-04-21 10:55:34 -05002451static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002452{
2453 return _timeout_remaining_get(&work->timeout);
2454}
2455
Anas Nashif166f5192018-02-25 08:02:36 -06002456/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002457
Allan Stephensc98da842016-11-11 15:45:03 -05002458/**
2459 * @cond INTERNAL_HIDDEN
2460 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002461
2462struct k_mutex {
2463 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002464 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002465 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002466 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002467
Anas Nashif2f203c22016-12-18 06:57:45 -05002468 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002469};
2470
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002471#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002472 { \
2473 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2474 .owner = NULL, \
2475 .lock_count = 0, \
2476 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002477 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002478 }
2479
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002480#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2481
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002482/**
Allan Stephensc98da842016-11-11 15:45:03 -05002483 * INTERNAL_HIDDEN @endcond
2484 */
2485
2486/**
2487 * @defgroup mutex_apis Mutex APIs
2488 * @ingroup kernel_apis
2489 * @{
2490 */
2491
2492/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002493 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002494 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002495 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002496 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002497 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002498 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002499 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002500 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002501#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002502 struct k_mutex name \
2503 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002504 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002505
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002506/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002507 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002508 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002509 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002510 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002511 * Upon completion, the mutex is available and does not have an owner.
2512 *
2513 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002514 *
2515 * @return N/A
2516 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002517__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002518
2519/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002520 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002521 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002522 * This routine locks @a mutex. If the mutex is locked by another thread,
2523 * the calling thread waits until the mutex becomes available or until
2524 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002525 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002526 * A thread is permitted to lock a mutex it has already locked. The operation
2527 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002528 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002529 * @param mutex Address of the mutex.
2530 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002531 * or one of the special values K_NO_WAIT and K_FOREVER.
2532 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002533 * @retval 0 Mutex locked.
2534 * @retval -EBUSY Returned without waiting.
2535 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002536 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002537__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002538
2539/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002540 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002541 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002542 * This routine unlocks @a mutex. The mutex must already be locked by the
2543 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002544 *
2545 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002546 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002547 * thread.
2548 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002549 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002550 *
2551 * @return N/A
2552 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002553__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002554
Allan Stephensc98da842016-11-11 15:45:03 -05002555/**
Anas Nashif166f5192018-02-25 08:02:36 -06002556 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002557 */
2558
2559/**
2560 * @cond INTERNAL_HIDDEN
2561 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002562
2563struct k_sem {
2564 _wait_q_t wait_q;
2565 unsigned int count;
2566 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002567 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002568
Anas Nashif2f203c22016-12-18 06:57:45 -05002569 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002570};
2571
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002572#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002573 { \
2574 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2575 .count = initial_count, \
2576 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002577 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002578 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002579 }
2580
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002581#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2582
Allan Stephensc98da842016-11-11 15:45:03 -05002583/**
2584 * INTERNAL_HIDDEN @endcond
2585 */
2586
2587/**
2588 * @defgroup semaphore_apis Semaphore APIs
2589 * @ingroup kernel_apis
2590 * @{
2591 */
2592
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002593/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002594 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002595 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002596 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002597 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002598 * @param sem Address of the semaphore.
2599 * @param initial_count Initial semaphore count.
2600 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002601 *
2602 * @return N/A
2603 */
Andrew Boie99280232017-09-29 14:17:47 -07002604__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2605 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002606
2607/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002608 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002609 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002610 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002611 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002612 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2613 *
2614 * @param sem Address of the semaphore.
2615 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002616 * or one of the special values K_NO_WAIT and K_FOREVER.
2617 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002618 * @note When porting code from the nanokernel legacy API to the new API, be
2619 * careful with the return value of this function. The return value is the
2620 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2621 * non-zero means failure, while the nano_sem_take family returns 1 for success
2622 * and 0 for failure.
2623 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002624 * @retval 0 Semaphore taken.
2625 * @retval -EBUSY Returned without waiting.
2626 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002627 */
Andrew Boie99280232017-09-29 14:17:47 -07002628__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002629
2630/**
2631 * @brief Give a semaphore.
2632 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002633 * This routine gives @a sem, unless the semaphore is already at its maximum
2634 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002635 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002636 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002637 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002638 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002639 *
2640 * @return N/A
2641 */
Andrew Boie99280232017-09-29 14:17:47 -07002642__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002643
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002644/**
2645 * @brief Reset a semaphore's count to zero.
2646 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002647 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002648 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002649 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002650 *
2651 * @return N/A
2652 */
Andrew Boie990bf162017-10-03 12:36:49 -07002653__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002654
Anas Nashif954d5502018-02-25 08:37:28 -06002655/**
2656 * @internal
2657 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002658static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002659{
2660 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002661}
2662
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002663/**
2664 * @brief Get a semaphore's count.
2665 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002666 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002667 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002668 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002669 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002670 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002671 */
Andrew Boie990bf162017-10-03 12:36:49 -07002672__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002673
Anas Nashif954d5502018-02-25 08:37:28 -06002674/**
2675 * @internal
2676 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002677static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002678{
2679 return sem->count;
2680}
2681
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002682/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002683 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002684 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002685 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002686 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002687 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002688 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002689 * @param name Name of the semaphore.
2690 * @param initial_count Initial semaphore count.
2691 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002692 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002693#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002694 struct k_sem name \
2695 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002696 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002697
Anas Nashif166f5192018-02-25 08:02:36 -06002698/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002699
2700/**
2701 * @defgroup alert_apis Alert APIs
2702 * @ingroup kernel_apis
2703 * @{
2704 */
2705
Allan Stephens5eceb852016-11-16 10:16:30 -05002706/**
2707 * @typedef k_alert_handler_t
2708 * @brief Alert handler function type.
2709 *
2710 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002711 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002712 * and is only invoked if the alert has been initialized with one.
2713 *
2714 * @param alert Address of the alert.
2715 *
2716 * @return 0 if alert has been consumed; non-zero if alert should pend.
2717 */
2718typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002719
Anas Nashif166f5192018-02-25 08:02:36 -06002720/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002721
2722/**
2723 * @cond INTERNAL_HIDDEN
2724 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002725
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002726#define K_ALERT_DEFAULT NULL
2727#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002728
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002729struct k_alert {
2730 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002731 atomic_t send_count;
2732 struct k_work work_item;
2733 struct k_sem sem;
2734
Anas Nashif2f203c22016-12-18 06:57:45 -05002735 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002736};
2737
Anas Nashif954d5502018-02-25 08:37:28 -06002738/**
2739 * @internal
2740 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002741extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002742
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002743#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002744 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002745 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002746 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002747 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2748 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002749 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002750 }
2751
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002752#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2753
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002754/**
Allan Stephensc98da842016-11-11 15:45:03 -05002755 * INTERNAL_HIDDEN @endcond
2756 */
2757
2758/**
2759 * @addtogroup alert_apis
2760 * @{
2761 */
2762
2763/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002764 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002765 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002766 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002767 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002768 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002769 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002770 * @param name Name of the alert.
2771 * @param alert_handler Action to take when alert is sent. Specify either
2772 * the address of a function to be invoked by the system workqueue
2773 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2774 * K_ALERT_DEFAULT (which causes the alert to pend).
2775 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002776 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002777#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002778 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002779 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002780 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002781 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002782
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002783/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002784 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002785 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002786 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002787 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002788 * @param alert Address of the alert.
2789 * @param handler Action to take when alert is sent. Specify either the address
2790 * of a function to be invoked by the system workqueue thread,
2791 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2792 * K_ALERT_DEFAULT (which causes the alert to pend).
2793 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002794 *
2795 * @return N/A
2796 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002797extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2798 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002799
2800/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002801 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002802 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002803 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002804 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002805 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2806 *
2807 * @param alert Address of the alert.
2808 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002809 * or one of the special values K_NO_WAIT and K_FOREVER.
2810 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002811 * @retval 0 Alert received.
2812 * @retval -EBUSY Returned without waiting.
2813 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002814 */
Andrew Boie310e9872017-09-29 04:41:15 -07002815__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002816
2817/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002818 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002820 * This routine signals @a alert. The action specified for @a alert will
2821 * be taken, which may trigger the execution of an alert handler function
2822 * and/or cause the alert to pend (assuming the alert has not reached its
2823 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002824 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002825 * @note Can be called by ISRs.
2826 *
2827 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002828 *
2829 * @return N/A
2830 */
Andrew Boie310e9872017-09-29 04:41:15 -07002831__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002832
2833/**
Anas Nashif166f5192018-02-25 08:02:36 -06002834 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002835 */
2836
Allan Stephensc98da842016-11-11 15:45:03 -05002837/**
2838 * @cond INTERNAL_HIDDEN
2839 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002840
2841struct k_msgq {
2842 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002843 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002844 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002845 char *buffer_start;
2846 char *buffer_end;
2847 char *read_ptr;
2848 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002849 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002850
Anas Nashif2f203c22016-12-18 06:57:45 -05002851 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002852};
2853
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002854#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002855 { \
2856 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002857 .max_msgs = q_max_msgs, \
2858 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002859 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002860 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002861 .read_ptr = q_buffer, \
2862 .write_ptr = q_buffer, \
2863 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002864 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002865 }
2866
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002867#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2868
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05302869struct k_msgq_attrs {
2870 size_t msg_size;
2871 u32_t max_msgs;
2872 u32_t used_msgs;
2873};
2874
Peter Mitsis1da807e2016-10-06 11:36:59 -04002875/**
Allan Stephensc98da842016-11-11 15:45:03 -05002876 * INTERNAL_HIDDEN @endcond
2877 */
2878
2879/**
2880 * @defgroup msgq_apis Message Queue APIs
2881 * @ingroup kernel_apis
2882 * @{
2883 */
2884
2885/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002886 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002887 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002888 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2889 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002890 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2891 * message is similarly aligned to this boundary, @a q_msg_size must also be
2892 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002893 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002894 * The message queue can be accessed outside the module where it is defined
2895 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002896 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002897 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002898 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002899 * @param q_name Name of the message queue.
2900 * @param q_msg_size Message size (in bytes).
2901 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002902 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002903 */
2904#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2905 static char __noinit __aligned(q_align) \
2906 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002907 struct k_msgq q_name \
2908 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002909 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002910 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002911
Peter Mitsisd7a37502016-10-13 11:37:40 -04002912/**
2913 * @brief Initialize a message queue.
2914 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002915 * This routine initializes a message queue object, prior to its first use.
2916 *
Allan Stephensda827222016-11-09 14:23:58 -06002917 * The message queue's ring buffer must contain space for @a max_msgs messages,
2918 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2919 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2920 * that each message is similarly aligned to this boundary, @a q_msg_size
2921 * must also be a multiple of N.
2922 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002923 * @param q Address of the message queue.
2924 * @param buffer Pointer to ring buffer that holds queued messages.
2925 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002926 * @param max_msgs Maximum number of messages that can be queued.
2927 *
2928 * @return N/A
2929 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002930__syscall void k_msgq_init(struct k_msgq *q, char *buffer,
2931 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002932
2933/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002934 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002935 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002936 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002937 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002938 * @note Can be called by ISRs.
2939 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002940 * @param q Address of the message queue.
2941 * @param data Pointer to the message.
2942 * @param timeout Waiting period to add the message (in milliseconds),
2943 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002944 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002945 * @retval 0 Message sent.
2946 * @retval -ENOMSG Returned without waiting or queue purged.
2947 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002948 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002949__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002950
2951/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002952 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002953 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002954 * This routine receives a message from message queue @a q in a "first in,
2955 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002956 *
Allan Stephensc98da842016-11-11 15:45:03 -05002957 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002958 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002959 * @param q Address of the message queue.
2960 * @param data Address of area to hold the received message.
2961 * @param timeout Waiting period to receive the message (in milliseconds),
2962 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002963 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002964 * @retval 0 Message received.
2965 * @retval -ENOMSG Returned without waiting.
2966 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002967 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002968__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002969
2970/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002971 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002972 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002973 * This routine discards all unreceived messages in a message queue's ring
2974 * buffer. Any threads that are blocked waiting to send a message to the
2975 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002976 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002977 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002978 *
2979 * @return N/A
2980 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002981__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002982
Peter Mitsis67be2492016-10-07 11:44:34 -04002983/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002984 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002986 * This routine returns the number of unused entries in a message queue's
2987 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002988 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002989 * @param q Address of the message queue.
2990 *
2991 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002992 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002993__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
2994
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05302995/**
2996 * @brief Get basic attributes of a message queue.
2997 *
2998 * This routine fetches basic attributes of message queue into attr argument.
2999 *
3000 * @param q Address of the message queue.
3001 * @param attrs pointer to message queue attribute structure.
3002 *
3003 * @return N/A
3004 */
3005__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3006
3007
Andrew Boie82edb6e2017-10-02 10:53:06 -07003008static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003009{
3010 return q->max_msgs - q->used_msgs;
3011}
3012
Peter Mitsisd7a37502016-10-13 11:37:40 -04003013/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003014 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003015 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003016 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003017 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003018 * @param q Address of the message queue.
3019 *
3020 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003021 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003022__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3023
3024static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003025{
3026 return q->used_msgs;
3027}
3028
Anas Nashif166f5192018-02-25 08:02:36 -06003029/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003030
3031/**
3032 * @defgroup mem_pool_apis Memory Pool APIs
3033 * @ingroup kernel_apis
3034 * @{
3035 */
3036
Andy Ross73cb9582017-05-09 10:42:39 -07003037/* Note on sizing: the use of a 20 bit field for block means that,
3038 * assuming a reasonable minimum block size of 16 bytes, we're limited
3039 * to 16M of memory managed by a single pool. Long term it would be
3040 * good to move to a variable bit size based on configuration.
3041 */
3042struct k_mem_block_id {
3043 u32_t pool : 8;
3044 u32_t level : 4;
3045 u32_t block : 20;
3046};
3047
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003048struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003049 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003050 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003051};
3052
Anas Nashif166f5192018-02-25 08:02:36 -06003053/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003054
3055/**
3056 * @defgroup mailbox_apis Mailbox APIs
3057 * @ingroup kernel_apis
3058 * @{
3059 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003060
3061struct k_mbox_msg {
3062 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003063 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003064 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003065 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003066 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003067 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003068 /** sender's message data buffer */
3069 void *tx_data;
3070 /** internal use only - needed for legacy API support */
3071 void *_rx_data;
3072 /** message data block descriptor */
3073 struct k_mem_block tx_block;
3074 /** source thread id */
3075 k_tid_t rx_source_thread;
3076 /** target thread id */
3077 k_tid_t tx_target_thread;
3078 /** internal use only - thread waiting on send (may be a dummy) */
3079 k_tid_t _syncing_thread;
3080#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3081 /** internal use only - semaphore used during asynchronous send */
3082 struct k_sem *_async_sem;
3083#endif
3084};
3085
Allan Stephensc98da842016-11-11 15:45:03 -05003086/**
3087 * @cond INTERNAL_HIDDEN
3088 */
3089
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003090struct k_mbox {
3091 _wait_q_t tx_msg_queue;
3092 _wait_q_t rx_msg_queue;
3093
Anas Nashif2f203c22016-12-18 06:57:45 -05003094 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003095};
3096
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003097#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003098 { \
3099 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3100 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003101 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003102 }
3103
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003104#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3105
Peter Mitsis12092702016-10-14 12:57:23 -04003106/**
Allan Stephensc98da842016-11-11 15:45:03 -05003107 * INTERNAL_HIDDEN @endcond
3108 */
3109
3110/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003111 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003112 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003113 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003114 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003115 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003116 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003117 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003118 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003119#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003120 struct k_mbox name \
3121 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003122 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003123
Peter Mitsis12092702016-10-14 12:57:23 -04003124/**
3125 * @brief Initialize a mailbox.
3126 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003127 * This routine initializes a mailbox object, prior to its first use.
3128 *
3129 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003130 *
3131 * @return N/A
3132 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003133extern void k_mbox_init(struct k_mbox *mbox);
3134
Peter Mitsis12092702016-10-14 12:57:23 -04003135/**
3136 * @brief Send a mailbox message in a synchronous manner.
3137 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003138 * This routine sends a message to @a mbox and waits for a receiver to both
3139 * receive and process it. The message data may be in a buffer, in a memory
3140 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003141 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003142 * @param mbox Address of the mailbox.
3143 * @param tx_msg Address of the transmit message descriptor.
3144 * @param timeout Waiting period for the message to be received (in
3145 * milliseconds), or one of the special values K_NO_WAIT
3146 * and K_FOREVER. Once the message has been received,
3147 * this routine waits as long as necessary for the message
3148 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003149 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003150 * @retval 0 Message sent.
3151 * @retval -ENOMSG Returned without waiting.
3152 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003153 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003154extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003155 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003156
Peter Mitsis12092702016-10-14 12:57:23 -04003157/**
3158 * @brief Send a mailbox message in an asynchronous manner.
3159 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003160 * This routine sends a message to @a mbox without waiting for a receiver
3161 * to process it. The message data may be in a buffer, in a memory pool block,
3162 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3163 * will be given when the message has been both received and completely
3164 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003165 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003166 * @param mbox Address of the mailbox.
3167 * @param tx_msg Address of the transmit message descriptor.
3168 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003169 *
3170 * @return N/A
3171 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003172extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003173 struct k_sem *sem);
3174
Peter Mitsis12092702016-10-14 12:57:23 -04003175/**
3176 * @brief Receive a mailbox message.
3177 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003178 * This routine receives a message from @a mbox, then optionally retrieves
3179 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003180 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003181 * @param mbox Address of the mailbox.
3182 * @param rx_msg Address of the receive message descriptor.
3183 * @param buffer Address of the buffer to receive data, or NULL to defer data
3184 * retrieval and message disposal until later.
3185 * @param timeout Waiting period for a message to be received (in
3186 * milliseconds), or one of the special values K_NO_WAIT
3187 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003188 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003189 * @retval 0 Message received.
3190 * @retval -ENOMSG Returned without waiting.
3191 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003192 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003193extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003194 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003195
3196/**
3197 * @brief Retrieve mailbox message data into a buffer.
3198 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003199 * This routine completes the processing of a received message by retrieving
3200 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003201 *
3202 * Alternatively, this routine can be used to dispose of a received message
3203 * without retrieving its data.
3204 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003205 * @param rx_msg Address of the receive message descriptor.
3206 * @param buffer Address of the buffer to receive data, or NULL to discard
3207 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003208 *
3209 * @return N/A
3210 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003211extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003212
3213/**
3214 * @brief Retrieve mailbox message data into a memory pool block.
3215 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003216 * This routine completes the processing of a received message by retrieving
3217 * its data into a memory pool block, then disposing of the message.
3218 * The memory pool block that results from successful retrieval must be
3219 * returned to the pool once the data has been processed, even in cases
3220 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003221 *
3222 * Alternatively, this routine can be used to dispose of a received message
3223 * without retrieving its data. In this case there is no need to return a
3224 * memory pool block to the pool.
3225 *
3226 * This routine allocates a new memory pool block for the data only if the
3227 * data is not already in one. If a new block cannot be allocated, the routine
3228 * returns a failure code and the received message is left unchanged. This
3229 * permits the caller to reattempt data retrieval at a later time or to dispose
3230 * of the received message without retrieving its data.
3231 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003232 * @param rx_msg Address of a receive message descriptor.
3233 * @param pool Address of memory pool, or NULL to discard data.
3234 * @param block Address of the area to hold memory pool block info.
3235 * @param timeout Waiting period to wait for a memory pool block (in
3236 * milliseconds), or one of the special values K_NO_WAIT
3237 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003238 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003239 * @retval 0 Data retrieved.
3240 * @retval -ENOMEM Returned without waiting.
3241 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003242 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003243extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003244 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003245 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003246
Anas Nashif166f5192018-02-25 08:02:36 -06003247/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003248
3249/**
3250 * @cond INTERNAL_HIDDEN
3251 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003252
3253struct k_pipe {
3254 unsigned char *buffer; /* Pipe buffer: may be NULL */
3255 size_t size; /* Buffer size */
3256 size_t bytes_used; /* # bytes used in buffer */
3257 size_t read_index; /* Where in buffer to read from */
3258 size_t write_index; /* Where in buffer to write */
3259
3260 struct {
3261 _wait_q_t readers; /* Reader wait queue */
3262 _wait_q_t writers; /* Writer wait queue */
3263 } wait_q;
3264
Anas Nashif2f203c22016-12-18 06:57:45 -05003265 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003266};
3267
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003268#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003269 { \
3270 .buffer = pipe_buffer, \
3271 .size = pipe_buffer_size, \
3272 .bytes_used = 0, \
3273 .read_index = 0, \
3274 .write_index = 0, \
3275 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3276 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003277 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003278 }
3279
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003280#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3281
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003282/**
Allan Stephensc98da842016-11-11 15:45:03 -05003283 * INTERNAL_HIDDEN @endcond
3284 */
3285
3286/**
3287 * @defgroup pipe_apis Pipe APIs
3288 * @ingroup kernel_apis
3289 * @{
3290 */
3291
3292/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003293 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003294 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003295 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003296 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003297 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003298 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003299 * @param name Name of the pipe.
3300 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3301 * or zero if no ring buffer is used.
3302 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003303 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003304#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3305 static unsigned char __noinit __aligned(pipe_align) \
3306 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003307 struct k_pipe name \
3308 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003309 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003310
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003311/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003312 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003313 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003314 * This routine initializes a pipe object, prior to its first use.
3315 *
3316 * @param pipe Address of the pipe.
3317 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3318 * is used.
3319 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3320 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003321 *
3322 * @return N/A
3323 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003324__syscall void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3325 size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003326
3327/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003328 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003329 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003330 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003331 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003332 * @param pipe Address of the pipe.
3333 * @param data Address of data to write.
3334 * @param bytes_to_write Size of data (in bytes).
3335 * @param bytes_written Address of area to hold the number of bytes written.
3336 * @param min_xfer Minimum number of bytes to write.
3337 * @param timeout Waiting period to wait for the data to be written (in
3338 * milliseconds), or one of the special values K_NO_WAIT
3339 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003340 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003341 * @retval 0 At least @a min_xfer bytes of data were written.
3342 * @retval -EIO Returned without waiting; zero data bytes were written.
3343 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003344 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003345 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003346__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3347 size_t bytes_to_write, size_t *bytes_written,
3348 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003349
3350/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003351 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003353 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003355 * @param pipe Address of the pipe.
3356 * @param data Address to place the data read from pipe.
3357 * @param bytes_to_read Maximum number of data bytes to read.
3358 * @param bytes_read Address of area to hold the number of bytes read.
3359 * @param min_xfer Minimum number of data bytes to read.
3360 * @param timeout Waiting period to wait for the data to be read (in
3361 * milliseconds), or one of the special values K_NO_WAIT
3362 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003363 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003364 * @retval 0 At least @a min_xfer bytes of data were read.
3365 * @retval -EIO Returned without waiting; zero data bytes were read.
3366 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003367 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003368 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003369__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3370 size_t bytes_to_read, size_t *bytes_read,
3371 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003372
3373/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003374 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003375 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003376 * This routine writes the data contained in a memory block to @a pipe.
3377 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003378 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003379 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003380 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003381 * @param block Memory block containing data to send
3382 * @param size Number of data bytes in memory block to send
3383 * @param sem Semaphore to signal upon completion (else NULL)
3384 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003385 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003386 */
3387extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3388 size_t size, struct k_sem *sem);
3389
Anas Nashif166f5192018-02-25 08:02:36 -06003390/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003391
Allan Stephensc98da842016-11-11 15:45:03 -05003392/**
3393 * @cond INTERNAL_HIDDEN
3394 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003395
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003396struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003397 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003398 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003399 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003400 char *buffer;
3401 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003402 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003403
Anas Nashif2f203c22016-12-18 06:57:45 -05003404 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003405};
3406
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003407#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003408 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003409 { \
3410 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003411 .num_blocks = slab_num_blocks, \
3412 .block_size = slab_block_size, \
3413 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003414 .free_list = NULL, \
3415 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003416 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003417 }
3418
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003419#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3420
3421
Peter Mitsis578f9112016-10-07 13:50:31 -04003422/**
Allan Stephensc98da842016-11-11 15:45:03 -05003423 * INTERNAL_HIDDEN @endcond
3424 */
3425
3426/**
3427 * @defgroup mem_slab_apis Memory Slab APIs
3428 * @ingroup kernel_apis
3429 * @{
3430 */
3431
3432/**
Allan Stephensda827222016-11-09 14:23:58 -06003433 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003434 *
Allan Stephensda827222016-11-09 14:23:58 -06003435 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003436 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003437 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3438 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003439 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003440 *
Allan Stephensda827222016-11-09 14:23:58 -06003441 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003442 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003443 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003444 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003445 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003446 * @param name Name of the memory slab.
3447 * @param slab_block_size Size of each memory block (in bytes).
3448 * @param slab_num_blocks Number memory blocks.
3449 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003450 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003451#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3452 char __noinit __aligned(slab_align) \
3453 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3454 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003455 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003456 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003457 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003458
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003459/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003460 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003461 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003462 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003463 *
Allan Stephensda827222016-11-09 14:23:58 -06003464 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3465 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3466 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3467 * To ensure that each memory block is similarly aligned to this boundary,
3468 * @a slab_block_size must also be a multiple of N.
3469 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003470 * @param slab Address of the memory slab.
3471 * @param buffer Pointer to buffer used for the memory blocks.
3472 * @param block_size Size of each memory block (in bytes).
3473 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003474 *
3475 * @return N/A
3476 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003477extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003478 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003479
3480/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003481 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003482 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003483 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003484 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003485 * @param slab Address of the memory slab.
3486 * @param mem Pointer to block address area.
3487 * @param timeout Maximum time to wait for operation to complete
3488 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3489 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003490 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003491 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003492 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003493 * @retval -ENOMEM Returned without waiting.
3494 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003495 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003496extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003497 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003498
3499/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003500 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003501 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003502 * This routine releases a previously allocated memory block back to its
3503 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003504 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003505 * @param slab Address of the memory slab.
3506 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003507 *
3508 * @return N/A
3509 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003510extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003511
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003512/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003513 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003514 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003515 * This routine gets the number of memory blocks that are currently
3516 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003517 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003518 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003519 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003520 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003521 */
Kumar Galacc334c72017-04-21 10:55:34 -05003522static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003523{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003524 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003525}
3526
Peter Mitsisc001aa82016-10-13 13:53:37 -04003527/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003528 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003529 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003530 * This routine gets the number of memory blocks that are currently
3531 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003532 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003533 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003534 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003535 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003536 */
Kumar Galacc334c72017-04-21 10:55:34 -05003537static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003538{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003539 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003540}
3541
Anas Nashif166f5192018-02-25 08:02:36 -06003542/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003543
3544/**
3545 * @cond INTERNAL_HIDDEN
3546 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003547
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003548struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003549 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003550 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003551};
3552
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003553/**
Allan Stephensc98da842016-11-11 15:45:03 -05003554 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003555 */
3556
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003557/**
Allan Stephensc98da842016-11-11 15:45:03 -05003558 * @addtogroup mem_pool_apis
3559 * @{
3560 */
3561
3562/**
3563 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003564 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003565 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3566 * long. The memory pool allows blocks to be repeatedly partitioned into
3567 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003568 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003569 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003570 * If the pool is to be accessed outside the module where it is defined, it
3571 * can be declared via
3572 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003573 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003574 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003575 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003576 * @param minsz Size of the smallest blocks in the pool (in bytes).
3577 * @param maxsz Size of the largest blocks in the pool (in bytes).
3578 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003579 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003580 */
Andy Ross73cb9582017-05-09 10:42:39 -07003581#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3582 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3583 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08003584 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07003585 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08003586 .base = { \
3587 .buf = _mpool_buf_##name, \
3588 .max_sz = maxsz, \
3589 .n_max = nmax, \
3590 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3591 .levels = _mpool_lvls_##name, \
3592 .flags = SYS_MEM_POOL_KERNEL \
3593 } \
Andy Ross73cb9582017-05-09 10:42:39 -07003594 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003595
Peter Mitsis937042c2016-10-13 13:18:26 -04003596/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003597 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003598 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003599 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003600 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003601 * @param pool Address of the memory pool.
3602 * @param block Pointer to block descriptor for the allocated memory.
3603 * @param size Amount of memory to allocate (in bytes).
3604 * @param timeout Maximum time to wait for operation to complete
3605 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3606 * or K_FOREVER to wait as long as necessary.
3607 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003608 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003609 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003610 * @retval -ENOMEM Returned without waiting.
3611 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003612 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003613extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003614 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003615
3616/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003617 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003618 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003619 * This routine releases a previously allocated memory block back to its
3620 * memory pool.
3621 *
3622 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003623 *
3624 * @return N/A
3625 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003626extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003627
3628/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003629 * @brief Free memory allocated from a memory pool.
3630 *
3631 * This routine releases a previously allocated memory block back to its
3632 * memory pool
3633 *
3634 * @param id Memory block identifier.
3635 *
3636 * @return N/A
3637 */
3638extern void k_mem_pool_free_id(struct k_mem_block_id *id);
3639
3640/**
Anas Nashif166f5192018-02-25 08:02:36 -06003641 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003642 */
3643
3644/**
3645 * @defgroup heap_apis Heap Memory Pool APIs
3646 * @ingroup kernel_apis
3647 * @{
3648 */
3649
3650/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003651 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003652 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003653 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003654 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003655 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003656 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003657 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003658 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003659 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003660extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003661
3662/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003663 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003664 *
3665 * This routine provides traditional free() semantics. The memory being
3666 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003667 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003668 * If @a ptr is NULL, no operation is performed.
3669 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003670 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003671 *
3672 * @return N/A
3673 */
3674extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003675
Allan Stephensc98da842016-11-11 15:45:03 -05003676/**
Andrew Boie7f95e832017-11-08 14:40:01 -08003677 * @brief Allocate memory from heap, array style
3678 *
3679 * This routine provides traditional calloc() semantics. Memory is
3680 * allocated from the heap memory pool and zeroed.
3681 *
3682 * @param nmemb Number of elements in the requested array
3683 * @param size Size of each array element (in bytes).
3684 *
3685 * @return Address of the allocated memory if successful; otherwise NULL.
3686 */
3687extern void *k_calloc(size_t nmemb, size_t size);
3688
Anas Nashif166f5192018-02-25 08:02:36 -06003689/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003690
Benjamin Walshacc68c12017-01-29 18:57:45 -05003691/* polling API - PRIVATE */
3692
Benjamin Walshb0179862017-02-02 16:39:57 -05003693#ifdef CONFIG_POLL
3694#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3695#else
3696#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3697#endif
3698
Benjamin Walshacc68c12017-01-29 18:57:45 -05003699/* private - implementation data created as needed, per-type */
3700struct _poller {
3701 struct k_thread *thread;
3702};
3703
3704/* private - types bit positions */
3705enum _poll_types_bits {
3706 /* can be used to ignore an event */
3707 _POLL_TYPE_IGNORE,
3708
3709 /* to be signaled by k_poll_signal() */
3710 _POLL_TYPE_SIGNAL,
3711
3712 /* semaphore availability */
3713 _POLL_TYPE_SEM_AVAILABLE,
3714
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003715 /* queue/fifo/lifo data availability */
3716 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003717
3718 _POLL_NUM_TYPES
3719};
3720
3721#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3722
3723/* private - states bit positions */
3724enum _poll_states_bits {
3725 /* default state when creating event */
3726 _POLL_STATE_NOT_READY,
3727
Benjamin Walshacc68c12017-01-29 18:57:45 -05003728 /* signaled by k_poll_signal() */
3729 _POLL_STATE_SIGNALED,
3730
3731 /* semaphore is available */
3732 _POLL_STATE_SEM_AVAILABLE,
3733
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003734 /* data is available to read on queue/fifo/lifo */
3735 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003736
3737 _POLL_NUM_STATES
3738};
3739
3740#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3741
3742#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003743 (32 - (0 \
3744 + 8 /* tag */ \
3745 + _POLL_NUM_TYPES \
3746 + _POLL_NUM_STATES \
3747 + 1 /* modes */ \
3748 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003749
Benjamin Walshacc68c12017-01-29 18:57:45 -05003750/* end of polling API - PRIVATE */
3751
3752
3753/**
3754 * @defgroup poll_apis Async polling APIs
3755 * @ingroup kernel_apis
3756 * @{
3757 */
3758
3759/* Public polling API */
3760
3761/* public - values for k_poll_event.type bitfield */
3762#define K_POLL_TYPE_IGNORE 0
3763#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3764#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003765#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3766#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003767
3768/* public - polling modes */
3769enum k_poll_modes {
3770 /* polling thread does not take ownership of objects when available */
3771 K_POLL_MODE_NOTIFY_ONLY = 0,
3772
3773 K_POLL_NUM_MODES
3774};
3775
3776/* public - values for k_poll_event.state bitfield */
3777#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003778#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3779#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003780#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3781#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003782
3783/* public - poll signal object */
3784struct k_poll_signal {
3785 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003786 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003787
3788 /*
3789 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3790 * user resets it to 0.
3791 */
3792 unsigned int signaled;
3793
3794 /* custom result value passed to k_poll_signal() if needed */
3795 int result;
3796};
3797
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003798#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003799 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003800 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003801 .signaled = 0, \
3802 .result = 0, \
3803 }
3804
3805struct k_poll_event {
3806 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003807 sys_dnode_t _node;
3808
3809 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003810 struct _poller *poller;
3811
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003812 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003813 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003814
Benjamin Walshacc68c12017-01-29 18:57:45 -05003815 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003816 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003817
3818 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003819 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003820
3821 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003822 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003823
3824 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003825 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003826
3827 /* per-type data */
3828 union {
3829 void *obj;
3830 struct k_poll_signal *signal;
3831 struct k_sem *sem;
3832 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003833 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003834 };
3835};
3836
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003837#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003838 { \
3839 .poller = NULL, \
3840 .type = event_type, \
3841 .state = K_POLL_STATE_NOT_READY, \
3842 .mode = event_mode, \
3843 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003844 { .obj = event_obj }, \
3845 }
3846
3847#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3848 event_tag) \
3849 { \
3850 .type = event_type, \
3851 .tag = event_tag, \
3852 .state = K_POLL_STATE_NOT_READY, \
3853 .mode = event_mode, \
3854 .unused = 0, \
3855 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003856 }
3857
3858/**
3859 * @brief Initialize one struct k_poll_event instance
3860 *
3861 * After this routine is called on a poll event, the event it ready to be
3862 * placed in an event array to be passed to k_poll().
3863 *
3864 * @param event The event to initialize.
3865 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3866 * values. Only values that apply to the same object being polled
3867 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3868 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003869 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003870 * @param obj Kernel object or poll signal.
3871 *
3872 * @return N/A
3873 */
3874
Kumar Galacc334c72017-04-21 10:55:34 -05003875extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003876 int mode, void *obj);
3877
3878/**
3879 * @brief Wait for one or many of multiple poll events to occur
3880 *
3881 * This routine allows a thread to wait concurrently for one or many of
3882 * multiple poll events to have occurred. Such events can be a kernel object
3883 * being available, like a semaphore, or a poll signal event.
3884 *
3885 * When an event notifies that a kernel object is available, the kernel object
3886 * is not "given" to the thread calling k_poll(): it merely signals the fact
3887 * that the object was available when the k_poll() call was in effect. Also,
3888 * all threads trying to acquire an object the regular way, i.e. by pending on
3889 * the object, have precedence over the thread polling on the object. This
3890 * means that the polling thread will never get the poll event on an object
3891 * until the object becomes available and its pend queue is empty. For this
3892 * reason, the k_poll() call is more effective when the objects being polled
3893 * only have one thread, the polling thread, trying to acquire them.
3894 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003895 * When k_poll() returns 0, the caller should loop on all the events that were
3896 * passed to k_poll() and check the state field for the values that were
3897 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003898 *
3899 * Before being reused for another call to k_poll(), the user has to reset the
3900 * state field to K_POLL_STATE_NOT_READY.
3901 *
3902 * @param events An array of pointers to events to be polled for.
3903 * @param num_events The number of events in the array.
3904 * @param timeout Waiting period for an event to be ready (in milliseconds),
3905 * or one of the special values K_NO_WAIT and K_FOREVER.
3906 *
3907 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003908 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02003909 * @retval -EINTR Poller thread has been interrupted.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003910 */
3911
3912extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003913 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003914
3915/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003916 * @brief Initialize a poll signal object.
3917 *
3918 * Ready a poll signal object to be signaled via k_poll_signal().
3919 *
3920 * @param signal A poll signal.
3921 *
3922 * @return N/A
3923 */
3924
3925extern void k_poll_signal_init(struct k_poll_signal *signal);
3926
3927/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003928 * @brief Signal a poll signal object.
3929 *
3930 * This routine makes ready a poll signal, which is basically a poll event of
3931 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3932 * made ready to run. A @a result value can be specified.
3933 *
3934 * The poll signal contains a 'signaled' field that, when set by
3935 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3936 * be reset by the user before being passed again to k_poll() or k_poll() will
3937 * consider it being signaled, and will return immediately.
3938 *
3939 * @param signal A poll signal.
3940 * @param result The value to store in the result field of the signal.
3941 *
3942 * @retval 0 The signal was delivered successfully.
3943 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3944 */
3945
3946extern int k_poll_signal(struct k_poll_signal *signal, int result);
3947
Anas Nashif954d5502018-02-25 08:37:28 -06003948/**
3949 * @internal
3950 */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003951extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003952
Anas Nashif166f5192018-02-25 08:02:36 -06003953/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003954
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003955/**
3956 * @brief Make the CPU idle.
3957 *
3958 * This function makes the CPU idle until an event wakes it up.
3959 *
3960 * In a regular system, the idle thread should be the only thread responsible
3961 * for making the CPU idle and triggering any type of power management.
3962 * However, in some more constrained systems, such as a single-threaded system,
3963 * the only thread would be responsible for this if needed.
3964 *
3965 * @return N/A
3966 */
3967extern void k_cpu_idle(void);
3968
3969/**
3970 * @brief Make the CPU idle in an atomic fashion.
3971 *
3972 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3973 * must be done atomically before making the CPU idle.
3974 *
3975 * @param key Interrupt locking key obtained from irq_lock().
3976 *
3977 * @return N/A
3978 */
3979extern void k_cpu_atomic_idle(unsigned int key);
3980
Anas Nashif954d5502018-02-25 08:37:28 -06003981
3982/**
3983 * @internal
3984 */
Kumar Galacc334c72017-04-21 10:55:34 -05003985extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003986
Andrew Boiecdb94d62017-04-18 15:22:05 -07003987#ifdef _ARCH_EXCEPT
3988/* This archtecture has direct support for triggering a CPU exception */
3989#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3990#else
3991
Andrew Boiecdb94d62017-04-18 15:22:05 -07003992/* NOTE: This is the implementation for arches that do not implement
3993 * _ARCH_EXCEPT() to generate a real CPU exception.
3994 *
3995 * We won't have a real exception frame to determine the PC value when
3996 * the oops occurred, so print file and line number before we jump into
3997 * the fatal error handler.
3998 */
3999#define _k_except_reason(reason) do { \
4000 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4001 _NanoFatalErrorHandler(reason, &_default_esf); \
4002 CODE_UNREACHABLE; \
4003 } while (0)
4004
4005#endif /* _ARCH__EXCEPT */
4006
4007/**
4008 * @brief Fatally terminate a thread
4009 *
4010 * This should be called when a thread has encountered an unrecoverable
4011 * runtime condition and needs to terminate. What this ultimately
4012 * means is determined by the _fatal_error_handler() implementation, which
4013 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4014 *
4015 * If this is called from ISR context, the default system fatal error handler
4016 * will treat it as an unrecoverable system error, just like k_panic().
4017 */
4018#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4019
4020/**
4021 * @brief Fatally terminate the system
4022 *
4023 * This should be called when the Zephyr kernel has encountered an
4024 * unrecoverable runtime condition and needs to terminate. What this ultimately
4025 * means is determined by the _fatal_error_handler() implementation, which
4026 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4027 */
4028#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4029
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004030/*
4031 * private APIs that are utilized by one or more public APIs
4032 */
4033
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004034#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004035/**
4036 * @internal
4037 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004038extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004039#else
Anas Nashif954d5502018-02-25 08:37:28 -06004040/**
4041 * @internal
4042 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004043#define _init_static_threads() do { } while ((0))
4044#endif
4045
Anas Nashif954d5502018-02-25 08:37:28 -06004046/**
4047 * @internal
4048 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004049extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004050/**
4051 * @internal
4052 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004053extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004054
Andrew Boiedc5d9352017-06-02 12:56:47 -07004055/* arch/cpu.h may declare an architecture or platform-specific macro
4056 * for properly declaring stacks, compatible with MMU/MPU constraints if
4057 * enabled
4058 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004059
4060/**
4061 * @brief Obtain an extern reference to a stack
4062 *
4063 * This macro properly brings the symbol of a thread stack declared
4064 * elsewhere into scope.
4065 *
4066 * @param sym Thread stack symbol name
4067 */
4068#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4069
Andrew Boiedc5d9352017-06-02 12:56:47 -07004070#ifdef _ARCH_THREAD_STACK_DEFINE
4071#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4072#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4073 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4074#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4075#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004076static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004077{
4078 return _ARCH_THREAD_STACK_BUFFER(sym);
4079}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004080#else
4081/**
4082 * @brief Declare a toplevel thread stack memory region
4083 *
4084 * This declares a region of memory suitable for use as a thread's stack.
4085 *
4086 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4087 * 'noinit' section so that it isn't zeroed at boot
4088 *
Andrew Boie507852a2017-07-25 18:47:07 -07004089 * The declared symbol will always be a k_thread_stack_t which can be passed to
4090 * k_thread_create, but should otherwise not be manipulated. If the buffer
4091 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004092 *
4093 * It is legal to precede this definition with the 'static' keyword.
4094 *
4095 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4096 * parameter of k_thread_create(), it may not be the same as the
4097 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4098 *
4099 * @param sym Thread stack symbol name
4100 * @param size Size of the stack memory region
4101 */
4102#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004103 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004104
4105/**
4106 * @brief Declare a toplevel array of thread stack memory regions
4107 *
4108 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4109 * definition for additional details and constraints.
4110 *
4111 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4112 * 'noinit' section so that it isn't zeroed at boot
4113 *
4114 * @param sym Thread stack symbol name
4115 * @param nmemb Number of stacks to declare
4116 * @param size Size of the stack memory region
4117 */
4118
4119#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004120 struct _k_thread_stack_element __noinit \
4121 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004122
4123/**
4124 * @brief Declare an embedded stack memory region
4125 *
4126 * Used for stacks embedded within other data structures. Use is highly
4127 * discouraged but in some cases necessary. For memory protection scenarios,
4128 * it is very important that any RAM preceding this member not be writable
4129 * by threads else a stack overflow will lead to silent corruption. In other
4130 * words, the containing data structure should live in RAM owned by the kernel.
4131 *
4132 * @param sym Thread stack symbol name
4133 * @param size Size of the stack memory region
4134 */
4135#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004136 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004137
4138/**
4139 * @brief Return the size in bytes of a stack memory region
4140 *
4141 * Convenience macro for passing the desired stack size to k_thread_create()
4142 * since the underlying implementation may actually create something larger
4143 * (for instance a guard area).
4144 *
4145 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004146 * passed to K_THREAD_STACK_DEFINE.
4147 *
4148 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4149 * it is not guaranteed to return the original value since each array
4150 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004151 *
4152 * @param sym Stack memory symbol
4153 * @return Size of the stack
4154 */
4155#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4156
4157/**
4158 * @brief Get a pointer to the physical stack buffer
4159 *
4160 * Convenience macro to get at the real underlying stack buffer used by
4161 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4162 * This is really only intended for diagnostic tools which want to examine
4163 * stack memory contents.
4164 *
4165 * @param sym Declared stack symbol name
4166 * @return The buffer itself, a char *
4167 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004168static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004169{
4170 return (char *)sym;
4171}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004172
4173#endif /* _ARCH_DECLARE_STACK */
4174
Chunlin Hane9c97022017-07-07 20:29:30 +08004175/**
4176 * @defgroup mem_domain_apis Memory domain APIs
4177 * @ingroup kernel_apis
4178 * @{
4179 */
4180
4181/** @def MEM_PARTITION_ENTRY
4182 * @brief Used to declare a memory partition entry
4183 */
4184#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4185 {\
4186 .start = _start, \
4187 .size = _size, \
4188 .attr = _attr, \
4189 }
4190
4191/** @def K_MEM_PARTITION_DEFINE
4192 * @brief Used to declare a memory partition
4193 */
4194#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4195#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4196 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304197 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004198 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4199#else
4200#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
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#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4204
Chunlin Hane9c97022017-07-07 20:29:30 +08004205/* memory partition */
4206struct k_mem_partition {
4207 /* start address of memory partition */
4208 u32_t start;
4209 /* size of memory partition */
4210 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304211#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004212 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304213 k_mem_partition_attr_t attr;
4214#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004215};
4216
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304217/* memory domian
4218 * Note: Always declare this structure with __kernel prefix
4219 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004220struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304221#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004222 /* partitions in the domain */
4223 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304224#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004225 /* domain q */
4226 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004227 /* number of partitions in the domain */
4228 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004229};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304230
Chunlin Hane9c97022017-07-07 20:29:30 +08004231
4232/**
4233 * @brief Initialize a memory domain.
4234 *
4235 * Initialize a memory domain with given name and memory partitions.
4236 *
4237 * @param domain The memory domain to be initialized.
4238 * @param num_parts The number of array items of "parts" parameter.
4239 * @param parts An array of pointers to the memory partitions. Can be NULL
4240 * if num_parts is zero.
4241 */
4242
Leandro Pereira08de6582018-02-28 14:22:57 -08004243extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004244 struct k_mem_partition *parts[]);
4245/**
4246 * @brief Destroy a memory domain.
4247 *
4248 * Destroy a memory domain.
4249 *
4250 * @param domain The memory domain to be destroyed.
4251 */
4252
4253extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4254
4255/**
4256 * @brief Add a memory partition into a memory domain.
4257 *
4258 * Add a memory partition into a memory domain.
4259 *
4260 * @param domain The memory domain to be added a memory partition.
4261 * @param part The memory partition to be added
4262 */
4263
4264extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4265 struct k_mem_partition *part);
4266
4267/**
4268 * @brief Remove a memory partition from a memory domain.
4269 *
4270 * Remove a memory partition from a memory domain.
4271 *
4272 * @param domain The memory domain to be removed a memory partition.
4273 * @param part The memory partition to be removed
4274 */
4275
4276extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4277 struct k_mem_partition *part);
4278
4279/**
4280 * @brief Add a thread into a memory domain.
4281 *
4282 * Add a thread into a memory domain.
4283 *
4284 * @param domain The memory domain that the thread is going to be added into.
4285 * @param thread ID of thread going to be added into the memory domain.
4286 */
4287
4288extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4289 k_tid_t thread);
4290
4291/**
4292 * @brief Remove a thread from its memory domain.
4293 *
4294 * Remove a thread from its memory domain.
4295 *
4296 * @param thread ID of thread going to be removed from its memory domain.
4297 */
4298
4299extern void k_mem_domain_remove_thread(k_tid_t thread);
4300
Anas Nashif166f5192018-02-25 08:02:36 -06004301/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004302
Andrew Boie756f9072017-10-10 16:01:49 -07004303/**
4304 * @brief Emit a character buffer to the console device
4305 *
4306 * @param c String of characters to print
4307 * @param n The length of the string
4308 */
4309__syscall void k_str_out(char *c, size_t n);
4310
Andy Rosse7172672018-01-24 15:48:32 -08004311/**
4312 * @brief Start a numbered CPU on a MP-capable system
4313
4314 * This starts and initializes a specific CPU. The main thread on
4315 * startup is running on CPU zero, other processors are numbered
4316 * sequentially. On return from this function, the CPU is known to
4317 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004318 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004319 * with the provided key will work to enable them.
4320 *
4321 * Normally, in SMP mode this function will be called by the kernel
4322 * initialization and should not be used as a user API. But it is
4323 * defined here for special-purpose apps which want Zephyr running on
4324 * one core and to use others for design-specific processing.
4325 *
4326 * @param cpu_num Integer number of the CPU
4327 * @param stack Stack memory for the CPU
4328 * @param sz Stack buffer size, in bytes
4329 * @param fn Function to begin running on the CPU. First argument is
4330 * an irq_unlock() key.
4331 * @param arg Untyped argument to be passed to "fn"
4332 */
4333extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4334 void (*fn)(int, void *), void *arg);
4335
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004336#ifdef __cplusplus
4337}
4338#endif
4339
Andrew Boiee004dec2016-11-07 09:01:19 -08004340#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4341/*
4342 * Define new and delete operators.
4343 * At this moment, the operators do nothing since objects are supposed
4344 * to be statically allocated.
4345 */
4346inline void operator delete(void *ptr)
4347{
4348 (void)ptr;
4349}
4350
4351inline void operator delete[](void *ptr)
4352{
4353 (void)ptr;
4354}
4355
4356inline void *operator new(size_t size)
4357{
4358 (void)size;
4359 return NULL;
4360}
4361
4362inline void *operator new[](size_t size)
4363{
4364 (void)size;
4365 return NULL;
4366}
4367
4368/* Placement versions of operator new and delete */
4369inline void operator delete(void *ptr1, void *ptr2)
4370{
4371 (void)ptr1;
4372 (void)ptr2;
4373}
4374
4375inline void operator delete[](void *ptr1, void *ptr2)
4376{
4377 (void)ptr1;
4378 (void)ptr2;
4379}
4380
4381inline void *operator new(size_t size, void *ptr)
4382{
4383 (void)size;
4384 return ptr;
4385}
4386
4387inline void *operator new[](size_t size, void *ptr)
4388{
4389 (void)size;
4390 return ptr;
4391}
4392
4393#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4394
Andrew Boiefa94ee72017-09-28 16:54:35 -07004395#include <syscalls/kernel.h>
4396
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004397#endif /* !_ASMLANGUAGE */
4398
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004399#endif /* _kernel__h_ */