blob: 2bfee10d88bb6b0535c7041d82ca372609f025f5 [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 Boie31bdfc02017-11-08 16:38:03 -0800302#ifdef CONFIG_DYNAMIC_OBJECTS
303/**
304 * Allocate a kernel object of a designated type
305 *
306 * This will instantiate at runtime a kernel object of the specified type,
307 * returning a pointer to it. The object will be returned in an uninitialized
308 * state, with the calling thread being granted permission on it. The memory
309 * for the object will be allocated out of the kernel's heap.
310 *
311 * Currently, allocation of thread stacks is not supported.
312 *
313 * @param otype Requested kernel object type
314 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
315 * available
316 */
317void *k_object_alloc(enum k_objects otype);
318
319/**
320 * Free a kernel object previously allocated with k_object_alloc()
321 *
322 * This will return memory for a kernel object back to the system heap.
323 * Care must be exercised that the object will not be used during or after
324 * when this call is made.
325 *
326 * @param obj Pointer to the kernel object memory address.
327 */
328void k_object_free(void *obj);
329#endif /* CONFIG_DYNAMIC_OBJECTS */
330
Andrew Boiebca15da2017-10-15 14:17:48 -0700331/* Using typedef deliberately here, this is quite intended to be an opaque
332 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
333 *
334 * The purpose of this data type is to clearly distinguish between the
335 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
336 * buffer which composes the stack data actually used by the underlying
337 * thread; they cannot be used interchangably as some arches precede the
338 * stack buffer region with guard areas that trigger a MPU or MMU fault
339 * if written to.
340 *
341 * APIs that want to work with the buffer inside should continue to use
342 * char *.
343 *
344 * Stacks should always be created with K_THREAD_STACK_DEFINE().
345 */
346struct __packed _k_thread_stack_element {
347 char data;
348};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700349typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700350
Andrew Boie73abd322017-04-04 13:19:13 -0700351/* timeouts */
352
353struct _timeout;
354typedef void (*_timeout_func_t)(struct _timeout *t);
355
356struct _timeout {
357 sys_dnode_t node;
358 struct k_thread *thread;
359 sys_dlist_t *wait_q;
360 s32_t delta_ticks_from_prev;
361 _timeout_func_t func;
362};
363
364extern s32_t _timeout_remaining_get(struct _timeout *timeout);
365
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700366/**
367 * @typedef k_thread_entry_t
368 * @brief Thread entry point function type.
369 *
370 * A thread's entry point function is invoked when the thread starts executing.
371 * Up to 3 argument values can be passed to the function.
372 *
373 * The thread terminates execution permanently if the entry point function
374 * returns. The thread is responsible for releasing any shared resources
375 * it may own (such as mutexes and dynamically allocated memory), prior to
376 * returning.
377 *
378 * @param p1 First argument.
379 * @param p2 Second argument.
380 * @param p3 Third argument.
381 *
382 * @return N/A
383 */
384typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700385
386#ifdef CONFIG_THREAD_MONITOR
387struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700388 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700389 void *parameter1;
390 void *parameter2;
391 void *parameter3;
392};
393#endif
394
395/* can be used for creating 'dummy' threads, e.g. for pending on objects */
396struct _thread_base {
397
398 /* this thread's entry in a ready/wait queue */
399 sys_dnode_t k_q_node;
400
401 /* user facing 'thread options'; values defined in include/kernel.h */
402 u8_t user_options;
403
404 /* thread state */
405 u8_t thread_state;
406
407 /*
408 * scheduler lock count and thread priority
409 *
410 * These two fields control the preemptibility of a thread.
411 *
412 * When the scheduler is locked, sched_locked is decremented, which
413 * means that the scheduler is locked for values from 0xff to 0x01. A
414 * thread is coop if its prio is negative, thus 0x80 to 0xff when
415 * looked at the value as unsigned.
416 *
417 * By putting them end-to-end, this means that a thread is
418 * non-preemptible if the bundled value is greater than or equal to
419 * 0x0080.
420 */
421 union {
422 struct {
423#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
424 u8_t sched_locked;
425 s8_t prio;
426#else /* LITTLE and PDP */
427 s8_t prio;
428 u8_t sched_locked;
429#endif
430 };
431 u16_t preempt;
432 };
433
Andy Ross2724fd12018-01-29 14:55:20 -0800434#ifdef CONFIG_SMP
435 /* True for the per-CPU idle threads */
436 u8_t is_idle;
437
438 /* Non-zero when actively running on a CPU */
439 u8_t active;
440
441 /* CPU index on which thread was last run */
442 u8_t cpu;
443#endif
444
Andrew Boie73abd322017-04-04 13:19:13 -0700445 /* data returned by APIs */
446 void *swap_data;
447
448#ifdef CONFIG_SYS_CLOCK_EXISTS
449 /* this thread's entry in a timeout queue */
450 struct _timeout timeout;
451#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700452};
453
454typedef struct _thread_base _thread_base_t;
455
456#if defined(CONFIG_THREAD_STACK_INFO)
457/* Contains the stack information of a thread */
458struct _thread_stack_info {
459 /* Stack Start */
460 u32_t start;
461 /* Stack Size */
462 u32_t size;
463};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700464
465typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700466#endif /* CONFIG_THREAD_STACK_INFO */
467
Chunlin Hane9c97022017-07-07 20:29:30 +0800468#if defined(CONFIG_USERSPACE)
469struct _mem_domain_info {
470 /* memory domain queue node */
471 sys_dnode_t mem_domain_q_node;
472 /* memory domain of the thread */
473 struct k_mem_domain *mem_domain;
474};
475
476#endif /* CONFIG_USERSPACE */
477
Andrew Boie73abd322017-04-04 13:19:13 -0700478struct k_thread {
479
480 struct _thread_base base;
481
482 /* defined by the architecture, but all archs need these */
483 struct _caller_saved caller_saved;
484 struct _callee_saved callee_saved;
485
486 /* static thread init data */
487 void *init_data;
488
489 /* abort function */
490 void (*fn_abort)(void);
491
492#if defined(CONFIG_THREAD_MONITOR)
493 /* thread entry and parameters description */
494 struct __thread_entry *entry;
495
496 /* next item in list of all threads */
497 struct k_thread *next_thread;
498#endif
499
500#ifdef CONFIG_THREAD_CUSTOM_DATA
501 /* crude thread-local storage */
502 void *custom_data;
503#endif
504
505#ifdef CONFIG_ERRNO
506 /* per-thread errno variable */
507 int errno_var;
508#endif
509
510#if defined(CONFIG_THREAD_STACK_INFO)
511 /* Stack Info */
512 struct _thread_stack_info stack_info;
513#endif /* CONFIG_THREAD_STACK_INFO */
514
Chunlin Hane9c97022017-07-07 20:29:30 +0800515#if defined(CONFIG_USERSPACE)
516 /* memory domain info of the thread */
517 struct _mem_domain_info mem_domain_info;
Andrew Boiebca15da2017-10-15 14:17:48 -0700518 /* Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700519 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800520#endif /* CONFIG_USERSPACE */
521
Andy Ross042d8ec2017-12-09 08:37:20 -0800522#if defined(CONFIG_USE_SWITCH)
523 /* When using __switch() a few previously arch-specific items
524 * become part of the core OS
525 */
526
527 /* _Swap() return value */
528 int swap_retval;
529
530 /* Context handle returned via _arch_switch() */
531 void *switch_handle;
532#endif
533
Andrew Boie73abd322017-04-04 13:19:13 -0700534 /* arch-specifics: must always be at the end */
535 struct _thread_arch arch;
536};
537
538typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400539typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700540#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400541
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400542enum execution_context_types {
543 K_ISR = 0,
544 K_COOP_THREAD,
545 K_PREEMPT_THREAD,
546};
547
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400548/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100549 * @defgroup profiling_apis Profiling APIs
550 * @ingroup kernel_apis
551 * @{
552 */
553
554/**
555 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
556 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700557 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100558 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
559 *
560 * CONFIG_MAIN_STACK_SIZE
561 * CONFIG_IDLE_STACK_SIZE
562 * CONFIG_ISR_STACK_SIZE
563 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
564 *
565 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
566 * produce output.
567 *
568 * @return N/A
569 */
570extern void k_call_stacks_analyze(void);
571
Anas Nashif166f5192018-02-25 08:02:36 -0600572/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100573
574/**
Allan Stephensc98da842016-11-11 15:45:03 -0500575 * @defgroup thread_apis Thread APIs
576 * @ingroup kernel_apis
577 * @{
578 */
579
Benjamin Walshed240f22017-01-22 13:05:08 -0500580#endif /* !_ASMLANGUAGE */
581
582
583/*
584 * Thread user options. May be needed by assembly code. Common part uses low
585 * bits, arch-specific use high bits.
586 */
587
588/* system thread that must not abort */
589#define K_ESSENTIAL (1 << 0)
590
591#if defined(CONFIG_FP_SHARING)
592/* thread uses floating point registers */
593#define K_FP_REGS (1 << 1)
594#endif
595
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700596/* This thread has dropped from supervisor mode to user mode and consequently
597 * has additional restrictions
598 */
599#define K_USER (1 << 2)
600
Andrew Boie47f8fd12017-10-05 11:11:02 -0700601/* Indicates that the thread being created should inherit all kernel object
602 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
603 * is not enabled.
604 */
605#define K_INHERIT_PERMS (1 << 3)
606
Benjamin Walshed240f22017-01-22 13:05:08 -0500607#ifdef CONFIG_X86
608/* x86 Bitmask definitions for threads user options */
609
610#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
611/* thread uses SSEx (and also FP) registers */
612#define K_SSE_REGS (1 << 7)
613#endif
614#endif
615
616/* end - thread options */
617
618#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400619/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700620 * @brief Create a thread.
621 *
622 * This routine initializes a thread, then schedules it for execution.
623 *
624 * The new thread may be scheduled for immediate execution or a delayed start.
625 * If the newly spawned thread does not have a delayed start the kernel
626 * scheduler may preempt the current thread to allow the new thread to
627 * execute.
628 *
629 * Thread options are architecture-specific, and can include K_ESSENTIAL,
630 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
631 * them using "|" (the logical OR operator).
632 *
633 * Historically, users often would use the beginning of the stack memory region
634 * to store the struct k_thread data, although corruption will occur if the
635 * stack overflows this region and stack protection features may not detect this
636 * situation.
637 *
638 * @param new_thread Pointer to uninitialized struct k_thread
639 * @param stack Pointer to the stack space.
640 * @param stack_size Stack size in bytes.
641 * @param entry Thread entry function.
642 * @param p1 1st entry point parameter.
643 * @param p2 2nd entry point parameter.
644 * @param p3 3rd entry point parameter.
645 * @param prio Thread priority.
646 * @param options Thread options.
647 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
648 *
649 * @return ID of new thread.
650 */
Andrew Boie662c3452017-10-02 10:51:18 -0700651__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700652 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700653 size_t stack_size,
654 k_thread_entry_t entry,
655 void *p1, void *p2, void *p3,
656 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700657
Andrew Boie3f091b52017-08-30 14:34:14 -0700658/**
659 * @brief Drop a thread's privileges permanently to user mode
660 *
661 * @param entry Function to start executing from
662 * @param p1 1st entry point parameter
663 * @param p2 2nd entry point parameter
664 * @param p3 3rd entry point parameter
665 */
666extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
667 void *p1, void *p2,
668 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700669
Andrew Boied26cf2d2017-03-30 13:07:02 -0700670/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700671 * @brief Grant a thread access to a NULL-terminated set of kernel objects
672 *
673 * This is a convenience function. For the provided thread, grant access to
674 * the remaining arguments, which must be pointers to kernel objects.
675 * The final argument must be a NULL.
676 *
677 * The thread object must be initialized (i.e. running). The objects don't
678 * need to be.
679 *
680 * @param thread Thread to grant access to objects
681 * @param ... NULL-terminated list of kernel object pointers
682 */
683extern void __attribute__((sentinel))
684 k_thread_access_grant(struct k_thread *thread, ...);
685
686/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500687 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400688 *
Allan Stephensc98da842016-11-11 15:45:03 -0500689 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500690 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400691 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500692 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400693 *
694 * @return N/A
695 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700696__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400697
698/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500699 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400700 *
701 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500702 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400703 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400704 * @return N/A
705 */
Kumar Galacc334c72017-04-21 10:55:34 -0500706extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400707
708/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500709 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400710 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500711 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400712 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500713 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400714 *
715 * @return N/A
716 */
Andrew Boie468190a2017-09-29 14:00:48 -0700717__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400718
719/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500720 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500722 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400723 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500724 * If @a thread is not currently sleeping, the routine has no effect.
725 *
726 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400727 *
728 * @return N/A
729 */
Andrew Boie468190a2017-09-29 14:00:48 -0700730__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400731
732/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500733 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400734 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500735 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400736 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700737__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400738
739/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500740 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400741 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500742 * This routine prevents @a thread from executing if it has not yet started
743 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400744 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500745 * @param thread ID of thread to cancel.
746 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700747 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500748 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700749 *
750 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400751 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700752__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400753
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400754/**
Allan Stephensc98da842016-11-11 15:45:03 -0500755 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400756 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500757 * This routine permanently stops execution of @a thread. The thread is taken
758 * off all kernel queues it is part of (i.e. the ready queue, the timeout
759 * queue, or a kernel object wait queue). However, any kernel resources the
760 * thread might currently own (such as mutexes or memory blocks) are not
761 * released. It is the responsibility of the caller of this routine to ensure
762 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400763 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500764 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400765 *
766 * @return N/A
767 */
Andrew Boie468190a2017-09-29 14:00:48 -0700768__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400769
Andrew Boie7d627c52017-08-30 11:01:56 -0700770
771/**
772 * @brief Start an inactive thread
773 *
774 * If a thread was created with K_FOREVER in the delay parameter, it will
775 * not be added to the scheduling queue until this function is called
776 * on it.
777 *
778 * @param thread thread to start
779 */
Andrew Boie468190a2017-09-29 14:00:48 -0700780__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700781
Allan Stephensc98da842016-11-11 15:45:03 -0500782/**
783 * @cond INTERNAL_HIDDEN
784 */
785
Benjamin Walshd211a522016-12-06 11:44:01 -0500786/* timeout has timed out and is not on _timeout_q anymore */
787#define _EXPIRED (-2)
788
789/* timeout is not in use */
790#define _INACTIVE (-1)
791
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400792struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700793 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700794 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400795 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700796 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500797 void *init_p1;
798 void *init_p2;
799 void *init_p3;
800 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500801 u32_t init_options;
802 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500803 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400804};
805
Andrew Boied26cf2d2017-03-30 13:07:02 -0700806#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400807 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500808 prio, options, delay, abort, groups) \
809 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700810 .init_thread = (thread), \
811 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500812 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700813 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400814 .init_p1 = (void *)p1, \
815 .init_p2 = (void *)p2, \
816 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500817 .init_prio = (prio), \
818 .init_options = (options), \
819 .init_delay = (delay), \
820 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400821 }
822
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400823/**
Allan Stephensc98da842016-11-11 15:45:03 -0500824 * INTERNAL_HIDDEN @endcond
825 */
826
827/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500828 * @brief Statically define and initialize a thread.
829 *
830 * The thread may be scheduled for immediate execution or a delayed start.
831 *
832 * Thread options are architecture-specific, and can include K_ESSENTIAL,
833 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
834 * them using "|" (the logical OR operator).
835 *
836 * The ID of the thread can be accessed using:
837 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500838 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500839 *
840 * @param name Name of the thread.
841 * @param stack_size Stack size in bytes.
842 * @param entry Thread entry function.
843 * @param p1 1st entry point parameter.
844 * @param p2 2nd entry point parameter.
845 * @param p3 3rd entry point parameter.
846 * @param prio Thread priority.
847 * @param options Thread options.
848 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400849 *
850 * @internal It has been observed that the x86 compiler by default aligns
851 * these _static_thread_data structures to 32-byte boundaries, thereby
852 * wasting space. To work around this, force a 4-byte alignment.
853 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500854#define K_THREAD_DEFINE(name, stack_size, \
855 entry, p1, p2, p3, \
856 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700857 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700858 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500859 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500860 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700861 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
862 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500863 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700864 NULL, 0); \
865 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400866
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400867/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500868 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400869 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500870 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400871 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500872 * @param thread ID of thread whose priority is needed.
873 *
874 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400875 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700876__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400877
878/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500879 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500881 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400882 *
883 * Rescheduling can occur immediately depending on the priority @a thread is
884 * set to:
885 *
886 * - If its priority is raised above the priority of the caller of this
887 * function, and the caller is preemptible, @a thread will be scheduled in.
888 *
889 * - If the caller operates on itself, it lowers its priority below that of
890 * other threads in the system, and the caller is preemptible, the thread of
891 * highest priority will be scheduled in.
892 *
893 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
894 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
895 * highest priority.
896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500897 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400898 * @param prio New priority.
899 *
900 * @warning Changing the priority of a thread currently involved in mutex
901 * priority inheritance may result in undefined behavior.
902 *
903 * @return N/A
904 */
Andrew Boie468190a2017-09-29 14:00:48 -0700905__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400906
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400907/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500908 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400909 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500910 * This routine prevents the kernel scheduler from making @a thread the
911 * current thread. All other internal operations on @a thread are still
912 * performed; for example, any timeout it is waiting on keeps ticking,
913 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400914 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500915 * If @a thread is already suspended, the routine has no effect.
916 *
917 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400918 *
919 * @return N/A
920 */
Andrew Boie468190a2017-09-29 14:00:48 -0700921__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400922
923/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500924 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400925 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500926 * This routine allows the kernel scheduler to make @a thread the current
927 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400928 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500929 * If @a thread is not currently suspended, the routine has no effect.
930 *
931 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400932 *
933 * @return N/A
934 */
Andrew Boie468190a2017-09-29 14:00:48 -0700935__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400936
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400937/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500938 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400939 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500940 * This routine specifies how the scheduler will perform time slicing of
941 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400942 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500943 * To enable time slicing, @a slice must be non-zero. The scheduler
944 * ensures that no thread runs for more than the specified time limit
945 * before other threads of that priority are given a chance to execute.
946 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700947 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400948 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500949 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400950 * execute. Once the scheduler selects a thread for execution, there is no
951 * minimum guaranteed time the thread will execute before threads of greater or
952 * equal priority are scheduled.
953 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500954 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400955 * for execution, this routine has no effect; the thread is immediately
956 * rescheduled after the slice period expires.
957 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500958 * To disable timeslicing, set both @a slice and @a prio to zero.
959 *
960 * @param slice Maximum time slice length (in milliseconds).
961 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400962 *
963 * @return N/A
964 */
Kumar Galacc334c72017-04-21 10:55:34 -0500965extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400966
Anas Nashif166f5192018-02-25 08:02:36 -0600967/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -0500968
969/**
970 * @addtogroup isr_apis
971 * @{
972 */
973
974/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500975 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400976 *
Allan Stephensc98da842016-11-11 15:45:03 -0500977 * This routine allows the caller to customize its actions, depending on
978 * whether it is a thread or an ISR.
979 *
980 * @note Can be called by ISRs.
981 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500982 * @return 0 if invoked by a thread.
983 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400984 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500985extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400986
Benjamin Walsh445830d2016-11-10 15:54:27 -0500987/**
988 * @brief Determine if code is running in a preemptible thread.
989 *
Allan Stephensc98da842016-11-11 15:45:03 -0500990 * This routine allows the caller to customize its actions, depending on
991 * whether it can be preempted by another thread. The routine returns a 'true'
992 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500993 *
Allan Stephensc98da842016-11-11 15:45:03 -0500994 * - The code is running in a thread, not at ISR.
995 * - The thread's priority is in the preemptible range.
996 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500997 *
Allan Stephensc98da842016-11-11 15:45:03 -0500998 * @note Can be called by ISRs.
999 *
1000 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001001 * @return Non-zero if invoked by a preemptible thread.
1002 */
Andrew Boie468190a2017-09-29 14:00:48 -07001003__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001004
Allan Stephensc98da842016-11-11 15:45:03 -05001005/**
Anas Nashif166f5192018-02-25 08:02:36 -06001006 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001007 */
1008
1009/**
1010 * @addtogroup thread_apis
1011 * @{
1012 */
1013
1014/**
1015 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001016 *
Allan Stephensc98da842016-11-11 15:45:03 -05001017 * This routine prevents the current thread from being preempted by another
1018 * thread by instructing the scheduler to treat it as a cooperative thread.
1019 * If the thread subsequently performs an operation that makes it unready,
1020 * it will be context switched out in the normal manner. When the thread
1021 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001022 *
Allan Stephensc98da842016-11-11 15:45:03 -05001023 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001024 *
Allan Stephensc98da842016-11-11 15:45:03 -05001025 * @note k_sched_lock() and k_sched_unlock() should normally be used
1026 * when the operation being performed can be safely interrupted by ISRs.
1027 * However, if the amount of processing involved is very small, better
1028 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001029 *
1030 * @return N/A
1031 */
1032extern void k_sched_lock(void);
1033
Allan Stephensc98da842016-11-11 15:45:03 -05001034/**
1035 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001036 *
Allan Stephensc98da842016-11-11 15:45:03 -05001037 * This routine reverses the effect of a previous call to k_sched_lock().
1038 * A thread must call the routine once for each time it called k_sched_lock()
1039 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001040 *
1041 * @return N/A
1042 */
1043extern void k_sched_unlock(void);
1044
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001045/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001046 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001047 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001048 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001049 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001050 * Custom data is not used by the kernel itself, and is freely available
1051 * for a thread to use as it sees fit. It can be used as a framework
1052 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001053 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001054 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001055 *
1056 * @return N/A
1057 */
Andrew Boie468190a2017-09-29 14:00:48 -07001058__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001059
1060/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001061 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001062 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001063 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001064 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001065 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001066 */
Andrew Boie468190a2017-09-29 14:00:48 -07001067__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001068
1069/**
Anas Nashif166f5192018-02-25 08:02:36 -06001070 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001071 */
1072
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001073#include <sys_clock.h>
1074
Allan Stephensc2f15a42016-11-17 12:24:22 -05001075/**
1076 * @addtogroup clock_apis
1077 * @{
1078 */
1079
1080/**
1081 * @brief Generate null timeout delay.
1082 *
1083 * This macro generates a timeout delay that that instructs a kernel API
1084 * not to wait if the requested operation cannot be performed immediately.
1085 *
1086 * @return Timeout delay value.
1087 */
1088#define K_NO_WAIT 0
1089
1090/**
1091 * @brief Generate timeout delay from milliseconds.
1092 *
1093 * This macro generates a timeout delay that that instructs a kernel API
1094 * to wait up to @a ms milliseconds to perform the requested operation.
1095 *
1096 * @param ms Duration in milliseconds.
1097 *
1098 * @return Timeout delay value.
1099 */
Johan Hedberg14471692016-11-13 10:52:15 +02001100#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001101
1102/**
1103 * @brief Generate timeout delay from seconds.
1104 *
1105 * This macro generates a timeout delay that that instructs a kernel API
1106 * to wait up to @a s seconds to perform the requested operation.
1107 *
1108 * @param s Duration in seconds.
1109 *
1110 * @return Timeout delay value.
1111 */
Johan Hedberg14471692016-11-13 10:52:15 +02001112#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001113
1114/**
1115 * @brief Generate timeout delay from minutes.
1116 *
1117 * This macro generates a timeout delay that that instructs a kernel API
1118 * to wait up to @a m minutes to perform the requested operation.
1119 *
1120 * @param m Duration in minutes.
1121 *
1122 * @return Timeout delay value.
1123 */
Johan Hedberg14471692016-11-13 10:52:15 +02001124#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001125
1126/**
1127 * @brief Generate timeout delay from hours.
1128 *
1129 * This macro generates a timeout delay that that instructs a kernel API
1130 * to wait up to @a h hours to perform the requested operation.
1131 *
1132 * @param h Duration in hours.
1133 *
1134 * @return Timeout delay value.
1135 */
Johan Hedberg14471692016-11-13 10:52:15 +02001136#define K_HOURS(h) K_MINUTES((h) * 60)
1137
Allan Stephensc98da842016-11-11 15:45:03 -05001138/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001139 * @brief Generate infinite timeout delay.
1140 *
1141 * This macro generates a timeout delay that that instructs a kernel API
1142 * to wait as long as necessary to perform the requested operation.
1143 *
1144 * @return Timeout delay value.
1145 */
1146#define K_FOREVER (-1)
1147
1148/**
Anas Nashif166f5192018-02-25 08:02:36 -06001149 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001150 */
1151
1152/**
Allan Stephensc98da842016-11-11 15:45:03 -05001153 * @cond INTERNAL_HIDDEN
1154 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001155
Benjamin Walsh62092182016-12-20 14:39:08 -05001156/* kernel clocks */
1157
1158#if (sys_clock_ticks_per_sec == 1000) || \
1159 (sys_clock_ticks_per_sec == 500) || \
1160 (sys_clock_ticks_per_sec == 250) || \
1161 (sys_clock_ticks_per_sec == 125) || \
1162 (sys_clock_ticks_per_sec == 100) || \
1163 (sys_clock_ticks_per_sec == 50) || \
1164 (sys_clock_ticks_per_sec == 25) || \
1165 (sys_clock_ticks_per_sec == 20) || \
1166 (sys_clock_ticks_per_sec == 10) || \
1167 (sys_clock_ticks_per_sec == 1)
1168
1169 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1170#else
1171 /* yields horrible 64-bit math on many architectures: try to avoid */
1172 #define _NON_OPTIMIZED_TICKS_PER_SEC
1173#endif
1174
1175#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001176extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001177#else
Kumar Galacc334c72017-04-21 10:55:34 -05001178static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001179{
Kumar Galacc334c72017-04-21 10:55:34 -05001180 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001181}
1182#endif
1183
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001184/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001185#ifdef CONFIG_TICKLESS_KERNEL
1186#define _TICK_ALIGN 0
1187#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001188#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001189#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001190
Kumar Galacc334c72017-04-21 10:55:34 -05001191static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001192{
Benjamin Walsh62092182016-12-20 14:39:08 -05001193#ifdef CONFIG_SYS_CLOCK_EXISTS
1194
1195#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001196 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001197#else
Kumar Galacc334c72017-04-21 10:55:34 -05001198 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001199#endif
1200
1201#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001202 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001203 return 0;
1204#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001205}
1206
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001207struct k_timer {
1208 /*
1209 * _timeout structure must be first here if we want to use
1210 * dynamic timer allocation. timeout.node is used in the double-linked
1211 * list of free timers
1212 */
1213 struct _timeout timeout;
1214
Allan Stephens45bfa372016-10-12 12:39:42 -05001215 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001216 _wait_q_t wait_q;
1217
1218 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001219 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001220
1221 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001222 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001223
1224 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001225 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001226
Allan Stephens45bfa372016-10-12 12:39:42 -05001227 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001228 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001229
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001230 /* user-specific data, also used to support legacy features */
1231 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001232
Anas Nashif2f203c22016-12-18 06:57:45 -05001233 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001234};
1235
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001236#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001237 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001238 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001239 .timeout.wait_q = NULL, \
1240 .timeout.thread = NULL, \
1241 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001242 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001243 .expiry_fn = expiry, \
1244 .stop_fn = stop, \
1245 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001246 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001247 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001248 }
1249
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001250#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1251
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001252/**
Allan Stephensc98da842016-11-11 15:45:03 -05001253 * INTERNAL_HIDDEN @endcond
1254 */
1255
1256/**
1257 * @defgroup timer_apis Timer APIs
1258 * @ingroup kernel_apis
1259 * @{
1260 */
1261
1262/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001263 * @typedef k_timer_expiry_t
1264 * @brief Timer expiry function type.
1265 *
1266 * A timer's expiry function is executed by the system clock interrupt handler
1267 * each time the timer expires. The expiry function is optional, and is only
1268 * invoked if the timer has been initialized with one.
1269 *
1270 * @param timer Address of timer.
1271 *
1272 * @return N/A
1273 */
1274typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1275
1276/**
1277 * @typedef k_timer_stop_t
1278 * @brief Timer stop function type.
1279 *
1280 * A timer's stop function is executed if the timer is stopped prematurely.
1281 * The function runs in the context of the thread that stops the timer.
1282 * The stop function is optional, and is only invoked if the timer has been
1283 * initialized with one.
1284 *
1285 * @param timer Address of timer.
1286 *
1287 * @return N/A
1288 */
1289typedef void (*k_timer_stop_t)(struct k_timer *timer);
1290
1291/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001292 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001293 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001294 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001295 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001296 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001297 *
1298 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001299 * @param expiry_fn Function to invoke each time the timer expires.
1300 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001301 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001302#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001303 struct k_timer name \
1304 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001305 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001306
Allan Stephens45bfa372016-10-12 12:39:42 -05001307/**
1308 * @brief Initialize a timer.
1309 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001310 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001311 *
1312 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001313 * @param expiry_fn Function to invoke each time the timer expires.
1314 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001315 *
1316 * @return N/A
1317 */
1318extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001319 k_timer_expiry_t expiry_fn,
1320 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001321
Allan Stephens45bfa372016-10-12 12:39:42 -05001322/**
1323 * @brief Start a timer.
1324 *
1325 * This routine starts a timer, and resets its status to zero. The timer
1326 * begins counting down using the specified duration and period values.
1327 *
1328 * Attempting to start a timer that is already running is permitted.
1329 * The timer's status is reset to zero and the timer begins counting down
1330 * using the new duration and period values.
1331 *
1332 * @param timer Address of timer.
1333 * @param duration Initial timer duration (in milliseconds).
1334 * @param period Timer period (in milliseconds).
1335 *
1336 * @return N/A
1337 */
Andrew Boiea354d492017-09-29 16:22:28 -07001338__syscall void k_timer_start(struct k_timer *timer,
1339 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001340
1341/**
1342 * @brief Stop a timer.
1343 *
1344 * This routine stops a running timer prematurely. The timer's stop function,
1345 * if one exists, is invoked by the caller.
1346 *
1347 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001348 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001349 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001350 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1351 * if @a k_timer_stop is to be called from ISRs.
1352 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001353 * @param timer Address of timer.
1354 *
1355 * @return N/A
1356 */
Andrew Boiea354d492017-09-29 16:22:28 -07001357__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001358
1359/**
1360 * @brief Read timer status.
1361 *
1362 * This routine reads the timer's status, which indicates the number of times
1363 * it has expired since its status was last read.
1364 *
1365 * Calling this routine resets the timer's status to zero.
1366 *
1367 * @param timer Address of timer.
1368 *
1369 * @return Timer status.
1370 */
Andrew Boiea354d492017-09-29 16:22:28 -07001371__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001372
1373/**
1374 * @brief Synchronize thread to timer expiration.
1375 *
1376 * This routine blocks the calling thread until the timer's status is non-zero
1377 * (indicating that it has expired at least once since it was last examined)
1378 * or the timer is stopped. If the timer status is already non-zero,
1379 * or the timer is already stopped, the caller continues without waiting.
1380 *
1381 * Calling this routine resets the timer's status to zero.
1382 *
1383 * This routine must not be used by interrupt handlers, since they are not
1384 * allowed to block.
1385 *
1386 * @param timer Address of timer.
1387 *
1388 * @return Timer status.
1389 */
Andrew Boiea354d492017-09-29 16:22:28 -07001390__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001391
1392/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001393 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001394 *
1395 * This routine computes the (approximate) time remaining before a running
1396 * timer next expires. If the timer is not running, it returns zero.
1397 *
1398 * @param timer Address of timer.
1399 *
1400 * @return Remaining time (in milliseconds).
1401 */
Andrew Boiea354d492017-09-29 16:22:28 -07001402__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1403
1404static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001405{
1406 return _timeout_remaining_get(&timer->timeout);
1407}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001408
Allan Stephensc98da842016-11-11 15:45:03 -05001409/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001410 * @brief Associate user-specific data with a timer.
1411 *
1412 * This routine records the @a user_data with the @a timer, to be retrieved
1413 * later.
1414 *
1415 * It can be used e.g. in a timer handler shared across multiple subsystems to
1416 * retrieve data specific to the subsystem this timer is associated with.
1417 *
1418 * @param timer Address of timer.
1419 * @param user_data User data to associate with the timer.
1420 *
1421 * @return N/A
1422 */
Andrew Boiea354d492017-09-29 16:22:28 -07001423__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1424
Anas Nashif954d5502018-02-25 08:37:28 -06001425/**
1426 * @internal
1427 */
Andrew Boiea354d492017-09-29 16:22:28 -07001428static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1429 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001430{
1431 timer->user_data = user_data;
1432}
1433
1434/**
1435 * @brief Retrieve the user-specific data from a timer.
1436 *
1437 * @param timer Address of timer.
1438 *
1439 * @return The user data.
1440 */
Andrew Boiea354d492017-09-29 16:22:28 -07001441__syscall void *k_timer_user_data_get(struct k_timer *timer);
1442
1443static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001444{
1445 return timer->user_data;
1446}
1447
Anas Nashif166f5192018-02-25 08:02:36 -06001448/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001449
Allan Stephensc98da842016-11-11 15:45:03 -05001450/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001451 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001452 * @{
1453 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001454
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001455/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001456 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001457 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001458 * This routine returns the elapsed time since the system booted,
1459 * in milliseconds.
1460 *
1461 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001462 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001463__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001464
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001465/**
1466 * @brief Enable clock always on in tickless kernel
1467 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001468 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001469 * there are no timer events programmed in tickless kernel
1470 * scheduling. This is necessary if the clock is used to track
1471 * passage of time.
1472 *
1473 * @retval prev_status Previous status of always on flag
1474 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301475#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001476static inline int k_enable_sys_clock_always_on(void)
1477{
1478 int prev_status = _sys_clock_always_on;
1479
1480 _sys_clock_always_on = 1;
1481 _enable_sys_clock();
1482
1483 return prev_status;
1484}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301485#else
1486#define k_enable_sys_clock_always_on() do { } while ((0))
1487#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001488
1489/**
1490 * @brief Disable clock always on in tickless kernel
1491 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001492 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001493 * there are no timer events programmed in tickless kernel
1494 * scheduling. To save power, this routine should be called
1495 * immediately when clock is not used to track time.
1496 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301497#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001498static inline void k_disable_sys_clock_always_on(void)
1499{
1500 _sys_clock_always_on = 0;
1501}
1502#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001503#define k_disable_sys_clock_always_on() do { } while ((0))
1504#endif
1505
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001506/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001507 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001508 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001509 * This routine returns the lower 32-bits of the elapsed time since the system
1510 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001511 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001512 * This routine can be more efficient than k_uptime_get(), as it reduces the
1513 * need for interrupt locking and 64-bit math. However, the 32-bit result
1514 * cannot hold a system uptime time larger than approximately 50 days, so the
1515 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001516 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001517 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001518 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001519__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001520
1521/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001522 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001523 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001524 * This routine computes the elapsed time between the current system uptime
1525 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001526 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001527 * @param reftime Pointer to a reference time, which is updated to the current
1528 * uptime upon return.
1529 *
1530 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001531 */
Kumar Galacc334c72017-04-21 10:55:34 -05001532extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001533
1534/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001535 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001536 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001537 * This routine computes the elapsed time between the current system uptime
1538 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001539 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001540 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1541 * need for interrupt locking and 64-bit math. However, the 32-bit result
1542 * cannot hold an elapsed time larger than approximately 50 days, so the
1543 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001544 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001545 * @param reftime Pointer to a reference time, which is updated to the current
1546 * uptime upon return.
1547 *
1548 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001549 */
Kumar Galacc334c72017-04-21 10:55:34 -05001550extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001551
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001552/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001553 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001554 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001555 * This routine returns the current time, as measured by the system's hardware
1556 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001557 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001558 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001559 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001560#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001561
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001562/**
Anas Nashif166f5192018-02-25 08:02:36 -06001563 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001564 */
1565
Allan Stephensc98da842016-11-11 15:45:03 -05001566/**
1567 * @cond INTERNAL_HIDDEN
1568 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001569
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001570struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001571 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001572 union {
1573 _wait_q_t wait_q;
1574
1575 _POLL_EVENT;
1576 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001577
1578 _OBJECT_TRACING_NEXT_PTR(k_queue);
1579};
1580
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001581#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001582 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001583 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Michael Hope5f67a612018-03-17 12:44:40 +01001584 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001585 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001586 _OBJECT_TRACING_INIT \
1587 }
1588
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001589#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1590
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001591/**
1592 * INTERNAL_HIDDEN @endcond
1593 */
1594
1595/**
1596 * @defgroup queue_apis Queue APIs
1597 * @ingroup kernel_apis
1598 * @{
1599 */
1600
1601/**
1602 * @brief Initialize a queue.
1603 *
1604 * This routine initializes a queue object, prior to its first use.
1605 *
1606 * @param queue Address of the queue.
1607 *
1608 * @return N/A
1609 */
1610extern void k_queue_init(struct k_queue *queue);
1611
1612/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001613 * @brief Cancel waiting on a queue.
1614 *
1615 * This routine causes first thread pending on @a queue, if any, to
1616 * return from k_queue_get() call with NULL value (as if timeout expired).
1617 *
1618 * @note Can be called by ISRs.
1619 *
1620 * @param queue Address of the queue.
1621 *
1622 * @return N/A
1623 */
1624extern void k_queue_cancel_wait(struct k_queue *queue);
1625
1626/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001627 * @brief Append an element to the end of a queue.
1628 *
1629 * This routine appends a data item to @a queue. A queue data item must be
1630 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1631 * reserved for the kernel's use.
1632 *
1633 * @note Can be called by ISRs.
1634 *
1635 * @param queue Address of the queue.
1636 * @param data Address of the data item.
1637 *
1638 * @return N/A
1639 */
1640extern void k_queue_append(struct k_queue *queue, void *data);
1641
1642/**
1643 * @brief Prepend an element to a queue.
1644 *
1645 * This routine prepends a data item to @a queue. A queue data item must be
1646 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1647 * reserved for the kernel's use.
1648 *
1649 * @note Can be called by ISRs.
1650 *
1651 * @param queue Address of the queue.
1652 * @param data Address of the data item.
1653 *
1654 * @return N/A
1655 */
1656extern void k_queue_prepend(struct k_queue *queue, void *data);
1657
1658/**
1659 * @brief Inserts an element to a queue.
1660 *
1661 * This routine inserts a data item to @a queue after previous item. A queue
1662 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1663 * item are reserved for the kernel's use.
1664 *
1665 * @note Can be called by ISRs.
1666 *
1667 * @param queue Address of the queue.
1668 * @param prev Address of the previous data item.
1669 * @param data Address of the data item.
1670 *
1671 * @return N/A
1672 */
1673extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1674
1675/**
1676 * @brief Atomically append a list of elements to a queue.
1677 *
1678 * This routine adds a list of data items to @a queue in one operation.
1679 * The data items must be in a singly-linked list, with the first 32 bits
1680 * in each data item pointing to the next data item; the list must be
1681 * NULL-terminated.
1682 *
1683 * @note Can be called by ISRs.
1684 *
1685 * @param queue Address of the queue.
1686 * @param head Pointer to first node in singly-linked list.
1687 * @param tail Pointer to last node in singly-linked list.
1688 *
1689 * @return N/A
1690 */
1691extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1692
1693/**
1694 * @brief Atomically add a list of elements to a queue.
1695 *
1696 * This routine adds a list of data items to @a queue in one operation.
1697 * The data items must be in a singly-linked list implemented using a
1698 * sys_slist_t object. Upon completion, the original list is empty.
1699 *
1700 * @note Can be called by ISRs.
1701 *
1702 * @param queue Address of the queue.
1703 * @param list Pointer to sys_slist_t object.
1704 *
1705 * @return N/A
1706 */
1707extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1708
1709/**
1710 * @brief Get an element from a queue.
1711 *
1712 * This routine removes first data item from @a queue. The first 32 bits of the
1713 * data item are reserved for the kernel's use.
1714 *
1715 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1716 *
1717 * @param queue Address of the queue.
1718 * @param timeout Waiting period to obtain a data item (in milliseconds),
1719 * or one of the special values K_NO_WAIT and K_FOREVER.
1720 *
1721 * @return Address of the data item if successful; NULL if returned
1722 * without waiting, or waiting period timed out.
1723 */
Kumar Galacc334c72017-04-21 10:55:34 -05001724extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001725
1726/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001727 * @brief Remove an element from a queue.
1728 *
1729 * This routine removes data item from @a queue. The first 32 bits of the
1730 * data item are reserved for the kernel's use. Removing elements from k_queue
1731 * rely on sys_slist_find_and_remove which is not a constant time operation.
1732 *
1733 * @note Can be called by ISRs
1734 *
1735 * @param queue Address of the queue.
1736 * @param data Address of the data item.
1737 *
1738 * @return true if data item was removed
1739 */
1740static inline bool k_queue_remove(struct k_queue *queue, void *data)
1741{
1742 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1743}
1744
1745/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001746 * @brief Query a queue to see if it has data available.
1747 *
1748 * Note that the data might be already gone by the time this function returns
1749 * if other threads are also trying to read from the queue.
1750 *
1751 * @note Can be called by ISRs.
1752 *
1753 * @param queue Address of the queue.
1754 *
1755 * @return Non-zero if the queue is empty.
1756 * @return 0 if data is available.
1757 */
1758static inline int k_queue_is_empty(struct k_queue *queue)
1759{
1760 return (int)sys_slist_is_empty(&queue->data_q);
1761}
1762
1763/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001764 * @brief Peek element at the head of queue.
1765 *
1766 * Return element from the head of queue without removing it.
1767 *
1768 * @param queue Address of the queue.
1769 *
1770 * @return Head element, or NULL if queue is empty.
1771 */
1772static inline void *k_queue_peek_head(struct k_queue *queue)
1773{
1774 return sys_slist_peek_head(&queue->data_q);
1775}
1776
1777/**
1778 * @brief Peek element at the tail of queue.
1779 *
1780 * Return element from the tail of queue without removing it.
1781 *
1782 * @param queue Address of the queue.
1783 *
1784 * @return Tail element, or NULL if queue is empty.
1785 */
1786static inline void *k_queue_peek_tail(struct k_queue *queue)
1787{
1788 return sys_slist_peek_tail(&queue->data_q);
1789}
1790
1791/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001792 * @brief Statically define and initialize a queue.
1793 *
1794 * The queue can be accessed outside the module where it is defined using:
1795 *
1796 * @code extern struct k_queue <name>; @endcode
1797 *
1798 * @param name Name of the queue.
1799 */
1800#define K_QUEUE_DEFINE(name) \
1801 struct k_queue name \
1802 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001803 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001804
Anas Nashif166f5192018-02-25 08:02:36 -06001805/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001806
1807/**
1808 * @cond INTERNAL_HIDDEN
1809 */
1810
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001811struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001812 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001813};
1814
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001815#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001816 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001817 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001818 }
1819
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001820#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1821
Allan Stephensc98da842016-11-11 15:45:03 -05001822/**
1823 * INTERNAL_HIDDEN @endcond
1824 */
1825
1826/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001827 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05001828 * @ingroup kernel_apis
1829 * @{
1830 */
1831
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001832/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001833 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001834 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001835 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001836 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001837 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001838 *
1839 * @return N/A
1840 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001841#define k_fifo_init(fifo) \
1842 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001843
1844/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001845 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001846 *
1847 * This routine causes first thread pending on @a fifo, if any, to
1848 * return from k_fifo_get() call with NULL value (as if timeout
1849 * expired).
1850 *
1851 * @note Can be called by ISRs.
1852 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001853 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001854 *
1855 * @return N/A
1856 */
1857#define k_fifo_cancel_wait(fifo) \
1858 k_queue_cancel_wait((struct k_queue *) fifo)
1859
1860/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001861 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001862 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001863 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001864 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1865 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001867 * @note Can be called by ISRs.
1868 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001869 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001870 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001871 *
1872 * @return N/A
1873 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001874#define k_fifo_put(fifo, data) \
1875 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001876
1877/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001878 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001879 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001880 * This routine adds a list of data items to @a fifo in one operation.
1881 * The data items must be in a singly-linked list, with the first 32 bits
1882 * each data item pointing to the next data item; the list must be
1883 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001884 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001885 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001886 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001887 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001888 * @param head Pointer to first node in singly-linked list.
1889 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001890 *
1891 * @return N/A
1892 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001893#define k_fifo_put_list(fifo, head, tail) \
1894 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001895
1896/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001897 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001898 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001899 * This routine adds a list of data items to @a fifo in one operation.
1900 * The data items must be in a singly-linked list implemented using a
1901 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001902 * and must be re-initialized via sys_slist_init().
1903 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001904 * @note Can be called by ISRs.
1905 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001906 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001907 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001908 *
1909 * @return N/A
1910 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001911#define k_fifo_put_slist(fifo, list) \
1912 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001913
1914/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001915 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001917 * This routine removes a data item from @a fifo in a "first in, first out"
1918 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001919 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001920 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1921 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001922 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001923 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001924 * or one of the special values K_NO_WAIT and K_FOREVER.
1925 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001926 * @return Address of the data item if successful; NULL if returned
1927 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001928 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001929#define k_fifo_get(fifo, timeout) \
1930 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001931
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001932/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001933 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001934 *
1935 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06001936 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001937 *
1938 * @note Can be called by ISRs.
1939 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001940 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001941 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001942 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001943 * @return 0 if data is available.
1944 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001945#define k_fifo_is_empty(fifo) \
1946 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001947
1948/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001949 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001950 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001951 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05301952 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001953 * on each iteration of processing, a head container will be peeked,
1954 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06001955 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001956 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001957 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001958 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001959 * @return Head element, or NULL if the FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001960 */
1961#define k_fifo_peek_head(fifo) \
1962 k_queue_peek_head((struct k_queue *) fifo)
1963
1964/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001965 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001966 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001967 * Return element from the tail of FIFO queue (without removing it). A usecase
1968 * for this is if elements of the FIFO queue are themselves containers. Then
1969 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001970 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001971 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001972 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001973 * @return Tail element, or NULL if a FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001974 */
1975#define k_fifo_peek_tail(fifo) \
1976 k_queue_peek_tail((struct k_queue *) fifo)
1977
1978/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001979 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001980 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001981 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001982 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001983 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001984 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001985 * @param name Name of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001986 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001987#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001988 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001989 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001990 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001991
Anas Nashif166f5192018-02-25 08:02:36 -06001992/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001993
1994/**
1995 * @cond INTERNAL_HIDDEN
1996 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001997
1998struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001999 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002000};
2001
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002002#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002003 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002004 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002005 }
2006
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002007#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2008
Allan Stephensc98da842016-11-11 15:45:03 -05002009/**
2010 * INTERNAL_HIDDEN @endcond
2011 */
2012
2013/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002014 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002015 * @ingroup kernel_apis
2016 * @{
2017 */
2018
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002019/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002020 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002021 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002022 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002023 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002024 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002025 *
2026 * @return N/A
2027 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002028#define k_lifo_init(lifo) \
2029 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002030
2031/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002032 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002033 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002034 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002035 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2036 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002037 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002038 * @note Can be called by ISRs.
2039 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002040 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002041 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002042 *
2043 * @return N/A
2044 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002045#define k_lifo_put(lifo, data) \
2046 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002047
2048/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002049 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002050 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002051 * This routine removes a data item from @a lifo in a "last in, first out"
2052 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002053 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002054 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2055 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002056 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002057 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002058 * or one of the special values K_NO_WAIT and K_FOREVER.
2059 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002060 * @return Address of the data item if successful; NULL if returned
2061 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002062 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002063#define k_lifo_get(lifo, timeout) \
2064 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002065
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002066/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002067 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002068 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002069 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002070 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002071 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002072 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002073 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002074 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002075#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002076 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002077 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002078 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002079
Anas Nashif166f5192018-02-25 08:02:36 -06002080/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002081
2082/**
2083 * @cond INTERNAL_HIDDEN
2084 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002085
2086struct k_stack {
2087 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002088 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002089
Anas Nashif2f203c22016-12-18 06:57:45 -05002090 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002091};
2092
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002093#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002094 { \
2095 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2096 .base = stack_buffer, \
2097 .next = stack_buffer, \
2098 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002099 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002100 }
2101
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002102#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2103
Allan Stephensc98da842016-11-11 15:45:03 -05002104/**
2105 * INTERNAL_HIDDEN @endcond
2106 */
2107
2108/**
2109 * @defgroup stack_apis Stack APIs
2110 * @ingroup kernel_apis
2111 * @{
2112 */
2113
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002114/**
2115 * @brief Initialize a stack.
2116 *
2117 * This routine initializes a stack object, prior to its first use.
2118 *
2119 * @param stack Address of the stack.
2120 * @param buffer Address of array used to hold stacked values.
2121 * @param num_entries Maximum number of values that can be stacked.
2122 *
2123 * @return N/A
2124 */
Andrew Boiee8734462017-09-29 16:42:07 -07002125__syscall void k_stack_init(struct k_stack *stack,
Andrew Boie8e3e6d02017-10-12 14:01:25 -07002126 u32_t *buffer, unsigned int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002127
2128/**
2129 * @brief Push an element onto a stack.
2130 *
2131 * This routine adds a 32-bit value @a data to @a stack.
2132 *
2133 * @note Can be called by ISRs.
2134 *
2135 * @param stack Address of the stack.
2136 * @param data Value to push onto the stack.
2137 *
2138 * @return N/A
2139 */
Andrew Boiee8734462017-09-29 16:42:07 -07002140__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002141
2142/**
2143 * @brief Pop an element from a stack.
2144 *
2145 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2146 * manner and stores the value in @a data.
2147 *
2148 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2149 *
2150 * @param stack Address of the stack.
2151 * @param data Address of area to hold the value popped from the stack.
2152 * @param timeout Waiting period to obtain a value (in milliseconds),
2153 * or one of the special values K_NO_WAIT and K_FOREVER.
2154 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002155 * @retval 0 Element popped from stack.
2156 * @retval -EBUSY Returned without waiting.
2157 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002158 */
Andrew Boiee8734462017-09-29 16:42:07 -07002159__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002160
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002161/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002162 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002163 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002164 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002165 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002166 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002167 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002168 * @param name Name of the stack.
2169 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002170 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002171#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002172 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002173 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002174 struct k_stack name \
2175 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002176 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002177 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002178
Anas Nashif166f5192018-02-25 08:02:36 -06002179/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002180
Allan Stephens6bba9b02016-11-16 14:56:54 -05002181struct k_work;
2182
Allan Stephensc98da842016-11-11 15:45:03 -05002183/**
2184 * @defgroup workqueue_apis Workqueue Thread APIs
2185 * @ingroup kernel_apis
2186 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002187 */
2188
Allan Stephens6bba9b02016-11-16 14:56:54 -05002189/**
2190 * @typedef k_work_handler_t
2191 * @brief Work item handler function type.
2192 *
2193 * A work item's handler function is executed by a workqueue's thread
2194 * when the work item is processed by the workqueue.
2195 *
2196 * @param work Address of the work item.
2197 *
2198 * @return N/A
2199 */
2200typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002201
2202/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002203 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002204 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002205
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002206struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002207 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002208 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002209};
2210
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002211enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002212 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002213};
2214
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002215struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002216 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002217 k_work_handler_t handler;
2218 atomic_t flags[1];
2219};
2220
Allan Stephens6bba9b02016-11-16 14:56:54 -05002221struct k_delayed_work {
2222 struct k_work work;
2223 struct _timeout timeout;
2224 struct k_work_q *work_q;
2225};
2226
2227extern struct k_work_q k_sys_work_q;
2228
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002229/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002230 * INTERNAL_HIDDEN @endcond
2231 */
2232
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002233#define _K_WORK_INITIALIZER(work_handler) \
2234 { \
2235 ._reserved = NULL, \
2236 .handler = work_handler, \
2237 .flags = { 0 } \
2238 }
2239
2240#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2241
Allan Stephens6bba9b02016-11-16 14:56:54 -05002242/**
2243 * @brief Initialize a statically-defined work item.
2244 *
2245 * This macro can be used to initialize a statically-defined workqueue work
2246 * item, prior to its first use. For example,
2247 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002248 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002249 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002250 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002251 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002252 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002253#define K_WORK_DEFINE(work, work_handler) \
2254 struct k_work work \
2255 __in_section(_k_work, static, work) = \
2256 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002257
2258/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002259 * @brief Initialize a work item.
2260 *
2261 * This routine initializes a workqueue work item, prior to its first use.
2262 *
2263 * @param work Address of work item.
2264 * @param handler Function to invoke each time work item is processed.
2265 *
2266 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002267 */
2268static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2269{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002270 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002271 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002272 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002273}
2274
2275/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002276 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002277 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002278 * This routine submits work item @a work to be processed by workqueue
2279 * @a work_q. If the work item is already pending in the workqueue's queue
2280 * as a result of an earlier submission, this routine has no effect on the
2281 * work item. If the work item has already been processed, or is currently
2282 * being processed, its work is considered complete and the work item can be
2283 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002284 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002285 * @warning
2286 * A submitted work item must not be modified until it has been processed
2287 * by the workqueue.
2288 *
2289 * @note Can be called by ISRs.
2290 *
2291 * @param work_q Address of workqueue.
2292 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002293 *
2294 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002295 */
2296static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2297 struct k_work *work)
2298{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002299 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002300 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002301 }
2302}
2303
2304/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002305 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002306 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002307 * This routine indicates if work item @a work is pending in a workqueue's
2308 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002309 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002310 * @note Can be called by ISRs.
2311 *
2312 * @param work Address of work item.
2313 *
2314 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002315 */
2316static inline int k_work_pending(struct k_work *work)
2317{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002318 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002319}
2320
2321/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002322 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002323 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002324 * This routine starts workqueue @a work_q. The workqueue spawns its work
2325 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002326 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002327 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002328 * @param stack Pointer to work queue thread's stack space, as defined by
2329 * K_THREAD_STACK_DEFINE()
2330 * @param stack_size Size of the work queue thread's stack (in bytes), which
2331 * should either be the same constant passed to
2332 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002333 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002334 *
2335 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002336 */
Andrew Boie507852a2017-07-25 18:47:07 -07002337extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002338 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002339 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002340
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002341/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002342 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002343 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002344 * This routine initializes a workqueue delayed work item, prior to
2345 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002346 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002347 * @param work Address of delayed work item.
2348 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002349 *
2350 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002351 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002352extern void k_delayed_work_init(struct k_delayed_work *work,
2353 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002354
2355/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002356 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002357 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002358 * This routine schedules work item @a work to be processed by workqueue
2359 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002360 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002361 * Only when the countdown completes is the work item actually submitted to
2362 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002363 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002364 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002365 * counting down cancels the existing submission and restarts the
2366 * countdown using the new delay. Note that this behavior is
2367 * inherently subject to race conditions with the pre-existing
2368 * timeouts and work queue, so care must be taken to synchronize such
2369 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002370 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002371 * @warning
2372 * A delayed work item must not be modified until it has been processed
2373 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002374 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002375 * @note Can be called by ISRs.
2376 *
2377 * @param work_q Address of workqueue.
2378 * @param work Address of delayed work item.
2379 * @param delay Delay before submitting the work item (in milliseconds).
2380 *
2381 * @retval 0 Work item countdown started.
2382 * @retval -EINPROGRESS Work item is already pending.
2383 * @retval -EINVAL Work item is being processed or has completed its work.
2384 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002385 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002386extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2387 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002388 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002389
2390/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002391 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002392 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002393 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002394 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002395 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002396 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002397 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002398 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002399 * @param work Address of delayed work item.
2400 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002401 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002402 * @retval -EINPROGRESS Work item is already pending.
2403 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002404 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002405extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002406
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002407/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002408 * @brief Submit a work item to the system workqueue.
2409 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002410 * This routine submits work item @a work to be processed by the system
2411 * workqueue. If the work item is already pending in the workqueue's queue
2412 * as a result of an earlier submission, this routine has no effect on the
2413 * work item. If the work item has already been processed, or is currently
2414 * being processed, its work is considered complete and the work item can be
2415 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002416 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002417 * @warning
2418 * Work items submitted to the system workqueue should avoid using handlers
2419 * that block or yield since this may prevent the system workqueue from
2420 * processing other work items in a timely manner.
2421 *
2422 * @note Can be called by ISRs.
2423 *
2424 * @param work Address of work item.
2425 *
2426 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002427 */
2428static inline void k_work_submit(struct k_work *work)
2429{
2430 k_work_submit_to_queue(&k_sys_work_q, work);
2431}
2432
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002433/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002434 * @brief Submit a delayed work item to the system workqueue.
2435 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002436 * This routine schedules work item @a work to be processed by the system
2437 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002438 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002439 * Only when the countdown completes is the work item actually submitted to
2440 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002441 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002442 * Submitting a previously submitted delayed work item that is still
2443 * counting down cancels the existing submission and restarts the countdown
2444 * using the new delay. If the work item is currently pending on the
2445 * workqueue's queue because the countdown has completed it is too late to
2446 * resubmit the item, and resubmission fails without impacting the work item.
2447 * If the work item has already been processed, or is currently being processed,
2448 * its work is considered complete and the work item can be resubmitted.
2449 *
2450 * @warning
2451 * Work items submitted to the system workqueue should avoid using handlers
2452 * that block or yield since this may prevent the system workqueue from
2453 * processing other work items in a timely manner.
2454 *
2455 * @note Can be called by ISRs.
2456 *
2457 * @param work Address of delayed work item.
2458 * @param delay Delay before submitting the work item (in milliseconds).
2459 *
2460 * @retval 0 Work item countdown started.
2461 * @retval -EINPROGRESS Work item is already pending.
2462 * @retval -EINVAL Work item is being processed or has completed its work.
2463 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002464 */
2465static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002466 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002467{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002468 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002469}
2470
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002471/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002472 * @brief Get time remaining before a delayed work gets scheduled.
2473 *
2474 * This routine computes the (approximate) time remaining before a
2475 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002476 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002477 *
2478 * @param work Delayed work item.
2479 *
2480 * @return Remaining time (in milliseconds).
2481 */
Kumar Galacc334c72017-04-21 10:55:34 -05002482static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002483{
2484 return _timeout_remaining_get(&work->timeout);
2485}
2486
Anas Nashif166f5192018-02-25 08:02:36 -06002487/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002488
Allan Stephensc98da842016-11-11 15:45:03 -05002489/**
2490 * @cond INTERNAL_HIDDEN
2491 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002492
2493struct k_mutex {
2494 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002495 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002496 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002497 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002498
Anas Nashif2f203c22016-12-18 06:57:45 -05002499 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002500};
2501
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002502#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002503 { \
2504 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2505 .owner = NULL, \
2506 .lock_count = 0, \
2507 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002508 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002509 }
2510
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002511#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2512
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002513/**
Allan Stephensc98da842016-11-11 15:45:03 -05002514 * INTERNAL_HIDDEN @endcond
2515 */
2516
2517/**
2518 * @defgroup mutex_apis Mutex APIs
2519 * @ingroup kernel_apis
2520 * @{
2521 */
2522
2523/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002524 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002525 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002526 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002527 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002528 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002529 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002530 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002531 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002532#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002533 struct k_mutex name \
2534 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002535 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002536
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002537/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002538 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002539 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002540 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002541 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002542 * Upon completion, the mutex is available and does not have an owner.
2543 *
2544 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002545 *
2546 * @return N/A
2547 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002548__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002549
2550/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002551 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002552 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002553 * This routine locks @a mutex. If the mutex is locked by another thread,
2554 * the calling thread waits until the mutex becomes available or until
2555 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002556 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002557 * A thread is permitted to lock a mutex it has already locked. The operation
2558 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002559 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002560 * @param mutex Address of the mutex.
2561 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002562 * or one of the special values K_NO_WAIT and K_FOREVER.
2563 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002564 * @retval 0 Mutex locked.
2565 * @retval -EBUSY Returned without waiting.
2566 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002567 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002568__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002569
2570/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002571 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002572 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002573 * This routine unlocks @a mutex. The mutex must already be locked by the
2574 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002575 *
2576 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002577 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002578 * thread.
2579 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002580 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002581 *
2582 * @return N/A
2583 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002584__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002585
Allan Stephensc98da842016-11-11 15:45:03 -05002586/**
Anas Nashif166f5192018-02-25 08:02:36 -06002587 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002588 */
2589
2590/**
2591 * @cond INTERNAL_HIDDEN
2592 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002593
2594struct k_sem {
2595 _wait_q_t wait_q;
2596 unsigned int count;
2597 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002598 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002599
Anas Nashif2f203c22016-12-18 06:57:45 -05002600 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002601};
2602
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002603#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002604 { \
2605 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2606 .count = initial_count, \
2607 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002608 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002609 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002610 }
2611
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002612#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2613
Allan Stephensc98da842016-11-11 15:45:03 -05002614/**
2615 * INTERNAL_HIDDEN @endcond
2616 */
2617
2618/**
2619 * @defgroup semaphore_apis Semaphore APIs
2620 * @ingroup kernel_apis
2621 * @{
2622 */
2623
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002624/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002625 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002626 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002627 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002628 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002629 * @param sem Address of the semaphore.
2630 * @param initial_count Initial semaphore count.
2631 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002632 *
2633 * @return N/A
2634 */
Andrew Boie99280232017-09-29 14:17:47 -07002635__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2636 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002637
2638/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002639 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002640 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002641 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002642 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002643 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2644 *
2645 * @param sem Address of the semaphore.
2646 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002647 * or one of the special values K_NO_WAIT and K_FOREVER.
2648 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002649 * @note When porting code from the nanokernel legacy API to the new API, be
2650 * careful with the return value of this function. The return value is the
2651 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2652 * non-zero means failure, while the nano_sem_take family returns 1 for success
2653 * and 0 for failure.
2654 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002655 * @retval 0 Semaphore taken.
2656 * @retval -EBUSY Returned without waiting.
2657 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002658 */
Andrew Boie99280232017-09-29 14:17:47 -07002659__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002660
2661/**
2662 * @brief Give a semaphore.
2663 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002664 * This routine gives @a sem, unless the semaphore is already at its maximum
2665 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002666 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002667 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002668 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002669 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002670 *
2671 * @return N/A
2672 */
Andrew Boie99280232017-09-29 14:17:47 -07002673__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002674
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002675/**
2676 * @brief Reset a semaphore's count to zero.
2677 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002678 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002679 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002680 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002681 *
2682 * @return N/A
2683 */
Andrew Boie990bf162017-10-03 12:36:49 -07002684__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002685
Anas Nashif954d5502018-02-25 08:37:28 -06002686/**
2687 * @internal
2688 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002689static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002690{
2691 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002692}
2693
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002694/**
2695 * @brief Get a semaphore's count.
2696 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002697 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002698 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002699 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002700 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002701 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002702 */
Andrew Boie990bf162017-10-03 12:36:49 -07002703__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002704
Anas Nashif954d5502018-02-25 08:37:28 -06002705/**
2706 * @internal
2707 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002708static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002709{
2710 return sem->count;
2711}
2712
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002713/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002714 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002716 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002717 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002718 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002719 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002720 * @param name Name of the semaphore.
2721 * @param initial_count Initial semaphore count.
2722 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002723 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002724#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002725 struct k_sem name \
2726 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07002727 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
2728 BUILD_ASSERT((count_limit) != 0); \
2729 BUILD_ASSERT((initial_count) <= (count_limit));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002730
Anas Nashif166f5192018-02-25 08:02:36 -06002731/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002732
2733/**
2734 * @defgroup alert_apis Alert APIs
2735 * @ingroup kernel_apis
2736 * @{
2737 */
2738
Allan Stephens5eceb852016-11-16 10:16:30 -05002739/**
2740 * @typedef k_alert_handler_t
2741 * @brief Alert handler function type.
2742 *
2743 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002744 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002745 * and is only invoked if the alert has been initialized with one.
2746 *
2747 * @param alert Address of the alert.
2748 *
2749 * @return 0 if alert has been consumed; non-zero if alert should pend.
2750 */
2751typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002752
Anas Nashif166f5192018-02-25 08:02:36 -06002753/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002754
2755/**
2756 * @cond INTERNAL_HIDDEN
2757 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002758
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002759#define K_ALERT_DEFAULT NULL
2760#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002761
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002762struct k_alert {
2763 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002764 atomic_t send_count;
2765 struct k_work work_item;
2766 struct k_sem sem;
2767
Anas Nashif2f203c22016-12-18 06:57:45 -05002768 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002769};
2770
Anas Nashif954d5502018-02-25 08:37:28 -06002771/**
2772 * @internal
2773 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002774extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002775
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002776#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002777 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002778 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002779 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002780 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2781 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002782 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002783 }
2784
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002785#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2786
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002787/**
Allan Stephensc98da842016-11-11 15:45:03 -05002788 * INTERNAL_HIDDEN @endcond
2789 */
2790
2791/**
2792 * @addtogroup alert_apis
2793 * @{
2794 */
2795
2796/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002797 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002798 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002799 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002800 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002801 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002802 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002803 * @param name Name of the alert.
2804 * @param alert_handler Action to take when alert is sent. Specify either
2805 * the address of a function to be invoked by the system workqueue
2806 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2807 * K_ALERT_DEFAULT (which causes the alert to pend).
2808 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002809 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002810#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002811 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002812 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002813 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002814 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002815
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002816/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002817 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002818 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002819 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002820 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002821 * @param alert Address of the alert.
2822 * @param handler Action to take when alert is sent. Specify either the address
2823 * of a function to be invoked by the system workqueue thread,
2824 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2825 * K_ALERT_DEFAULT (which causes the alert to pend).
2826 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002827 *
2828 * @return N/A
2829 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002830extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2831 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002832
2833/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002834 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002835 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002836 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002837 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002838 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2839 *
2840 * @param alert Address of the alert.
2841 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002842 * or one of the special values K_NO_WAIT and K_FOREVER.
2843 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002844 * @retval 0 Alert received.
2845 * @retval -EBUSY Returned without waiting.
2846 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002847 */
Andrew Boie310e9872017-09-29 04:41:15 -07002848__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002849
2850/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002851 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002852 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002853 * This routine signals @a alert. The action specified for @a alert will
2854 * be taken, which may trigger the execution of an alert handler function
2855 * and/or cause the alert to pend (assuming the alert has not reached its
2856 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002857 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002858 * @note Can be called by ISRs.
2859 *
2860 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002861 *
2862 * @return N/A
2863 */
Andrew Boie310e9872017-09-29 04:41:15 -07002864__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002865
2866/**
Anas Nashif166f5192018-02-25 08:02:36 -06002867 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002868 */
2869
Allan Stephensc98da842016-11-11 15:45:03 -05002870/**
2871 * @cond INTERNAL_HIDDEN
2872 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002873
2874struct k_msgq {
2875 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002876 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002877 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002878 char *buffer_start;
2879 char *buffer_end;
2880 char *read_ptr;
2881 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002882 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002883
Anas Nashif2f203c22016-12-18 06:57:45 -05002884 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002885};
2886
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002887#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002888 { \
2889 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002890 .max_msgs = q_max_msgs, \
2891 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002892 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002893 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002894 .read_ptr = q_buffer, \
2895 .write_ptr = q_buffer, \
2896 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002897 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002898 }
2899
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002900#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2901
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05302902struct k_msgq_attrs {
2903 size_t msg_size;
2904 u32_t max_msgs;
2905 u32_t used_msgs;
2906};
2907
Peter Mitsis1da807e2016-10-06 11:36:59 -04002908/**
Allan Stephensc98da842016-11-11 15:45:03 -05002909 * INTERNAL_HIDDEN @endcond
2910 */
2911
2912/**
2913 * @defgroup msgq_apis Message Queue APIs
2914 * @ingroup kernel_apis
2915 * @{
2916 */
2917
2918/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002919 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002920 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002921 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2922 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002923 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2924 * message is similarly aligned to this boundary, @a q_msg_size must also be
2925 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002926 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002927 * The message queue can be accessed outside the module where it is defined
2928 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002929 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002930 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002931 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002932 * @param q_name Name of the message queue.
2933 * @param q_msg_size Message size (in bytes).
2934 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002935 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002936 */
2937#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2938 static char __noinit __aligned(q_align) \
2939 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002940 struct k_msgq q_name \
2941 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002942 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002943 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002944
Peter Mitsisd7a37502016-10-13 11:37:40 -04002945/**
2946 * @brief Initialize a message queue.
2947 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002948 * This routine initializes a message queue object, prior to its first use.
2949 *
Allan Stephensda827222016-11-09 14:23:58 -06002950 * The message queue's ring buffer must contain space for @a max_msgs messages,
2951 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2952 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2953 * that each message is similarly aligned to this boundary, @a q_msg_size
2954 * must also be a multiple of N.
2955 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002956 * @param q Address of the message queue.
2957 * @param buffer Pointer to ring buffer that holds queued messages.
2958 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002959 * @param max_msgs Maximum number of messages that can be queued.
2960 *
2961 * @return N/A
2962 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002963__syscall void k_msgq_init(struct k_msgq *q, char *buffer,
2964 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002965
2966/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002967 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002968 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002969 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002970 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002971 * @note Can be called by ISRs.
2972 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002973 * @param q Address of the message queue.
2974 * @param data Pointer to the message.
2975 * @param timeout Waiting period to add the message (in milliseconds),
2976 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002977 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002978 * @retval 0 Message sent.
2979 * @retval -ENOMSG Returned without waiting or queue purged.
2980 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002981 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002982__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002983
2984/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002985 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002986 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002987 * This routine receives a message from message queue @a q in a "first in,
2988 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002989 *
Allan Stephensc98da842016-11-11 15:45:03 -05002990 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002991 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002992 * @param q Address of the message queue.
2993 * @param data Address of area to hold the received message.
2994 * @param timeout Waiting period to receive the message (in milliseconds),
2995 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002996 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002997 * @retval 0 Message received.
2998 * @retval -ENOMSG Returned without waiting.
2999 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003000 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003001__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003002
3003/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003004 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003005 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003006 * This routine discards all unreceived messages in a message queue's ring
3007 * buffer. Any threads that are blocked waiting to send a message to the
3008 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003009 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003010 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003011 *
3012 * @return N/A
3013 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003014__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003015
Peter Mitsis67be2492016-10-07 11:44:34 -04003016/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003017 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003018 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003019 * This routine returns the number of unused entries in a message queue's
3020 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003021 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003022 * @param q Address of the message queue.
3023 *
3024 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04003025 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003026__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3027
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303028/**
3029 * @brief Get basic attributes of a message queue.
3030 *
3031 * This routine fetches basic attributes of message queue into attr argument.
3032 *
3033 * @param q Address of the message queue.
3034 * @param attrs pointer to message queue attribute structure.
3035 *
3036 * @return N/A
3037 */
3038__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3039
3040
Andrew Boie82edb6e2017-10-02 10:53:06 -07003041static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003042{
3043 return q->max_msgs - q->used_msgs;
3044}
3045
Peter Mitsisd7a37502016-10-13 11:37:40 -04003046/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003047 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003048 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003049 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003050 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003051 * @param q Address of the message queue.
3052 *
3053 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003054 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003055__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3056
3057static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003058{
3059 return q->used_msgs;
3060}
3061
Anas Nashif166f5192018-02-25 08:02:36 -06003062/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003063
3064/**
3065 * @defgroup mem_pool_apis Memory Pool APIs
3066 * @ingroup kernel_apis
3067 * @{
3068 */
3069
Andy Ross73cb9582017-05-09 10:42:39 -07003070/* Note on sizing: the use of a 20 bit field for block means that,
3071 * assuming a reasonable minimum block size of 16 bytes, we're limited
3072 * to 16M of memory managed by a single pool. Long term it would be
3073 * good to move to a variable bit size based on configuration.
3074 */
3075struct k_mem_block_id {
3076 u32_t pool : 8;
3077 u32_t level : 4;
3078 u32_t block : 20;
3079};
3080
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003081struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003082 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003083 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003084};
3085
Anas Nashif166f5192018-02-25 08:02:36 -06003086/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003087
3088/**
3089 * @defgroup mailbox_apis Mailbox APIs
3090 * @ingroup kernel_apis
3091 * @{
3092 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003093
3094struct k_mbox_msg {
3095 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003096 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003097 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003098 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003099 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003100 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003101 /** sender's message data buffer */
3102 void *tx_data;
3103 /** internal use only - needed for legacy API support */
3104 void *_rx_data;
3105 /** message data block descriptor */
3106 struct k_mem_block tx_block;
3107 /** source thread id */
3108 k_tid_t rx_source_thread;
3109 /** target thread id */
3110 k_tid_t tx_target_thread;
3111 /** internal use only - thread waiting on send (may be a dummy) */
3112 k_tid_t _syncing_thread;
3113#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3114 /** internal use only - semaphore used during asynchronous send */
3115 struct k_sem *_async_sem;
3116#endif
3117};
3118
Allan Stephensc98da842016-11-11 15:45:03 -05003119/**
3120 * @cond INTERNAL_HIDDEN
3121 */
3122
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003123struct k_mbox {
3124 _wait_q_t tx_msg_queue;
3125 _wait_q_t rx_msg_queue;
3126
Anas Nashif2f203c22016-12-18 06:57:45 -05003127 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003128};
3129
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003130#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003131 { \
3132 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3133 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003134 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003135 }
3136
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003137#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3138
Peter Mitsis12092702016-10-14 12:57:23 -04003139/**
Allan Stephensc98da842016-11-11 15:45:03 -05003140 * INTERNAL_HIDDEN @endcond
3141 */
3142
3143/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003144 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003145 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003146 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003147 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003148 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003149 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003150 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003151 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003152#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003153 struct k_mbox name \
3154 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003155 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003156
Peter Mitsis12092702016-10-14 12:57:23 -04003157/**
3158 * @brief Initialize a mailbox.
3159 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003160 * This routine initializes a mailbox object, prior to its first use.
3161 *
3162 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003163 *
3164 * @return N/A
3165 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003166extern void k_mbox_init(struct k_mbox *mbox);
3167
Peter Mitsis12092702016-10-14 12:57:23 -04003168/**
3169 * @brief Send a mailbox message in a synchronous manner.
3170 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003171 * This routine sends a message to @a mbox and waits for a receiver to both
3172 * receive and process it. The message data may be in a buffer, in a memory
3173 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003174 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003175 * @param mbox Address of the mailbox.
3176 * @param tx_msg Address of the transmit message descriptor.
3177 * @param timeout Waiting period for the message to be received (in
3178 * milliseconds), or one of the special values K_NO_WAIT
3179 * and K_FOREVER. Once the message has been received,
3180 * this routine waits as long as necessary for the message
3181 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003182 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003183 * @retval 0 Message sent.
3184 * @retval -ENOMSG Returned without waiting.
3185 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003186 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003187extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003188 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003189
Peter Mitsis12092702016-10-14 12:57:23 -04003190/**
3191 * @brief Send a mailbox message in an asynchronous manner.
3192 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003193 * This routine sends a message to @a mbox without waiting for a receiver
3194 * to process it. The message data may be in a buffer, in a memory pool block,
3195 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3196 * will be given when the message has been both received and completely
3197 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003198 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003199 * @param mbox Address of the mailbox.
3200 * @param tx_msg Address of the transmit message descriptor.
3201 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003202 *
3203 * @return N/A
3204 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003205extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003206 struct k_sem *sem);
3207
Peter Mitsis12092702016-10-14 12:57:23 -04003208/**
3209 * @brief Receive a mailbox message.
3210 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003211 * This routine receives a message from @a mbox, then optionally retrieves
3212 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003213 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003214 * @param mbox Address of the mailbox.
3215 * @param rx_msg Address of the receive message descriptor.
3216 * @param buffer Address of the buffer to receive data, or NULL to defer data
3217 * retrieval and message disposal until later.
3218 * @param timeout Waiting period for a message to be received (in
3219 * milliseconds), or one of the special values K_NO_WAIT
3220 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003221 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003222 * @retval 0 Message received.
3223 * @retval -ENOMSG Returned without waiting.
3224 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003225 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003226extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003227 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003228
3229/**
3230 * @brief Retrieve mailbox message data into a buffer.
3231 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003232 * This routine completes the processing of a received message by retrieving
3233 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003234 *
3235 * Alternatively, this routine can be used to dispose of a received message
3236 * without retrieving its data.
3237 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003238 * @param rx_msg Address of the receive message descriptor.
3239 * @param buffer Address of the buffer to receive data, or NULL to discard
3240 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003241 *
3242 * @return N/A
3243 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003244extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003245
3246/**
3247 * @brief Retrieve mailbox message data into a memory pool block.
3248 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003249 * This routine completes the processing of a received message by retrieving
3250 * its data into a memory pool block, then disposing of the message.
3251 * The memory pool block that results from successful retrieval must be
3252 * returned to the pool once the data has been processed, even in cases
3253 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003254 *
3255 * Alternatively, this routine can be used to dispose of a received message
3256 * without retrieving its data. In this case there is no need to return a
3257 * memory pool block to the pool.
3258 *
3259 * This routine allocates a new memory pool block for the data only if the
3260 * data is not already in one. If a new block cannot be allocated, the routine
3261 * returns a failure code and the received message is left unchanged. This
3262 * permits the caller to reattempt data retrieval at a later time or to dispose
3263 * of the received message without retrieving its data.
3264 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003265 * @param rx_msg Address of a receive message descriptor.
3266 * @param pool Address of memory pool, or NULL to discard data.
3267 * @param block Address of the area to hold memory pool block info.
3268 * @param timeout Waiting period to wait for a memory pool block (in
3269 * milliseconds), or one of the special values K_NO_WAIT
3270 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003271 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003272 * @retval 0 Data retrieved.
3273 * @retval -ENOMEM Returned without waiting.
3274 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003275 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003276extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003277 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003278 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003279
Anas Nashif166f5192018-02-25 08:02:36 -06003280/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003281
3282/**
3283 * @cond INTERNAL_HIDDEN
3284 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003285
3286struct k_pipe {
3287 unsigned char *buffer; /* Pipe buffer: may be NULL */
3288 size_t size; /* Buffer size */
3289 size_t bytes_used; /* # bytes used in buffer */
3290 size_t read_index; /* Where in buffer to read from */
3291 size_t write_index; /* Where in buffer to write */
3292
3293 struct {
3294 _wait_q_t readers; /* Reader wait queue */
3295 _wait_q_t writers; /* Writer wait queue */
3296 } wait_q;
3297
Anas Nashif2f203c22016-12-18 06:57:45 -05003298 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003299};
3300
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003301#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003302 { \
3303 .buffer = pipe_buffer, \
3304 .size = pipe_buffer_size, \
3305 .bytes_used = 0, \
3306 .read_index = 0, \
3307 .write_index = 0, \
3308 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3309 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003310 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003311 }
3312
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003313#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3314
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003315/**
Allan Stephensc98da842016-11-11 15:45:03 -05003316 * INTERNAL_HIDDEN @endcond
3317 */
3318
3319/**
3320 * @defgroup pipe_apis Pipe APIs
3321 * @ingroup kernel_apis
3322 * @{
3323 */
3324
3325/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003326 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003327 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003328 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003329 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003330 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003331 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003332 * @param name Name of the pipe.
3333 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3334 * or zero if no ring buffer is used.
3335 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003336 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003337#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3338 static unsigned char __noinit __aligned(pipe_align) \
3339 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003340 struct k_pipe name \
3341 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003342 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003343
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003344/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003345 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003346 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003347 * This routine initializes a pipe object, prior to its first use.
3348 *
3349 * @param pipe Address of the pipe.
3350 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3351 * is used.
3352 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3353 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003354 *
3355 * @return N/A
3356 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003357__syscall void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3358 size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003359
3360/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003361 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003362 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003363 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003364 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003365 * @param pipe Address of the pipe.
3366 * @param data Address of data to write.
3367 * @param bytes_to_write Size of data (in bytes).
3368 * @param bytes_written Address of area to hold the number of bytes written.
3369 * @param min_xfer Minimum number of bytes to write.
3370 * @param timeout Waiting period to wait for the data to be written (in
3371 * milliseconds), or one of the special values K_NO_WAIT
3372 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003373 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003374 * @retval 0 At least @a min_xfer bytes of data were written.
3375 * @retval -EIO Returned without waiting; zero data bytes were written.
3376 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003377 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003378 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003379__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3380 size_t bytes_to_write, size_t *bytes_written,
3381 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003382
3383/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003384 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003385 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003386 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003387 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003388 * @param pipe Address of the pipe.
3389 * @param data Address to place the data read from pipe.
3390 * @param bytes_to_read Maximum number of data bytes to read.
3391 * @param bytes_read Address of area to hold the number of bytes read.
3392 * @param min_xfer Minimum number of data bytes to read.
3393 * @param timeout Waiting period to wait for the data to be read (in
3394 * milliseconds), or one of the special values K_NO_WAIT
3395 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003396 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003397 * @retval 0 At least @a min_xfer bytes of data were read.
3398 * @retval -EIO Returned without waiting; zero data bytes were read.
3399 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003400 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003401 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003402__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3403 size_t bytes_to_read, size_t *bytes_read,
3404 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003405
3406/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003407 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003408 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003409 * This routine writes the data contained in a memory block to @a pipe.
3410 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003411 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003412 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003413 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003414 * @param block Memory block containing data to send
3415 * @param size Number of data bytes in memory block to send
3416 * @param sem Semaphore to signal upon completion (else NULL)
3417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003418 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003419 */
3420extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3421 size_t size, struct k_sem *sem);
3422
Anas Nashif166f5192018-02-25 08:02:36 -06003423/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003424
Allan Stephensc98da842016-11-11 15:45:03 -05003425/**
3426 * @cond INTERNAL_HIDDEN
3427 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003428
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003429struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003430 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003431 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003432 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003433 char *buffer;
3434 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003435 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003436
Anas Nashif2f203c22016-12-18 06:57:45 -05003437 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003438};
3439
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003440#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003441 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003442 { \
3443 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003444 .num_blocks = slab_num_blocks, \
3445 .block_size = slab_block_size, \
3446 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003447 .free_list = NULL, \
3448 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003449 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003450 }
3451
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003452#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3453
3454
Peter Mitsis578f9112016-10-07 13:50:31 -04003455/**
Allan Stephensc98da842016-11-11 15:45:03 -05003456 * INTERNAL_HIDDEN @endcond
3457 */
3458
3459/**
3460 * @defgroup mem_slab_apis Memory Slab APIs
3461 * @ingroup kernel_apis
3462 * @{
3463 */
3464
3465/**
Allan Stephensda827222016-11-09 14:23:58 -06003466 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003467 *
Allan Stephensda827222016-11-09 14:23:58 -06003468 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003469 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003470 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3471 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003472 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003473 *
Allan Stephensda827222016-11-09 14:23:58 -06003474 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003475 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003476 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003477 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003478 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003479 * @param name Name of the memory slab.
3480 * @param slab_block_size Size of each memory block (in bytes).
3481 * @param slab_num_blocks Number memory blocks.
3482 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003483 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003484#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3485 char __noinit __aligned(slab_align) \
3486 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3487 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003488 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003489 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003490 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003491
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003492/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003493 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003494 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003495 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003496 *
Allan Stephensda827222016-11-09 14:23:58 -06003497 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3498 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3499 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3500 * To ensure that each memory block is similarly aligned to this boundary,
3501 * @a slab_block_size must also be a multiple of N.
3502 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003503 * @param slab Address of the memory slab.
3504 * @param buffer Pointer to buffer used for the memory blocks.
3505 * @param block_size Size of each memory block (in bytes).
3506 * @param num_blocks Number of memory blocks.
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_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003511 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003512
3513/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003514 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003515 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003516 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003517 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003518 * @param slab Address of the memory slab.
3519 * @param mem Pointer to block address area.
3520 * @param timeout Maximum time to wait for operation to complete
3521 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3522 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003523 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003524 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003525 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003526 * @retval -ENOMEM Returned without waiting.
3527 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003528 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003529extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003530 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003531
3532/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003533 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003534 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003535 * This routine releases a previously allocated memory block back to its
3536 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003537 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003538 * @param slab Address of the memory slab.
3539 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003540 *
3541 * @return N/A
3542 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003543extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003544
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003545/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003546 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003547 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003548 * This routine gets the number of memory blocks that are currently
3549 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003550 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003551 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003552 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003553 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003554 */
Kumar Galacc334c72017-04-21 10:55:34 -05003555static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003556{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003557 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003558}
3559
Peter Mitsisc001aa82016-10-13 13:53:37 -04003560/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003561 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003562 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003563 * This routine gets the number of memory blocks that are currently
3564 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003565 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003566 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003567 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003568 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003569 */
Kumar Galacc334c72017-04-21 10:55:34 -05003570static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003571{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003572 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003573}
3574
Anas Nashif166f5192018-02-25 08:02:36 -06003575/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003576
3577/**
3578 * @cond INTERNAL_HIDDEN
3579 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003580
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003581struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003582 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003583 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003584};
3585
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003586/**
Allan Stephensc98da842016-11-11 15:45:03 -05003587 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003588 */
3589
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003590/**
Allan Stephensc98da842016-11-11 15:45:03 -05003591 * @addtogroup mem_pool_apis
3592 * @{
3593 */
3594
3595/**
3596 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003597 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003598 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3599 * long. The memory pool allows blocks to be repeatedly partitioned into
3600 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003601 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003602 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003603 * If the pool is to be accessed outside the module where it is defined, it
3604 * can be declared via
3605 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003606 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003607 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003608 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003609 * @param minsz Size of the smallest blocks in the pool (in bytes).
3610 * @param maxsz Size of the largest blocks in the pool (in bytes).
3611 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003612 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003613 */
Andy Ross73cb9582017-05-09 10:42:39 -07003614#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3615 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3616 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08003617 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07003618 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08003619 .base = { \
3620 .buf = _mpool_buf_##name, \
3621 .max_sz = maxsz, \
3622 .n_max = nmax, \
3623 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3624 .levels = _mpool_lvls_##name, \
3625 .flags = SYS_MEM_POOL_KERNEL \
3626 } \
Andy Ross73cb9582017-05-09 10:42:39 -07003627 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003628
Peter Mitsis937042c2016-10-13 13:18:26 -04003629/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003630 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003631 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003632 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003633 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003634 * @param pool Address of the memory pool.
3635 * @param block Pointer to block descriptor for the allocated memory.
3636 * @param size Amount of memory to allocate (in bytes).
3637 * @param timeout Maximum time to wait for operation to complete
3638 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3639 * or K_FOREVER to wait as long as necessary.
3640 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003641 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003642 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003643 * @retval -ENOMEM Returned without waiting.
3644 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003645 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003646extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003647 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003648
3649/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003650 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003651 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003652 * This routine releases a previously allocated memory block back to its
3653 * memory pool.
3654 *
3655 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003656 *
3657 * @return N/A
3658 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003659extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003660
3661/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003662 * @brief Free memory allocated from a memory pool.
3663 *
3664 * This routine releases a previously allocated memory block back to its
3665 * memory pool
3666 *
3667 * @param id Memory block identifier.
3668 *
3669 * @return N/A
3670 */
3671extern void k_mem_pool_free_id(struct k_mem_block_id *id);
3672
3673/**
Anas Nashif166f5192018-02-25 08:02:36 -06003674 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003675 */
3676
3677/**
3678 * @defgroup heap_apis Heap Memory Pool APIs
3679 * @ingroup kernel_apis
3680 * @{
3681 */
3682
3683/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003684 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003685 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003686 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003687 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003688 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003689 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003690 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003691 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003692 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003693extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003694
3695/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003696 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003697 *
3698 * This routine provides traditional free() semantics. The memory being
3699 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003700 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003701 * If @a ptr is NULL, no operation is performed.
3702 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003703 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003704 *
3705 * @return N/A
3706 */
3707extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003708
Allan Stephensc98da842016-11-11 15:45:03 -05003709/**
Andrew Boie7f95e832017-11-08 14:40:01 -08003710 * @brief Allocate memory from heap, array style
3711 *
3712 * This routine provides traditional calloc() semantics. Memory is
3713 * allocated from the heap memory pool and zeroed.
3714 *
3715 * @param nmemb Number of elements in the requested array
3716 * @param size Size of each array element (in bytes).
3717 *
3718 * @return Address of the allocated memory if successful; otherwise NULL.
3719 */
3720extern void *k_calloc(size_t nmemb, size_t size);
3721
Anas Nashif166f5192018-02-25 08:02:36 -06003722/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003723
Benjamin Walshacc68c12017-01-29 18:57:45 -05003724/* polling API - PRIVATE */
3725
Benjamin Walshb0179862017-02-02 16:39:57 -05003726#ifdef CONFIG_POLL
3727#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3728#else
3729#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3730#endif
3731
Benjamin Walshacc68c12017-01-29 18:57:45 -05003732/* private - implementation data created as needed, per-type */
3733struct _poller {
3734 struct k_thread *thread;
3735};
3736
3737/* private - types bit positions */
3738enum _poll_types_bits {
3739 /* can be used to ignore an event */
3740 _POLL_TYPE_IGNORE,
3741
3742 /* to be signaled by k_poll_signal() */
3743 _POLL_TYPE_SIGNAL,
3744
3745 /* semaphore availability */
3746 _POLL_TYPE_SEM_AVAILABLE,
3747
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003748 /* queue/fifo/lifo data availability */
3749 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003750
3751 _POLL_NUM_TYPES
3752};
3753
3754#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3755
3756/* private - states bit positions */
3757enum _poll_states_bits {
3758 /* default state when creating event */
3759 _POLL_STATE_NOT_READY,
3760
Benjamin Walshacc68c12017-01-29 18:57:45 -05003761 /* signaled by k_poll_signal() */
3762 _POLL_STATE_SIGNALED,
3763
3764 /* semaphore is available */
3765 _POLL_STATE_SEM_AVAILABLE,
3766
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003767 /* data is available to read on queue/fifo/lifo */
3768 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003769
3770 _POLL_NUM_STATES
3771};
3772
3773#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3774
3775#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003776 (32 - (0 \
3777 + 8 /* tag */ \
3778 + _POLL_NUM_TYPES \
3779 + _POLL_NUM_STATES \
3780 + 1 /* modes */ \
3781 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003782
Benjamin Walshacc68c12017-01-29 18:57:45 -05003783/* end of polling API - PRIVATE */
3784
3785
3786/**
3787 * @defgroup poll_apis Async polling APIs
3788 * @ingroup kernel_apis
3789 * @{
3790 */
3791
3792/* Public polling API */
3793
3794/* public - values for k_poll_event.type bitfield */
3795#define K_POLL_TYPE_IGNORE 0
3796#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3797#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003798#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3799#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003800
3801/* public - polling modes */
3802enum k_poll_modes {
3803 /* polling thread does not take ownership of objects when available */
3804 K_POLL_MODE_NOTIFY_ONLY = 0,
3805
3806 K_POLL_NUM_MODES
3807};
3808
3809/* public - values for k_poll_event.state bitfield */
3810#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003811#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3812#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003813#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3814#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003815
3816/* public - poll signal object */
3817struct k_poll_signal {
3818 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003819 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003820
3821 /*
3822 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3823 * user resets it to 0.
3824 */
3825 unsigned int signaled;
3826
3827 /* custom result value passed to k_poll_signal() if needed */
3828 int result;
3829};
3830
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003831#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003832 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003833 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003834 .signaled = 0, \
3835 .result = 0, \
3836 }
3837
3838struct k_poll_event {
3839 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003840 sys_dnode_t _node;
3841
3842 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003843 struct _poller *poller;
3844
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003845 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003846 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003847
Benjamin Walshacc68c12017-01-29 18:57:45 -05003848 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003849 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003850
3851 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003852 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003853
3854 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003855 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003856
3857 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003858 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003859
3860 /* per-type data */
3861 union {
3862 void *obj;
3863 struct k_poll_signal *signal;
3864 struct k_sem *sem;
3865 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003866 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003867 };
3868};
3869
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003870#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003871 { \
3872 .poller = NULL, \
3873 .type = event_type, \
3874 .state = K_POLL_STATE_NOT_READY, \
3875 .mode = event_mode, \
3876 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003877 { .obj = event_obj }, \
3878 }
3879
3880#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3881 event_tag) \
3882 { \
3883 .type = event_type, \
3884 .tag = event_tag, \
3885 .state = K_POLL_STATE_NOT_READY, \
3886 .mode = event_mode, \
3887 .unused = 0, \
3888 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003889 }
3890
3891/**
3892 * @brief Initialize one struct k_poll_event instance
3893 *
3894 * After this routine is called on a poll event, the event it ready to be
3895 * placed in an event array to be passed to k_poll().
3896 *
3897 * @param event The event to initialize.
3898 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3899 * values. Only values that apply to the same object being polled
3900 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3901 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003902 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003903 * @param obj Kernel object or poll signal.
3904 *
3905 * @return N/A
3906 */
3907
Kumar Galacc334c72017-04-21 10:55:34 -05003908extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003909 int mode, void *obj);
3910
3911/**
3912 * @brief Wait for one or many of multiple poll events to occur
3913 *
3914 * This routine allows a thread to wait concurrently for one or many of
3915 * multiple poll events to have occurred. Such events can be a kernel object
3916 * being available, like a semaphore, or a poll signal event.
3917 *
3918 * When an event notifies that a kernel object is available, the kernel object
3919 * is not "given" to the thread calling k_poll(): it merely signals the fact
3920 * that the object was available when the k_poll() call was in effect. Also,
3921 * all threads trying to acquire an object the regular way, i.e. by pending on
3922 * the object, have precedence over the thread polling on the object. This
3923 * means that the polling thread will never get the poll event on an object
3924 * until the object becomes available and its pend queue is empty. For this
3925 * reason, the k_poll() call is more effective when the objects being polled
3926 * only have one thread, the polling thread, trying to acquire them.
3927 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003928 * When k_poll() returns 0, the caller should loop on all the events that were
3929 * passed to k_poll() and check the state field for the values that were
3930 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003931 *
3932 * Before being reused for another call to k_poll(), the user has to reset the
3933 * state field to K_POLL_STATE_NOT_READY.
3934 *
3935 * @param events An array of pointers to events to be polled for.
3936 * @param num_events The number of events in the array.
3937 * @param timeout Waiting period for an event to be ready (in milliseconds),
3938 * or one of the special values K_NO_WAIT and K_FOREVER.
3939 *
3940 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003941 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02003942 * @retval -EINTR Poller thread has been interrupted.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003943 */
3944
3945extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003946 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003947
3948/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003949 * @brief Initialize a poll signal object.
3950 *
3951 * Ready a poll signal object to be signaled via k_poll_signal().
3952 *
3953 * @param signal A poll signal.
3954 *
3955 * @return N/A
3956 */
3957
3958extern void k_poll_signal_init(struct k_poll_signal *signal);
3959
3960/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003961 * @brief Signal a poll signal object.
3962 *
3963 * This routine makes ready a poll signal, which is basically a poll event of
3964 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3965 * made ready to run. A @a result value can be specified.
3966 *
3967 * The poll signal contains a 'signaled' field that, when set by
3968 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3969 * be reset by the user before being passed again to k_poll() or k_poll() will
3970 * consider it being signaled, and will return immediately.
3971 *
3972 * @param signal A poll signal.
3973 * @param result The value to store in the result field of the signal.
3974 *
3975 * @retval 0 The signal was delivered successfully.
3976 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3977 */
3978
3979extern int k_poll_signal(struct k_poll_signal *signal, int result);
3980
Anas Nashif954d5502018-02-25 08:37:28 -06003981/**
3982 * @internal
3983 */
Andy Ross8606fab2018-03-26 10:54:40 -07003984extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003985
Anas Nashif166f5192018-02-25 08:02:36 -06003986/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003987
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003988/**
3989 * @brief Make the CPU idle.
3990 *
3991 * This function makes the CPU idle until an event wakes it up.
3992 *
3993 * In a regular system, the idle thread should be the only thread responsible
3994 * for making the CPU idle and triggering any type of power management.
3995 * However, in some more constrained systems, such as a single-threaded system,
3996 * the only thread would be responsible for this if needed.
3997 *
3998 * @return N/A
3999 */
4000extern void k_cpu_idle(void);
4001
4002/**
4003 * @brief Make the CPU idle in an atomic fashion.
4004 *
4005 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4006 * must be done atomically before making the CPU idle.
4007 *
4008 * @param key Interrupt locking key obtained from irq_lock().
4009 *
4010 * @return N/A
4011 */
4012extern void k_cpu_atomic_idle(unsigned int key);
4013
Anas Nashif954d5502018-02-25 08:37:28 -06004014
4015/**
4016 * @internal
4017 */
Kumar Galacc334c72017-04-21 10:55:34 -05004018extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004019
Andrew Boiecdb94d62017-04-18 15:22:05 -07004020#ifdef _ARCH_EXCEPT
4021/* This archtecture has direct support for triggering a CPU exception */
4022#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4023#else
4024
Andrew Boiecdb94d62017-04-18 15:22:05 -07004025/* NOTE: This is the implementation for arches that do not implement
4026 * _ARCH_EXCEPT() to generate a real CPU exception.
4027 *
4028 * We won't have a real exception frame to determine the PC value when
4029 * the oops occurred, so print file and line number before we jump into
4030 * the fatal error handler.
4031 */
4032#define _k_except_reason(reason) do { \
4033 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4034 _NanoFatalErrorHandler(reason, &_default_esf); \
4035 CODE_UNREACHABLE; \
4036 } while (0)
4037
4038#endif /* _ARCH__EXCEPT */
4039
4040/**
4041 * @brief Fatally terminate a thread
4042 *
4043 * This should be called when a thread has encountered an unrecoverable
4044 * runtime condition and needs to terminate. What this ultimately
4045 * means is determined by the _fatal_error_handler() implementation, which
4046 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4047 *
4048 * If this is called from ISR context, the default system fatal error handler
4049 * will treat it as an unrecoverable system error, just like k_panic().
4050 */
4051#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4052
4053/**
4054 * @brief Fatally terminate the system
4055 *
4056 * This should be called when the Zephyr kernel has encountered an
4057 * unrecoverable runtime condition and needs to terminate. What this ultimately
4058 * means is determined by the _fatal_error_handler() implementation, which
4059 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4060 */
4061#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4062
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004063/*
4064 * private APIs that are utilized by one or more public APIs
4065 */
4066
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004067#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004068/**
4069 * @internal
4070 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004071extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004072#else
Anas Nashif954d5502018-02-25 08:37:28 -06004073/**
4074 * @internal
4075 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004076#define _init_static_threads() do { } while ((0))
4077#endif
4078
Anas Nashif954d5502018-02-25 08:37:28 -06004079/**
4080 * @internal
4081 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004082extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004083/**
4084 * @internal
4085 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004086extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004087
Andrew Boiedc5d9352017-06-02 12:56:47 -07004088/* arch/cpu.h may declare an architecture or platform-specific macro
4089 * for properly declaring stacks, compatible with MMU/MPU constraints if
4090 * enabled
4091 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004092
4093/**
4094 * @brief Obtain an extern reference to a stack
4095 *
4096 * This macro properly brings the symbol of a thread stack declared
4097 * elsewhere into scope.
4098 *
4099 * @param sym Thread stack symbol name
4100 */
4101#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4102
Andrew Boiedc5d9352017-06-02 12:56:47 -07004103#ifdef _ARCH_THREAD_STACK_DEFINE
4104#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4105#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4106 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4107#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4108#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004109static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004110{
4111 return _ARCH_THREAD_STACK_BUFFER(sym);
4112}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004113#else
4114/**
4115 * @brief Declare a toplevel thread stack memory region
4116 *
4117 * This declares a region of memory suitable for use as a thread's stack.
4118 *
4119 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4120 * 'noinit' section so that it isn't zeroed at boot
4121 *
Andrew Boie507852a2017-07-25 18:47:07 -07004122 * The declared symbol will always be a k_thread_stack_t which can be passed to
4123 * k_thread_create, but should otherwise not be manipulated. If the buffer
4124 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004125 *
4126 * It is legal to precede this definition with the 'static' keyword.
4127 *
4128 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4129 * parameter of k_thread_create(), it may not be the same as the
4130 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4131 *
4132 * @param sym Thread stack symbol name
4133 * @param size Size of the stack memory region
4134 */
4135#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004136 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004137
4138/**
4139 * @brief Declare a toplevel array of thread stack memory regions
4140 *
4141 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4142 * definition for additional details and constraints.
4143 *
4144 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4145 * 'noinit' section so that it isn't zeroed at boot
4146 *
4147 * @param sym Thread stack symbol name
4148 * @param nmemb Number of stacks to declare
4149 * @param size Size of the stack memory region
4150 */
4151
4152#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004153 struct _k_thread_stack_element __noinit \
4154 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004155
4156/**
4157 * @brief Declare an embedded stack memory region
4158 *
4159 * Used for stacks embedded within other data structures. Use is highly
4160 * discouraged but in some cases necessary. For memory protection scenarios,
4161 * it is very important that any RAM preceding this member not be writable
4162 * by threads else a stack overflow will lead to silent corruption. In other
4163 * words, the containing data structure should live in RAM owned by the kernel.
4164 *
4165 * @param sym Thread stack symbol name
4166 * @param size Size of the stack memory region
4167 */
4168#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004169 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004170
4171/**
4172 * @brief Return the size in bytes of a stack memory region
4173 *
4174 * Convenience macro for passing the desired stack size to k_thread_create()
4175 * since the underlying implementation may actually create something larger
4176 * (for instance a guard area).
4177 *
4178 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004179 * passed to K_THREAD_STACK_DEFINE.
4180 *
4181 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4182 * it is not guaranteed to return the original value since each array
4183 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004184 *
4185 * @param sym Stack memory symbol
4186 * @return Size of the stack
4187 */
4188#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4189
4190/**
4191 * @brief Get a pointer to the physical stack buffer
4192 *
4193 * Convenience macro to get at the real underlying stack buffer used by
4194 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4195 * This is really only intended for diagnostic tools which want to examine
4196 * stack memory contents.
4197 *
4198 * @param sym Declared stack symbol name
4199 * @return The buffer itself, a char *
4200 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004201static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004202{
4203 return (char *)sym;
4204}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004205
4206#endif /* _ARCH_DECLARE_STACK */
4207
Chunlin Hane9c97022017-07-07 20:29:30 +08004208/**
4209 * @defgroup mem_domain_apis Memory domain APIs
4210 * @ingroup kernel_apis
4211 * @{
4212 */
4213
4214/** @def MEM_PARTITION_ENTRY
4215 * @brief Used to declare a memory partition entry
4216 */
4217#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4218 {\
4219 .start = _start, \
4220 .size = _size, \
4221 .attr = _attr, \
4222 }
4223
4224/** @def K_MEM_PARTITION_DEFINE
4225 * @brief Used to declare a memory partition
4226 */
4227#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4228#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4229 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304230 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004231 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4232#else
4233#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304234 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004235 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4236#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4237
Chunlin Hane9c97022017-07-07 20:29:30 +08004238/* memory partition */
4239struct k_mem_partition {
4240 /* start address of memory partition */
4241 u32_t start;
4242 /* size of memory partition */
4243 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304244#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004245 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304246 k_mem_partition_attr_t attr;
4247#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004248};
4249
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304250/* memory domian
4251 * Note: Always declare this structure with __kernel prefix
4252 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004253struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304254#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004255 /* partitions in the domain */
4256 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304257#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004258 /* domain q */
4259 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004260 /* number of partitions in the domain */
4261 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004262};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304263
Chunlin Hane9c97022017-07-07 20:29:30 +08004264
4265/**
4266 * @brief Initialize a memory domain.
4267 *
4268 * Initialize a memory domain with given name and memory partitions.
4269 *
4270 * @param domain The memory domain to be initialized.
4271 * @param num_parts The number of array items of "parts" parameter.
4272 * @param parts An array of pointers to the memory partitions. Can be NULL
4273 * if num_parts is zero.
4274 */
4275
Leandro Pereira08de6582018-02-28 14:22:57 -08004276extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004277 struct k_mem_partition *parts[]);
4278/**
4279 * @brief Destroy a memory domain.
4280 *
4281 * Destroy a memory domain.
4282 *
4283 * @param domain The memory domain to be destroyed.
4284 */
4285
4286extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4287
4288/**
4289 * @brief Add a memory partition into a memory domain.
4290 *
4291 * Add a memory partition into a memory domain.
4292 *
4293 * @param domain The memory domain to be added a memory partition.
4294 * @param part The memory partition to be added
4295 */
4296
4297extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4298 struct k_mem_partition *part);
4299
4300/**
4301 * @brief Remove a memory partition from a memory domain.
4302 *
4303 * Remove a memory partition from a memory domain.
4304 *
4305 * @param domain The memory domain to be removed a memory partition.
4306 * @param part The memory partition to be removed
4307 */
4308
4309extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4310 struct k_mem_partition *part);
4311
4312/**
4313 * @brief Add a thread into a memory domain.
4314 *
4315 * Add a thread into a memory domain.
4316 *
4317 * @param domain The memory domain that the thread is going to be added into.
4318 * @param thread ID of thread going to be added into the memory domain.
4319 */
4320
4321extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4322 k_tid_t thread);
4323
4324/**
4325 * @brief Remove a thread from its memory domain.
4326 *
4327 * Remove a thread from its memory domain.
4328 *
4329 * @param thread ID of thread going to be removed from its memory domain.
4330 */
4331
4332extern void k_mem_domain_remove_thread(k_tid_t thread);
4333
Anas Nashif166f5192018-02-25 08:02:36 -06004334/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004335
Andrew Boie756f9072017-10-10 16:01:49 -07004336/**
4337 * @brief Emit a character buffer to the console device
4338 *
4339 * @param c String of characters to print
4340 * @param n The length of the string
4341 */
4342__syscall void k_str_out(char *c, size_t n);
4343
Andy Rosse7172672018-01-24 15:48:32 -08004344/**
4345 * @brief Start a numbered CPU on a MP-capable system
4346
4347 * This starts and initializes a specific CPU. The main thread on
4348 * startup is running on CPU zero, other processors are numbered
4349 * sequentially. On return from this function, the CPU is known to
4350 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004351 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004352 * with the provided key will work to enable them.
4353 *
4354 * Normally, in SMP mode this function will be called by the kernel
4355 * initialization and should not be used as a user API. But it is
4356 * defined here for special-purpose apps which want Zephyr running on
4357 * one core and to use others for design-specific processing.
4358 *
4359 * @param cpu_num Integer number of the CPU
4360 * @param stack Stack memory for the CPU
4361 * @param sz Stack buffer size, in bytes
4362 * @param fn Function to begin running on the CPU. First argument is
4363 * an irq_unlock() key.
4364 * @param arg Untyped argument to be passed to "fn"
4365 */
4366extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4367 void (*fn)(int, void *), void *arg);
4368
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004369#ifdef __cplusplus
4370}
4371#endif
4372
Andrew Boiee004dec2016-11-07 09:01:19 -08004373#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4374/*
4375 * Define new and delete operators.
4376 * At this moment, the operators do nothing since objects are supposed
4377 * to be statically allocated.
4378 */
4379inline void operator delete(void *ptr)
4380{
4381 (void)ptr;
4382}
4383
4384inline void operator delete[](void *ptr)
4385{
4386 (void)ptr;
4387}
4388
4389inline void *operator new(size_t size)
4390{
4391 (void)size;
4392 return NULL;
4393}
4394
4395inline void *operator new[](size_t size)
4396{
4397 (void)size;
4398 return NULL;
4399}
4400
4401/* Placement versions of operator new and delete */
4402inline void operator delete(void *ptr1, void *ptr2)
4403{
4404 (void)ptr1;
4405 (void)ptr2;
4406}
4407
4408inline void operator delete[](void *ptr1, void *ptr2)
4409{
4410 (void)ptr1;
4411 (void)ptr2;
4412}
4413
4414inline void *operator new(size_t size, void *ptr)
4415{
4416 (void)size;
4417 return ptr;
4418}
4419
4420inline void *operator new[](size_t size, void *ptr)
4421{
4422 (void)size;
4423 return ptr;
4424}
4425
4426#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4427
Andrew Boiefa94ee72017-09-28 16:54:35 -07004428#include <syscalls/kernel.h>
4429
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004430#endif /* !_ASMLANGUAGE */
4431
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004432#endif /* _kernel__h_ */