blob: 315a88912655eb81251e94b0bd362a3960a91cd1 [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @file
9 *
10 * @brief Public kernel APIs.
11 */
12
13#ifndef _kernel__h_
14#define _kernel__h_
15
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050016#if !defined(_ASMLANGUAGE)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040017#include <stddef.h>
Kumar Gala78908162017-04-19 10:32:08 -050018#include <zephyr/types.h>
Anas Nashif173902f2017-01-17 07:08:56 -050019#include <limits.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020#include <toolchain.h>
Anas Nashif397d29d2017-06-17 11:30:47 -040021#include <linker/sections.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040022#include <atomic.h>
23#include <errno.h>
24#include <misc/__assert.h>
25#include <misc/dlist.h>
26#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050027#include <misc/util.h>
Andrew Boieaa6de292018-03-06 17:12:37 -080028#include <misc/mempool_base.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050029#include <kernel_version.h>
Leandro Pereiraadce1d12017-10-13 15:45:02 -070030#include <random/rand32.h>
Andrew Boie73abd322017-04-04 13:19:13 -070031#include <kernel_arch_thread.h>
Andrew Boie13ca6fe2017-09-23 12:05:49 -070032#include <syscall.h>
Andrew Boie43263fc2017-11-02 11:07:31 -070033#include <misc/printk.h>
34#include <arch/cpu.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040035
36#ifdef __cplusplus
37extern "C" {
38#endif
39
Anas Nashifbbb157d2017-01-15 08:46:31 -050040/**
41 * @brief Kernel APIs
42 * @defgroup kernel_apis Kernel APIs
43 * @{
44 * @}
45 */
46
Anas Nashif61f4b242016-11-18 10:53:59 -050047#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040048#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
49#else
50#define K_DEBUG(fmt, ...)
51#endif
52
Benjamin Walsh2f280412017-01-14 19:23:46 -050053#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
54#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
55#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
56#elif defined(CONFIG_COOP_ENABLED)
57#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
58#define _NUM_PREEMPT_PRIO (0)
59#elif defined(CONFIG_PREEMPT_ENABLED)
60#define _NUM_COOP_PRIO (0)
61#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
62#else
63#error "invalid configuration"
64#endif
65
66#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040067#define K_PRIO_PREEMPT(x) (x)
68
Benjamin Walsh456c6da2016-09-02 18:55:39 -040069#define K_ANY NULL
70#define K_END NULL
71
Benjamin Walshedb35702017-01-14 18:47:22 -050072#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040073#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050074#elif defined(CONFIG_COOP_ENABLED)
75#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
76#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040077#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050078#else
79#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040080#endif
81
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050082#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040083#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
84#else
85#define K_LOWEST_THREAD_PRIO -1
86#endif
87
Benjamin Walshfab8d922016-11-08 15:36:36 -050088#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
89
Benjamin Walsh456c6da2016-09-02 18:55:39 -040090#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
91#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
92
93typedef sys_dlist_t _wait_q_t;
94
Anas Nashif2f203c22016-12-18 06:57:45 -050095#ifdef CONFIG_OBJECT_TRACING
96#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
97#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040098#else
Anas Nashif2f203c22016-12-18 06:57:45 -050099#define _OBJECT_TRACING_INIT
100#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400101#endif
102
Benjamin Walshacc68c12017-01-29 18:57:45 -0500103#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300104#define _POLL_EVENT_OBJ_INIT(obj) \
105 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
106#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500107#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300108#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500109#define _POLL_EVENT
110#endif
111
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500112struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400113struct k_mutex;
114struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400115struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400116struct k_msgq;
117struct k_mbox;
118struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200119struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400120struct k_fifo;
121struct k_lifo;
122struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400123struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400124struct k_mem_pool;
125struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500126struct k_poll_event;
127struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800128struct k_mem_domain;
129struct k_mem_partition;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400130
Andrew Boie5bd891d2017-09-27 12:59:28 -0700131/* This enumeration needs to be kept in sync with the lists of kernel objects
132 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
133 * function in kernel/userspace.c
134 */
Andrew Boie945af952017-08-22 13:15:23 -0700135enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700136 K_OBJ_ANY,
137
Leandro Pereirac2003672018-04-04 13:50:32 -0700138 /** @cond
139 * Doxygen should ignore this build-time generated include file
140 * when genrating API documentation. Enumeration values are
141 * generated during build by gen_kobject_list.py. It includes
142 * basic kernel objects (e.g. pipes and mutexes) and driver types.
143 */
144#include <kobj-types-enum.h>
145 /** @endcond
146 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700147
Andrew Boie945af952017-08-22 13:15:23 -0700148 K_OBJ_LAST
149};
150
151#ifdef CONFIG_USERSPACE
152/* Table generated by gperf, these objects are retrieved via
153 * _k_object_find() */
154struct _k_object {
155 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700156 u8_t perms[CONFIG_MAX_THREAD_BYTES];
157 u8_t type;
158 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700159 u32_t data;
Andrew Boie945af952017-08-22 13:15:23 -0700160} __packed;
161
Andrew Boie877f82e2017-10-17 11:20:22 -0700162struct _k_object_assignment {
163 struct k_thread *thread;
164 void * const *objects;
165};
166
167/**
168 * @brief Grant a static thread access to a list of kernel objects
169 *
170 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
171 * a set of kernel objects. These objects do not need to be in an initialized
172 * state. The permissions will be granted when the threads are initialized
173 * in the early boot sequence.
174 *
175 * All arguments beyond the first must be pointers to kernel objects.
176 *
177 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
178 */
179#define K_THREAD_ACCESS_GRANT(name_, ...) \
180 static void * const _CONCAT(_object_list_, name_)[] = \
181 { __VA_ARGS__, NULL }; \
182 static __used __in_section_unique(object_access) \
183 const struct _k_object_assignment \
184 _CONCAT(_object_access_, name_) = \
185 { (&_k_thread_obj_ ## name_), \
186 (_CONCAT(_object_list_, name_)) }
187
Andrew Boie945af952017-08-22 13:15:23 -0700188#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700189#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie945af952017-08-22 13:15:23 -0700190
191/**
192 * Lookup a kernel object and init its metadata if it exists
193 *
194 * Calling this on an object will make it usable from userspace.
195 * Intended to be called as the last statement in kernel object init
196 * functions.
197 *
198 * @param object Address of the kernel object
199 */
200void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700201#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700202
203#define K_THREAD_ACCESS_GRANT(thread, ...)
204
Anas Nashif954d5502018-02-25 08:37:28 -0600205/**
206 * @internal
207 */
Andrew Boie743e4682017-10-04 12:25:50 -0700208static inline void _k_object_init(void *obj)
209{
210 ARG_UNUSED(obj);
211}
212
Anas Nashif954d5502018-02-25 08:37:28 -0600213/**
214 * @internal
215 */
Andrew Boie743e4682017-10-04 12:25:50 -0700216static inline void _impl_k_object_access_grant(void *object,
217 struct k_thread *thread)
218{
219 ARG_UNUSED(object);
220 ARG_UNUSED(thread);
221}
222
Anas Nashif954d5502018-02-25 08:37:28 -0600223/**
224 * @internal
225 */
Andrew Boiea89bf012017-10-09 14:47:55 -0700226static inline void _impl_k_object_access_revoke(void *object,
227 struct k_thread *thread)
228{
229 ARG_UNUSED(object);
230 ARG_UNUSED(thread);
231}
232
Andrew Boie41bab6e2017-10-14 14:42:23 -0700233static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700234{
235 ARG_UNUSED(object);
236}
237#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700238
239/**
240 * grant a thread access to a kernel object
241 *
242 * The thread will be granted access to the object if the caller is from
243 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700244 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700245 *
246 * @param object Address of kernel object
247 * @param thread Thread to grant access to the object
248 */
Andrew Boie743e4682017-10-04 12:25:50 -0700249__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700250
Andrew Boiea89bf012017-10-09 14:47:55 -0700251/**
252 * grant a thread access to a kernel object
253 *
254 * The thread will lose access to the object if the caller is from
255 * supervisor mode, or the caller is from user mode AND has permissions
256 * on both the object and the thread whose access is being revoked.
257 *
258 * @param object Address of kernel object
259 * @param thread Thread to remove access to the object
260 */
261__syscall void k_object_access_revoke(void *object, struct k_thread *thread);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700262
263/**
264 * grant all present and future threads access to an object
265 *
266 * If the caller is from supervisor mode, or the caller is from user mode and
267 * have sufficient permissions on the object, then that object will have
268 * permissions granted to it for *all* current and future threads running in
269 * the system, effectively becoming a public kernel object.
270 *
271 * Use of this API should be avoided on systems that are running untrusted code
272 * as it is possible for such code to derive the addresses of kernel objects
273 * and perform unwanted operations on them.
274 *
Andrew Boie04caa672017-10-13 13:57:07 -0700275 * It is not possible to revoke permissions on public objects; once public,
276 * any thread may use it.
277 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700278 * @param object Address of kernel object
279 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700280void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700281
Andrew Boie31bdfc02017-11-08 16:38:03 -0800282#ifdef CONFIG_DYNAMIC_OBJECTS
283/**
284 * Allocate a kernel object of a designated type
285 *
286 * This will instantiate at runtime a kernel object of the specified type,
287 * returning a pointer to it. The object will be returned in an uninitialized
288 * state, with the calling thread being granted permission on it. The memory
289 * for the object will be allocated out of the kernel's heap.
290 *
291 * Currently, allocation of thread stacks is not supported.
292 *
293 * @param otype Requested kernel object type
294 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
295 * available
296 */
297void *k_object_alloc(enum k_objects otype);
298
299/**
300 * Free a kernel object previously allocated with k_object_alloc()
301 *
302 * This will return memory for a kernel object back to the system heap.
303 * Care must be exercised that the object will not be used during or after
304 * when this call is made.
305 *
306 * @param obj Pointer to the kernel object memory address.
307 */
308void k_object_free(void *obj);
309#endif /* CONFIG_DYNAMIC_OBJECTS */
310
Andrew Boiebca15da2017-10-15 14:17:48 -0700311/* Using typedef deliberately here, this is quite intended to be an opaque
312 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
313 *
314 * The purpose of this data type is to clearly distinguish between the
315 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
316 * buffer which composes the stack data actually used by the underlying
317 * thread; they cannot be used interchangably as some arches precede the
318 * stack buffer region with guard areas that trigger a MPU or MMU fault
319 * if written to.
320 *
321 * APIs that want to work with the buffer inside should continue to use
322 * char *.
323 *
324 * Stacks should always be created with K_THREAD_STACK_DEFINE().
325 */
326struct __packed _k_thread_stack_element {
327 char data;
328};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700329typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700330
Andrew Boie73abd322017-04-04 13:19:13 -0700331/* timeouts */
332
333struct _timeout;
334typedef void (*_timeout_func_t)(struct _timeout *t);
335
336struct _timeout {
337 sys_dnode_t node;
338 struct k_thread *thread;
339 sys_dlist_t *wait_q;
340 s32_t delta_ticks_from_prev;
341 _timeout_func_t func;
342};
343
344extern s32_t _timeout_remaining_get(struct _timeout *timeout);
345
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700346/**
347 * @typedef k_thread_entry_t
348 * @brief Thread entry point function type.
349 *
350 * A thread's entry point function is invoked when the thread starts executing.
351 * Up to 3 argument values can be passed to the function.
352 *
353 * The thread terminates execution permanently if the entry point function
354 * returns. The thread is responsible for releasing any shared resources
355 * it may own (such as mutexes and dynamically allocated memory), prior to
356 * returning.
357 *
358 * @param p1 First argument.
359 * @param p2 Second argument.
360 * @param p3 Third argument.
361 *
362 * @return N/A
363 */
364typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700365
366#ifdef CONFIG_THREAD_MONITOR
367struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700368 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700369 void *parameter1;
370 void *parameter2;
371 void *parameter3;
372};
373#endif
374
375/* can be used for creating 'dummy' threads, e.g. for pending on objects */
376struct _thread_base {
377
378 /* this thread's entry in a ready/wait queue */
379 sys_dnode_t k_q_node;
380
381 /* user facing 'thread options'; values defined in include/kernel.h */
382 u8_t user_options;
383
384 /* thread state */
385 u8_t thread_state;
386
387 /*
388 * scheduler lock count and thread priority
389 *
390 * These two fields control the preemptibility of a thread.
391 *
392 * When the scheduler is locked, sched_locked is decremented, which
393 * means that the scheduler is locked for values from 0xff to 0x01. A
394 * thread is coop if its prio is negative, thus 0x80 to 0xff when
395 * looked at the value as unsigned.
396 *
397 * By putting them end-to-end, this means that a thread is
398 * non-preemptible if the bundled value is greater than or equal to
399 * 0x0080.
400 */
401 union {
402 struct {
403#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
404 u8_t sched_locked;
405 s8_t prio;
406#else /* LITTLE and PDP */
407 s8_t prio;
408 u8_t sched_locked;
409#endif
410 };
411 u16_t preempt;
412 };
413
Andy Ross2724fd12018-01-29 14:55:20 -0800414#ifdef CONFIG_SMP
415 /* True for the per-CPU idle threads */
416 u8_t is_idle;
417
418 /* Non-zero when actively running on a CPU */
419 u8_t active;
420
421 /* CPU index on which thread was last run */
422 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700423
424 /* Recursive count of irq_lock() calls */
425 u8_t global_lock_count;
Andy Ross2724fd12018-01-29 14:55:20 -0800426#endif
427
Andrew Boie73abd322017-04-04 13:19:13 -0700428 /* data returned by APIs */
429 void *swap_data;
430
431#ifdef CONFIG_SYS_CLOCK_EXISTS
432 /* this thread's entry in a timeout queue */
433 struct _timeout timeout;
434#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700435};
436
437typedef struct _thread_base _thread_base_t;
438
439#if defined(CONFIG_THREAD_STACK_INFO)
440/* Contains the stack information of a thread */
441struct _thread_stack_info {
442 /* Stack Start */
443 u32_t start;
444 /* Stack Size */
445 u32_t size;
446};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700447
448typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700449#endif /* CONFIG_THREAD_STACK_INFO */
450
Chunlin Hane9c97022017-07-07 20:29:30 +0800451#if defined(CONFIG_USERSPACE)
452struct _mem_domain_info {
453 /* memory domain queue node */
454 sys_dnode_t mem_domain_q_node;
455 /* memory domain of the thread */
456 struct k_mem_domain *mem_domain;
457};
458
459#endif /* CONFIG_USERSPACE */
460
Andrew Boie73abd322017-04-04 13:19:13 -0700461struct k_thread {
462
463 struct _thread_base base;
464
465 /* defined by the architecture, but all archs need these */
466 struct _caller_saved caller_saved;
467 struct _callee_saved callee_saved;
468
469 /* static thread init data */
470 void *init_data;
471
472 /* abort function */
473 void (*fn_abort)(void);
474
475#if defined(CONFIG_THREAD_MONITOR)
476 /* thread entry and parameters description */
477 struct __thread_entry *entry;
478
479 /* next item in list of all threads */
480 struct k_thread *next_thread;
481#endif
482
483#ifdef CONFIG_THREAD_CUSTOM_DATA
484 /* crude thread-local storage */
485 void *custom_data;
486#endif
487
488#ifdef CONFIG_ERRNO
489 /* per-thread errno variable */
490 int errno_var;
491#endif
492
493#if defined(CONFIG_THREAD_STACK_INFO)
494 /* Stack Info */
495 struct _thread_stack_info stack_info;
496#endif /* CONFIG_THREAD_STACK_INFO */
497
Chunlin Hane9c97022017-07-07 20:29:30 +0800498#if defined(CONFIG_USERSPACE)
499 /* memory domain info of the thread */
500 struct _mem_domain_info mem_domain_info;
Andrew Boiebca15da2017-10-15 14:17:48 -0700501 /* Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700502 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800503#endif /* CONFIG_USERSPACE */
504
Andy Ross042d8ec2017-12-09 08:37:20 -0800505#if defined(CONFIG_USE_SWITCH)
506 /* When using __switch() a few previously arch-specific items
507 * become part of the core OS
508 */
509
510 /* _Swap() return value */
511 int swap_retval;
512
513 /* Context handle returned via _arch_switch() */
514 void *switch_handle;
515#endif
516
Andrew Boie73abd322017-04-04 13:19:13 -0700517 /* arch-specifics: must always be at the end */
518 struct _thread_arch arch;
519};
520
521typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400522typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700523#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400524
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400525enum execution_context_types {
526 K_ISR = 0,
527 K_COOP_THREAD,
528 K_PREEMPT_THREAD,
529};
530
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400531/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100532 * @defgroup profiling_apis Profiling APIs
533 * @ingroup kernel_apis
534 * @{
535 */
536
537/**
538 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
539 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700540 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100541 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
542 *
543 * CONFIG_MAIN_STACK_SIZE
544 * CONFIG_IDLE_STACK_SIZE
545 * CONFIG_ISR_STACK_SIZE
546 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
547 *
548 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
549 * produce output.
550 *
551 * @return N/A
552 */
553extern void k_call_stacks_analyze(void);
554
Anas Nashif166f5192018-02-25 08:02:36 -0600555/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100556
557/**
Allan Stephensc98da842016-11-11 15:45:03 -0500558 * @defgroup thread_apis Thread APIs
559 * @ingroup kernel_apis
560 * @{
561 */
562
Benjamin Walshed240f22017-01-22 13:05:08 -0500563#endif /* !_ASMLANGUAGE */
564
565
566/*
567 * Thread user options. May be needed by assembly code. Common part uses low
568 * bits, arch-specific use high bits.
569 */
570
571/* system thread that must not abort */
572#define K_ESSENTIAL (1 << 0)
573
574#if defined(CONFIG_FP_SHARING)
575/* thread uses floating point registers */
576#define K_FP_REGS (1 << 1)
577#endif
578
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700579/* This thread has dropped from supervisor mode to user mode and consequently
580 * has additional restrictions
581 */
582#define K_USER (1 << 2)
583
Andrew Boie47f8fd12017-10-05 11:11:02 -0700584/* Indicates that the thread being created should inherit all kernel object
585 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
586 * is not enabled.
587 */
588#define K_INHERIT_PERMS (1 << 3)
589
Benjamin Walshed240f22017-01-22 13:05:08 -0500590#ifdef CONFIG_X86
591/* x86 Bitmask definitions for threads user options */
592
593#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
594/* thread uses SSEx (and also FP) registers */
595#define K_SSE_REGS (1 << 7)
596#endif
597#endif
598
599/* end - thread options */
600
601#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400602/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700603 * @brief Create a thread.
604 *
605 * This routine initializes a thread, then schedules it for execution.
606 *
607 * The new thread may be scheduled for immediate execution or a delayed start.
608 * If the newly spawned thread does not have a delayed start the kernel
609 * scheduler may preempt the current thread to allow the new thread to
610 * execute.
611 *
612 * Thread options are architecture-specific, and can include K_ESSENTIAL,
613 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
614 * them using "|" (the logical OR operator).
615 *
616 * Historically, users often would use the beginning of the stack memory region
617 * to store the struct k_thread data, although corruption will occur if the
618 * stack overflows this region and stack protection features may not detect this
619 * situation.
620 *
621 * @param new_thread Pointer to uninitialized struct k_thread
622 * @param stack Pointer to the stack space.
623 * @param stack_size Stack size in bytes.
624 * @param entry Thread entry function.
625 * @param p1 1st entry point parameter.
626 * @param p2 2nd entry point parameter.
627 * @param p3 3rd entry point parameter.
628 * @param prio Thread priority.
629 * @param options Thread options.
630 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
631 *
632 * @return ID of new thread.
633 */
Andrew Boie662c3452017-10-02 10:51:18 -0700634__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700635 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700636 size_t stack_size,
637 k_thread_entry_t entry,
638 void *p1, void *p2, void *p3,
639 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700640
Andrew Boie3f091b52017-08-30 14:34:14 -0700641/**
642 * @brief Drop a thread's privileges permanently to user mode
643 *
644 * @param entry Function to start executing from
645 * @param p1 1st entry point parameter
646 * @param p2 2nd entry point parameter
647 * @param p3 3rd entry point parameter
648 */
649extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
650 void *p1, void *p2,
651 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700652
Andrew Boied26cf2d2017-03-30 13:07:02 -0700653/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700654 * @brief Grant a thread access to a NULL-terminated set of kernel objects
655 *
656 * This is a convenience function. For the provided thread, grant access to
657 * the remaining arguments, which must be pointers to kernel objects.
658 * The final argument must be a NULL.
659 *
660 * The thread object must be initialized (i.e. running). The objects don't
661 * need to be.
662 *
663 * @param thread Thread to grant access to objects
664 * @param ... NULL-terminated list of kernel object pointers
665 */
666extern void __attribute__((sentinel))
667 k_thread_access_grant(struct k_thread *thread, ...);
668
669/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500670 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400671 *
Allan Stephensc98da842016-11-11 15:45:03 -0500672 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500673 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400674 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500675 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400676 *
677 * @return N/A
678 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700679__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400680
681/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500682 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400683 *
684 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500685 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400686 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400687 * @return N/A
688 */
Kumar Galacc334c72017-04-21 10:55:34 -0500689extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400690
691/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500692 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500694 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400695 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500696 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400697 *
698 * @return N/A
699 */
Andrew Boie468190a2017-09-29 14:00:48 -0700700__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400701
702/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500703 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400704 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500705 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400706 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500707 * If @a thread is not currently sleeping, the routine has no effect.
708 *
709 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400710 *
711 * @return N/A
712 */
Andrew Boie468190a2017-09-29 14:00:48 -0700713__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400714
715/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500716 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400717 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500718 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400719 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700720__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400721
722/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500723 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400724 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500725 * This routine prevents @a thread from executing if it has not yet started
726 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400727 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500728 * @param thread ID of thread to cancel.
729 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700730 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500731 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700732 *
733 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400734 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700735__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400736
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400737/**
Allan Stephensc98da842016-11-11 15:45:03 -0500738 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400739 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500740 * This routine permanently stops execution of @a thread. The thread is taken
741 * off all kernel queues it is part of (i.e. the ready queue, the timeout
742 * queue, or a kernel object wait queue). However, any kernel resources the
743 * thread might currently own (such as mutexes or memory blocks) are not
744 * released. It is the responsibility of the caller of this routine to ensure
745 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400746 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500747 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400748 *
749 * @return N/A
750 */
Andrew Boie468190a2017-09-29 14:00:48 -0700751__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400752
Andrew Boie7d627c52017-08-30 11:01:56 -0700753
754/**
755 * @brief Start an inactive thread
756 *
757 * If a thread was created with K_FOREVER in the delay parameter, it will
758 * not be added to the scheduling queue until this function is called
759 * on it.
760 *
761 * @param thread thread to start
762 */
Andrew Boie468190a2017-09-29 14:00:48 -0700763__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700764
Allan Stephensc98da842016-11-11 15:45:03 -0500765/**
766 * @cond INTERNAL_HIDDEN
767 */
768
Benjamin Walshd211a522016-12-06 11:44:01 -0500769/* timeout has timed out and is not on _timeout_q anymore */
770#define _EXPIRED (-2)
771
772/* timeout is not in use */
773#define _INACTIVE (-1)
774
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400775struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700776 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700777 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400778 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700779 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500780 void *init_p1;
781 void *init_p2;
782 void *init_p3;
783 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500784 u32_t init_options;
785 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500786 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400787};
788
Andrew Boied26cf2d2017-03-30 13:07:02 -0700789#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400790 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500791 prio, options, delay, abort, groups) \
792 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700793 .init_thread = (thread), \
794 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500795 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700796 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400797 .init_p1 = (void *)p1, \
798 .init_p2 = (void *)p2, \
799 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500800 .init_prio = (prio), \
801 .init_options = (options), \
802 .init_delay = (delay), \
803 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400804 }
805
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400806/**
Allan Stephensc98da842016-11-11 15:45:03 -0500807 * INTERNAL_HIDDEN @endcond
808 */
809
810/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500811 * @brief Statically define and initialize a thread.
812 *
813 * The thread may be scheduled for immediate execution or a delayed start.
814 *
815 * Thread options are architecture-specific, and can include K_ESSENTIAL,
816 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
817 * them using "|" (the logical OR operator).
818 *
819 * The ID of the thread can be accessed using:
820 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500821 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500822 *
823 * @param name Name of the thread.
824 * @param stack_size Stack size in bytes.
825 * @param entry Thread entry function.
826 * @param p1 1st entry point parameter.
827 * @param p2 2nd entry point parameter.
828 * @param p3 3rd entry point parameter.
829 * @param prio Thread priority.
830 * @param options Thread options.
831 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400832 *
833 * @internal It has been observed that the x86 compiler by default aligns
834 * these _static_thread_data structures to 32-byte boundaries, thereby
835 * wasting space. To work around this, force a 4-byte alignment.
836 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500837#define K_THREAD_DEFINE(name, stack_size, \
838 entry, p1, p2, p3, \
839 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700840 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700841 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500842 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500843 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700844 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
845 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500846 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700847 NULL, 0); \
848 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400849
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400850/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500851 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400852 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500853 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400854 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500855 * @param thread ID of thread whose priority is needed.
856 *
857 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400858 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700859__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400860
861/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500862 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400863 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500864 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400865 *
866 * Rescheduling can occur immediately depending on the priority @a thread is
867 * set to:
868 *
869 * - If its priority is raised above the priority of the caller of this
870 * function, and the caller is preemptible, @a thread will be scheduled in.
871 *
872 * - If the caller operates on itself, it lowers its priority below that of
873 * other threads in the system, and the caller is preemptible, the thread of
874 * highest priority will be scheduled in.
875 *
876 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
877 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
878 * highest priority.
879 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500880 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400881 * @param prio New priority.
882 *
883 * @warning Changing the priority of a thread currently involved in mutex
884 * priority inheritance may result in undefined behavior.
885 *
886 * @return N/A
887 */
Andrew Boie468190a2017-09-29 14:00:48 -0700888__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400889
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400890/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500891 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400892 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500893 * This routine prevents the kernel scheduler from making @a thread the
894 * current thread. All other internal operations on @a thread are still
895 * performed; for example, any timeout it is waiting on keeps ticking,
896 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400897 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500898 * If @a thread is already suspended, the routine has no effect.
899 *
900 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400901 *
902 * @return N/A
903 */
Andrew Boie468190a2017-09-29 14:00:48 -0700904__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400905
906/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500907 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400908 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500909 * This routine allows the kernel scheduler to make @a thread the current
910 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400911 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500912 * If @a thread is not currently suspended, the routine has no effect.
913 *
914 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400915 *
916 * @return N/A
917 */
Andrew Boie468190a2017-09-29 14:00:48 -0700918__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400919
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400920/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500921 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400922 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500923 * This routine specifies how the scheduler will perform time slicing of
924 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400925 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500926 * To enable time slicing, @a slice must be non-zero. The scheduler
927 * ensures that no thread runs for more than the specified time limit
928 * before other threads of that priority are given a chance to execute.
929 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700930 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400931 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500932 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400933 * execute. Once the scheduler selects a thread for execution, there is no
934 * minimum guaranteed time the thread will execute before threads of greater or
935 * equal priority are scheduled.
936 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500937 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400938 * for execution, this routine has no effect; the thread is immediately
939 * rescheduled after the slice period expires.
940 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500941 * To disable timeslicing, set both @a slice and @a prio to zero.
942 *
943 * @param slice Maximum time slice length (in milliseconds).
944 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400945 *
946 * @return N/A
947 */
Kumar Galacc334c72017-04-21 10:55:34 -0500948extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400949
Anas Nashif166f5192018-02-25 08:02:36 -0600950/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -0500951
952/**
953 * @addtogroup isr_apis
954 * @{
955 */
956
957/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500958 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400959 *
Allan Stephensc98da842016-11-11 15:45:03 -0500960 * This routine allows the caller to customize its actions, depending on
961 * whether it is a thread or an ISR.
962 *
963 * @note Can be called by ISRs.
964 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500965 * @return 0 if invoked by a thread.
966 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400967 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500968extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400969
Benjamin Walsh445830d2016-11-10 15:54:27 -0500970/**
971 * @brief Determine if code is running in a preemptible thread.
972 *
Allan Stephensc98da842016-11-11 15:45:03 -0500973 * This routine allows the caller to customize its actions, depending on
974 * whether it can be preempted by another thread. The routine returns a 'true'
975 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500976 *
Allan Stephensc98da842016-11-11 15:45:03 -0500977 * - The code is running in a thread, not at ISR.
978 * - The thread's priority is in the preemptible range.
979 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500980 *
Allan Stephensc98da842016-11-11 15:45:03 -0500981 * @note Can be called by ISRs.
982 *
983 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500984 * @return Non-zero if invoked by a preemptible thread.
985 */
Andrew Boie468190a2017-09-29 14:00:48 -0700986__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -0500987
Allan Stephensc98da842016-11-11 15:45:03 -0500988/**
Anas Nashif166f5192018-02-25 08:02:36 -0600989 * @}
Allan Stephensc98da842016-11-11 15:45:03 -0500990 */
991
992/**
993 * @addtogroup thread_apis
994 * @{
995 */
996
997/**
998 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500999 *
Allan Stephensc98da842016-11-11 15:45:03 -05001000 * This routine prevents the current thread from being preempted by another
1001 * thread by instructing the scheduler to treat it as a cooperative thread.
1002 * If the thread subsequently performs an operation that makes it unready,
1003 * it will be context switched out in the normal manner. When the thread
1004 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001005 *
Allan Stephensc98da842016-11-11 15:45:03 -05001006 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001007 *
Allan Stephensc98da842016-11-11 15:45:03 -05001008 * @note k_sched_lock() and k_sched_unlock() should normally be used
1009 * when the operation being performed can be safely interrupted by ISRs.
1010 * However, if the amount of processing involved is very small, better
1011 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001012 *
1013 * @return N/A
1014 */
1015extern void k_sched_lock(void);
1016
Allan Stephensc98da842016-11-11 15:45:03 -05001017/**
1018 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001019 *
Allan Stephensc98da842016-11-11 15:45:03 -05001020 * This routine reverses the effect of a previous call to k_sched_lock().
1021 * A thread must call the routine once for each time it called k_sched_lock()
1022 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001023 *
1024 * @return N/A
1025 */
1026extern void k_sched_unlock(void);
1027
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001028/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001029 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001030 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001031 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001032 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001033 * Custom data is not used by the kernel itself, and is freely available
1034 * for a thread to use as it sees fit. It can be used as a framework
1035 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001036 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001037 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001038 *
1039 * @return N/A
1040 */
Andrew Boie468190a2017-09-29 14:00:48 -07001041__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001042
1043/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001044 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001045 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001046 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001047 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001048 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001049 */
Andrew Boie468190a2017-09-29 14:00:48 -07001050__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001051
1052/**
Anas Nashif166f5192018-02-25 08:02:36 -06001053 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001054 */
1055
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001056#include <sys_clock.h>
1057
Allan Stephensc2f15a42016-11-17 12:24:22 -05001058/**
1059 * @addtogroup clock_apis
1060 * @{
1061 */
1062
1063/**
1064 * @brief Generate null timeout delay.
1065 *
1066 * This macro generates a timeout delay that that instructs a kernel API
1067 * not to wait if the requested operation cannot be performed immediately.
1068 *
1069 * @return Timeout delay value.
1070 */
1071#define K_NO_WAIT 0
1072
1073/**
1074 * @brief Generate timeout delay from milliseconds.
1075 *
1076 * This macro generates a timeout delay that that instructs a kernel API
1077 * to wait up to @a ms milliseconds to perform the requested operation.
1078 *
1079 * @param ms Duration in milliseconds.
1080 *
1081 * @return Timeout delay value.
1082 */
Johan Hedberg14471692016-11-13 10:52:15 +02001083#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001084
1085/**
1086 * @brief Generate timeout delay from seconds.
1087 *
1088 * This macro generates a timeout delay that that instructs a kernel API
1089 * to wait up to @a s seconds to perform the requested operation.
1090 *
1091 * @param s Duration in seconds.
1092 *
1093 * @return Timeout delay value.
1094 */
Johan Hedberg14471692016-11-13 10:52:15 +02001095#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001096
1097/**
1098 * @brief Generate timeout delay from minutes.
1099 *
1100 * This macro generates a timeout delay that that instructs a kernel API
1101 * to wait up to @a m minutes to perform the requested operation.
1102 *
1103 * @param m Duration in minutes.
1104 *
1105 * @return Timeout delay value.
1106 */
Johan Hedberg14471692016-11-13 10:52:15 +02001107#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001108
1109/**
1110 * @brief Generate timeout delay from hours.
1111 *
1112 * This macro generates a timeout delay that that instructs a kernel API
1113 * to wait up to @a h hours to perform the requested operation.
1114 *
1115 * @param h Duration in hours.
1116 *
1117 * @return Timeout delay value.
1118 */
Johan Hedberg14471692016-11-13 10:52:15 +02001119#define K_HOURS(h) K_MINUTES((h) * 60)
1120
Allan Stephensc98da842016-11-11 15:45:03 -05001121/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001122 * @brief Generate infinite timeout delay.
1123 *
1124 * This macro generates a timeout delay that that instructs a kernel API
1125 * to wait as long as necessary to perform the requested operation.
1126 *
1127 * @return Timeout delay value.
1128 */
1129#define K_FOREVER (-1)
1130
1131/**
Anas Nashif166f5192018-02-25 08:02:36 -06001132 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001133 */
1134
1135/**
Allan Stephensc98da842016-11-11 15:45:03 -05001136 * @cond INTERNAL_HIDDEN
1137 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001138
Benjamin Walsh62092182016-12-20 14:39:08 -05001139/* kernel clocks */
1140
1141#if (sys_clock_ticks_per_sec == 1000) || \
1142 (sys_clock_ticks_per_sec == 500) || \
1143 (sys_clock_ticks_per_sec == 250) || \
1144 (sys_clock_ticks_per_sec == 125) || \
1145 (sys_clock_ticks_per_sec == 100) || \
1146 (sys_clock_ticks_per_sec == 50) || \
1147 (sys_clock_ticks_per_sec == 25) || \
1148 (sys_clock_ticks_per_sec == 20) || \
1149 (sys_clock_ticks_per_sec == 10) || \
1150 (sys_clock_ticks_per_sec == 1)
1151
1152 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1153#else
1154 /* yields horrible 64-bit math on many architectures: try to avoid */
1155 #define _NON_OPTIMIZED_TICKS_PER_SEC
1156#endif
1157
1158#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001159extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001160#else
Kumar Galacc334c72017-04-21 10:55:34 -05001161static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001162{
Kumar Galacc334c72017-04-21 10:55:34 -05001163 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001164}
1165#endif
1166
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001167/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001168#ifdef CONFIG_TICKLESS_KERNEL
1169#define _TICK_ALIGN 0
1170#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001171#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001172#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001173
Kumar Galacc334c72017-04-21 10:55:34 -05001174static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001175{
Benjamin Walsh62092182016-12-20 14:39:08 -05001176#ifdef CONFIG_SYS_CLOCK_EXISTS
1177
1178#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001179 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001180#else
Kumar Galacc334c72017-04-21 10:55:34 -05001181 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001182#endif
1183
1184#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001185 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001186 return 0;
1187#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001188}
1189
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001190struct k_timer {
1191 /*
1192 * _timeout structure must be first here if we want to use
1193 * dynamic timer allocation. timeout.node is used in the double-linked
1194 * list of free timers
1195 */
1196 struct _timeout timeout;
1197
Allan Stephens45bfa372016-10-12 12:39:42 -05001198 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001199 _wait_q_t wait_q;
1200
1201 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001202 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001203
1204 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001205 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001206
1207 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001208 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001209
Allan Stephens45bfa372016-10-12 12:39:42 -05001210 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001211 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001212
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001213 /* user-specific data, also used to support legacy features */
1214 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001215
Anas Nashif2f203c22016-12-18 06:57:45 -05001216 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001217};
1218
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001219#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001220 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001221 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001222 .timeout.wait_q = NULL, \
1223 .timeout.thread = NULL, \
1224 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001225 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001226 .expiry_fn = expiry, \
1227 .stop_fn = stop, \
1228 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001229 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001230 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001231 }
1232
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001233#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1234
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001235/**
Allan Stephensc98da842016-11-11 15:45:03 -05001236 * INTERNAL_HIDDEN @endcond
1237 */
1238
1239/**
1240 * @defgroup timer_apis Timer APIs
1241 * @ingroup kernel_apis
1242 * @{
1243 */
1244
1245/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001246 * @typedef k_timer_expiry_t
1247 * @brief Timer expiry function type.
1248 *
1249 * A timer's expiry function is executed by the system clock interrupt handler
1250 * each time the timer expires. The expiry function is optional, and is only
1251 * invoked if the timer has been initialized with one.
1252 *
1253 * @param timer Address of timer.
1254 *
1255 * @return N/A
1256 */
1257typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1258
1259/**
1260 * @typedef k_timer_stop_t
1261 * @brief Timer stop function type.
1262 *
1263 * A timer's stop function is executed if the timer is stopped prematurely.
1264 * The function runs in the context of the thread that stops the timer.
1265 * The stop function is optional, and is only invoked if the timer has been
1266 * initialized with one.
1267 *
1268 * @param timer Address of timer.
1269 *
1270 * @return N/A
1271 */
1272typedef void (*k_timer_stop_t)(struct k_timer *timer);
1273
1274/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001275 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001276 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001277 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001278 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001279 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001280 *
1281 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001282 * @param expiry_fn Function to invoke each time the timer expires.
1283 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001284 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001285#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001286 struct k_timer name \
1287 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001288 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001289
Allan Stephens45bfa372016-10-12 12:39:42 -05001290/**
1291 * @brief Initialize a timer.
1292 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001293 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001294 *
1295 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001296 * @param expiry_fn Function to invoke each time the timer expires.
1297 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001298 *
1299 * @return N/A
1300 */
1301extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001302 k_timer_expiry_t expiry_fn,
1303 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001304
Allan Stephens45bfa372016-10-12 12:39:42 -05001305/**
1306 * @brief Start a timer.
1307 *
1308 * This routine starts a timer, and resets its status to zero. The timer
1309 * begins counting down using the specified duration and period values.
1310 *
1311 * Attempting to start a timer that is already running is permitted.
1312 * The timer's status is reset to zero and the timer begins counting down
1313 * using the new duration and period values.
1314 *
1315 * @param timer Address of timer.
1316 * @param duration Initial timer duration (in milliseconds).
1317 * @param period Timer period (in milliseconds).
1318 *
1319 * @return N/A
1320 */
Andrew Boiea354d492017-09-29 16:22:28 -07001321__syscall void k_timer_start(struct k_timer *timer,
1322 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001323
1324/**
1325 * @brief Stop a timer.
1326 *
1327 * This routine stops a running timer prematurely. The timer's stop function,
1328 * if one exists, is invoked by the caller.
1329 *
1330 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001331 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001332 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001333 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1334 * if @a k_timer_stop is to be called from ISRs.
1335 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001336 * @param timer Address of timer.
1337 *
1338 * @return N/A
1339 */
Andrew Boiea354d492017-09-29 16:22:28 -07001340__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001341
1342/**
1343 * @brief Read timer status.
1344 *
1345 * This routine reads the timer's status, which indicates the number of times
1346 * it has expired since its status was last read.
1347 *
1348 * Calling this routine resets the timer's status to zero.
1349 *
1350 * @param timer Address of timer.
1351 *
1352 * @return Timer status.
1353 */
Andrew Boiea354d492017-09-29 16:22:28 -07001354__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001355
1356/**
1357 * @brief Synchronize thread to timer expiration.
1358 *
1359 * This routine blocks the calling thread until the timer's status is non-zero
1360 * (indicating that it has expired at least once since it was last examined)
1361 * or the timer is stopped. If the timer status is already non-zero,
1362 * or the timer is already stopped, the caller continues without waiting.
1363 *
1364 * Calling this routine resets the timer's status to zero.
1365 *
1366 * This routine must not be used by interrupt handlers, since they are not
1367 * allowed to block.
1368 *
1369 * @param timer Address of timer.
1370 *
1371 * @return Timer status.
1372 */
Andrew Boiea354d492017-09-29 16:22:28 -07001373__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001374
1375/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001376 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001377 *
1378 * This routine computes the (approximate) time remaining before a running
1379 * timer next expires. If the timer is not running, it returns zero.
1380 *
1381 * @param timer Address of timer.
1382 *
1383 * @return Remaining time (in milliseconds).
1384 */
Andrew Boiea354d492017-09-29 16:22:28 -07001385__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1386
1387static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001388{
1389 return _timeout_remaining_get(&timer->timeout);
1390}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001391
Allan Stephensc98da842016-11-11 15:45:03 -05001392/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001393 * @brief Associate user-specific data with a timer.
1394 *
1395 * This routine records the @a user_data with the @a timer, to be retrieved
1396 * later.
1397 *
1398 * It can be used e.g. in a timer handler shared across multiple subsystems to
1399 * retrieve data specific to the subsystem this timer is associated with.
1400 *
1401 * @param timer Address of timer.
1402 * @param user_data User data to associate with the timer.
1403 *
1404 * @return N/A
1405 */
Andrew Boiea354d492017-09-29 16:22:28 -07001406__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1407
Anas Nashif954d5502018-02-25 08:37:28 -06001408/**
1409 * @internal
1410 */
Andrew Boiea354d492017-09-29 16:22:28 -07001411static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1412 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001413{
1414 timer->user_data = user_data;
1415}
1416
1417/**
1418 * @brief Retrieve the user-specific data from a timer.
1419 *
1420 * @param timer Address of timer.
1421 *
1422 * @return The user data.
1423 */
Andrew Boiea354d492017-09-29 16:22:28 -07001424__syscall void *k_timer_user_data_get(struct k_timer *timer);
1425
1426static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001427{
1428 return timer->user_data;
1429}
1430
Anas Nashif166f5192018-02-25 08:02:36 -06001431/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001432
Allan Stephensc98da842016-11-11 15:45:03 -05001433/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001434 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001435 * @{
1436 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001437
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001438/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001439 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001440 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001441 * This routine returns the elapsed time since the system booted,
1442 * in milliseconds.
1443 *
1444 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001445 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001446__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001447
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001448/**
1449 * @brief Enable clock always on in tickless kernel
1450 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001451 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001452 * there are no timer events programmed in tickless kernel
1453 * scheduling. This is necessary if the clock is used to track
1454 * passage of time.
1455 *
1456 * @retval prev_status Previous status of always on flag
1457 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301458#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001459static inline int k_enable_sys_clock_always_on(void)
1460{
1461 int prev_status = _sys_clock_always_on;
1462
1463 _sys_clock_always_on = 1;
1464 _enable_sys_clock();
1465
1466 return prev_status;
1467}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301468#else
1469#define k_enable_sys_clock_always_on() do { } while ((0))
1470#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001471
1472/**
1473 * @brief Disable clock always on in tickless kernel
1474 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001475 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001476 * there are no timer events programmed in tickless kernel
1477 * scheduling. To save power, this routine should be called
1478 * immediately when clock is not used to track time.
1479 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301480#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001481static inline void k_disable_sys_clock_always_on(void)
1482{
1483 _sys_clock_always_on = 0;
1484}
1485#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001486#define k_disable_sys_clock_always_on() do { } while ((0))
1487#endif
1488
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001489/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001490 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001491 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001492 * This routine returns the lower 32-bits of the elapsed time since the system
1493 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001494 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001495 * This routine can be more efficient than k_uptime_get(), as it reduces the
1496 * need for interrupt locking and 64-bit math. However, the 32-bit result
1497 * cannot hold a system uptime time larger than approximately 50 days, so the
1498 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001499 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001500 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001501 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001502__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001503
1504/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001505 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001506 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001507 * This routine computes the elapsed time between the current system uptime
1508 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001509 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001510 * @param reftime Pointer to a reference time, which is updated to the current
1511 * uptime upon return.
1512 *
1513 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001514 */
Kumar Galacc334c72017-04-21 10:55:34 -05001515extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001516
1517/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001518 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001519 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001520 * This routine computes the elapsed time between the current system uptime
1521 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001522 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001523 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1524 * need for interrupt locking and 64-bit math. However, the 32-bit result
1525 * cannot hold an elapsed time larger than approximately 50 days, so the
1526 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001527 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001528 * @param reftime Pointer to a reference time, which is updated to the current
1529 * uptime upon return.
1530 *
1531 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001532 */
Kumar Galacc334c72017-04-21 10:55:34 -05001533extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001534
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001535/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001536 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001537 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001538 * This routine returns the current time, as measured by the system's hardware
1539 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001540 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001541 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001542 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001543#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001544
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001545/**
Anas Nashif166f5192018-02-25 08:02:36 -06001546 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001547 */
1548
Allan Stephensc98da842016-11-11 15:45:03 -05001549/**
1550 * @cond INTERNAL_HIDDEN
1551 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001552
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001553struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001554 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001555 union {
1556 _wait_q_t wait_q;
1557
1558 _POLL_EVENT;
1559 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001560
1561 _OBJECT_TRACING_NEXT_PTR(k_queue);
1562};
1563
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001564#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001565 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001566 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Michael Hope5f67a612018-03-17 12:44:40 +01001567 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001568 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001569 _OBJECT_TRACING_INIT \
1570 }
1571
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001572#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1573
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001574/**
1575 * INTERNAL_HIDDEN @endcond
1576 */
1577
1578/**
1579 * @defgroup queue_apis Queue APIs
1580 * @ingroup kernel_apis
1581 * @{
1582 */
1583
1584/**
1585 * @brief Initialize a queue.
1586 *
1587 * This routine initializes a queue object, prior to its first use.
1588 *
1589 * @param queue Address of the queue.
1590 *
1591 * @return N/A
1592 */
1593extern void k_queue_init(struct k_queue *queue);
1594
1595/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001596 * @brief Cancel waiting on a queue.
1597 *
1598 * This routine causes first thread pending on @a queue, if any, to
1599 * return from k_queue_get() call with NULL value (as if timeout expired).
1600 *
1601 * @note Can be called by ISRs.
1602 *
1603 * @param queue Address of the queue.
1604 *
1605 * @return N/A
1606 */
1607extern void k_queue_cancel_wait(struct k_queue *queue);
1608
1609/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001610 * @brief Append an element to the end of a queue.
1611 *
1612 * This routine appends a data item to @a queue. A queue data item must be
1613 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1614 * reserved for the kernel's use.
1615 *
1616 * @note Can be called by ISRs.
1617 *
1618 * @param queue Address of the queue.
1619 * @param data Address of the data item.
1620 *
1621 * @return N/A
1622 */
1623extern void k_queue_append(struct k_queue *queue, void *data);
1624
1625/**
1626 * @brief Prepend an element to a queue.
1627 *
1628 * This routine prepends a data item to @a queue. A queue data item must be
1629 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1630 * reserved for the kernel's use.
1631 *
1632 * @note Can be called by ISRs.
1633 *
1634 * @param queue Address of the queue.
1635 * @param data Address of the data item.
1636 *
1637 * @return N/A
1638 */
1639extern void k_queue_prepend(struct k_queue *queue, void *data);
1640
1641/**
1642 * @brief Inserts an element to a queue.
1643 *
1644 * This routine inserts a data item to @a queue after previous item. A queue
1645 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1646 * item are reserved for the kernel's use.
1647 *
1648 * @note Can be called by ISRs.
1649 *
1650 * @param queue Address of the queue.
1651 * @param prev Address of the previous data item.
1652 * @param data Address of the data item.
1653 *
1654 * @return N/A
1655 */
1656extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1657
1658/**
1659 * @brief Atomically append a list of elements to a queue.
1660 *
1661 * This routine adds a list of data items to @a queue in one operation.
1662 * The data items must be in a singly-linked list, with the first 32 bits
1663 * in each data item pointing to the next data item; the list must be
1664 * NULL-terminated.
1665 *
1666 * @note Can be called by ISRs.
1667 *
1668 * @param queue Address of the queue.
1669 * @param head Pointer to first node in singly-linked list.
1670 * @param tail Pointer to last node in singly-linked list.
1671 *
1672 * @return N/A
1673 */
1674extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1675
1676/**
1677 * @brief Atomically add a list of elements to a queue.
1678 *
1679 * This routine adds a list of data items to @a queue in one operation.
1680 * The data items must be in a singly-linked list implemented using a
1681 * sys_slist_t object. Upon completion, the original list is empty.
1682 *
1683 * @note Can be called by ISRs.
1684 *
1685 * @param queue Address of the queue.
1686 * @param list Pointer to sys_slist_t object.
1687 *
1688 * @return N/A
1689 */
1690extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1691
1692/**
1693 * @brief Get an element from a queue.
1694 *
1695 * This routine removes first data item from @a queue. The first 32 bits of the
1696 * data item are reserved for the kernel's use.
1697 *
1698 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1699 *
1700 * @param queue Address of the queue.
1701 * @param timeout Waiting period to obtain a data item (in milliseconds),
1702 * or one of the special values K_NO_WAIT and K_FOREVER.
1703 *
1704 * @return Address of the data item if successful; NULL if returned
1705 * without waiting, or waiting period timed out.
1706 */
Kumar Galacc334c72017-04-21 10:55:34 -05001707extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001708
1709/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001710 * @brief Remove an element from a queue.
1711 *
1712 * This routine removes data item from @a queue. The first 32 bits of the
1713 * data item are reserved for the kernel's use. Removing elements from k_queue
1714 * rely on sys_slist_find_and_remove which is not a constant time operation.
1715 *
1716 * @note Can be called by ISRs
1717 *
1718 * @param queue Address of the queue.
1719 * @param data Address of the data item.
1720 *
1721 * @return true if data item was removed
1722 */
1723static inline bool k_queue_remove(struct k_queue *queue, void *data)
1724{
1725 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1726}
1727
1728/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001729 * @brief Query a queue to see if it has data available.
1730 *
1731 * Note that the data might be already gone by the time this function returns
1732 * if other threads are also trying to read from the queue.
1733 *
1734 * @note Can be called by ISRs.
1735 *
1736 * @param queue Address of the queue.
1737 *
1738 * @return Non-zero if the queue is empty.
1739 * @return 0 if data is available.
1740 */
1741static inline int k_queue_is_empty(struct k_queue *queue)
1742{
1743 return (int)sys_slist_is_empty(&queue->data_q);
1744}
1745
1746/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001747 * @brief Peek element at the head of queue.
1748 *
1749 * Return element from the head of queue without removing it.
1750 *
1751 * @param queue Address of the queue.
1752 *
1753 * @return Head element, or NULL if queue is empty.
1754 */
1755static inline void *k_queue_peek_head(struct k_queue *queue)
1756{
1757 return sys_slist_peek_head(&queue->data_q);
1758}
1759
1760/**
1761 * @brief Peek element at the tail of queue.
1762 *
1763 * Return element from the tail of queue without removing it.
1764 *
1765 * @param queue Address of the queue.
1766 *
1767 * @return Tail element, or NULL if queue is empty.
1768 */
1769static inline void *k_queue_peek_tail(struct k_queue *queue)
1770{
1771 return sys_slist_peek_tail(&queue->data_q);
1772}
1773
1774/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001775 * @brief Statically define and initialize a queue.
1776 *
1777 * The queue can be accessed outside the module where it is defined using:
1778 *
1779 * @code extern struct k_queue <name>; @endcode
1780 *
1781 * @param name Name of the queue.
1782 */
1783#define K_QUEUE_DEFINE(name) \
1784 struct k_queue name \
1785 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001786 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001787
Anas Nashif166f5192018-02-25 08:02:36 -06001788/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001789
1790/**
1791 * @cond INTERNAL_HIDDEN
1792 */
1793
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001794struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001795 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001796};
1797
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001798#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001799 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001800 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001801 }
1802
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001803#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1804
Allan Stephensc98da842016-11-11 15:45:03 -05001805/**
1806 * INTERNAL_HIDDEN @endcond
1807 */
1808
1809/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001810 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05001811 * @ingroup kernel_apis
1812 * @{
1813 */
1814
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001815/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001816 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001817 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001818 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001819 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001820 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001821 *
1822 * @return N/A
1823 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001824#define k_fifo_init(fifo) \
1825 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001826
1827/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001828 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001829 *
1830 * This routine causes first thread pending on @a fifo, if any, to
1831 * return from k_fifo_get() call with NULL value (as if timeout
1832 * expired).
1833 *
1834 * @note Can be called by ISRs.
1835 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001836 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001837 *
1838 * @return N/A
1839 */
1840#define k_fifo_cancel_wait(fifo) \
1841 k_queue_cancel_wait((struct k_queue *) fifo)
1842
1843/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001844 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001845 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001846 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001847 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1848 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001849 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001850 * @note Can be called by ISRs.
1851 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001852 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001853 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001854 *
1855 * @return N/A
1856 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001857#define k_fifo_put(fifo, data) \
1858 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001859
1860/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001861 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001862 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001863 * This routine adds a list of data items to @a fifo in one operation.
1864 * The data items must be in a singly-linked list, with the first 32 bits
1865 * each data item pointing to the next data item; the list must be
1866 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001867 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001868 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001869 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001870 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001871 * @param head Pointer to first node in singly-linked list.
1872 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001873 *
1874 * @return N/A
1875 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001876#define k_fifo_put_list(fifo, head, tail) \
1877 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001878
1879/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001880 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001882 * This routine adds a list of data items to @a fifo in one operation.
1883 * The data items must be in a singly-linked list implemented using a
1884 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001885 * and must be re-initialized via sys_slist_init().
1886 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001887 * @note Can be called by ISRs.
1888 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001889 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001890 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001891 *
1892 * @return N/A
1893 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001894#define k_fifo_put_slist(fifo, list) \
1895 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001896
1897/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001898 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001899 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001900 * This routine removes a data item from @a fifo in a "first in, first out"
1901 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001902 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001903 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1904 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001905 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001906 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001907 * or one of the special values K_NO_WAIT and K_FOREVER.
1908 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001909 * @return Address of the data item if successful; NULL if returned
1910 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001911 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001912#define k_fifo_get(fifo, timeout) \
1913 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001914
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001915/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001916 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001917 *
1918 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06001919 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001920 *
1921 * @note Can be called by ISRs.
1922 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001923 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001924 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001925 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001926 * @return 0 if data is available.
1927 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001928#define k_fifo_is_empty(fifo) \
1929 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001930
1931/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001932 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001933 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001934 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05301935 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001936 * on each iteration of processing, a head container will be peeked,
1937 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06001938 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001939 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001940 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001941 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001942 * @return Head element, or NULL if the FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001943 */
1944#define k_fifo_peek_head(fifo) \
1945 k_queue_peek_head((struct k_queue *) fifo)
1946
1947/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001948 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001949 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001950 * Return element from the tail of FIFO queue (without removing it). A usecase
1951 * for this is if elements of the FIFO queue are themselves containers. Then
1952 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001953 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001954 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001955 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001956 * @return Tail element, or NULL if a FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001957 */
1958#define k_fifo_peek_tail(fifo) \
1959 k_queue_peek_tail((struct k_queue *) fifo)
1960
1961/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001962 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001963 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001964 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001965 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001966 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001967 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001968 * @param name Name of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001969 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001970#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001971 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001972 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001973 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001974
Anas Nashif166f5192018-02-25 08:02:36 -06001975/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001976
1977/**
1978 * @cond INTERNAL_HIDDEN
1979 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001980
1981struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001982 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001983};
1984
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001985#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001986 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001987 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001988 }
1989
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001990#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1991
Allan Stephensc98da842016-11-11 15:45:03 -05001992/**
1993 * INTERNAL_HIDDEN @endcond
1994 */
1995
1996/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001997 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05001998 * @ingroup kernel_apis
1999 * @{
2000 */
2001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002002/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002003 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002004 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002005 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002006 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002007 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002008 *
2009 * @return N/A
2010 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002011#define k_lifo_init(lifo) \
2012 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002013
2014/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002015 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002016 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002017 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002018 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2019 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002020 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002021 * @note Can be called by ISRs.
2022 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002023 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002024 * @param data Address of the data item.
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_put(lifo, data) \
2029 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002030
2031/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002032 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002033 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002034 * This routine removes a data item from @a lifo in a "last in, first out"
2035 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002036 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002037 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2038 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002039 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002040 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002041 * or one of the special values K_NO_WAIT and K_FOREVER.
2042 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002043 * @return Address of the data item if successful; NULL if returned
2044 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002045 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002046#define k_lifo_get(lifo, timeout) \
2047 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002048
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002049/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002050 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002051 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002052 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002053 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002054 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002055 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002056 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002057 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002058#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002059 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002060 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002061 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002062
Anas Nashif166f5192018-02-25 08:02:36 -06002063/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002064
2065/**
2066 * @cond INTERNAL_HIDDEN
2067 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002068
2069struct k_stack {
2070 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002071 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002072
Anas Nashif2f203c22016-12-18 06:57:45 -05002073 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002074};
2075
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002076#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002077 { \
2078 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2079 .base = stack_buffer, \
2080 .next = stack_buffer, \
2081 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002082 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002083 }
2084
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002085#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2086
Allan Stephensc98da842016-11-11 15:45:03 -05002087/**
2088 * INTERNAL_HIDDEN @endcond
2089 */
2090
2091/**
2092 * @defgroup stack_apis Stack APIs
2093 * @ingroup kernel_apis
2094 * @{
2095 */
2096
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002097/**
2098 * @brief Initialize a stack.
2099 *
2100 * This routine initializes a stack object, prior to its first use.
2101 *
2102 * @param stack Address of the stack.
2103 * @param buffer Address of array used to hold stacked values.
2104 * @param num_entries Maximum number of values that can be stacked.
2105 *
2106 * @return N/A
2107 */
Andrew Boiee8734462017-09-29 16:42:07 -07002108__syscall void k_stack_init(struct k_stack *stack,
Andrew Boie8e3e6d02017-10-12 14:01:25 -07002109 u32_t *buffer, unsigned int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002110
2111/**
2112 * @brief Push an element onto a stack.
2113 *
2114 * This routine adds a 32-bit value @a data to @a stack.
2115 *
2116 * @note Can be called by ISRs.
2117 *
2118 * @param stack Address of the stack.
2119 * @param data Value to push onto the stack.
2120 *
2121 * @return N/A
2122 */
Andrew Boiee8734462017-09-29 16:42:07 -07002123__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002124
2125/**
2126 * @brief Pop an element from a stack.
2127 *
2128 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2129 * manner and stores the value in @a data.
2130 *
2131 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2132 *
2133 * @param stack Address of the stack.
2134 * @param data Address of area to hold the value popped from the stack.
2135 * @param timeout Waiting period to obtain a value (in milliseconds),
2136 * or one of the special values K_NO_WAIT and K_FOREVER.
2137 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002138 * @retval 0 Element popped from stack.
2139 * @retval -EBUSY Returned without waiting.
2140 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002141 */
Andrew Boiee8734462017-09-29 16:42:07 -07002142__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002143
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002144/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002145 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002146 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002147 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002148 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002149 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002150 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002151 * @param name Name of the stack.
2152 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002153 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002154#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002155 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002156 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002157 struct k_stack name \
2158 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002159 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002160 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002161
Anas Nashif166f5192018-02-25 08:02:36 -06002162/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002163
Allan Stephens6bba9b02016-11-16 14:56:54 -05002164struct k_work;
2165
Allan Stephensc98da842016-11-11 15:45:03 -05002166/**
2167 * @defgroup workqueue_apis Workqueue Thread APIs
2168 * @ingroup kernel_apis
2169 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002170 */
2171
Allan Stephens6bba9b02016-11-16 14:56:54 -05002172/**
2173 * @typedef k_work_handler_t
2174 * @brief Work item handler function type.
2175 *
2176 * A work item's handler function is executed by a workqueue's thread
2177 * when the work item is processed by the workqueue.
2178 *
2179 * @param work Address of the work item.
2180 *
2181 * @return N/A
2182 */
2183typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002184
2185/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002186 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002187 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002188
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002189struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002190 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002191 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002192};
2193
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002194enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002195 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002196};
2197
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002198struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002199 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002200 k_work_handler_t handler;
2201 atomic_t flags[1];
2202};
2203
Allan Stephens6bba9b02016-11-16 14:56:54 -05002204struct k_delayed_work {
2205 struct k_work work;
2206 struct _timeout timeout;
2207 struct k_work_q *work_q;
2208};
2209
2210extern struct k_work_q k_sys_work_q;
2211
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002212/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002213 * INTERNAL_HIDDEN @endcond
2214 */
2215
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002216#define _K_WORK_INITIALIZER(work_handler) \
2217 { \
2218 ._reserved = NULL, \
2219 .handler = work_handler, \
2220 .flags = { 0 } \
2221 }
2222
2223#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2224
Allan Stephens6bba9b02016-11-16 14:56:54 -05002225/**
2226 * @brief Initialize a statically-defined work item.
2227 *
2228 * This macro can be used to initialize a statically-defined workqueue work
2229 * item, prior to its first use. For example,
2230 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002231 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002232 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002233 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002234 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002235 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002236#define K_WORK_DEFINE(work, work_handler) \
2237 struct k_work work \
2238 __in_section(_k_work, static, work) = \
2239 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002240
2241/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002242 * @brief Initialize a work item.
2243 *
2244 * This routine initializes a workqueue work item, prior to its first use.
2245 *
2246 * @param work Address of work item.
2247 * @param handler Function to invoke each time work item is processed.
2248 *
2249 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002250 */
2251static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2252{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002253 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002254 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002255 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002256}
2257
2258/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002259 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002260 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002261 * This routine submits work item @a work to be processed by workqueue
2262 * @a work_q. If the work item is already pending in the workqueue's queue
2263 * as a result of an earlier submission, this routine has no effect on the
2264 * work item. If the work item has already been processed, or is currently
2265 * being processed, its work is considered complete and the work item can be
2266 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002267 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002268 * @warning
2269 * A submitted work item must not be modified until it has been processed
2270 * by the workqueue.
2271 *
2272 * @note Can be called by ISRs.
2273 *
2274 * @param work_q Address of workqueue.
2275 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002276 *
2277 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002278 */
2279static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2280 struct k_work *work)
2281{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002282 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002283 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002284 }
2285}
2286
2287/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002288 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002289 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002290 * This routine indicates if work item @a work is pending in a workqueue's
2291 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002292 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002293 * @note Can be called by ISRs.
2294 *
2295 * @param work Address of work item.
2296 *
2297 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002298 */
2299static inline int k_work_pending(struct k_work *work)
2300{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002301 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002302}
2303
2304/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002305 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002306 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002307 * This routine starts workqueue @a work_q. The workqueue spawns its work
2308 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002309 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002310 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002311 * @param stack Pointer to work queue thread's stack space, as defined by
2312 * K_THREAD_STACK_DEFINE()
2313 * @param stack_size Size of the work queue thread's stack (in bytes), which
2314 * should either be the same constant passed to
2315 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002316 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002317 *
2318 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002319 */
Andrew Boie507852a2017-07-25 18:47:07 -07002320extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002321 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002322 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002323
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002324/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002325 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002326 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002327 * This routine initializes a workqueue delayed work item, prior to
2328 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002329 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002330 * @param work Address of delayed work item.
2331 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002332 *
2333 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002334 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002335extern void k_delayed_work_init(struct k_delayed_work *work,
2336 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002337
2338/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002339 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002340 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002341 * This routine schedules work item @a work to be processed by workqueue
2342 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002343 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002344 * Only when the countdown completes is the work item actually submitted to
2345 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002346 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002347 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002348 * counting down cancels the existing submission and restarts the
2349 * countdown using the new delay. Note that this behavior is
2350 * inherently subject to race conditions with the pre-existing
2351 * timeouts and work queue, so care must be taken to synchronize such
2352 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002353 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002354 * @warning
2355 * A delayed work item must not be modified until it has been processed
2356 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002357 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002358 * @note Can be called by ISRs.
2359 *
2360 * @param work_q Address of workqueue.
2361 * @param work Address of delayed work item.
2362 * @param delay Delay before submitting the work item (in milliseconds).
2363 *
2364 * @retval 0 Work item countdown started.
2365 * @retval -EINPROGRESS Work item is already pending.
2366 * @retval -EINVAL Work item is being processed or has completed its work.
2367 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002368 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002369extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2370 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002371 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002372
2373/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002374 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002375 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002376 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002377 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002378 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002379 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002380 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002381 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002382 * @param work Address of delayed work item.
2383 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002384 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002385 * @retval -EINPROGRESS Work item is already pending.
2386 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002387 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002388extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002389
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002390/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002391 * @brief Submit a work item to the system workqueue.
2392 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002393 * This routine submits work item @a work to be processed by the system
2394 * workqueue. If the work item is already pending in the workqueue's queue
2395 * as a result of an earlier submission, this routine has no effect on the
2396 * work item. If the work item has already been processed, or is currently
2397 * being processed, its work is considered complete and the work item can be
2398 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002399 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002400 * @warning
2401 * Work items submitted to the system workqueue should avoid using handlers
2402 * that block or yield since this may prevent the system workqueue from
2403 * processing other work items in a timely manner.
2404 *
2405 * @note Can be called by ISRs.
2406 *
2407 * @param work Address of work item.
2408 *
2409 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002410 */
2411static inline void k_work_submit(struct k_work *work)
2412{
2413 k_work_submit_to_queue(&k_sys_work_q, work);
2414}
2415
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002416/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002417 * @brief Submit a delayed work item to the system workqueue.
2418 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002419 * This routine schedules work item @a work to be processed by the system
2420 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002421 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002422 * Only when the countdown completes is the work item actually submitted to
2423 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002424 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002425 * Submitting a previously submitted delayed work item that is still
2426 * counting down cancels the existing submission and restarts the countdown
2427 * using the new delay. If the work item is currently pending on the
2428 * workqueue's queue because the countdown has completed it is too late to
2429 * resubmit the item, and resubmission fails without impacting the work item.
2430 * If the work item has already been processed, or is currently being processed,
2431 * its work is considered complete and the work item can be resubmitted.
2432 *
2433 * @warning
2434 * Work items submitted to the system workqueue should avoid using handlers
2435 * that block or yield since this may prevent the system workqueue from
2436 * processing other work items in a timely manner.
2437 *
2438 * @note Can be called by ISRs.
2439 *
2440 * @param work Address of delayed work item.
2441 * @param delay Delay before submitting the work item (in milliseconds).
2442 *
2443 * @retval 0 Work item countdown started.
2444 * @retval -EINPROGRESS Work item is already pending.
2445 * @retval -EINVAL Work item is being processed or has completed its work.
2446 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002447 */
2448static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002449 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002450{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002451 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002452}
2453
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002454/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002455 * @brief Get time remaining before a delayed work gets scheduled.
2456 *
2457 * This routine computes the (approximate) time remaining before a
2458 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002459 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002460 *
2461 * @param work Delayed work item.
2462 *
2463 * @return Remaining time (in milliseconds).
2464 */
Kumar Galacc334c72017-04-21 10:55:34 -05002465static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002466{
2467 return _timeout_remaining_get(&work->timeout);
2468}
2469
Anas Nashif166f5192018-02-25 08:02:36 -06002470/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002471
Allan Stephensc98da842016-11-11 15:45:03 -05002472/**
2473 * @cond INTERNAL_HIDDEN
2474 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002475
2476struct k_mutex {
2477 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002478 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002479 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002480 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002481
Anas Nashif2f203c22016-12-18 06:57:45 -05002482 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002483};
2484
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002485#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002486 { \
2487 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2488 .owner = NULL, \
2489 .lock_count = 0, \
2490 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002491 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002492 }
2493
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002494#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2495
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002496/**
Allan Stephensc98da842016-11-11 15:45:03 -05002497 * INTERNAL_HIDDEN @endcond
2498 */
2499
2500/**
2501 * @defgroup mutex_apis Mutex APIs
2502 * @ingroup kernel_apis
2503 * @{
2504 */
2505
2506/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002507 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002508 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002509 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002510 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002511 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002512 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002513 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002514 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002515#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002516 struct k_mutex name \
2517 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002518 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002519
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002520/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002521 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002522 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002523 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002524 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002525 * Upon completion, the mutex is available and does not have an owner.
2526 *
2527 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002528 *
2529 * @return N/A
2530 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002531__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002532
2533/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002534 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002535 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002536 * This routine locks @a mutex. If the mutex is locked by another thread,
2537 * the calling thread waits until the mutex becomes available or until
2538 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002539 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002540 * A thread is permitted to lock a mutex it has already locked. The operation
2541 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002542 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002543 * @param mutex Address of the mutex.
2544 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002545 * or one of the special values K_NO_WAIT and K_FOREVER.
2546 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002547 * @retval 0 Mutex locked.
2548 * @retval -EBUSY Returned without waiting.
2549 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002550 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002551__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002552
2553/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002554 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002555 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002556 * This routine unlocks @a mutex. The mutex must already be locked by the
2557 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002558 *
2559 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002560 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002561 * thread.
2562 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002563 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002564 *
2565 * @return N/A
2566 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002567__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002568
Allan Stephensc98da842016-11-11 15:45:03 -05002569/**
Anas Nashif166f5192018-02-25 08:02:36 -06002570 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002571 */
2572
2573/**
2574 * @cond INTERNAL_HIDDEN
2575 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002576
2577struct k_sem {
2578 _wait_q_t wait_q;
2579 unsigned int count;
2580 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002581 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002582
Anas Nashif2f203c22016-12-18 06:57:45 -05002583 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002584};
2585
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002586#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002587 { \
2588 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2589 .count = initial_count, \
2590 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002591 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002592 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002593 }
2594
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002595#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2596
Allan Stephensc98da842016-11-11 15:45:03 -05002597/**
2598 * INTERNAL_HIDDEN @endcond
2599 */
2600
2601/**
2602 * @defgroup semaphore_apis Semaphore APIs
2603 * @ingroup kernel_apis
2604 * @{
2605 */
2606
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002607/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002608 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002609 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002610 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002611 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002612 * @param sem Address of the semaphore.
2613 * @param initial_count Initial semaphore count.
2614 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002615 *
2616 * @return N/A
2617 */
Andrew Boie99280232017-09-29 14:17:47 -07002618__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2619 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002620
2621/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002622 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002623 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002624 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002626 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2627 *
2628 * @param sem Address of the semaphore.
2629 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002630 * or one of the special values K_NO_WAIT and K_FOREVER.
2631 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002632 * @note When porting code from the nanokernel legacy API to the new API, be
2633 * careful with the return value of this function. The return value is the
2634 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2635 * non-zero means failure, while the nano_sem_take family returns 1 for success
2636 * and 0 for failure.
2637 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002638 * @retval 0 Semaphore taken.
2639 * @retval -EBUSY Returned without waiting.
2640 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002641 */
Andrew Boie99280232017-09-29 14:17:47 -07002642__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002643
2644/**
2645 * @brief Give a semaphore.
2646 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002647 * This routine gives @a sem, unless the semaphore is already at its maximum
2648 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002649 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002650 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002651 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002652 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002653 *
2654 * @return N/A
2655 */
Andrew Boie99280232017-09-29 14:17:47 -07002656__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002657
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002658/**
2659 * @brief Reset a semaphore's count to zero.
2660 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002661 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002662 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002663 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002664 *
2665 * @return N/A
2666 */
Andrew Boie990bf162017-10-03 12:36:49 -07002667__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002668
Anas Nashif954d5502018-02-25 08:37:28 -06002669/**
2670 * @internal
2671 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002672static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002673{
2674 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002675}
2676
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002677/**
2678 * @brief Get a semaphore's count.
2679 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002680 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002681 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002682 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002683 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002684 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002685 */
Andrew Boie990bf162017-10-03 12:36:49 -07002686__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002687
Anas Nashif954d5502018-02-25 08:37:28 -06002688/**
2689 * @internal
2690 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002691static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002692{
2693 return sem->count;
2694}
2695
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002696/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002697 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002698 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002699 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002700 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002701 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002702 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002703 * @param name Name of the semaphore.
2704 * @param initial_count Initial semaphore count.
2705 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002706 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002707#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002708 struct k_sem name \
2709 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07002710 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05302711 BUILD_ASSERT(((count_limit) != 0) && \
2712 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002713
Anas Nashif166f5192018-02-25 08:02:36 -06002714/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002715
2716/**
2717 * @defgroup alert_apis Alert APIs
2718 * @ingroup kernel_apis
2719 * @{
2720 */
2721
Allan Stephens5eceb852016-11-16 10:16:30 -05002722/**
2723 * @typedef k_alert_handler_t
2724 * @brief Alert handler function type.
2725 *
2726 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002727 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002728 * and is only invoked if the alert has been initialized with one.
2729 *
2730 * @param alert Address of the alert.
2731 *
2732 * @return 0 if alert has been consumed; non-zero if alert should pend.
2733 */
2734typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002735
Anas Nashif166f5192018-02-25 08:02:36 -06002736/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002737
2738/**
2739 * @cond INTERNAL_HIDDEN
2740 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002741
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002742#define K_ALERT_DEFAULT NULL
2743#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002744
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002745struct k_alert {
2746 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002747 atomic_t send_count;
2748 struct k_work work_item;
2749 struct k_sem sem;
2750
Anas Nashif2f203c22016-12-18 06:57:45 -05002751 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002752};
2753
Anas Nashif954d5502018-02-25 08:37:28 -06002754/**
2755 * @internal
2756 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002757extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002758
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002759#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002760 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002761 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002762 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002763 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2764 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002765 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002766 }
2767
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002768#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2769
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002770/**
Allan Stephensc98da842016-11-11 15:45:03 -05002771 * INTERNAL_HIDDEN @endcond
2772 */
2773
2774/**
2775 * @addtogroup alert_apis
2776 * @{
2777 */
2778
2779/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002780 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002781 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002782 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002783 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002784 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002785 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002786 * @param name Name of the alert.
2787 * @param alert_handler Action to take when alert is sent. Specify either
2788 * the address of a function to be invoked by the system workqueue
2789 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2790 * K_ALERT_DEFAULT (which causes the alert to pend).
2791 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002792 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002793#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002794 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002795 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002796 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002797 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002798
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002799/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002800 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002801 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002802 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002803 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002804 * @param alert Address of the alert.
2805 * @param handler Action to take when alert is sent. Specify either the address
2806 * of a function to be invoked by the system workqueue thread,
2807 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2808 * K_ALERT_DEFAULT (which causes the alert to pend).
2809 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002810 *
2811 * @return N/A
2812 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002813extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2814 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002815
2816/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002817 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002818 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002819 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002820 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002821 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2822 *
2823 * @param alert Address of the alert.
2824 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002825 * or one of the special values K_NO_WAIT and K_FOREVER.
2826 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002827 * @retval 0 Alert received.
2828 * @retval -EBUSY Returned without waiting.
2829 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002830 */
Andrew Boie310e9872017-09-29 04:41:15 -07002831__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002832
2833/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002834 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002835 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002836 * This routine signals @a alert. The action specified for @a alert will
2837 * be taken, which may trigger the execution of an alert handler function
2838 * and/or cause the alert to pend (assuming the alert has not reached its
2839 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002840 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002841 * @note Can be called by ISRs.
2842 *
2843 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002844 *
2845 * @return N/A
2846 */
Andrew Boie310e9872017-09-29 04:41:15 -07002847__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002848
2849/**
Anas Nashif166f5192018-02-25 08:02:36 -06002850 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002851 */
2852
Allan Stephensc98da842016-11-11 15:45:03 -05002853/**
2854 * @cond INTERNAL_HIDDEN
2855 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002856
2857struct k_msgq {
2858 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002859 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002860 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002861 char *buffer_start;
2862 char *buffer_end;
2863 char *read_ptr;
2864 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002865 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002866
Anas Nashif2f203c22016-12-18 06:57:45 -05002867 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002868};
2869
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002870#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002871 { \
2872 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002873 .max_msgs = q_max_msgs, \
2874 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002875 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002876 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002877 .read_ptr = q_buffer, \
2878 .write_ptr = q_buffer, \
2879 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002880 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002881 }
2882
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002883#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2884
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05302885struct k_msgq_attrs {
2886 size_t msg_size;
2887 u32_t max_msgs;
2888 u32_t used_msgs;
2889};
2890
Peter Mitsis1da807e2016-10-06 11:36:59 -04002891/**
Allan Stephensc98da842016-11-11 15:45:03 -05002892 * INTERNAL_HIDDEN @endcond
2893 */
2894
2895/**
2896 * @defgroup msgq_apis Message Queue APIs
2897 * @ingroup kernel_apis
2898 * @{
2899 */
2900
2901/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002902 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002903 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002904 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2905 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002906 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2907 * message is similarly aligned to this boundary, @a q_msg_size must also be
2908 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002909 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002910 * The message queue can be accessed outside the module where it is defined
2911 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002912 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002913 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002914 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002915 * @param q_name Name of the message queue.
2916 * @param q_msg_size Message size (in bytes).
2917 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002918 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002919 */
2920#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2921 static char __noinit __aligned(q_align) \
2922 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002923 struct k_msgq q_name \
2924 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002925 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002926 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002927
Peter Mitsisd7a37502016-10-13 11:37:40 -04002928/**
2929 * @brief Initialize a message queue.
2930 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002931 * This routine initializes a message queue object, prior to its first use.
2932 *
Allan Stephensda827222016-11-09 14:23:58 -06002933 * The message queue's ring buffer must contain space for @a max_msgs messages,
2934 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2935 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2936 * that each message is similarly aligned to this boundary, @a q_msg_size
2937 * must also be a multiple of N.
2938 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002939 * @param q Address of the message queue.
2940 * @param buffer Pointer to ring buffer that holds queued messages.
2941 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002942 * @param max_msgs Maximum number of messages that can be queued.
2943 *
2944 * @return N/A
2945 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002946__syscall void k_msgq_init(struct k_msgq *q, char *buffer,
2947 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002948
2949/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002950 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002951 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002952 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002953 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002954 * @note Can be called by ISRs.
2955 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002956 * @param q Address of the message queue.
2957 * @param data Pointer to the message.
2958 * @param timeout Waiting period to add the message (in milliseconds),
2959 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002960 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002961 * @retval 0 Message sent.
2962 * @retval -ENOMSG Returned without waiting or queue purged.
2963 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002964 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002965__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002966
2967/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002968 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002969 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002970 * This routine receives a message from message queue @a q in a "first in,
2971 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002972 *
Allan Stephensc98da842016-11-11 15:45:03 -05002973 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002975 * @param q Address of the message queue.
2976 * @param data Address of area to hold the received message.
2977 * @param timeout Waiting period to receive the message (in milliseconds),
2978 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002979 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002980 * @retval 0 Message received.
2981 * @retval -ENOMSG Returned without waiting.
2982 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002983 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002984__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002985
2986/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002987 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002988 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002989 * This routine discards all unreceived messages in a message queue's ring
2990 * buffer. Any threads that are blocked waiting to send a message to the
2991 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002993 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002994 *
2995 * @return N/A
2996 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002997__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002998
Peter Mitsis67be2492016-10-07 11:44:34 -04002999/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003000 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003001 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003002 * This routine returns the number of unused entries in a message queue's
3003 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003004 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003005 * @param q Address of the message queue.
3006 *
3007 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04003008 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003009__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3010
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303011/**
3012 * @brief Get basic attributes of a message queue.
3013 *
3014 * This routine fetches basic attributes of message queue into attr argument.
3015 *
3016 * @param q Address of the message queue.
3017 * @param attrs pointer to message queue attribute structure.
3018 *
3019 * @return N/A
3020 */
3021__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3022
3023
Andrew Boie82edb6e2017-10-02 10:53:06 -07003024static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003025{
3026 return q->max_msgs - q->used_msgs;
3027}
3028
Peter Mitsisd7a37502016-10-13 11:37:40 -04003029/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003030 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003031 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003032 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003033 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003034 * @param q Address of the message queue.
3035 *
3036 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003037 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003038__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3039
3040static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003041{
3042 return q->used_msgs;
3043}
3044
Anas Nashif166f5192018-02-25 08:02:36 -06003045/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003046
3047/**
3048 * @defgroup mem_pool_apis Memory Pool APIs
3049 * @ingroup kernel_apis
3050 * @{
3051 */
3052
Andy Ross73cb9582017-05-09 10:42:39 -07003053/* Note on sizing: the use of a 20 bit field for block means that,
3054 * assuming a reasonable minimum block size of 16 bytes, we're limited
3055 * to 16M of memory managed by a single pool. Long term it would be
3056 * good to move to a variable bit size based on configuration.
3057 */
3058struct k_mem_block_id {
3059 u32_t pool : 8;
3060 u32_t level : 4;
3061 u32_t block : 20;
3062};
3063
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003064struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003065 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003066 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003067};
3068
Anas Nashif166f5192018-02-25 08:02:36 -06003069/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003070
3071/**
3072 * @defgroup mailbox_apis Mailbox APIs
3073 * @ingroup kernel_apis
3074 * @{
3075 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003076
3077struct k_mbox_msg {
3078 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003079 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003080 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003081 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003082 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003083 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003084 /** sender's message data buffer */
3085 void *tx_data;
3086 /** internal use only - needed for legacy API support */
3087 void *_rx_data;
3088 /** message data block descriptor */
3089 struct k_mem_block tx_block;
3090 /** source thread id */
3091 k_tid_t rx_source_thread;
3092 /** target thread id */
3093 k_tid_t tx_target_thread;
3094 /** internal use only - thread waiting on send (may be a dummy) */
3095 k_tid_t _syncing_thread;
3096#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3097 /** internal use only - semaphore used during asynchronous send */
3098 struct k_sem *_async_sem;
3099#endif
3100};
3101
Allan Stephensc98da842016-11-11 15:45:03 -05003102/**
3103 * @cond INTERNAL_HIDDEN
3104 */
3105
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003106struct k_mbox {
3107 _wait_q_t tx_msg_queue;
3108 _wait_q_t rx_msg_queue;
3109
Anas Nashif2f203c22016-12-18 06:57:45 -05003110 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003111};
3112
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003113#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003114 { \
3115 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3116 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003117 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003118 }
3119
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003120#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3121
Peter Mitsis12092702016-10-14 12:57:23 -04003122/**
Allan Stephensc98da842016-11-11 15:45:03 -05003123 * INTERNAL_HIDDEN @endcond
3124 */
3125
3126/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003127 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003128 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003129 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003130 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003131 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003132 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003133 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003134 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003135#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003136 struct k_mbox name \
3137 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003138 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003139
Peter Mitsis12092702016-10-14 12:57:23 -04003140/**
3141 * @brief Initialize a mailbox.
3142 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003143 * This routine initializes a mailbox object, prior to its first use.
3144 *
3145 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003146 *
3147 * @return N/A
3148 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003149extern void k_mbox_init(struct k_mbox *mbox);
3150
Peter Mitsis12092702016-10-14 12:57:23 -04003151/**
3152 * @brief Send a mailbox message in a synchronous manner.
3153 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003154 * This routine sends a message to @a mbox and waits for a receiver to both
3155 * receive and process it. The message data may be in a buffer, in a memory
3156 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003157 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003158 * @param mbox Address of the mailbox.
3159 * @param tx_msg Address of the transmit message descriptor.
3160 * @param timeout Waiting period for the message to be received (in
3161 * milliseconds), or one of the special values K_NO_WAIT
3162 * and K_FOREVER. Once the message has been received,
3163 * this routine waits as long as necessary for the message
3164 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003165 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003166 * @retval 0 Message sent.
3167 * @retval -ENOMSG Returned without waiting.
3168 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003169 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003170extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003171 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003172
Peter Mitsis12092702016-10-14 12:57:23 -04003173/**
3174 * @brief Send a mailbox message in an asynchronous manner.
3175 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003176 * This routine sends a message to @a mbox without waiting for a receiver
3177 * to process it. The message data may be in a buffer, in a memory pool block,
3178 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3179 * will be given when the message has been both received and completely
3180 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003181 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003182 * @param mbox Address of the mailbox.
3183 * @param tx_msg Address of the transmit message descriptor.
3184 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003185 *
3186 * @return N/A
3187 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003188extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003189 struct k_sem *sem);
3190
Peter Mitsis12092702016-10-14 12:57:23 -04003191/**
3192 * @brief Receive a mailbox message.
3193 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003194 * This routine receives a message from @a mbox, then optionally retrieves
3195 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003196 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003197 * @param mbox Address of the mailbox.
3198 * @param rx_msg Address of the receive message descriptor.
3199 * @param buffer Address of the buffer to receive data, or NULL to defer data
3200 * retrieval and message disposal until later.
3201 * @param timeout Waiting period for a message to be received (in
3202 * milliseconds), or one of the special values K_NO_WAIT
3203 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003204 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003205 * @retval 0 Message received.
3206 * @retval -ENOMSG Returned without waiting.
3207 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003208 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003209extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003210 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003211
3212/**
3213 * @brief Retrieve mailbox message data into a buffer.
3214 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003215 * This routine completes the processing of a received message by retrieving
3216 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003217 *
3218 * Alternatively, this routine can be used to dispose of a received message
3219 * without retrieving its data.
3220 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003221 * @param rx_msg Address of the receive message descriptor.
3222 * @param buffer Address of the buffer to receive data, or NULL to discard
3223 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003224 *
3225 * @return N/A
3226 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003227extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003228
3229/**
3230 * @brief Retrieve mailbox message data into a memory pool block.
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 memory pool block, then disposing of the message.
3234 * The memory pool block that results from successful retrieval must be
3235 * returned to the pool once the data has been processed, even in cases
3236 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003237 *
3238 * Alternatively, this routine can be used to dispose of a received message
3239 * without retrieving its data. In this case there is no need to return a
3240 * memory pool block to the pool.
3241 *
3242 * This routine allocates a new memory pool block for the data only if the
3243 * data is not already in one. If a new block cannot be allocated, the routine
3244 * returns a failure code and the received message is left unchanged. This
3245 * permits the caller to reattempt data retrieval at a later time or to dispose
3246 * of the received message without retrieving its data.
3247 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003248 * @param rx_msg Address of a receive message descriptor.
3249 * @param pool Address of memory pool, or NULL to discard data.
3250 * @param block Address of the area to hold memory pool block info.
3251 * @param timeout Waiting period to wait for a memory pool block (in
3252 * milliseconds), or one of the special values K_NO_WAIT
3253 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003254 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003255 * @retval 0 Data retrieved.
3256 * @retval -ENOMEM Returned without waiting.
3257 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003258 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003259extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003260 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003261 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003262
Anas Nashif166f5192018-02-25 08:02:36 -06003263/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003264
3265/**
3266 * @cond INTERNAL_HIDDEN
3267 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003268
3269struct k_pipe {
3270 unsigned char *buffer; /* Pipe buffer: may be NULL */
3271 size_t size; /* Buffer size */
3272 size_t bytes_used; /* # bytes used in buffer */
3273 size_t read_index; /* Where in buffer to read from */
3274 size_t write_index; /* Where in buffer to write */
3275
3276 struct {
3277 _wait_q_t readers; /* Reader wait queue */
3278 _wait_q_t writers; /* Writer wait queue */
3279 } wait_q;
3280
Anas Nashif2f203c22016-12-18 06:57:45 -05003281 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003282};
3283
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003284#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003285 { \
3286 .buffer = pipe_buffer, \
3287 .size = pipe_buffer_size, \
3288 .bytes_used = 0, \
3289 .read_index = 0, \
3290 .write_index = 0, \
3291 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3292 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003293 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003294 }
3295
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003296#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3297
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003298/**
Allan Stephensc98da842016-11-11 15:45:03 -05003299 * INTERNAL_HIDDEN @endcond
3300 */
3301
3302/**
3303 * @defgroup pipe_apis Pipe APIs
3304 * @ingroup kernel_apis
3305 * @{
3306 */
3307
3308/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003309 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003310 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003311 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003312 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003313 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003314 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003315 * @param name Name of the pipe.
3316 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3317 * or zero if no ring buffer is used.
3318 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003319 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003320#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3321 static unsigned char __noinit __aligned(pipe_align) \
3322 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003323 struct k_pipe name \
3324 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003325 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003326
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003327/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003328 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003329 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003330 * This routine initializes a pipe object, prior to its first use.
3331 *
3332 * @param pipe Address of the pipe.
3333 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3334 * is used.
3335 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3336 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003337 *
3338 * @return N/A
3339 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003340__syscall void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3341 size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003342
3343/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003344 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003345 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003346 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003347 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003348 * @param pipe Address of the pipe.
3349 * @param data Address of data to write.
3350 * @param bytes_to_write Size of data (in bytes).
3351 * @param bytes_written Address of area to hold the number of bytes written.
3352 * @param min_xfer Minimum number of bytes to write.
3353 * @param timeout Waiting period to wait for the data to be written (in
3354 * milliseconds), or one of the special values K_NO_WAIT
3355 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003356 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003357 * @retval 0 At least @a min_xfer bytes of data were written.
3358 * @retval -EIO Returned without waiting; zero data bytes were written.
3359 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003360 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003361 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003362__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3363 size_t bytes_to_write, size_t *bytes_written,
3364 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003365
3366/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003367 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003368 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003369 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003370 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003371 * @param pipe Address of the pipe.
3372 * @param data Address to place the data read from pipe.
3373 * @param bytes_to_read Maximum number of data bytes to read.
3374 * @param bytes_read Address of area to hold the number of bytes read.
3375 * @param min_xfer Minimum number of data bytes to read.
3376 * @param timeout Waiting period to wait for the data to be read (in
3377 * milliseconds), or one of the special values K_NO_WAIT
3378 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003379 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003380 * @retval 0 At least @a min_xfer bytes of data were read.
3381 * @retval -EIO Returned without waiting; zero data bytes were read.
3382 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003383 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003384 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003385__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3386 size_t bytes_to_read, size_t *bytes_read,
3387 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003388
3389/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003390 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003391 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003392 * This routine writes the data contained in a memory block to @a pipe.
3393 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003394 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003395 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003396 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003397 * @param block Memory block containing data to send
3398 * @param size Number of data bytes in memory block to send
3399 * @param sem Semaphore to signal upon completion (else NULL)
3400 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003401 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003402 */
3403extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3404 size_t size, struct k_sem *sem);
3405
Anas Nashif166f5192018-02-25 08:02:36 -06003406/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003407
Allan Stephensc98da842016-11-11 15:45:03 -05003408/**
3409 * @cond INTERNAL_HIDDEN
3410 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003411
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003412struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003413 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003414 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003415 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003416 char *buffer;
3417 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003418 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003419
Anas Nashif2f203c22016-12-18 06:57:45 -05003420 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003421};
3422
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003423#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003424 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003425 { \
3426 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003427 .num_blocks = slab_num_blocks, \
3428 .block_size = slab_block_size, \
3429 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003430 .free_list = NULL, \
3431 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003432 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003433 }
3434
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003435#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3436
3437
Peter Mitsis578f9112016-10-07 13:50:31 -04003438/**
Allan Stephensc98da842016-11-11 15:45:03 -05003439 * INTERNAL_HIDDEN @endcond
3440 */
3441
3442/**
3443 * @defgroup mem_slab_apis Memory Slab APIs
3444 * @ingroup kernel_apis
3445 * @{
3446 */
3447
3448/**
Allan Stephensda827222016-11-09 14:23:58 -06003449 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003450 *
Allan Stephensda827222016-11-09 14:23:58 -06003451 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003452 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003453 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3454 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003455 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003456 *
Allan Stephensda827222016-11-09 14:23:58 -06003457 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003458 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003459 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003460 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003461 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003462 * @param name Name of the memory slab.
3463 * @param slab_block_size Size of each memory block (in bytes).
3464 * @param slab_num_blocks Number memory blocks.
3465 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003466 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003467#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3468 char __noinit __aligned(slab_align) \
3469 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3470 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003471 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003472 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003473 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003474
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003475/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003476 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003477 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003478 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003479 *
Allan Stephensda827222016-11-09 14:23:58 -06003480 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3481 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3482 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3483 * To ensure that each memory block is similarly aligned to this boundary,
3484 * @a slab_block_size must also be a multiple of N.
3485 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003486 * @param slab Address of the memory slab.
3487 * @param buffer Pointer to buffer used for the memory blocks.
3488 * @param block_size Size of each memory block (in bytes).
3489 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003490 *
3491 * @return N/A
3492 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003493extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003494 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003495
3496/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003497 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003498 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003499 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003500 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003501 * @param slab Address of the memory slab.
3502 * @param mem Pointer to block address area.
3503 * @param timeout Maximum time to wait for operation to complete
3504 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3505 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003506 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003507 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003508 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003509 * @retval -ENOMEM Returned without waiting.
3510 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003511 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003512extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003513 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003514
3515/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003516 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003517 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003518 * This routine releases a previously allocated memory block back to its
3519 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003520 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003521 * @param slab Address of the memory slab.
3522 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003523 *
3524 * @return N/A
3525 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003526extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003527
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003528/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003529 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003530 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003531 * This routine gets the number of memory blocks that are currently
3532 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003533 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003534 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003535 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003536 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003537 */
Kumar Galacc334c72017-04-21 10:55:34 -05003538static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003539{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003540 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003541}
3542
Peter Mitsisc001aa82016-10-13 13:53:37 -04003543/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003544 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003545 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003546 * This routine gets the number of memory blocks that are currently
3547 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003548 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003549 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003550 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003551 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003552 */
Kumar Galacc334c72017-04-21 10:55:34 -05003553static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003554{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003555 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003556}
3557
Anas Nashif166f5192018-02-25 08:02:36 -06003558/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003559
3560/**
3561 * @cond INTERNAL_HIDDEN
3562 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003563
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003564struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003565 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003566 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003567};
3568
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003569/**
Allan Stephensc98da842016-11-11 15:45:03 -05003570 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003571 */
3572
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003573/**
Allan Stephensc98da842016-11-11 15:45:03 -05003574 * @addtogroup mem_pool_apis
3575 * @{
3576 */
3577
3578/**
3579 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003580 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003581 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3582 * long. The memory pool allows blocks to be repeatedly partitioned into
3583 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003584 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003585 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003586 * If the pool is to be accessed outside the module where it is defined, it
3587 * can be declared via
3588 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003589 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003590 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003591 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003592 * @param minsz Size of the smallest blocks in the pool (in bytes).
3593 * @param maxsz Size of the largest blocks in the pool (in bytes).
3594 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003595 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003596 */
Andy Ross73cb9582017-05-09 10:42:39 -07003597#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3598 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3599 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08003600 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07003601 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08003602 .base = { \
3603 .buf = _mpool_buf_##name, \
3604 .max_sz = maxsz, \
3605 .n_max = nmax, \
3606 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3607 .levels = _mpool_lvls_##name, \
3608 .flags = SYS_MEM_POOL_KERNEL \
3609 } \
Andy Ross73cb9582017-05-09 10:42:39 -07003610 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003611
Peter Mitsis937042c2016-10-13 13:18:26 -04003612/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003613 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003614 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003615 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003616 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003617 * @param pool Address of the memory pool.
3618 * @param block Pointer to block descriptor for the allocated memory.
3619 * @param size Amount of memory to allocate (in bytes).
3620 * @param timeout Maximum time to wait for operation to complete
3621 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3622 * or K_FOREVER to wait as long as necessary.
3623 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003624 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003625 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003626 * @retval -ENOMEM Returned without waiting.
3627 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003628 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003629extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003630 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003631
3632/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003633 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003634 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003635 * This routine releases a previously allocated memory block back to its
3636 * memory pool.
3637 *
3638 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003639 *
3640 * @return N/A
3641 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003642extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003643
3644/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003645 * @brief Free memory allocated from a memory pool.
3646 *
3647 * This routine releases a previously allocated memory block back to its
3648 * memory pool
3649 *
3650 * @param id Memory block identifier.
3651 *
3652 * @return N/A
3653 */
3654extern void k_mem_pool_free_id(struct k_mem_block_id *id);
3655
3656/**
Anas Nashif166f5192018-02-25 08:02:36 -06003657 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003658 */
3659
3660/**
3661 * @defgroup heap_apis Heap Memory Pool APIs
3662 * @ingroup kernel_apis
3663 * @{
3664 */
3665
3666/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003667 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003668 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003669 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003670 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003671 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003672 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003673 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003674 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003675 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003676extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003677
3678/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003679 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003680 *
3681 * This routine provides traditional free() semantics. The memory being
3682 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003683 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003684 * If @a ptr is NULL, no operation is performed.
3685 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003686 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003687 *
3688 * @return N/A
3689 */
3690extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003691
Allan Stephensc98da842016-11-11 15:45:03 -05003692/**
Andrew Boie7f95e832017-11-08 14:40:01 -08003693 * @brief Allocate memory from heap, array style
3694 *
3695 * This routine provides traditional calloc() semantics. Memory is
3696 * allocated from the heap memory pool and zeroed.
3697 *
3698 * @param nmemb Number of elements in the requested array
3699 * @param size Size of each array element (in bytes).
3700 *
3701 * @return Address of the allocated memory if successful; otherwise NULL.
3702 */
3703extern void *k_calloc(size_t nmemb, size_t size);
3704
Anas Nashif166f5192018-02-25 08:02:36 -06003705/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003706
Benjamin Walshacc68c12017-01-29 18:57:45 -05003707/* polling API - PRIVATE */
3708
Benjamin Walshb0179862017-02-02 16:39:57 -05003709#ifdef CONFIG_POLL
3710#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3711#else
3712#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3713#endif
3714
Benjamin Walshacc68c12017-01-29 18:57:45 -05003715/* private - implementation data created as needed, per-type */
3716struct _poller {
3717 struct k_thread *thread;
3718};
3719
3720/* private - types bit positions */
3721enum _poll_types_bits {
3722 /* can be used to ignore an event */
3723 _POLL_TYPE_IGNORE,
3724
3725 /* to be signaled by k_poll_signal() */
3726 _POLL_TYPE_SIGNAL,
3727
3728 /* semaphore availability */
3729 _POLL_TYPE_SEM_AVAILABLE,
3730
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003731 /* queue/fifo/lifo data availability */
3732 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003733
3734 _POLL_NUM_TYPES
3735};
3736
3737#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3738
3739/* private - states bit positions */
3740enum _poll_states_bits {
3741 /* default state when creating event */
3742 _POLL_STATE_NOT_READY,
3743
Benjamin Walshacc68c12017-01-29 18:57:45 -05003744 /* signaled by k_poll_signal() */
3745 _POLL_STATE_SIGNALED,
3746
3747 /* semaphore is available */
3748 _POLL_STATE_SEM_AVAILABLE,
3749
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003750 /* data is available to read on queue/fifo/lifo */
3751 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003752
3753 _POLL_NUM_STATES
3754};
3755
3756#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3757
3758#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003759 (32 - (0 \
3760 + 8 /* tag */ \
3761 + _POLL_NUM_TYPES \
3762 + _POLL_NUM_STATES \
3763 + 1 /* modes */ \
3764 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003765
Benjamin Walshacc68c12017-01-29 18:57:45 -05003766/* end of polling API - PRIVATE */
3767
3768
3769/**
3770 * @defgroup poll_apis Async polling APIs
3771 * @ingroup kernel_apis
3772 * @{
3773 */
3774
3775/* Public polling API */
3776
3777/* public - values for k_poll_event.type bitfield */
3778#define K_POLL_TYPE_IGNORE 0
3779#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3780#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003781#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3782#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003783
3784/* public - polling modes */
3785enum k_poll_modes {
3786 /* polling thread does not take ownership of objects when available */
3787 K_POLL_MODE_NOTIFY_ONLY = 0,
3788
3789 K_POLL_NUM_MODES
3790};
3791
3792/* public - values for k_poll_event.state bitfield */
3793#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003794#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3795#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003796#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3797#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003798
3799/* public - poll signal object */
3800struct k_poll_signal {
3801 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003802 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003803
3804 /*
3805 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3806 * user resets it to 0.
3807 */
3808 unsigned int signaled;
3809
3810 /* custom result value passed to k_poll_signal() if needed */
3811 int result;
3812};
3813
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003814#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003815 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003816 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003817 .signaled = 0, \
3818 .result = 0, \
3819 }
3820
3821struct k_poll_event {
3822 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003823 sys_dnode_t _node;
3824
3825 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003826 struct _poller *poller;
3827
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003828 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003829 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003830
Benjamin Walshacc68c12017-01-29 18:57:45 -05003831 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003832 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003833
3834 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003835 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003836
3837 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003838 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003839
3840 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003841 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003842
3843 /* per-type data */
3844 union {
3845 void *obj;
3846 struct k_poll_signal *signal;
3847 struct k_sem *sem;
3848 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003849 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003850 };
3851};
3852
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003853#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003854 { \
3855 .poller = NULL, \
3856 .type = event_type, \
3857 .state = K_POLL_STATE_NOT_READY, \
3858 .mode = event_mode, \
3859 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003860 { .obj = event_obj }, \
3861 }
3862
3863#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3864 event_tag) \
3865 { \
3866 .type = event_type, \
3867 .tag = event_tag, \
3868 .state = K_POLL_STATE_NOT_READY, \
3869 .mode = event_mode, \
3870 .unused = 0, \
3871 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003872 }
3873
3874/**
3875 * @brief Initialize one struct k_poll_event instance
3876 *
3877 * After this routine is called on a poll event, the event it ready to be
3878 * placed in an event array to be passed to k_poll().
3879 *
3880 * @param event The event to initialize.
3881 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3882 * values. Only values that apply to the same object being polled
3883 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3884 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003885 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003886 * @param obj Kernel object or poll signal.
3887 *
3888 * @return N/A
3889 */
3890
Kumar Galacc334c72017-04-21 10:55:34 -05003891extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003892 int mode, void *obj);
3893
3894/**
3895 * @brief Wait for one or many of multiple poll events to occur
3896 *
3897 * This routine allows a thread to wait concurrently for one or many of
3898 * multiple poll events to have occurred. Such events can be a kernel object
3899 * being available, like a semaphore, or a poll signal event.
3900 *
3901 * When an event notifies that a kernel object is available, the kernel object
3902 * is not "given" to the thread calling k_poll(): it merely signals the fact
3903 * that the object was available when the k_poll() call was in effect. Also,
3904 * all threads trying to acquire an object the regular way, i.e. by pending on
3905 * the object, have precedence over the thread polling on the object. This
3906 * means that the polling thread will never get the poll event on an object
3907 * until the object becomes available and its pend queue is empty. For this
3908 * reason, the k_poll() call is more effective when the objects being polled
3909 * only have one thread, the polling thread, trying to acquire them.
3910 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003911 * When k_poll() returns 0, the caller should loop on all the events that were
3912 * passed to k_poll() and check the state field for the values that were
3913 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003914 *
3915 * Before being reused for another call to k_poll(), the user has to reset the
3916 * state field to K_POLL_STATE_NOT_READY.
3917 *
3918 * @param events An array of pointers to events to be polled for.
3919 * @param num_events The number of events in the array.
3920 * @param timeout Waiting period for an event to be ready (in milliseconds),
3921 * or one of the special values K_NO_WAIT and K_FOREVER.
3922 *
3923 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003924 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02003925 * @retval -EINTR Poller thread has been interrupted.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003926 */
3927
3928extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003929 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003930
3931/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003932 * @brief Initialize a poll signal object.
3933 *
3934 * Ready a poll signal object to be signaled via k_poll_signal().
3935 *
3936 * @param signal A poll signal.
3937 *
3938 * @return N/A
3939 */
3940
3941extern void k_poll_signal_init(struct k_poll_signal *signal);
3942
3943/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003944 * @brief Signal a poll signal object.
3945 *
3946 * This routine makes ready a poll signal, which is basically a poll event of
3947 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3948 * made ready to run. A @a result value can be specified.
3949 *
3950 * The poll signal contains a 'signaled' field that, when set by
3951 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3952 * be reset by the user before being passed again to k_poll() or k_poll() will
3953 * consider it being signaled, and will return immediately.
3954 *
3955 * @param signal A poll signal.
3956 * @param result The value to store in the result field of the signal.
3957 *
3958 * @retval 0 The signal was delivered successfully.
3959 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3960 */
3961
3962extern int k_poll_signal(struct k_poll_signal *signal, int result);
3963
Anas Nashif954d5502018-02-25 08:37:28 -06003964/**
3965 * @internal
3966 */
Andy Ross8606fab2018-03-26 10:54:40 -07003967extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003968
Anas Nashif166f5192018-02-25 08:02:36 -06003969/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003970
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003971/**
3972 * @brief Make the CPU idle.
3973 *
3974 * This function makes the CPU idle until an event wakes it up.
3975 *
3976 * In a regular system, the idle thread should be the only thread responsible
3977 * for making the CPU idle and triggering any type of power management.
3978 * However, in some more constrained systems, such as a single-threaded system,
3979 * the only thread would be responsible for this if needed.
3980 *
3981 * @return N/A
3982 */
3983extern void k_cpu_idle(void);
3984
3985/**
3986 * @brief Make the CPU idle in an atomic fashion.
3987 *
3988 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3989 * must be done atomically before making the CPU idle.
3990 *
3991 * @param key Interrupt locking key obtained from irq_lock().
3992 *
3993 * @return N/A
3994 */
3995extern void k_cpu_atomic_idle(unsigned int key);
3996
Anas Nashif954d5502018-02-25 08:37:28 -06003997
3998/**
3999 * @internal
4000 */
Kumar Galacc334c72017-04-21 10:55:34 -05004001extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004002
Andrew Boiecdb94d62017-04-18 15:22:05 -07004003#ifdef _ARCH_EXCEPT
4004/* This archtecture has direct support for triggering a CPU exception */
4005#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4006#else
4007
Andrew Boiecdb94d62017-04-18 15:22:05 -07004008/* NOTE: This is the implementation for arches that do not implement
4009 * _ARCH_EXCEPT() to generate a real CPU exception.
4010 *
4011 * We won't have a real exception frame to determine the PC value when
4012 * the oops occurred, so print file and line number before we jump into
4013 * the fatal error handler.
4014 */
4015#define _k_except_reason(reason) do { \
4016 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4017 _NanoFatalErrorHandler(reason, &_default_esf); \
4018 CODE_UNREACHABLE; \
4019 } while (0)
4020
4021#endif /* _ARCH__EXCEPT */
4022
4023/**
4024 * @brief Fatally terminate a thread
4025 *
4026 * This should be called when a thread has encountered an unrecoverable
4027 * runtime condition and needs to terminate. What this ultimately
4028 * means is determined by the _fatal_error_handler() implementation, which
4029 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4030 *
4031 * If this is called from ISR context, the default system fatal error handler
4032 * will treat it as an unrecoverable system error, just like k_panic().
4033 */
4034#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4035
4036/**
4037 * @brief Fatally terminate the system
4038 *
4039 * This should be called when the Zephyr kernel has encountered an
4040 * unrecoverable runtime condition and needs to terminate. What this ultimately
4041 * means is determined by the _fatal_error_handler() implementation, which
4042 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4043 */
4044#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4045
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004046/*
4047 * private APIs that are utilized by one or more public APIs
4048 */
4049
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004050#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004051/**
4052 * @internal
4053 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004054extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004055#else
Anas Nashif954d5502018-02-25 08:37:28 -06004056/**
4057 * @internal
4058 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004059#define _init_static_threads() do { } while ((0))
4060#endif
4061
Anas Nashif954d5502018-02-25 08:37:28 -06004062/**
4063 * @internal
4064 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004065extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004066/**
4067 * @internal
4068 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004069extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004070
Andrew Boiedc5d9352017-06-02 12:56:47 -07004071/* arch/cpu.h may declare an architecture or platform-specific macro
4072 * for properly declaring stacks, compatible with MMU/MPU constraints if
4073 * enabled
4074 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004075
4076/**
4077 * @brief Obtain an extern reference to a stack
4078 *
4079 * This macro properly brings the symbol of a thread stack declared
4080 * elsewhere into scope.
4081 *
4082 * @param sym Thread stack symbol name
4083 */
4084#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4085
Andrew Boiedc5d9352017-06-02 12:56:47 -07004086#ifdef _ARCH_THREAD_STACK_DEFINE
4087#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4088#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4089 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4090#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4091#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004092static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004093{
4094 return _ARCH_THREAD_STACK_BUFFER(sym);
4095}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004096#else
4097/**
4098 * @brief Declare a toplevel thread stack memory region
4099 *
4100 * This declares a region of memory suitable for use as a thread's stack.
4101 *
4102 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4103 * 'noinit' section so that it isn't zeroed at boot
4104 *
Andrew Boie507852a2017-07-25 18:47:07 -07004105 * The declared symbol will always be a k_thread_stack_t which can be passed to
4106 * k_thread_create, but should otherwise not be manipulated. If the buffer
4107 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004108 *
4109 * It is legal to precede this definition with the 'static' keyword.
4110 *
4111 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4112 * parameter of k_thread_create(), it may not be the same as the
4113 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4114 *
4115 * @param sym Thread stack symbol name
4116 * @param size Size of the stack memory region
4117 */
4118#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004119 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004120
4121/**
4122 * @brief Declare a toplevel array of thread stack memory regions
4123 *
4124 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4125 * definition for additional details and constraints.
4126 *
4127 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4128 * 'noinit' section so that it isn't zeroed at boot
4129 *
4130 * @param sym Thread stack symbol name
4131 * @param nmemb Number of stacks to declare
4132 * @param size Size of the stack memory region
4133 */
4134
4135#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004136 struct _k_thread_stack_element __noinit \
4137 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004138
4139/**
4140 * @brief Declare an embedded stack memory region
4141 *
4142 * Used for stacks embedded within other data structures. Use is highly
4143 * discouraged but in some cases necessary. For memory protection scenarios,
4144 * it is very important that any RAM preceding this member not be writable
4145 * by threads else a stack overflow will lead to silent corruption. In other
4146 * words, the containing data structure should live in RAM owned by the kernel.
4147 *
4148 * @param sym Thread stack symbol name
4149 * @param size Size of the stack memory region
4150 */
4151#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004152 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004153
4154/**
4155 * @brief Return the size in bytes of a stack memory region
4156 *
4157 * Convenience macro for passing the desired stack size to k_thread_create()
4158 * since the underlying implementation may actually create something larger
4159 * (for instance a guard area).
4160 *
4161 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004162 * passed to K_THREAD_STACK_DEFINE.
4163 *
4164 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4165 * it is not guaranteed to return the original value since each array
4166 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004167 *
4168 * @param sym Stack memory symbol
4169 * @return Size of the stack
4170 */
4171#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4172
4173/**
4174 * @brief Get a pointer to the physical stack buffer
4175 *
4176 * Convenience macro to get at the real underlying stack buffer used by
4177 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4178 * This is really only intended for diagnostic tools which want to examine
4179 * stack memory contents.
4180 *
4181 * @param sym Declared stack symbol name
4182 * @return The buffer itself, a char *
4183 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004184static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004185{
4186 return (char *)sym;
4187}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004188
4189#endif /* _ARCH_DECLARE_STACK */
4190
Chunlin Hane9c97022017-07-07 20:29:30 +08004191/**
4192 * @defgroup mem_domain_apis Memory domain APIs
4193 * @ingroup kernel_apis
4194 * @{
4195 */
4196
4197/** @def MEM_PARTITION_ENTRY
4198 * @brief Used to declare a memory partition entry
4199 */
4200#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4201 {\
4202 .start = _start, \
4203 .size = _size, \
4204 .attr = _attr, \
4205 }
4206
4207/** @def K_MEM_PARTITION_DEFINE
4208 * @brief Used to declare a memory partition
4209 */
4210#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4211#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4212 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304213 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004214 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4215#else
4216#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304217 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004218 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4219#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4220
Chunlin Hane9c97022017-07-07 20:29:30 +08004221/* memory partition */
4222struct k_mem_partition {
4223 /* start address of memory partition */
4224 u32_t start;
4225 /* size of memory partition */
4226 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304227#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004228 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304229 k_mem_partition_attr_t attr;
4230#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004231};
4232
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304233/* memory domian
4234 * Note: Always declare this structure with __kernel prefix
4235 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004236struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304237#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004238 /* partitions in the domain */
4239 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304240#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004241 /* domain q */
4242 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004243 /* number of partitions in the domain */
4244 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004245};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304246
Chunlin Hane9c97022017-07-07 20:29:30 +08004247
4248/**
4249 * @brief Initialize a memory domain.
4250 *
4251 * Initialize a memory domain with given name and memory partitions.
4252 *
4253 * @param domain The memory domain to be initialized.
4254 * @param num_parts The number of array items of "parts" parameter.
4255 * @param parts An array of pointers to the memory partitions. Can be NULL
4256 * if num_parts is zero.
4257 */
4258
Leandro Pereira08de6582018-02-28 14:22:57 -08004259extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004260 struct k_mem_partition *parts[]);
4261/**
4262 * @brief Destroy a memory domain.
4263 *
4264 * Destroy a memory domain.
4265 *
4266 * @param domain The memory domain to be destroyed.
4267 */
4268
4269extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4270
4271/**
4272 * @brief Add a memory partition into a memory domain.
4273 *
4274 * Add a memory partition into a memory domain.
4275 *
4276 * @param domain The memory domain to be added a memory partition.
4277 * @param part The memory partition to be added
4278 */
4279
4280extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4281 struct k_mem_partition *part);
4282
4283/**
4284 * @brief Remove a memory partition from a memory domain.
4285 *
4286 * Remove a memory partition from a memory domain.
4287 *
4288 * @param domain The memory domain to be removed a memory partition.
4289 * @param part The memory partition to be removed
4290 */
4291
4292extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4293 struct k_mem_partition *part);
4294
4295/**
4296 * @brief Add a thread into a memory domain.
4297 *
4298 * Add a thread into a memory domain.
4299 *
4300 * @param domain The memory domain that the thread is going to be added into.
4301 * @param thread ID of thread going to be added into the memory domain.
4302 */
4303
4304extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4305 k_tid_t thread);
4306
4307/**
4308 * @brief Remove a thread from its memory domain.
4309 *
4310 * Remove a thread from its memory domain.
4311 *
4312 * @param thread ID of thread going to be removed from its memory domain.
4313 */
4314
4315extern void k_mem_domain_remove_thread(k_tid_t thread);
4316
Anas Nashif166f5192018-02-25 08:02:36 -06004317/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004318
Andrew Boie756f9072017-10-10 16:01:49 -07004319/**
4320 * @brief Emit a character buffer to the console device
4321 *
4322 * @param c String of characters to print
4323 * @param n The length of the string
4324 */
4325__syscall void k_str_out(char *c, size_t n);
4326
Andy Rosse7172672018-01-24 15:48:32 -08004327/**
4328 * @brief Start a numbered CPU on a MP-capable system
4329
4330 * This starts and initializes a specific CPU. The main thread on
4331 * startup is running on CPU zero, other processors are numbered
4332 * sequentially. On return from this function, the CPU is known to
4333 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004334 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004335 * with the provided key will work to enable them.
4336 *
4337 * Normally, in SMP mode this function will be called by the kernel
4338 * initialization and should not be used as a user API. But it is
4339 * defined here for special-purpose apps which want Zephyr running on
4340 * one core and to use others for design-specific processing.
4341 *
4342 * @param cpu_num Integer number of the CPU
4343 * @param stack Stack memory for the CPU
4344 * @param sz Stack buffer size, in bytes
4345 * @param fn Function to begin running on the CPU. First argument is
4346 * an irq_unlock() key.
4347 * @param arg Untyped argument to be passed to "fn"
4348 */
4349extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4350 void (*fn)(int, void *), void *arg);
4351
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004352#ifdef __cplusplus
4353}
4354#endif
4355
Andrew Boiee004dec2016-11-07 09:01:19 -08004356#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4357/*
4358 * Define new and delete operators.
4359 * At this moment, the operators do nothing since objects are supposed
4360 * to be statically allocated.
4361 */
4362inline void operator delete(void *ptr)
4363{
4364 (void)ptr;
4365}
4366
4367inline void operator delete[](void *ptr)
4368{
4369 (void)ptr;
4370}
4371
4372inline void *operator new(size_t size)
4373{
4374 (void)size;
4375 return NULL;
4376}
4377
4378inline void *operator new[](size_t size)
4379{
4380 (void)size;
4381 return NULL;
4382}
4383
4384/* Placement versions of operator new and delete */
4385inline void operator delete(void *ptr1, void *ptr2)
4386{
4387 (void)ptr1;
4388 (void)ptr2;
4389}
4390
4391inline void operator delete[](void *ptr1, void *ptr2)
4392{
4393 (void)ptr1;
4394 (void)ptr2;
4395}
4396
4397inline void *operator new(size_t size, void *ptr)
4398{
4399 (void)size;
4400 return ptr;
4401}
4402
4403inline void *operator new[](size_t size, void *ptr)
4404{
4405 (void)size;
4406 return ptr;
4407}
4408
4409#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4410
Andrew Boiefa94ee72017-09-28 16:54:35 -07004411#include <syscalls/kernel.h>
4412
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004413#endif /* !_ASMLANGUAGE */
4414
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004415#endif /* _kernel__h_ */