blob: 759d0ef3df6adc2b8b14ddd0dd92a4108b323679 [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>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050028#include <kernel_version.h>
Anas Nashifa6149502017-01-17 07:47:31 -050029#include <drivers/rand32.h>
Andrew Boie73abd322017-04-04 13:19:13 -070030#include <kernel_arch_thread.h>
Andrew Boie13ca6fe2017-09-23 12:05:49 -070031#include <syscall.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040032
33#ifdef __cplusplus
34extern "C" {
35#endif
36
Anas Nashifbbb157d2017-01-15 08:46:31 -050037/**
38 * @brief Kernel APIs
39 * @defgroup kernel_apis Kernel APIs
40 * @{
41 * @}
42 */
43
Anas Nashif61f4b242016-11-18 10:53:59 -050044#ifdef CONFIG_KERNEL_DEBUG
45#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040046#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
47#else
48#define K_DEBUG(fmt, ...)
49#endif
50
Benjamin Walsh2f280412017-01-14 19:23:46 -050051#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
52#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
53#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
54#elif defined(CONFIG_COOP_ENABLED)
55#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
56#define _NUM_PREEMPT_PRIO (0)
57#elif defined(CONFIG_PREEMPT_ENABLED)
58#define _NUM_COOP_PRIO (0)
59#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
60#else
61#error "invalid configuration"
62#endif
63
64#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040065#define K_PRIO_PREEMPT(x) (x)
66
Benjamin Walsh456c6da2016-09-02 18:55:39 -040067#define K_ANY NULL
68#define K_END NULL
69
Benjamin Walshedb35702017-01-14 18:47:22 -050070#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040071#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050072#elif defined(CONFIG_COOP_ENABLED)
73#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
74#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040075#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050076#else
77#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040078#endif
79
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050080#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040081#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
82#else
83#define K_LOWEST_THREAD_PRIO -1
84#endif
85
Benjamin Walshfab8d922016-11-08 15:36:36 -050086#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
87
Benjamin Walsh456c6da2016-09-02 18:55:39 -040088#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
89#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
90
91typedef sys_dlist_t _wait_q_t;
92
Anas Nashif2f203c22016-12-18 06:57:45 -050093#ifdef CONFIG_OBJECT_TRACING
94#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
95#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040096#else
Anas Nashif2f203c22016-12-18 06:57:45 -050097#define _OBJECT_TRACING_INIT
98#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040099#endif
100
Benjamin Walshacc68c12017-01-29 18:57:45 -0500101#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300102#define _POLL_EVENT_OBJ_INIT(obj) \
103 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
104#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500105#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300106#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500107#define _POLL_EVENT
108#endif
109
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500110struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400111struct k_mutex;
112struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400113struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400114struct k_msgq;
115struct k_mbox;
116struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200117struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400118struct k_fifo;
119struct k_lifo;
120struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400121struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400122struct k_mem_pool;
123struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500124struct k_poll_event;
125struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800126struct k_mem_domain;
127struct k_mem_partition;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400128
Andrew Boie5bd891d2017-09-27 12:59:28 -0700129/* This enumeration needs to be kept in sync with the lists of kernel objects
130 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
131 * function in kernel/userspace.c
132 */
Andrew Boie945af952017-08-22 13:15:23 -0700133enum k_objects {
Andrew Boie5bd891d2017-09-27 12:59:28 -0700134 /* Core kernel objects */
Andrew Boie945af952017-08-22 13:15:23 -0700135 K_OBJ_ALERT,
136 K_OBJ_DELAYED_WORK,
137 K_OBJ_MEM_SLAB,
138 K_OBJ_MSGQ,
139 K_OBJ_MUTEX,
140 K_OBJ_PIPE,
141 K_OBJ_SEM,
142 K_OBJ_STACK,
143 K_OBJ_THREAD,
144 K_OBJ_TIMER,
145 K_OBJ_WORK,
146 K_OBJ_WORK_Q,
147
Andrew Boie5bd891d2017-09-27 12:59:28 -0700148 /* Driver subsystems */
149 K_OBJ_DRIVER_ADC,
150 K_OBJ_DRIVER_AIO_CMP,
151 K_OBJ_DRIVER_CLOCK_CONTROL,
152 K_OBJ_DRIVER_COUNTER,
153 K_OBJ_DRIVER_CRYPTO,
154 K_OBJ_DRIVER_DMA,
155 K_OBJ_DRIVER_ETH,
156 K_OBJ_DRIVER_FLASH,
157 K_OBJ_DRIVER_GPIO,
158 K_OBJ_DRIVER_I2C,
159 K_OBJ_DRIVER_I2S,
160 K_OBJ_DRIVER_IPM,
161 K_OBJ_DRIVER_PINMUX,
162 K_OBJ_DRIVER_PWM,
163 K_OBJ_DRIVER_RANDOM,
164 K_OBJ_DRIVER_RTC,
165 K_OBJ_DRIVER_SENSOR,
166 K_OBJ_DRIVER_SHARED_IRQ,
167 K_OBJ_DRIVER_SPI,
168 K_OBJ_DRIVER_UART,
169 K_OBJ_DRIVER_WDT,
170
Andrew Boie945af952017-08-22 13:15:23 -0700171 K_OBJ_LAST
172};
173
174#ifdef CONFIG_USERSPACE
175/* Table generated by gperf, these objects are retrieved via
176 * _k_object_find() */
177struct _k_object {
178 char *name;
179 char perms[CONFIG_MAX_THREAD_BYTES];
180 char type;
181 char flags;
182} __packed;
183
184#define K_OBJ_FLAG_INITIALIZED BIT(0)
185/**
186 * Ensure a system object is a valid object of the expected type
187 *
188 * Searches for the object and ensures that it is indeed an object
189 * of the expected type, that the caller has the right permissions on it,
190 * and that the object has been initialized.
191 *
192 * This function is intended to be called on the kernel-side system
193 * call handlers to validate kernel object pointers passed in from
194 * userspace.
195 *
196 * @param obj Address of the kernel object
197 * @param otype Expected type of the kernel object
198 * @param init If true, this is for an init function and we will not error
199 * out if the object is not initialized
200 * @return 0 If the object is valid
201 * -EBADF if not a valid object of the specified type
202 * -EPERM If the caller does not have permissions
David B. Kinder8065dbc2017-09-21 15:25:40 -0700203 * -EINVAL Object is not initialized
Andrew Boie945af952017-08-22 13:15:23 -0700204 */
205int _k_object_validate(void *obj, enum k_objects otype, int init);
206
207
208/**
209 * Lookup a kernel object and init its metadata if it exists
210 *
211 * Calling this on an object will make it usable from userspace.
212 * Intended to be called as the last statement in kernel object init
213 * functions.
214 *
215 * @param object Address of the kernel object
216 */
217void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700218#else
219static inline int _k_object_validate(void *obj, enum k_objects otype, int init)
220{
221 ARG_UNUSED(obj);
222 ARG_UNUSED(otype);
223 ARG_UNUSED(init);
Andrew Boie945af952017-08-22 13:15:23 -0700224
Andrew Boie743e4682017-10-04 12:25:50 -0700225 return 0;
226}
227
228static inline void _k_object_init(void *obj)
229{
230 ARG_UNUSED(obj);
231}
232
233static inline void _impl_k_object_access_grant(void *object,
234 struct k_thread *thread)
235{
236 ARG_UNUSED(object);
237 ARG_UNUSED(thread);
238}
239
240static inline void _impl_k_object_access_all_grant(void *object)
241{
242 ARG_UNUSED(object);
243}
244#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700245
246/**
247 * grant a thread access to a kernel object
248 *
249 * The thread will be granted access to the object if the caller is from
250 * supervisor mode, or the caller is from user mode AND has permissions
251 * on the object already.
252 *
253 * @param object Address of kernel object
254 * @param thread Thread to grant access to the object
255 */
Andrew Boie743e4682017-10-04 12:25:50 -0700256__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700257
Andrew Boie3b5ae802017-10-04 12:10:32 -0700258
259/**
260 * grant all present and future threads access to an object
261 *
262 * If the caller is from supervisor mode, or the caller is from user mode and
263 * have sufficient permissions on the object, then that object will have
264 * permissions granted to it for *all* current and future threads running in
265 * the system, effectively becoming a public kernel object.
266 *
267 * Use of this API should be avoided on systems that are running untrusted code
268 * as it is possible for such code to derive the addresses of kernel objects
269 * and perform unwanted operations on them.
270 *
271 * @param object Address of kernel object
272 */
Andrew Boie743e4682017-10-04 12:25:50 -0700273__syscall void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700274
Andrew Boie73abd322017-04-04 13:19:13 -0700275/* timeouts */
276
277struct _timeout;
278typedef void (*_timeout_func_t)(struct _timeout *t);
279
280struct _timeout {
281 sys_dnode_t node;
282 struct k_thread *thread;
283 sys_dlist_t *wait_q;
284 s32_t delta_ticks_from_prev;
285 _timeout_func_t func;
286};
287
288extern s32_t _timeout_remaining_get(struct _timeout *timeout);
289
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700290/**
291 * @typedef k_thread_entry_t
292 * @brief Thread entry point function type.
293 *
294 * A thread's entry point function is invoked when the thread starts executing.
295 * Up to 3 argument values can be passed to the function.
296 *
297 * The thread terminates execution permanently if the entry point function
298 * returns. The thread is responsible for releasing any shared resources
299 * it may own (such as mutexes and dynamically allocated memory), prior to
300 * returning.
301 *
302 * @param p1 First argument.
303 * @param p2 Second argument.
304 * @param p3 Third argument.
305 *
306 * @return N/A
307 */
308typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700309
310#ifdef CONFIG_THREAD_MONITOR
311struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700312 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700313 void *parameter1;
314 void *parameter2;
315 void *parameter3;
316};
317#endif
318
319/* can be used for creating 'dummy' threads, e.g. for pending on objects */
320struct _thread_base {
321
322 /* this thread's entry in a ready/wait queue */
323 sys_dnode_t k_q_node;
324
325 /* user facing 'thread options'; values defined in include/kernel.h */
326 u8_t user_options;
327
328 /* thread state */
329 u8_t thread_state;
330
331 /*
332 * scheduler lock count and thread priority
333 *
334 * These two fields control the preemptibility of a thread.
335 *
336 * When the scheduler is locked, sched_locked is decremented, which
337 * means that the scheduler is locked for values from 0xff to 0x01. A
338 * thread is coop if its prio is negative, thus 0x80 to 0xff when
339 * looked at the value as unsigned.
340 *
341 * By putting them end-to-end, this means that a thread is
342 * non-preemptible if the bundled value is greater than or equal to
343 * 0x0080.
344 */
345 union {
346 struct {
347#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
348 u8_t sched_locked;
349 s8_t prio;
350#else /* LITTLE and PDP */
351 s8_t prio;
352 u8_t sched_locked;
353#endif
354 };
355 u16_t preempt;
356 };
357
358 /* data returned by APIs */
359 void *swap_data;
360
361#ifdef CONFIG_SYS_CLOCK_EXISTS
362 /* this thread's entry in a timeout queue */
363 struct _timeout timeout;
364#endif
Andrew Boie2acfcd62017-08-30 14:31:03 -0700365
366#ifdef CONFIG_USERSPACE
367 /* Bit position in kernel object permissions bitfield for this thread */
368 unsigned int perm_index;
369#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700370};
371
372typedef struct _thread_base _thread_base_t;
373
374#if defined(CONFIG_THREAD_STACK_INFO)
375/* Contains the stack information of a thread */
376struct _thread_stack_info {
377 /* Stack Start */
378 u32_t start;
379 /* Stack Size */
380 u32_t size;
381};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700382
383typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700384#endif /* CONFIG_THREAD_STACK_INFO */
385
Chunlin Hane9c97022017-07-07 20:29:30 +0800386#if defined(CONFIG_USERSPACE)
387struct _mem_domain_info {
388 /* memory domain queue node */
389 sys_dnode_t mem_domain_q_node;
390 /* memory domain of the thread */
391 struct k_mem_domain *mem_domain;
392};
393
394#endif /* CONFIG_USERSPACE */
395
Andrew Boie73abd322017-04-04 13:19:13 -0700396struct k_thread {
397
398 struct _thread_base base;
399
400 /* defined by the architecture, but all archs need these */
401 struct _caller_saved caller_saved;
402 struct _callee_saved callee_saved;
403
404 /* static thread init data */
405 void *init_data;
406
407 /* abort function */
408 void (*fn_abort)(void);
409
410#if defined(CONFIG_THREAD_MONITOR)
411 /* thread entry and parameters description */
412 struct __thread_entry *entry;
413
414 /* next item in list of all threads */
415 struct k_thread *next_thread;
416#endif
417
418#ifdef CONFIG_THREAD_CUSTOM_DATA
419 /* crude thread-local storage */
420 void *custom_data;
421#endif
422
423#ifdef CONFIG_ERRNO
424 /* per-thread errno variable */
425 int errno_var;
426#endif
427
428#if defined(CONFIG_THREAD_STACK_INFO)
429 /* Stack Info */
430 struct _thread_stack_info stack_info;
431#endif /* CONFIG_THREAD_STACK_INFO */
432
Chunlin Hane9c97022017-07-07 20:29:30 +0800433#if defined(CONFIG_USERSPACE)
434 /* memory domain info of the thread */
435 struct _mem_domain_info mem_domain_info;
436#endif /* CONFIG_USERSPACE */
437
Andrew Boie73abd322017-04-04 13:19:13 -0700438 /* arch-specifics: must always be at the end */
439 struct _thread_arch arch;
440};
441
442typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400443typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700444#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400445
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400446enum execution_context_types {
447 K_ISR = 0,
448 K_COOP_THREAD,
449 K_PREEMPT_THREAD,
450};
451
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400452/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100453 * @defgroup profiling_apis Profiling APIs
454 * @ingroup kernel_apis
455 * @{
456 */
457
458/**
459 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
460 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700461 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100462 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
463 *
464 * CONFIG_MAIN_STACK_SIZE
465 * CONFIG_IDLE_STACK_SIZE
466 * CONFIG_ISR_STACK_SIZE
467 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
468 *
469 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
470 * produce output.
471 *
472 * @return N/A
473 */
474extern void k_call_stacks_analyze(void);
475
476/**
477 * @} end defgroup profiling_apis
478 */
479
480/**
Allan Stephensc98da842016-11-11 15:45:03 -0500481 * @defgroup thread_apis Thread APIs
482 * @ingroup kernel_apis
483 * @{
484 */
485
Benjamin Walshed240f22017-01-22 13:05:08 -0500486#endif /* !_ASMLANGUAGE */
487
488
489/*
490 * Thread user options. May be needed by assembly code. Common part uses low
491 * bits, arch-specific use high bits.
492 */
493
494/* system thread that must not abort */
495#define K_ESSENTIAL (1 << 0)
496
497#if defined(CONFIG_FP_SHARING)
498/* thread uses floating point registers */
499#define K_FP_REGS (1 << 1)
500#endif
501
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700502/* This thread has dropped from supervisor mode to user mode and consequently
503 * has additional restrictions
504 */
505#define K_USER (1 << 2)
506
Benjamin Walshed240f22017-01-22 13:05:08 -0500507#ifdef CONFIG_X86
508/* x86 Bitmask definitions for threads user options */
509
510#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
511/* thread uses SSEx (and also FP) registers */
512#define K_SSE_REGS (1 << 7)
513#endif
514#endif
515
516/* end - thread options */
517
518#if !defined(_ASMLANGUAGE)
519
Andrew Boie507852a2017-07-25 18:47:07 -0700520/* Using typedef deliberately here, this is quite intended to be an opaque
521 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
522 *
523 * The purpose of this data type is to clearly distinguish between the
524 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
525 * buffer which composes the stack data actually used by the underlying
526 * thread; they cannot be used interchangably as some arches precede the
527 * stack buffer region with guard areas that trigger a MPU or MMU fault
528 * if written to.
529 *
530 * APIs that want to work with the buffer inside should continue to use
531 * char *.
532 *
533 * Stacks should always be created with K_THREAD_STACK_DEFINE().
534 */
535struct __packed _k_thread_stack_element {
536 char data;
537};
538typedef struct _k_thread_stack_element *k_thread_stack_t;
539
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400540
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400541/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700542 * @brief Create a thread.
543 *
544 * This routine initializes a thread, then schedules it for execution.
545 *
546 * The new thread may be scheduled for immediate execution or a delayed start.
547 * If the newly spawned thread does not have a delayed start the kernel
548 * scheduler may preempt the current thread to allow the new thread to
549 * execute.
550 *
551 * Thread options are architecture-specific, and can include K_ESSENTIAL,
552 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
553 * them using "|" (the logical OR operator).
554 *
555 * Historically, users often would use the beginning of the stack memory region
556 * to store the struct k_thread data, although corruption will occur if the
557 * stack overflows this region and stack protection features may not detect this
558 * situation.
559 *
560 * @param new_thread Pointer to uninitialized struct k_thread
561 * @param stack Pointer to the stack space.
562 * @param stack_size Stack size in bytes.
563 * @param entry Thread entry function.
564 * @param p1 1st entry point parameter.
565 * @param p2 2nd entry point parameter.
566 * @param p3 3rd entry point parameter.
567 * @param prio Thread priority.
568 * @param options Thread options.
569 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
570 *
571 * @return ID of new thread.
572 */
Andrew Boie507852a2017-07-25 18:47:07 -0700573extern k_tid_t k_thread_create(struct k_thread *new_thread,
574 k_thread_stack_t stack,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700575 size_t stack_size,
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700576 k_thread_entry_t entry,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700577 void *p1, void *p2, void *p3,
578 int prio, u32_t options, s32_t delay);
579
Andrew Boie3f091b52017-08-30 14:34:14 -0700580/**
581 * @brief Drop a thread's privileges permanently to user mode
582 *
583 * @param entry Function to start executing from
584 * @param p1 1st entry point parameter
585 * @param p2 2nd entry point parameter
586 * @param p3 3rd entry point parameter
587 */
588extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
589 void *p1, void *p2,
590 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700591
Andrew Boied26cf2d2017-03-30 13:07:02 -0700592/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500593 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400594 *
Allan Stephensc98da842016-11-11 15:45:03 -0500595 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500596 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400597 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500598 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400599 *
600 * @return N/A
601 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700602__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400603
604/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500605 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400606 *
607 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500608 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400609 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400610 * @return N/A
611 */
Kumar Galacc334c72017-04-21 10:55:34 -0500612extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400613
614/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500615 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400616 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500617 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400618 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500619 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400620 *
621 * @return N/A
622 */
Andrew Boie468190a2017-09-29 14:00:48 -0700623__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400624
625/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500626 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400627 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500628 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400629 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500630 * If @a thread is not currently sleeping, the routine has no effect.
631 *
632 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400633 *
634 * @return N/A
635 */
Andrew Boie468190a2017-09-29 14:00:48 -0700636__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400637
638/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500639 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400640 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500641 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400642 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700643__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400644
645/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500646 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400647 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500648 * This routine prevents @a thread from executing if it has not yet started
649 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400650 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500651 * @param thread ID of thread to cancel.
652 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700653 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500654 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400655 */
Andrew Boie468190a2017-09-29 14:00:48 -0700656__syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400657
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400658/**
Allan Stephensc98da842016-11-11 15:45:03 -0500659 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400660 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500661 * This routine permanently stops execution of @a thread. The thread is taken
662 * off all kernel queues it is part of (i.e. the ready queue, the timeout
663 * queue, or a kernel object wait queue). However, any kernel resources the
664 * thread might currently own (such as mutexes or memory blocks) are not
665 * released. It is the responsibility of the caller of this routine to ensure
666 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400667 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500668 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400669 *
670 * @return N/A
671 */
Andrew Boie468190a2017-09-29 14:00:48 -0700672__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400673
Andrew Boie7d627c52017-08-30 11:01:56 -0700674
675/**
676 * @brief Start an inactive thread
677 *
678 * If a thread was created with K_FOREVER in the delay parameter, it will
679 * not be added to the scheduling queue until this function is called
680 * on it.
681 *
682 * @param thread thread to start
683 */
Andrew Boie468190a2017-09-29 14:00:48 -0700684__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700685
Allan Stephensc98da842016-11-11 15:45:03 -0500686/**
687 * @cond INTERNAL_HIDDEN
688 */
689
Benjamin Walshd211a522016-12-06 11:44:01 -0500690/* timeout has timed out and is not on _timeout_q anymore */
691#define _EXPIRED (-2)
692
693/* timeout is not in use */
694#define _INACTIVE (-1)
695
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400696struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700697 struct k_thread *init_thread;
Andrew Boie507852a2017-07-25 18:47:07 -0700698 k_thread_stack_t init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400699 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700700 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500701 void *init_p1;
702 void *init_p2;
703 void *init_p3;
704 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500705 u32_t init_options;
706 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500707 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500708 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400709};
710
Andrew Boied26cf2d2017-03-30 13:07:02 -0700711#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400712 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500713 prio, options, delay, abort, groups) \
714 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700715 .init_thread = (thread), \
716 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500717 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700718 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400719 .init_p1 = (void *)p1, \
720 .init_p2 = (void *)p2, \
721 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500722 .init_prio = (prio), \
723 .init_options = (options), \
724 .init_delay = (delay), \
725 .init_abort = (abort), \
726 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400727 }
728
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400729/**
Allan Stephensc98da842016-11-11 15:45:03 -0500730 * INTERNAL_HIDDEN @endcond
731 */
732
733/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500734 * @brief Statically define and initialize a thread.
735 *
736 * The thread may be scheduled for immediate execution or a delayed start.
737 *
738 * Thread options are architecture-specific, and can include K_ESSENTIAL,
739 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
740 * them using "|" (the logical OR operator).
741 *
742 * The ID of the thread can be accessed using:
743 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500744 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500745 *
746 * @param name Name of the thread.
747 * @param stack_size Stack size in bytes.
748 * @param entry Thread entry function.
749 * @param p1 1st entry point parameter.
750 * @param p2 2nd entry point parameter.
751 * @param p3 3rd entry point parameter.
752 * @param prio Thread priority.
753 * @param options Thread options.
754 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400755 *
756 * @internal It has been observed that the x86 compiler by default aligns
757 * these _static_thread_data structures to 32-byte boundaries, thereby
758 * wasting space. To work around this, force a 4-byte alignment.
759 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500760#define K_THREAD_DEFINE(name, stack_size, \
761 entry, p1, p2, p3, \
762 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700763 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700764 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500765 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500766 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700767 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
768 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500769 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700770 NULL, 0); \
771 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400772
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400773/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500774 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400775 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500776 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400777 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500778 * @param thread ID of thread whose priority is needed.
779 *
780 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400781 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700782__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400783
784/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500785 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400786 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500787 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400788 *
789 * Rescheduling can occur immediately depending on the priority @a thread is
790 * set to:
791 *
792 * - If its priority is raised above the priority of the caller of this
793 * function, and the caller is preemptible, @a thread will be scheduled in.
794 *
795 * - If the caller operates on itself, it lowers its priority below that of
796 * other threads in the system, and the caller is preemptible, the thread of
797 * highest priority will be scheduled in.
798 *
799 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
800 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
801 * highest priority.
802 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500803 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400804 * @param prio New priority.
805 *
806 * @warning Changing the priority of a thread currently involved in mutex
807 * priority inheritance may result in undefined behavior.
808 *
809 * @return N/A
810 */
Andrew Boie468190a2017-09-29 14:00:48 -0700811__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400812
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400813/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500814 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400815 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500816 * This routine prevents the kernel scheduler from making @a thread the
817 * current thread. All other internal operations on @a thread are still
818 * performed; for example, any timeout it is waiting on keeps ticking,
819 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400820 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500821 * If @a thread is already suspended, the routine has no effect.
822 *
823 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400824 *
825 * @return N/A
826 */
Andrew Boie468190a2017-09-29 14:00:48 -0700827__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400828
829/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500830 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400831 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500832 * This routine allows the kernel scheduler to make @a thread the current
833 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400834 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500835 * If @a thread is not currently suspended, the routine has no effect.
836 *
837 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400838 *
839 * @return N/A
840 */
Andrew Boie468190a2017-09-29 14:00:48 -0700841__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400842
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400843/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500844 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400845 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500846 * This routine specifies how the scheduler will perform time slicing of
847 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400848 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500849 * To enable time slicing, @a slice must be non-zero. The scheduler
850 * ensures that no thread runs for more than the specified time limit
851 * before other threads of that priority are given a chance to execute.
852 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700853 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400854 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500855 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400856 * execute. Once the scheduler selects a thread for execution, there is no
857 * minimum guaranteed time the thread will execute before threads of greater or
858 * equal priority are scheduled.
859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500860 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400861 * for execution, this routine has no effect; the thread is immediately
862 * rescheduled after the slice period expires.
863 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500864 * To disable timeslicing, set both @a slice and @a prio to zero.
865 *
866 * @param slice Maximum time slice length (in milliseconds).
867 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400868 *
869 * @return N/A
870 */
Kumar Galacc334c72017-04-21 10:55:34 -0500871extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400872
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400873/**
Allan Stephensc98da842016-11-11 15:45:03 -0500874 * @} end defgroup thread_apis
875 */
876
877/**
878 * @addtogroup isr_apis
879 * @{
880 */
881
882/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500883 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400884 *
Allan Stephensc98da842016-11-11 15:45:03 -0500885 * This routine allows the caller to customize its actions, depending on
886 * whether it is a thread or an ISR.
887 *
888 * @note Can be called by ISRs.
889 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500890 * @return 0 if invoked by a thread.
891 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400892 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500893extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400894
Benjamin Walsh445830d2016-11-10 15:54:27 -0500895/**
896 * @brief Determine if code is running in a preemptible thread.
897 *
Allan Stephensc98da842016-11-11 15:45:03 -0500898 * This routine allows the caller to customize its actions, depending on
899 * whether it can be preempted by another thread. The routine returns a 'true'
900 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500901 *
Allan Stephensc98da842016-11-11 15:45:03 -0500902 * - The code is running in a thread, not at ISR.
903 * - The thread's priority is in the preemptible range.
904 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500905 *
Allan Stephensc98da842016-11-11 15:45:03 -0500906 * @note Can be called by ISRs.
907 *
908 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500909 * @return Non-zero if invoked by a preemptible thread.
910 */
Andrew Boie468190a2017-09-29 14:00:48 -0700911__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -0500912
Allan Stephensc98da842016-11-11 15:45:03 -0500913/**
914 * @} end addtogroup isr_apis
915 */
916
917/**
918 * @addtogroup thread_apis
919 * @{
920 */
921
922/**
923 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500924 *
Allan Stephensc98da842016-11-11 15:45:03 -0500925 * This routine prevents the current thread from being preempted by another
926 * thread by instructing the scheduler to treat it as a cooperative thread.
927 * If the thread subsequently performs an operation that makes it unready,
928 * it will be context switched out in the normal manner. When the thread
929 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500930 *
Allan Stephensc98da842016-11-11 15:45:03 -0500931 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500932 *
Allan Stephensc98da842016-11-11 15:45:03 -0500933 * @note k_sched_lock() and k_sched_unlock() should normally be used
934 * when the operation being performed can be safely interrupted by ISRs.
935 * However, if the amount of processing involved is very small, better
936 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500937 *
938 * @return N/A
939 */
940extern void k_sched_lock(void);
941
Allan Stephensc98da842016-11-11 15:45:03 -0500942/**
943 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500944 *
Allan Stephensc98da842016-11-11 15:45:03 -0500945 * This routine reverses the effect of a previous call to k_sched_lock().
946 * A thread must call the routine once for each time it called k_sched_lock()
947 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500948 *
949 * @return N/A
950 */
951extern void k_sched_unlock(void);
952
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400953/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500954 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400955 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500956 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400957 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500958 * Custom data is not used by the kernel itself, and is freely available
959 * for a thread to use as it sees fit. It can be used as a framework
960 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400961 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500962 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400963 *
964 * @return N/A
965 */
Andrew Boie468190a2017-09-29 14:00:48 -0700966__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400967
968/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500969 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500971 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400972 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500973 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400974 */
Andrew Boie468190a2017-09-29 14:00:48 -0700975__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400976
977/**
Allan Stephensc98da842016-11-11 15:45:03 -0500978 * @} end addtogroup thread_apis
979 */
980
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400981#include <sys_clock.h>
982
Allan Stephensc2f15a42016-11-17 12:24:22 -0500983/**
984 * @addtogroup clock_apis
985 * @{
986 */
987
988/**
989 * @brief Generate null timeout delay.
990 *
991 * This macro generates a timeout delay that that instructs a kernel API
992 * not to wait if the requested operation cannot be performed immediately.
993 *
994 * @return Timeout delay value.
995 */
996#define K_NO_WAIT 0
997
998/**
999 * @brief Generate timeout delay from milliseconds.
1000 *
1001 * This macro generates a timeout delay that that instructs a kernel API
1002 * to wait up to @a ms milliseconds to perform the requested operation.
1003 *
1004 * @param ms Duration in milliseconds.
1005 *
1006 * @return Timeout delay value.
1007 */
Johan Hedberg14471692016-11-13 10:52:15 +02001008#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001009
1010/**
1011 * @brief Generate timeout delay from seconds.
1012 *
1013 * This macro generates a timeout delay that that instructs a kernel API
1014 * to wait up to @a s seconds to perform the requested operation.
1015 *
1016 * @param s Duration in seconds.
1017 *
1018 * @return Timeout delay value.
1019 */
Johan Hedberg14471692016-11-13 10:52:15 +02001020#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001021
1022/**
1023 * @brief Generate timeout delay from minutes.
1024 *
1025 * This macro generates a timeout delay that that instructs a kernel API
1026 * to wait up to @a m minutes to perform the requested operation.
1027 *
1028 * @param m Duration in minutes.
1029 *
1030 * @return Timeout delay value.
1031 */
Johan Hedberg14471692016-11-13 10:52:15 +02001032#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001033
1034/**
1035 * @brief Generate timeout delay from hours.
1036 *
1037 * This macro generates a timeout delay that that instructs a kernel API
1038 * to wait up to @a h hours to perform the requested operation.
1039 *
1040 * @param h Duration in hours.
1041 *
1042 * @return Timeout delay value.
1043 */
Johan Hedberg14471692016-11-13 10:52:15 +02001044#define K_HOURS(h) K_MINUTES((h) * 60)
1045
Allan Stephensc98da842016-11-11 15:45:03 -05001046/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001047 * @brief Generate infinite timeout delay.
1048 *
1049 * This macro generates a timeout delay that that instructs a kernel API
1050 * to wait as long as necessary to perform the requested operation.
1051 *
1052 * @return Timeout delay value.
1053 */
1054#define K_FOREVER (-1)
1055
1056/**
1057 * @} end addtogroup clock_apis
1058 */
1059
1060/**
Allan Stephensc98da842016-11-11 15:45:03 -05001061 * @cond INTERNAL_HIDDEN
1062 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001063
Benjamin Walsh62092182016-12-20 14:39:08 -05001064/* kernel clocks */
1065
1066#if (sys_clock_ticks_per_sec == 1000) || \
1067 (sys_clock_ticks_per_sec == 500) || \
1068 (sys_clock_ticks_per_sec == 250) || \
1069 (sys_clock_ticks_per_sec == 125) || \
1070 (sys_clock_ticks_per_sec == 100) || \
1071 (sys_clock_ticks_per_sec == 50) || \
1072 (sys_clock_ticks_per_sec == 25) || \
1073 (sys_clock_ticks_per_sec == 20) || \
1074 (sys_clock_ticks_per_sec == 10) || \
1075 (sys_clock_ticks_per_sec == 1)
1076
1077 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1078#else
1079 /* yields horrible 64-bit math on many architectures: try to avoid */
1080 #define _NON_OPTIMIZED_TICKS_PER_SEC
1081#endif
1082
1083#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001084extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001085#else
Kumar Galacc334c72017-04-21 10:55:34 -05001086static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001087{
Kumar Galacc334c72017-04-21 10:55:34 -05001088 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001089}
1090#endif
1091
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001092/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001093#ifdef CONFIG_TICKLESS_KERNEL
1094#define _TICK_ALIGN 0
1095#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001096#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001097#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001098
Kumar Galacc334c72017-04-21 10:55:34 -05001099static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001100{
Benjamin Walsh62092182016-12-20 14:39:08 -05001101#ifdef CONFIG_SYS_CLOCK_EXISTS
1102
1103#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001104 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001105#else
Kumar Galacc334c72017-04-21 10:55:34 -05001106 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001107#endif
1108
1109#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001110 __ASSERT(ticks == 0, "");
1111 return 0;
1112#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001113}
1114
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001115struct k_timer {
1116 /*
1117 * _timeout structure must be first here if we want to use
1118 * dynamic timer allocation. timeout.node is used in the double-linked
1119 * list of free timers
1120 */
1121 struct _timeout timeout;
1122
Allan Stephens45bfa372016-10-12 12:39:42 -05001123 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001124 _wait_q_t wait_q;
1125
1126 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001127 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001128
1129 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001130 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001131
1132 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001133 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001134
Allan Stephens45bfa372016-10-12 12:39:42 -05001135 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001136 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001137
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001138 /* user-specific data, also used to support legacy features */
1139 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001140
Anas Nashif2f203c22016-12-18 06:57:45 -05001141 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001142};
1143
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001144#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001145 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001146 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001147 .timeout.wait_q = NULL, \
1148 .timeout.thread = NULL, \
1149 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001150 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001151 .expiry_fn = expiry, \
1152 .stop_fn = stop, \
1153 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001154 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001155 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001156 }
1157
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001158#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1159
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001160/**
Allan Stephensc98da842016-11-11 15:45:03 -05001161 * INTERNAL_HIDDEN @endcond
1162 */
1163
1164/**
1165 * @defgroup timer_apis Timer APIs
1166 * @ingroup kernel_apis
1167 * @{
1168 */
1169
1170/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001171 * @typedef k_timer_expiry_t
1172 * @brief Timer expiry function type.
1173 *
1174 * A timer's expiry function is executed by the system clock interrupt handler
1175 * each time the timer expires. The expiry function is optional, and is only
1176 * invoked if the timer has been initialized with one.
1177 *
1178 * @param timer Address of timer.
1179 *
1180 * @return N/A
1181 */
1182typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1183
1184/**
1185 * @typedef k_timer_stop_t
1186 * @brief Timer stop function type.
1187 *
1188 * A timer's stop function is executed if the timer is stopped prematurely.
1189 * The function runs in the context of the thread that stops the timer.
1190 * The stop function is optional, and is only invoked if the timer has been
1191 * initialized with one.
1192 *
1193 * @param timer Address of timer.
1194 *
1195 * @return N/A
1196 */
1197typedef void (*k_timer_stop_t)(struct k_timer *timer);
1198
1199/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001200 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001201 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001202 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001203 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001204 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001205 *
1206 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001207 * @param expiry_fn Function to invoke each time the timer expires.
1208 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001209 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001210#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001211 struct k_timer name \
1212 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001213 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001214
Allan Stephens45bfa372016-10-12 12:39:42 -05001215/**
1216 * @brief Initialize a timer.
1217 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001218 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001219 *
1220 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001221 * @param expiry_fn Function to invoke each time the timer expires.
1222 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001223 *
1224 * @return N/A
1225 */
1226extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001227 k_timer_expiry_t expiry_fn,
1228 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001229
Allan Stephens45bfa372016-10-12 12:39:42 -05001230/**
1231 * @brief Start a timer.
1232 *
1233 * This routine starts a timer, and resets its status to zero. The timer
1234 * begins counting down using the specified duration and period values.
1235 *
1236 * Attempting to start a timer that is already running is permitted.
1237 * The timer's status is reset to zero and the timer begins counting down
1238 * using the new duration and period values.
1239 *
1240 * @param timer Address of timer.
1241 * @param duration Initial timer duration (in milliseconds).
1242 * @param period Timer period (in milliseconds).
1243 *
1244 * @return N/A
1245 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001246extern void k_timer_start(struct k_timer *timer,
Kumar Galacc334c72017-04-21 10:55:34 -05001247 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001248
1249/**
1250 * @brief Stop a timer.
1251 *
1252 * This routine stops a running timer prematurely. The timer's stop function,
1253 * if one exists, is invoked by the caller.
1254 *
1255 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001256 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001257 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001258 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1259 * if @a k_timer_stop is to be called from ISRs.
1260 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001261 * @param timer Address of timer.
1262 *
1263 * @return N/A
1264 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001265extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001266
1267/**
1268 * @brief Read timer status.
1269 *
1270 * This routine reads the timer's status, which indicates the number of times
1271 * it has expired since its status was last read.
1272 *
1273 * Calling this routine resets the timer's status to zero.
1274 *
1275 * @param timer Address of timer.
1276 *
1277 * @return Timer status.
1278 */
Kumar Galacc334c72017-04-21 10:55:34 -05001279extern u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001280
1281/**
1282 * @brief Synchronize thread to timer expiration.
1283 *
1284 * This routine blocks the calling thread until the timer's status is non-zero
1285 * (indicating that it has expired at least once since it was last examined)
1286 * or the timer is stopped. If the timer status is already non-zero,
1287 * or the timer is already stopped, the caller continues without waiting.
1288 *
1289 * Calling this routine resets the timer's status to zero.
1290 *
1291 * This routine must not be used by interrupt handlers, since they are not
1292 * allowed to block.
1293 *
1294 * @param timer Address of timer.
1295 *
1296 * @return Timer status.
1297 */
Kumar Galacc334c72017-04-21 10:55:34 -05001298extern u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001299
1300/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001301 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001302 *
1303 * This routine computes the (approximate) time remaining before a running
1304 * timer next expires. If the timer is not running, it returns zero.
1305 *
1306 * @param timer Address of timer.
1307 *
1308 * @return Remaining time (in milliseconds).
1309 */
Kumar Galacc334c72017-04-21 10:55:34 -05001310static inline s32_t k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001311{
1312 return _timeout_remaining_get(&timer->timeout);
1313}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001314
Allan Stephensc98da842016-11-11 15:45:03 -05001315/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001316 * @brief Associate user-specific data with a timer.
1317 *
1318 * This routine records the @a user_data with the @a timer, to be retrieved
1319 * later.
1320 *
1321 * It can be used e.g. in a timer handler shared across multiple subsystems to
1322 * retrieve data specific to the subsystem this timer is associated with.
1323 *
1324 * @param timer Address of timer.
1325 * @param user_data User data to associate with the timer.
1326 *
1327 * @return N/A
1328 */
1329static inline void k_timer_user_data_set(struct k_timer *timer,
1330 void *user_data)
1331{
1332 timer->user_data = user_data;
1333}
1334
1335/**
1336 * @brief Retrieve the user-specific data from a timer.
1337 *
1338 * @param timer Address of timer.
1339 *
1340 * @return The user data.
1341 */
1342static inline void *k_timer_user_data_get(struct k_timer *timer)
1343{
1344 return timer->user_data;
1345}
1346
1347/**
Allan Stephensc98da842016-11-11 15:45:03 -05001348 * @} end defgroup timer_apis
1349 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001350
Allan Stephensc98da842016-11-11 15:45:03 -05001351/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001352 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001353 * @{
1354 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001355
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001356/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001357 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001358 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001359 * This routine returns the elapsed time since the system booted,
1360 * in milliseconds.
1361 *
1362 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001363 */
Kumar Galacc334c72017-04-21 10:55:34 -05001364extern s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001365
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001366#ifdef CONFIG_TICKLESS_KERNEL
1367/**
1368 * @brief Enable clock always on in tickless kernel
1369 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001370 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001371 * there are no timer events programmed in tickless kernel
1372 * scheduling. This is necessary if the clock is used to track
1373 * passage of time.
1374 *
1375 * @retval prev_status Previous status of always on flag
1376 */
1377static inline int k_enable_sys_clock_always_on(void)
1378{
1379 int prev_status = _sys_clock_always_on;
1380
1381 _sys_clock_always_on = 1;
1382 _enable_sys_clock();
1383
1384 return prev_status;
1385}
1386
1387/**
1388 * @brief Disable clock always on in tickless kernel
1389 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001390 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001391 * there are no timer events programmed in tickless kernel
1392 * scheduling. To save power, this routine should be called
1393 * immediately when clock is not used to track time.
1394 */
1395static inline void k_disable_sys_clock_always_on(void)
1396{
1397 _sys_clock_always_on = 0;
1398}
1399#else
1400#define k_enable_sys_clock_always_on() do { } while ((0))
1401#define k_disable_sys_clock_always_on() do { } while ((0))
1402#endif
1403
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001404/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001405 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001406 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001407 * This routine returns the lower 32-bits of the elapsed time since the system
1408 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001409 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001410 * This routine can be more efficient than k_uptime_get(), as it reduces the
1411 * need for interrupt locking and 64-bit math. However, the 32-bit result
1412 * cannot hold a system uptime time larger than approximately 50 days, so the
1413 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001414 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001415 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001416 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001417__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001418
1419/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001420 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001421 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001422 * This routine computes the elapsed time between the current system uptime
1423 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001424 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001425 * @param reftime Pointer to a reference time, which is updated to the current
1426 * uptime upon return.
1427 *
1428 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001429 */
Kumar Galacc334c72017-04-21 10:55:34 -05001430extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001431
1432/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001433 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001434 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001435 * This routine computes the elapsed time between the current system uptime
1436 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001437 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001438 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1439 * need for interrupt locking and 64-bit math. However, the 32-bit result
1440 * cannot hold an elapsed time larger than approximately 50 days, so the
1441 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001442 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001443 * @param reftime Pointer to a reference time, which is updated to the current
1444 * uptime upon return.
1445 *
1446 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001447 */
Kumar Galacc334c72017-04-21 10:55:34 -05001448extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001449
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001450/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001451 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001452 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001453 * This routine returns the current time, as measured by the system's hardware
1454 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001455 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001456 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001457 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001458#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001459
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001460/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001461 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001462 */
1463
Allan Stephensc98da842016-11-11 15:45:03 -05001464/**
1465 * @cond INTERNAL_HIDDEN
1466 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001467
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001468struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001469 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001470 union {
1471 _wait_q_t wait_q;
1472
1473 _POLL_EVENT;
1474 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001475
1476 _OBJECT_TRACING_NEXT_PTR(k_queue);
1477};
1478
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001479#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001480 { \
1481 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1482 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001483 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001484 _OBJECT_TRACING_INIT \
1485 }
1486
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001487#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1488
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001489/**
1490 * INTERNAL_HIDDEN @endcond
1491 */
1492
1493/**
1494 * @defgroup queue_apis Queue APIs
1495 * @ingroup kernel_apis
1496 * @{
1497 */
1498
1499/**
1500 * @brief Initialize a queue.
1501 *
1502 * This routine initializes a queue object, prior to its first use.
1503 *
1504 * @param queue Address of the queue.
1505 *
1506 * @return N/A
1507 */
1508extern void k_queue_init(struct k_queue *queue);
1509
1510/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001511 * @brief Cancel waiting on a queue.
1512 *
1513 * This routine causes first thread pending on @a queue, if any, to
1514 * return from k_queue_get() call with NULL value (as if timeout expired).
1515 *
1516 * @note Can be called by ISRs.
1517 *
1518 * @param queue Address of the queue.
1519 *
1520 * @return N/A
1521 */
1522extern void k_queue_cancel_wait(struct k_queue *queue);
1523
1524/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001525 * @brief Append an element to the end of a queue.
1526 *
1527 * This routine appends a data item to @a queue. A queue data item must be
1528 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1529 * reserved for the kernel's use.
1530 *
1531 * @note Can be called by ISRs.
1532 *
1533 * @param queue Address of the queue.
1534 * @param data Address of the data item.
1535 *
1536 * @return N/A
1537 */
1538extern void k_queue_append(struct k_queue *queue, void *data);
1539
1540/**
1541 * @brief Prepend an element to a queue.
1542 *
1543 * This routine prepends a data item to @a queue. A queue data item must be
1544 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1545 * reserved for the kernel's use.
1546 *
1547 * @note Can be called by ISRs.
1548 *
1549 * @param queue Address of the queue.
1550 * @param data Address of the data item.
1551 *
1552 * @return N/A
1553 */
1554extern void k_queue_prepend(struct k_queue *queue, void *data);
1555
1556/**
1557 * @brief Inserts an element to a queue.
1558 *
1559 * This routine inserts a data item to @a queue after previous item. A queue
1560 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1561 * item are reserved for the kernel's use.
1562 *
1563 * @note Can be called by ISRs.
1564 *
1565 * @param queue Address of the queue.
1566 * @param prev Address of the previous data item.
1567 * @param data Address of the data item.
1568 *
1569 * @return N/A
1570 */
1571extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1572
1573/**
1574 * @brief Atomically append a list of elements to a queue.
1575 *
1576 * This routine adds a list of data items to @a queue in one operation.
1577 * The data items must be in a singly-linked list, with the first 32 bits
1578 * in each data item pointing to the next data item; the list must be
1579 * NULL-terminated.
1580 *
1581 * @note Can be called by ISRs.
1582 *
1583 * @param queue Address of the queue.
1584 * @param head Pointer to first node in singly-linked list.
1585 * @param tail Pointer to last node in singly-linked list.
1586 *
1587 * @return N/A
1588 */
1589extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1590
1591/**
1592 * @brief Atomically add a list of elements to a queue.
1593 *
1594 * This routine adds a list of data items to @a queue in one operation.
1595 * The data items must be in a singly-linked list implemented using a
1596 * sys_slist_t object. Upon completion, the original list is empty.
1597 *
1598 * @note Can be called by ISRs.
1599 *
1600 * @param queue Address of the queue.
1601 * @param list Pointer to sys_slist_t object.
1602 *
1603 * @return N/A
1604 */
1605extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1606
1607/**
1608 * @brief Get an element from a queue.
1609 *
1610 * This routine removes first data item from @a queue. The first 32 bits of the
1611 * data item are reserved for the kernel's use.
1612 *
1613 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1614 *
1615 * @param queue Address of the queue.
1616 * @param timeout Waiting period to obtain a data item (in milliseconds),
1617 * or one of the special values K_NO_WAIT and K_FOREVER.
1618 *
1619 * @return Address of the data item if successful; NULL if returned
1620 * without waiting, or waiting period timed out.
1621 */
Kumar Galacc334c72017-04-21 10:55:34 -05001622extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001623
1624/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001625 * @brief Remove an element from a queue.
1626 *
1627 * This routine removes data item from @a queue. The first 32 bits of the
1628 * data item are reserved for the kernel's use. Removing elements from k_queue
1629 * rely on sys_slist_find_and_remove which is not a constant time operation.
1630 *
1631 * @note Can be called by ISRs
1632 *
1633 * @param queue Address of the queue.
1634 * @param data Address of the data item.
1635 *
1636 * @return true if data item was removed
1637 */
1638static inline bool k_queue_remove(struct k_queue *queue, void *data)
1639{
1640 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1641}
1642
1643/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001644 * @brief Query a queue to see if it has data available.
1645 *
1646 * Note that the data might be already gone by the time this function returns
1647 * if other threads are also trying to read from the queue.
1648 *
1649 * @note Can be called by ISRs.
1650 *
1651 * @param queue Address of the queue.
1652 *
1653 * @return Non-zero if the queue is empty.
1654 * @return 0 if data is available.
1655 */
1656static inline int k_queue_is_empty(struct k_queue *queue)
1657{
1658 return (int)sys_slist_is_empty(&queue->data_q);
1659}
1660
1661/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001662 * @brief Peek element at the head of queue.
1663 *
1664 * Return element from the head of queue without removing it.
1665 *
1666 * @param queue Address of the queue.
1667 *
1668 * @return Head element, or NULL if queue is empty.
1669 */
1670static inline void *k_queue_peek_head(struct k_queue *queue)
1671{
1672 return sys_slist_peek_head(&queue->data_q);
1673}
1674
1675/**
1676 * @brief Peek element at the tail of queue.
1677 *
1678 * Return element from the tail of queue without removing it.
1679 *
1680 * @param queue Address of the queue.
1681 *
1682 * @return Tail element, or NULL if queue is empty.
1683 */
1684static inline void *k_queue_peek_tail(struct k_queue *queue)
1685{
1686 return sys_slist_peek_tail(&queue->data_q);
1687}
1688
1689/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001690 * @brief Statically define and initialize a queue.
1691 *
1692 * The queue can be accessed outside the module where it is defined using:
1693 *
1694 * @code extern struct k_queue <name>; @endcode
1695 *
1696 * @param name Name of the queue.
1697 */
1698#define K_QUEUE_DEFINE(name) \
1699 struct k_queue name \
1700 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001701 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001702
1703/**
1704 * @} end defgroup queue_apis
1705 */
1706
1707/**
1708 * @cond INTERNAL_HIDDEN
1709 */
1710
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001711struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001712 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001713};
1714
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001715#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001716 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001717 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001718 }
1719
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001720#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1721
Allan Stephensc98da842016-11-11 15:45:03 -05001722/**
1723 * INTERNAL_HIDDEN @endcond
1724 */
1725
1726/**
1727 * @defgroup fifo_apis Fifo APIs
1728 * @ingroup kernel_apis
1729 * @{
1730 */
1731
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001732/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001733 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001734 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001735 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001736 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001737 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001738 *
1739 * @return N/A
1740 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001741#define k_fifo_init(fifo) \
1742 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001743
1744/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001745 * @brief Cancel waiting on a fifo.
1746 *
1747 * This routine causes first thread pending on @a fifo, if any, to
1748 * return from k_fifo_get() call with NULL value (as if timeout
1749 * expired).
1750 *
1751 * @note Can be called by ISRs.
1752 *
1753 * @param fifo Address of the fifo.
1754 *
1755 * @return N/A
1756 */
1757#define k_fifo_cancel_wait(fifo) \
1758 k_queue_cancel_wait((struct k_queue *) fifo)
1759
1760/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001761 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001762 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001763 * This routine adds a data item to @a fifo. A fifo data item must be
1764 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1765 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001766 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001767 * @note Can be called by ISRs.
1768 *
1769 * @param fifo Address of the fifo.
1770 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001771 *
1772 * @return N/A
1773 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001774#define k_fifo_put(fifo, data) \
1775 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001776
1777/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001778 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001779 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001780 * This routine adds a list of data items to @a fifo in one operation.
1781 * The data items must be in a singly-linked list, with the first 32 bits
1782 * each data item pointing to the next data item; the list must be
1783 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001784 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001785 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001786 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001787 * @param fifo Address of the fifo.
1788 * @param head Pointer to first node in singly-linked list.
1789 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001790 *
1791 * @return N/A
1792 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001793#define k_fifo_put_list(fifo, head, tail) \
1794 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001795
1796/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001797 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001799 * This routine adds a list of data items to @a fifo in one operation.
1800 * The data items must be in a singly-linked list implemented using a
1801 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001802 * and must be re-initialized via sys_slist_init().
1803 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001804 * @note Can be called by ISRs.
1805 *
1806 * @param fifo Address of the fifo.
1807 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001808 *
1809 * @return N/A
1810 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001811#define k_fifo_put_slist(fifo, list) \
1812 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001813
1814/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001815 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001817 * This routine removes a data item from @a fifo in a "first in, first out"
1818 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001820 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1821 *
1822 * @param fifo Address of the fifo.
1823 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001824 * or one of the special values K_NO_WAIT and K_FOREVER.
1825 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001826 * @return Address of the data item if successful; NULL if returned
1827 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001828 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001829#define k_fifo_get(fifo, timeout) \
1830 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001831
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001832/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001833 * @brief Query a fifo to see if it has data available.
1834 *
1835 * Note that the data might be already gone by the time this function returns
1836 * if other threads is also trying to read from the fifo.
1837 *
1838 * @note Can be called by ISRs.
1839 *
1840 * @param fifo Address of the fifo.
1841 *
1842 * @return Non-zero if the fifo is empty.
1843 * @return 0 if data is available.
1844 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001845#define k_fifo_is_empty(fifo) \
1846 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001847
1848/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001849 * @brief Peek element at the head of fifo.
1850 *
1851 * Return element from the head of fifo without removing it. A usecase
1852 * for this is if elements of the fifo are themselves containers. Then
1853 * on each iteration of processing, a head container will be peeked,
1854 * and some data processed out of it, and only if the container is empty,
1855 * it will be completely remove from the fifo.
1856 *
1857 * @param fifo Address of the fifo.
1858 *
1859 * @return Head element, or NULL if the fifo is empty.
1860 */
1861#define k_fifo_peek_head(fifo) \
1862 k_queue_peek_head((struct k_queue *) fifo)
1863
1864/**
1865 * @brief Peek element at the tail of fifo.
1866 *
1867 * Return element from the tail of fifo (without removing it). A usecase
1868 * for this is if elements of the fifo are themselves containers. Then
1869 * it may be useful to add more data to the last container in fifo.
1870 *
1871 * @param fifo Address of the fifo.
1872 *
1873 * @return Tail element, or NULL if fifo is empty.
1874 */
1875#define k_fifo_peek_tail(fifo) \
1876 k_queue_peek_tail((struct k_queue *) fifo)
1877
1878/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001879 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001881 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001882 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001883 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001884 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001885 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001886 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001887#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001888 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001889 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001890 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001891
Allan Stephensc98da842016-11-11 15:45:03 -05001892/**
1893 * @} end defgroup fifo_apis
1894 */
1895
1896/**
1897 * @cond INTERNAL_HIDDEN
1898 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001899
1900struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001901 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001902};
1903
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001904#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001905 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001906 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001907 }
1908
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001909#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1910
Allan Stephensc98da842016-11-11 15:45:03 -05001911/**
1912 * INTERNAL_HIDDEN @endcond
1913 */
1914
1915/**
1916 * @defgroup lifo_apis Lifo APIs
1917 * @ingroup kernel_apis
1918 * @{
1919 */
1920
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001921/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001922 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001923 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001924 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001925 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001926 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001927 *
1928 * @return N/A
1929 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001930#define k_lifo_init(lifo) \
1931 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001932
1933/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001934 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001935 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001936 * This routine adds a data item to @a lifo. A lifo data item must be
1937 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1938 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001939 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001940 * @note Can be called by ISRs.
1941 *
1942 * @param lifo Address of the lifo.
1943 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001944 *
1945 * @return N/A
1946 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001947#define k_lifo_put(lifo, data) \
1948 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001949
1950/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001951 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001952 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001953 * This routine removes a data item from @a lifo in a "last in, first out"
1954 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001955 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001956 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1957 *
1958 * @param lifo Address of the lifo.
1959 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001960 * or one of the special values K_NO_WAIT and K_FOREVER.
1961 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001962 * @return Address of the data item if successful; NULL if returned
1963 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001964 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001965#define k_lifo_get(lifo, timeout) \
1966 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001967
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001968/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001969 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001971 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001972 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001973 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001975 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001976 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001977#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001978 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001979 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001980 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001981
Allan Stephensc98da842016-11-11 15:45:03 -05001982/**
1983 * @} end defgroup lifo_apis
1984 */
1985
1986/**
1987 * @cond INTERNAL_HIDDEN
1988 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001989
1990struct k_stack {
1991 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001992 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001993
Anas Nashif2f203c22016-12-18 06:57:45 -05001994 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001995};
1996
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001997#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05001998 { \
1999 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2000 .base = stack_buffer, \
2001 .next = stack_buffer, \
2002 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002003 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002004 }
2005
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002006#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2007
Allan Stephensc98da842016-11-11 15:45:03 -05002008/**
2009 * INTERNAL_HIDDEN @endcond
2010 */
2011
2012/**
2013 * @defgroup stack_apis Stack APIs
2014 * @ingroup kernel_apis
2015 * @{
2016 */
2017
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002018/**
2019 * @brief Initialize a stack.
2020 *
2021 * This routine initializes a stack object, prior to its first use.
2022 *
2023 * @param stack Address of the stack.
2024 * @param buffer Address of array used to hold stacked values.
2025 * @param num_entries Maximum number of values that can be stacked.
2026 *
2027 * @return N/A
2028 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05002029extern void k_stack_init(struct k_stack *stack,
Kumar Galacc334c72017-04-21 10:55:34 -05002030 u32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002031
2032/**
2033 * @brief Push an element onto a stack.
2034 *
2035 * This routine adds a 32-bit value @a data to @a stack.
2036 *
2037 * @note Can be called by ISRs.
2038 *
2039 * @param stack Address of the stack.
2040 * @param data Value to push onto the stack.
2041 *
2042 * @return N/A
2043 */
Kumar Galacc334c72017-04-21 10:55:34 -05002044extern void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002045
2046/**
2047 * @brief Pop an element from a stack.
2048 *
2049 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2050 * manner and stores the value in @a data.
2051 *
2052 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2053 *
2054 * @param stack Address of the stack.
2055 * @param data Address of area to hold the value popped from the stack.
2056 * @param timeout Waiting period to obtain a value (in milliseconds),
2057 * or one of the special values K_NO_WAIT and K_FOREVER.
2058 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002059 * @retval 0 Element popped from stack.
2060 * @retval -EBUSY Returned without waiting.
2061 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002062 */
Kumar Galacc334c72017-04-21 10:55:34 -05002063extern int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002064
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002065/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002066 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002067 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002068 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002069 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002070 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002071 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002072 * @param name Name of the stack.
2073 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002074 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002075#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002076 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002077 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002078 struct k_stack name \
2079 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002080 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002081 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002082
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002083/**
Allan Stephensc98da842016-11-11 15:45:03 -05002084 * @} end defgroup stack_apis
2085 */
2086
Allan Stephens6bba9b02016-11-16 14:56:54 -05002087struct k_work;
2088
Allan Stephensc98da842016-11-11 15:45:03 -05002089/**
2090 * @defgroup workqueue_apis Workqueue Thread APIs
2091 * @ingroup kernel_apis
2092 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002093 */
2094
Allan Stephens6bba9b02016-11-16 14:56:54 -05002095/**
2096 * @typedef k_work_handler_t
2097 * @brief Work item handler function type.
2098 *
2099 * A work item's handler function is executed by a workqueue's thread
2100 * when the work item is processed by the workqueue.
2101 *
2102 * @param work Address of the work item.
2103 *
2104 * @return N/A
2105 */
2106typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002107
2108/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002109 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002110 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002111
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002112struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002113 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002114 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002115};
2116
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002117enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002118 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002119};
2120
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002121struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002122 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002123 k_work_handler_t handler;
2124 atomic_t flags[1];
2125};
2126
Allan Stephens6bba9b02016-11-16 14:56:54 -05002127struct k_delayed_work {
2128 struct k_work work;
2129 struct _timeout timeout;
2130 struct k_work_q *work_q;
2131};
2132
2133extern struct k_work_q k_sys_work_q;
2134
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002135/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002136 * INTERNAL_HIDDEN @endcond
2137 */
2138
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002139#define _K_WORK_INITIALIZER(work_handler) \
2140 { \
2141 ._reserved = NULL, \
2142 .handler = work_handler, \
2143 .flags = { 0 } \
2144 }
2145
2146#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2147
Allan Stephens6bba9b02016-11-16 14:56:54 -05002148/**
2149 * @brief Initialize a statically-defined work item.
2150 *
2151 * This macro can be used to initialize a statically-defined workqueue work
2152 * item, prior to its first use. For example,
2153 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002154 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002155 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002156 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002157 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002158 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002159#define K_WORK_DEFINE(work, work_handler) \
2160 struct k_work work \
2161 __in_section(_k_work, static, work) = \
2162 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002163
2164/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002165 * @brief Initialize a work item.
2166 *
2167 * This routine initializes a workqueue work item, prior to its first use.
2168 *
2169 * @param work Address of work item.
2170 * @param handler Function to invoke each time work item is processed.
2171 *
2172 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002173 */
2174static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2175{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002176 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002177 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002178 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002179}
2180
2181/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002182 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002183 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002184 * This routine submits work item @a work to be processed by workqueue
2185 * @a work_q. If the work item is already pending in the workqueue's queue
2186 * as a result of an earlier submission, this routine has no effect on the
2187 * work item. If the work item has already been processed, or is currently
2188 * being processed, its work is considered complete and the work item can be
2189 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002190 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002191 * @warning
2192 * A submitted work item must not be modified until it has been processed
2193 * by the workqueue.
2194 *
2195 * @note Can be called by ISRs.
2196 *
2197 * @param work_q Address of workqueue.
2198 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002199 *
2200 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002201 */
2202static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2203 struct k_work *work)
2204{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002205 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002206 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002207 }
2208}
2209
2210/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002211 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002212 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002213 * This routine indicates if work item @a work is pending in a workqueue's
2214 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002215 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002216 * @note Can be called by ISRs.
2217 *
2218 * @param work Address of work item.
2219 *
2220 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002221 */
2222static inline int k_work_pending(struct k_work *work)
2223{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002224 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002225}
2226
2227/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002228 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002229 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002230 * This routine starts workqueue @a work_q. The workqueue spawns its work
2231 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002232 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002233 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002234 * @param stack Pointer to work queue thread's stack space, as defined by
2235 * K_THREAD_STACK_DEFINE()
2236 * @param stack_size Size of the work queue thread's stack (in bytes), which
2237 * should either be the same constant passed to
2238 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002239 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002240 *
2241 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002242 */
Andrew Boie507852a2017-07-25 18:47:07 -07002243extern void k_work_q_start(struct k_work_q *work_q,
2244 k_thread_stack_t stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002245 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002246
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002247/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002248 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002249 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002250 * This routine initializes a workqueue delayed work item, prior to
2251 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002252 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002253 * @param work Address of delayed work item.
2254 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002255 *
2256 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002257 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002258extern void k_delayed_work_init(struct k_delayed_work *work,
2259 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002260
2261/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002262 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002263 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002264 * This routine schedules work item @a work to be processed by workqueue
2265 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002266 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002267 * Only when the countdown completes is the work item actually submitted to
2268 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002269 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002270 * Submitting a previously submitted delayed work item that is still
2271 * counting down cancels the existing submission and restarts the countdown
2272 * using the new delay. If the work item is currently pending on the
2273 * workqueue's queue because the countdown has completed it is too late to
2274 * resubmit the item, and resubmission fails without impacting the work item.
2275 * If the work item has already been processed, or is currently being processed,
2276 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002277 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002278 * @warning
2279 * A delayed work item must not be modified until it has been processed
2280 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002281 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002282 * @note Can be called by ISRs.
2283 *
2284 * @param work_q Address of workqueue.
2285 * @param work Address of delayed work item.
2286 * @param delay Delay before submitting the work item (in milliseconds).
2287 *
2288 * @retval 0 Work item countdown started.
2289 * @retval -EINPROGRESS Work item is already pending.
2290 * @retval -EINVAL Work item is being processed or has completed its work.
2291 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002292 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002293extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2294 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002295 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002296
2297/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002298 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002299 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002300 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002301 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002302 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002303 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002304 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002305 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002306 * @param work Address of delayed work item.
2307 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002308 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002309 * @retval -EINPROGRESS Work item is already pending.
2310 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002311 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002312extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002313
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002314/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002315 * @brief Submit a work item to the system workqueue.
2316 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002317 * This routine submits work item @a work to be processed by the system
2318 * workqueue. If the work item is already pending in the workqueue's queue
2319 * as a result of an earlier submission, this routine has no effect on the
2320 * work item. If the work item has already been processed, or is currently
2321 * being processed, its work is considered complete and the work item can be
2322 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002323 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002324 * @warning
2325 * Work items submitted to the system workqueue should avoid using handlers
2326 * that block or yield since this may prevent the system workqueue from
2327 * processing other work items in a timely manner.
2328 *
2329 * @note Can be called by ISRs.
2330 *
2331 * @param work Address of work item.
2332 *
2333 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002334 */
2335static inline void k_work_submit(struct k_work *work)
2336{
2337 k_work_submit_to_queue(&k_sys_work_q, work);
2338}
2339
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002340/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002341 * @brief Submit a delayed work item to the system workqueue.
2342 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002343 * This routine schedules work item @a work to be processed by the system
2344 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002345 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002346 * Only when the countdown completes is the work item actually submitted to
2347 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002348 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002349 * Submitting a previously submitted delayed work item that is still
2350 * counting down cancels the existing submission and restarts the countdown
2351 * using the new delay. If the work item is currently pending on the
2352 * workqueue's queue because the countdown has completed it is too late to
2353 * resubmit the item, and resubmission fails without impacting the work item.
2354 * If the work item has already been processed, or is currently being processed,
2355 * its work is considered complete and the work item can be resubmitted.
2356 *
2357 * @warning
2358 * Work items submitted to the system workqueue should avoid using handlers
2359 * that block or yield since this may prevent the system workqueue from
2360 * processing other work items in a timely manner.
2361 *
2362 * @note Can be called by ISRs.
2363 *
2364 * @param work Address of delayed work item.
2365 * @param delay Delay before submitting the work item (in milliseconds).
2366 *
2367 * @retval 0 Work item countdown started.
2368 * @retval -EINPROGRESS Work item is already pending.
2369 * @retval -EINVAL Work item is being processed or has completed its work.
2370 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002371 */
2372static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002373 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002374{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002375 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002376}
2377
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002378/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002379 * @brief Get time remaining before a delayed work gets scheduled.
2380 *
2381 * This routine computes the (approximate) time remaining before a
2382 * delayed work gets executed. If the delayed work is not waiting to be
2383 * schedules, it returns zero.
2384 *
2385 * @param work Delayed work item.
2386 *
2387 * @return Remaining time (in milliseconds).
2388 */
Kumar Galacc334c72017-04-21 10:55:34 -05002389static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002390{
2391 return _timeout_remaining_get(&work->timeout);
2392}
2393
2394/**
Allan Stephensc98da842016-11-11 15:45:03 -05002395 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002396 */
2397
Allan Stephensc98da842016-11-11 15:45:03 -05002398/**
2399 * @cond INTERNAL_HIDDEN
2400 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002401
2402struct k_mutex {
2403 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002404 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002405 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002406 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002407
Anas Nashif2f203c22016-12-18 06:57:45 -05002408 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002409};
2410
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002411#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002412 { \
2413 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2414 .owner = NULL, \
2415 .lock_count = 0, \
2416 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002417 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002418 }
2419
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002420#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2421
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002422/**
Allan Stephensc98da842016-11-11 15:45:03 -05002423 * INTERNAL_HIDDEN @endcond
2424 */
2425
2426/**
2427 * @defgroup mutex_apis Mutex APIs
2428 * @ingroup kernel_apis
2429 * @{
2430 */
2431
2432/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002433 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002434 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002435 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002436 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002437 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002438 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002439 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002440 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002441#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002442 struct k_mutex name \
2443 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002444 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002445
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002446/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002447 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002448 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002449 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002450 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002451 * Upon completion, the mutex is available and does not have an owner.
2452 *
2453 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002454 *
2455 * @return N/A
2456 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002457__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002458
2459/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002460 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002461 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002462 * This routine locks @a mutex. If the mutex is locked by another thread,
2463 * the calling thread waits until the mutex becomes available or until
2464 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002465 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002466 * A thread is permitted to lock a mutex it has already locked. The operation
2467 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002468 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002469 * @param mutex Address of the mutex.
2470 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002471 * or one of the special values K_NO_WAIT and K_FOREVER.
2472 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002473 * @retval 0 Mutex locked.
2474 * @retval -EBUSY Returned without waiting.
2475 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002476 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002477__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002478
2479/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002480 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002481 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002482 * This routine unlocks @a mutex. The mutex must already be locked by the
2483 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002484 *
2485 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002486 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002487 * thread.
2488 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002489 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002490 *
2491 * @return N/A
2492 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002493__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002494
Allan Stephensc98da842016-11-11 15:45:03 -05002495/**
2496 * @} end defgroup mutex_apis
2497 */
2498
2499/**
2500 * @cond INTERNAL_HIDDEN
2501 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002502
2503struct k_sem {
2504 _wait_q_t wait_q;
2505 unsigned int count;
2506 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002507 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002508
Anas Nashif2f203c22016-12-18 06:57:45 -05002509 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002510};
2511
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002512#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002513 { \
2514 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2515 .count = initial_count, \
2516 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002517 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002518 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002519 }
2520
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002521#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2522
Allan Stephensc98da842016-11-11 15:45:03 -05002523/**
2524 * INTERNAL_HIDDEN @endcond
2525 */
2526
2527/**
2528 * @defgroup semaphore_apis Semaphore APIs
2529 * @ingroup kernel_apis
2530 * @{
2531 */
2532
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002533/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002534 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002535 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002536 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002537 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002538 * @param sem Address of the semaphore.
2539 * @param initial_count Initial semaphore count.
2540 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002541 *
2542 * @return N/A
2543 */
Andrew Boie99280232017-09-29 14:17:47 -07002544__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2545 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002546
2547/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002548 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002549 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002550 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002551 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002552 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2553 *
2554 * @param sem Address of the semaphore.
2555 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002556 * or one of the special values K_NO_WAIT and K_FOREVER.
2557 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002558 * @note When porting code from the nanokernel legacy API to the new API, be
2559 * careful with the return value of this function. The return value is the
2560 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2561 * non-zero means failure, while the nano_sem_take family returns 1 for success
2562 * and 0 for failure.
2563 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002564 * @retval 0 Semaphore taken.
2565 * @retval -EBUSY Returned without waiting.
2566 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002567 */
Andrew Boie99280232017-09-29 14:17:47 -07002568__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002569
2570/**
2571 * @brief Give a semaphore.
2572 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002573 * This routine gives @a sem, unless the semaphore is already at its maximum
2574 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002575 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002576 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002577 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002578 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002579 *
2580 * @return N/A
2581 */
Andrew Boie99280232017-09-29 14:17:47 -07002582__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002583
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002584/**
2585 * @brief Reset a semaphore's count to zero.
2586 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002587 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002588 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002589 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002590 *
2591 * @return N/A
2592 */
Andrew Boie990bf162017-10-03 12:36:49 -07002593__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002594
2595static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002596{
2597 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002598}
2599
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002600/**
2601 * @brief Get a semaphore's count.
2602 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002603 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002604 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002605 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002606 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002607 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002608 */
Andrew Boie990bf162017-10-03 12:36:49 -07002609__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002610
2611static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002612{
2613 return sem->count;
2614}
2615
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002616/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002617 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002618 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002619 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002620 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002621 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002622 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002623 * @param name Name of the semaphore.
2624 * @param initial_count Initial semaphore count.
2625 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002626 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002627#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002628 struct k_sem name \
2629 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002630 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002631
Allan Stephensc98da842016-11-11 15:45:03 -05002632/**
2633 * @} end defgroup semaphore_apis
2634 */
2635
2636/**
2637 * @defgroup alert_apis Alert APIs
2638 * @ingroup kernel_apis
2639 * @{
2640 */
2641
Allan Stephens5eceb852016-11-16 10:16:30 -05002642/**
2643 * @typedef k_alert_handler_t
2644 * @brief Alert handler function type.
2645 *
2646 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002647 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002648 * and is only invoked if the alert has been initialized with one.
2649 *
2650 * @param alert Address of the alert.
2651 *
2652 * @return 0 if alert has been consumed; non-zero if alert should pend.
2653 */
2654typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002655
2656/**
2657 * @} end defgroup alert_apis
2658 */
2659
2660/**
2661 * @cond INTERNAL_HIDDEN
2662 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002663
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002664#define K_ALERT_DEFAULT NULL
2665#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002666
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002667struct k_alert {
2668 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002669 atomic_t send_count;
2670 struct k_work work_item;
2671 struct k_sem sem;
2672
Anas Nashif2f203c22016-12-18 06:57:45 -05002673 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002674};
2675
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002676extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002677
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002678#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002679 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002680 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002681 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002682 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2683 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002684 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002685 }
2686
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002687#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2688
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002689/**
Allan Stephensc98da842016-11-11 15:45:03 -05002690 * INTERNAL_HIDDEN @endcond
2691 */
2692
2693/**
2694 * @addtogroup alert_apis
2695 * @{
2696 */
2697
2698/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002699 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002700 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002701 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002702 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002703 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002704 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002705 * @param name Name of the alert.
2706 * @param alert_handler Action to take when alert is sent. Specify either
2707 * the address of a function to be invoked by the system workqueue
2708 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2709 * K_ALERT_DEFAULT (which causes the alert to pend).
2710 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002711 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002712#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002713 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002714 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002715 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002716 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002717
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002718/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002719 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002720 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002721 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002722 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002723 * @param alert Address of the alert.
2724 * @param handler Action to take when alert is sent. Specify either the address
2725 * of a function to be invoked by the system workqueue thread,
2726 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2727 * K_ALERT_DEFAULT (which causes the alert to pend).
2728 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002729 *
2730 * @return N/A
2731 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002732extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2733 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002734
2735/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002736 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002737 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002738 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002739 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002740 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2741 *
2742 * @param alert Address of the alert.
2743 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002744 * or one of the special values K_NO_WAIT and K_FOREVER.
2745 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002746 * @retval 0 Alert received.
2747 * @retval -EBUSY Returned without waiting.
2748 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002749 */
Andrew Boie310e9872017-09-29 04:41:15 -07002750__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002751
2752/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002753 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002754 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002755 * This routine signals @a alert. The action specified for @a alert will
2756 * be taken, which may trigger the execution of an alert handler function
2757 * and/or cause the alert to pend (assuming the alert has not reached its
2758 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002759 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002760 * @note Can be called by ISRs.
2761 *
2762 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002763 *
2764 * @return N/A
2765 */
Andrew Boie310e9872017-09-29 04:41:15 -07002766__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002767
2768/**
Allan Stephensc98da842016-11-11 15:45:03 -05002769 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002770 */
2771
Allan Stephensc98da842016-11-11 15:45:03 -05002772/**
2773 * @cond INTERNAL_HIDDEN
2774 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002775
2776struct k_msgq {
2777 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002778 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002779 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002780 char *buffer_start;
2781 char *buffer_end;
2782 char *read_ptr;
2783 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002784 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002785
Anas Nashif2f203c22016-12-18 06:57:45 -05002786 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002787};
2788
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002789#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002790 { \
2791 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002792 .max_msgs = q_max_msgs, \
2793 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002794 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002795 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002796 .read_ptr = q_buffer, \
2797 .write_ptr = q_buffer, \
2798 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002799 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002800 }
2801
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002802#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2803
Peter Mitsis1da807e2016-10-06 11:36:59 -04002804/**
Allan Stephensc98da842016-11-11 15:45:03 -05002805 * INTERNAL_HIDDEN @endcond
2806 */
2807
2808/**
2809 * @defgroup msgq_apis Message Queue APIs
2810 * @ingroup kernel_apis
2811 * @{
2812 */
2813
2814/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002815 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002817 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2818 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002819 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2820 * message is similarly aligned to this boundary, @a q_msg_size must also be
2821 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002822 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002823 * The message queue can be accessed outside the module where it is defined
2824 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002825 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002826 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002827 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002828 * @param q_name Name of the message queue.
2829 * @param q_msg_size Message size (in bytes).
2830 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002831 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002832 */
2833#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2834 static char __noinit __aligned(q_align) \
2835 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002836 struct k_msgq q_name \
2837 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002838 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002839 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002840
Peter Mitsisd7a37502016-10-13 11:37:40 -04002841/**
2842 * @brief Initialize a message queue.
2843 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002844 * This routine initializes a message queue object, prior to its first use.
2845 *
Allan Stephensda827222016-11-09 14:23:58 -06002846 * The message queue's ring buffer must contain space for @a max_msgs messages,
2847 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2848 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2849 * that each message is similarly aligned to this boundary, @a q_msg_size
2850 * must also be a multiple of N.
2851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002852 * @param q Address of the message queue.
2853 * @param buffer Pointer to ring buffer that holds queued messages.
2854 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002855 * @param max_msgs Maximum number of messages that can be queued.
2856 *
2857 * @return N/A
2858 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002859extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002860 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002861
2862/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002863 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002865 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002866 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002867 * @note Can be called by ISRs.
2868 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002869 * @param q Address of the message queue.
2870 * @param data Pointer to the message.
2871 * @param timeout Waiting period to add the message (in milliseconds),
2872 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002873 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002874 * @retval 0 Message sent.
2875 * @retval -ENOMSG Returned without waiting or queue purged.
2876 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002877 */
Kumar Galacc334c72017-04-21 10:55:34 -05002878extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002879
2880/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002881 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002883 * This routine receives a message from message queue @a q in a "first in,
2884 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002885 *
Allan Stephensc98da842016-11-11 15:45:03 -05002886 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002887 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002888 * @param q Address of the message queue.
2889 * @param data Address of area to hold the received message.
2890 * @param timeout Waiting period to receive the message (in milliseconds),
2891 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002892 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002893 * @retval 0 Message received.
2894 * @retval -ENOMSG Returned without waiting.
2895 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002896 */
Kumar Galacc334c72017-04-21 10:55:34 -05002897extern int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002898
2899/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002900 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002901 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002902 * This routine discards all unreceived messages in a message queue's ring
2903 * buffer. Any threads that are blocked waiting to send a message to the
2904 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002905 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002906 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002907 *
2908 * @return N/A
2909 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002910extern void k_msgq_purge(struct k_msgq *q);
2911
Peter Mitsis67be2492016-10-07 11:44:34 -04002912/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002913 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002914 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002915 * This routine returns the number of unused entries in a message queue's
2916 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002917 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002918 * @param q Address of the message queue.
2919 *
2920 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002921 */
Kumar Galacc334c72017-04-21 10:55:34 -05002922static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002923{
2924 return q->max_msgs - q->used_msgs;
2925}
2926
Peter Mitsisd7a37502016-10-13 11:37:40 -04002927/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002928 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002929 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002930 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002931 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002932 * @param q Address of the message queue.
2933 *
2934 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002935 */
Kumar Galacc334c72017-04-21 10:55:34 -05002936static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002937{
2938 return q->used_msgs;
2939}
2940
Allan Stephensc98da842016-11-11 15:45:03 -05002941/**
2942 * @} end defgroup msgq_apis
2943 */
2944
2945/**
2946 * @defgroup mem_pool_apis Memory Pool APIs
2947 * @ingroup kernel_apis
2948 * @{
2949 */
2950
Andy Ross73cb9582017-05-09 10:42:39 -07002951/* Note on sizing: the use of a 20 bit field for block means that,
2952 * assuming a reasonable minimum block size of 16 bytes, we're limited
2953 * to 16M of memory managed by a single pool. Long term it would be
2954 * good to move to a variable bit size based on configuration.
2955 */
2956struct k_mem_block_id {
2957 u32_t pool : 8;
2958 u32_t level : 4;
2959 u32_t block : 20;
2960};
2961
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002962struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002963 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002964 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002965};
2966
Allan Stephensc98da842016-11-11 15:45:03 -05002967/**
2968 * @} end defgroup mem_pool_apis
2969 */
2970
2971/**
2972 * @defgroup mailbox_apis Mailbox APIs
2973 * @ingroup kernel_apis
2974 * @{
2975 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002976
2977struct k_mbox_msg {
2978 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002979 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002980 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002981 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002982 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002983 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002984 /** sender's message data buffer */
2985 void *tx_data;
2986 /** internal use only - needed for legacy API support */
2987 void *_rx_data;
2988 /** message data block descriptor */
2989 struct k_mem_block tx_block;
2990 /** source thread id */
2991 k_tid_t rx_source_thread;
2992 /** target thread id */
2993 k_tid_t tx_target_thread;
2994 /** internal use only - thread waiting on send (may be a dummy) */
2995 k_tid_t _syncing_thread;
2996#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2997 /** internal use only - semaphore used during asynchronous send */
2998 struct k_sem *_async_sem;
2999#endif
3000};
3001
Allan Stephensc98da842016-11-11 15:45:03 -05003002/**
3003 * @cond INTERNAL_HIDDEN
3004 */
3005
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003006struct k_mbox {
3007 _wait_q_t tx_msg_queue;
3008 _wait_q_t rx_msg_queue;
3009
Anas Nashif2f203c22016-12-18 06:57:45 -05003010 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003011};
3012
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003013#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003014 { \
3015 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3016 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003017 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003018 }
3019
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003020#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3021
Peter Mitsis12092702016-10-14 12:57:23 -04003022/**
Allan Stephensc98da842016-11-11 15:45:03 -05003023 * INTERNAL_HIDDEN @endcond
3024 */
3025
3026/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003027 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003028 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003029 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003030 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003031 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003032 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003033 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003034 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003035#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003036 struct k_mbox name \
3037 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003038 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003039
Peter Mitsis12092702016-10-14 12:57:23 -04003040/**
3041 * @brief Initialize a mailbox.
3042 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003043 * This routine initializes a mailbox object, prior to its first use.
3044 *
3045 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003046 *
3047 * @return N/A
3048 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003049extern void k_mbox_init(struct k_mbox *mbox);
3050
Peter Mitsis12092702016-10-14 12:57:23 -04003051/**
3052 * @brief Send a mailbox message in a synchronous manner.
3053 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003054 * This routine sends a message to @a mbox and waits for a receiver to both
3055 * receive and process it. The message data may be in a buffer, in a memory
3056 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003058 * @param mbox Address of the mailbox.
3059 * @param tx_msg Address of the transmit message descriptor.
3060 * @param timeout Waiting period for the message to be received (in
3061 * milliseconds), or one of the special values K_NO_WAIT
3062 * and K_FOREVER. Once the message has been received,
3063 * this routine waits as long as necessary for the message
3064 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003065 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003066 * @retval 0 Message sent.
3067 * @retval -ENOMSG Returned without waiting.
3068 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003069 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003070extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003071 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003072
Peter Mitsis12092702016-10-14 12:57:23 -04003073/**
3074 * @brief Send a mailbox message in an asynchronous manner.
3075 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003076 * This routine sends a message to @a mbox without waiting for a receiver
3077 * to process it. The message data may be in a buffer, in a memory pool block,
3078 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3079 * will be given when the message has been both received and completely
3080 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003081 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003082 * @param mbox Address of the mailbox.
3083 * @param tx_msg Address of the transmit message descriptor.
3084 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003085 *
3086 * @return N/A
3087 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003088extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003089 struct k_sem *sem);
3090
Peter Mitsis12092702016-10-14 12:57:23 -04003091/**
3092 * @brief Receive a mailbox message.
3093 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003094 * This routine receives a message from @a mbox, then optionally retrieves
3095 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003096 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003097 * @param mbox Address of the mailbox.
3098 * @param rx_msg Address of the receive message descriptor.
3099 * @param buffer Address of the buffer to receive data, or NULL to defer data
3100 * retrieval and message disposal until later.
3101 * @param timeout Waiting period for a message to be received (in
3102 * milliseconds), or one of the special values K_NO_WAIT
3103 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003104 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003105 * @retval 0 Message received.
3106 * @retval -ENOMSG Returned without waiting.
3107 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003108 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003109extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003110 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003111
3112/**
3113 * @brief Retrieve mailbox message data into a buffer.
3114 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003115 * This routine completes the processing of a received message by retrieving
3116 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003117 *
3118 * Alternatively, this routine can be used to dispose of a received message
3119 * without retrieving its data.
3120 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003121 * @param rx_msg Address of the receive message descriptor.
3122 * @param buffer Address of the buffer to receive data, or NULL to discard
3123 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003124 *
3125 * @return N/A
3126 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003127extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003128
3129/**
3130 * @brief Retrieve mailbox message data into a memory pool block.
3131 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003132 * This routine completes the processing of a received message by retrieving
3133 * its data into a memory pool block, then disposing of the message.
3134 * The memory pool block that results from successful retrieval must be
3135 * returned to the pool once the data has been processed, even in cases
3136 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003137 *
3138 * Alternatively, this routine can be used to dispose of a received message
3139 * without retrieving its data. In this case there is no need to return a
3140 * memory pool block to the pool.
3141 *
3142 * This routine allocates a new memory pool block for the data only if the
3143 * data is not already in one. If a new block cannot be allocated, the routine
3144 * returns a failure code and the received message is left unchanged. This
3145 * permits the caller to reattempt data retrieval at a later time or to dispose
3146 * of the received message without retrieving its data.
3147 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003148 * @param rx_msg Address of a receive message descriptor.
3149 * @param pool Address of memory pool, or NULL to discard data.
3150 * @param block Address of the area to hold memory pool block info.
3151 * @param timeout Waiting period to wait for a memory pool block (in
3152 * milliseconds), or one of the special values K_NO_WAIT
3153 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003154 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003155 * @retval 0 Data retrieved.
3156 * @retval -ENOMEM Returned without waiting.
3157 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003158 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003159extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003160 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003161 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003162
Allan Stephensc98da842016-11-11 15:45:03 -05003163/**
3164 * @} end defgroup mailbox_apis
3165 */
3166
3167/**
3168 * @cond INTERNAL_HIDDEN
3169 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003170
3171struct k_pipe {
3172 unsigned char *buffer; /* Pipe buffer: may be NULL */
3173 size_t size; /* Buffer size */
3174 size_t bytes_used; /* # bytes used in buffer */
3175 size_t read_index; /* Where in buffer to read from */
3176 size_t write_index; /* Where in buffer to write */
3177
3178 struct {
3179 _wait_q_t readers; /* Reader wait queue */
3180 _wait_q_t writers; /* Writer wait queue */
3181 } wait_q;
3182
Anas Nashif2f203c22016-12-18 06:57:45 -05003183 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003184};
3185
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003186#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003187 { \
3188 .buffer = pipe_buffer, \
3189 .size = pipe_buffer_size, \
3190 .bytes_used = 0, \
3191 .read_index = 0, \
3192 .write_index = 0, \
3193 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3194 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003195 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003196 }
3197
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003198#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3199
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003200/**
Allan Stephensc98da842016-11-11 15:45:03 -05003201 * INTERNAL_HIDDEN @endcond
3202 */
3203
3204/**
3205 * @defgroup pipe_apis Pipe APIs
3206 * @ingroup kernel_apis
3207 * @{
3208 */
3209
3210/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003211 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003212 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003213 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003214 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003215 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003216 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003217 * @param name Name of the pipe.
3218 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3219 * or zero if no ring buffer is used.
3220 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003221 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003222#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3223 static unsigned char __noinit __aligned(pipe_align) \
3224 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003225 struct k_pipe name \
3226 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003227 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003228
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003229/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003230 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003231 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003232 * This routine initializes a pipe object, prior to its first use.
3233 *
3234 * @param pipe Address of the pipe.
3235 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3236 * is used.
3237 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3238 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003239 *
3240 * @return N/A
3241 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003242__syscall void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3243 size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003244
3245/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003246 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003247 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003248 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003249 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003250 * @param pipe Address of the pipe.
3251 * @param data Address of data to write.
3252 * @param bytes_to_write Size of data (in bytes).
3253 * @param bytes_written Address of area to hold the number of bytes written.
3254 * @param min_xfer Minimum number of bytes to write.
3255 * @param timeout Waiting period to wait for the data to be written (in
3256 * milliseconds), or one of the special values K_NO_WAIT
3257 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003258 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003259 * @retval 0 At least @a min_xfer bytes of data were written.
3260 * @retval -EIO Returned without waiting; zero data bytes were written.
3261 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003262 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003263 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003264__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3265 size_t bytes_to_write, size_t *bytes_written,
3266 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003267
3268/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003269 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003270 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003271 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003272 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003273 * @param pipe Address of the pipe.
3274 * @param data Address to place the data read from pipe.
3275 * @param bytes_to_read Maximum number of data bytes to read.
3276 * @param bytes_read Address of area to hold the number of bytes read.
3277 * @param min_xfer Minimum number of data bytes to read.
3278 * @param timeout Waiting period to wait for the data to be read (in
3279 * milliseconds), or one of the special values K_NO_WAIT
3280 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003281 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003282 * @retval 0 At least @a min_xfer bytes of data were read.
3283 * @retval -EIO Returned without waiting; zero data bytes were read.
3284 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003285 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003286 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003287__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3288 size_t bytes_to_read, size_t *bytes_read,
3289 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003290
3291/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003292 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003293 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003294 * This routine writes the data contained in a memory block to @a pipe.
3295 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003296 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003297 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003298 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003299 * @param block Memory block containing data to send
3300 * @param size Number of data bytes in memory block to send
3301 * @param sem Semaphore to signal upon completion (else NULL)
3302 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003303 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003304 */
3305extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3306 size_t size, struct k_sem *sem);
3307
3308/**
Allan Stephensc98da842016-11-11 15:45:03 -05003309 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003310 */
3311
Allan Stephensc98da842016-11-11 15:45:03 -05003312/**
3313 * @cond INTERNAL_HIDDEN
3314 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003315
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003316struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003317 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003318 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003319 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003320 char *buffer;
3321 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003322 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003323
Anas Nashif2f203c22016-12-18 06:57:45 -05003324 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003325};
3326
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003327#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003328 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003329 { \
3330 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003331 .num_blocks = slab_num_blocks, \
3332 .block_size = slab_block_size, \
3333 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003334 .free_list = NULL, \
3335 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003336 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003337 }
3338
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003339#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3340
3341
Peter Mitsis578f9112016-10-07 13:50:31 -04003342/**
Allan Stephensc98da842016-11-11 15:45:03 -05003343 * INTERNAL_HIDDEN @endcond
3344 */
3345
3346/**
3347 * @defgroup mem_slab_apis Memory Slab APIs
3348 * @ingroup kernel_apis
3349 * @{
3350 */
3351
3352/**
Allan Stephensda827222016-11-09 14:23:58 -06003353 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003354 *
Allan Stephensda827222016-11-09 14:23:58 -06003355 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003356 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003357 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3358 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003359 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003360 *
Allan Stephensda827222016-11-09 14:23:58 -06003361 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003362 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003363 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003364 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003365 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003366 * @param name Name of the memory slab.
3367 * @param slab_block_size Size of each memory block (in bytes).
3368 * @param slab_num_blocks Number memory blocks.
3369 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003370 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003371#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3372 char __noinit __aligned(slab_align) \
3373 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3374 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003375 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003376 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003377 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003378
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003379/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003380 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003381 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003382 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003383 *
Allan Stephensda827222016-11-09 14:23:58 -06003384 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3385 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3386 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3387 * To ensure that each memory block is similarly aligned to this boundary,
3388 * @a slab_block_size must also be a multiple of N.
3389 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003390 * @param slab Address of the memory slab.
3391 * @param buffer Pointer to buffer used for the memory blocks.
3392 * @param block_size Size of each memory block (in bytes).
3393 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003394 *
3395 * @return N/A
3396 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003397extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003398 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003399
3400/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003401 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003402 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003403 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003404 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003405 * @param slab Address of the memory slab.
3406 * @param mem Pointer to block address area.
3407 * @param timeout Maximum time to wait for operation to complete
3408 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3409 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003410 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003411 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003412 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003413 * @retval -ENOMEM Returned without waiting.
3414 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003415 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003416extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003417 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003418
3419/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003420 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003421 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003422 * This routine releases a previously allocated memory block back to its
3423 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003424 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003425 * @param slab Address of the memory slab.
3426 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003427 *
3428 * @return N/A
3429 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003430extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003431
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003432/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003433 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003434 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003435 * This routine gets the number of memory blocks that are currently
3436 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003437 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003438 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003439 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003440 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003441 */
Kumar Galacc334c72017-04-21 10:55:34 -05003442static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003443{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003444 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003445}
3446
Peter Mitsisc001aa82016-10-13 13:53:37 -04003447/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003448 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003449 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003450 * This routine gets the number of memory blocks that are currently
3451 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003452 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003453 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003454 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003455 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003456 */
Kumar Galacc334c72017-04-21 10:55:34 -05003457static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003458{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003459 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003460}
3461
Allan Stephensc98da842016-11-11 15:45:03 -05003462/**
3463 * @} end defgroup mem_slab_apis
3464 */
3465
3466/**
3467 * @cond INTERNAL_HIDDEN
3468 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003469
Andy Ross73cb9582017-05-09 10:42:39 -07003470struct k_mem_pool_lvl {
3471 union {
3472 u32_t *bits_p;
3473 u32_t bits;
3474 };
3475 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003476};
3477
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003478struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003479 void *buf;
3480 size_t max_sz;
3481 u16_t n_max;
3482 u8_t n_levels;
3483 u8_t max_inline_level;
3484 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003485 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003486};
3487
Andy Ross73cb9582017-05-09 10:42:39 -07003488#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003489
Andy Ross73cb9582017-05-09 10:42:39 -07003490#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3491
3492#define _MPOOL_LVLS(maxsz, minsz) \
3493 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3494 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3495 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3496 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3497 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3498 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3499 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3500 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3501 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3502 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3503 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3504 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3505 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3506 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3507 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3508 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3509
3510/* Rounds the needed bits up to integer multiples of u32_t */
3511#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3512 ((((n_max) << (2*(l))) + 31) / 32)
3513
3514/* One word gets stored free unioned with the pointer, otherwise the
3515 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003516 */
Andy Ross73cb9582017-05-09 10:42:39 -07003517#define _MPOOL_LBIT_WORDS(n_max, l) \
3518 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3519 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003520
Andy Ross73cb9582017-05-09 10:42:39 -07003521/* How many bytes for the bitfields of a single level? */
3522#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3523 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3524 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003525
Andy Ross73cb9582017-05-09 10:42:39 -07003526/* Size of the bitmap array that follows the buffer in allocated memory */
3527#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3528 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3529 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3530 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3531 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3532 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3533 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3534 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3535 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3536 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3537 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3538 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3539 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3540 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3541 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3542 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3543 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003544
3545/**
Allan Stephensc98da842016-11-11 15:45:03 -05003546 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003547 */
3548
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003549/**
Allan Stephensc98da842016-11-11 15:45:03 -05003550 * @addtogroup mem_pool_apis
3551 * @{
3552 */
3553
3554/**
3555 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003556 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003557 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3558 * long. The memory pool allows blocks to be repeatedly partitioned into
3559 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003560 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003561 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003562 * If the pool is to be accessed outside the module where it is defined, it
3563 * can be declared via
3564 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003565 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003566 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003567 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003568 * @param minsz Size of the smallest blocks in the pool (in bytes).
3569 * @param maxsz Size of the largest blocks in the pool (in bytes).
3570 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003571 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003572 */
Andy Ross73cb9582017-05-09 10:42:39 -07003573#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3574 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3575 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3576 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3577 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3578 .buf = _mpool_buf_##name, \
3579 .max_sz = maxsz, \
3580 .n_max = nmax, \
3581 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3582 .levels = _mpool_lvls_##name, \
3583 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003584
Peter Mitsis937042c2016-10-13 13:18:26 -04003585/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003586 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003587 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003588 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003589 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003590 * @param pool Address of the memory pool.
3591 * @param block Pointer to block descriptor for the allocated memory.
3592 * @param size Amount of memory to allocate (in bytes).
3593 * @param timeout Maximum time to wait for operation to complete
3594 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3595 * or K_FOREVER to wait as long as necessary.
3596 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003597 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003598 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003599 * @retval -ENOMEM Returned without waiting.
3600 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003601 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003602extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003603 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003604
3605/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003606 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003607 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003608 * This routine releases a previously allocated memory block back to its
3609 * memory pool.
3610 *
3611 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003612 *
3613 * @return N/A
3614 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003615extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003616
3617/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003618 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003619 *
Andy Ross73cb9582017-05-09 10:42:39 -07003620 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003621 *
Andy Ross73cb9582017-05-09 10:42:39 -07003622 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003623 *
3624 * @return N/A
3625 */
Andy Ross73cb9582017-05-09 10:42:39 -07003626static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003627
3628/**
Allan Stephensc98da842016-11-11 15:45:03 -05003629 * @} end addtogroup mem_pool_apis
3630 */
3631
3632/**
3633 * @defgroup heap_apis Heap Memory Pool APIs
3634 * @ingroup kernel_apis
3635 * @{
3636 */
3637
3638/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003639 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003640 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003641 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003642 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003643 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003644 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003645 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003646 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003647 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003648extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003649
3650/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003651 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003652 *
3653 * This routine provides traditional free() semantics. The memory being
3654 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003655 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003656 * If @a ptr is NULL, no operation is performed.
3657 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003658 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003659 *
3660 * @return N/A
3661 */
3662extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003663
Allan Stephensc98da842016-11-11 15:45:03 -05003664/**
3665 * @} end defgroup heap_apis
3666 */
3667
Benjamin Walshacc68c12017-01-29 18:57:45 -05003668/* polling API - PRIVATE */
3669
Benjamin Walshb0179862017-02-02 16:39:57 -05003670#ifdef CONFIG_POLL
3671#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3672#else
3673#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3674#endif
3675
Benjamin Walshacc68c12017-01-29 18:57:45 -05003676/* private - implementation data created as needed, per-type */
3677struct _poller {
3678 struct k_thread *thread;
3679};
3680
3681/* private - types bit positions */
3682enum _poll_types_bits {
3683 /* can be used to ignore an event */
3684 _POLL_TYPE_IGNORE,
3685
3686 /* to be signaled by k_poll_signal() */
3687 _POLL_TYPE_SIGNAL,
3688
3689 /* semaphore availability */
3690 _POLL_TYPE_SEM_AVAILABLE,
3691
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003692 /* queue/fifo/lifo data availability */
3693 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003694
3695 _POLL_NUM_TYPES
3696};
3697
3698#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3699
3700/* private - states bit positions */
3701enum _poll_states_bits {
3702 /* default state when creating event */
3703 _POLL_STATE_NOT_READY,
3704
Benjamin Walshacc68c12017-01-29 18:57:45 -05003705 /* signaled by k_poll_signal() */
3706 _POLL_STATE_SIGNALED,
3707
3708 /* semaphore is available */
3709 _POLL_STATE_SEM_AVAILABLE,
3710
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003711 /* data is available to read on queue/fifo/lifo */
3712 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003713
3714 _POLL_NUM_STATES
3715};
3716
3717#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3718
3719#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003720 (32 - (0 \
3721 + 8 /* tag */ \
3722 + _POLL_NUM_TYPES \
3723 + _POLL_NUM_STATES \
3724 + 1 /* modes */ \
3725 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003726
3727#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3728#error overflow of 32-bit word in struct k_poll_event
3729#endif
3730
3731/* end of polling API - PRIVATE */
3732
3733
3734/**
3735 * @defgroup poll_apis Async polling APIs
3736 * @ingroup kernel_apis
3737 * @{
3738 */
3739
3740/* Public polling API */
3741
3742/* public - values for k_poll_event.type bitfield */
3743#define K_POLL_TYPE_IGNORE 0
3744#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3745#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003746#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3747#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003748
3749/* public - polling modes */
3750enum k_poll_modes {
3751 /* polling thread does not take ownership of objects when available */
3752 K_POLL_MODE_NOTIFY_ONLY = 0,
3753
3754 K_POLL_NUM_MODES
3755};
3756
3757/* public - values for k_poll_event.state bitfield */
3758#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003759#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3760#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003761#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3762#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003763
3764/* public - poll signal object */
3765struct k_poll_signal {
3766 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003767 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003768
3769 /*
3770 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3771 * user resets it to 0.
3772 */
3773 unsigned int signaled;
3774
3775 /* custom result value passed to k_poll_signal() if needed */
3776 int result;
3777};
3778
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003779#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003780 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003781 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003782 .signaled = 0, \
3783 .result = 0, \
3784 }
3785
3786struct k_poll_event {
3787 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003788 sys_dnode_t _node;
3789
3790 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003791 struct _poller *poller;
3792
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003793 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003794 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003795
Benjamin Walshacc68c12017-01-29 18:57:45 -05003796 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003797 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003798
3799 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003800 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003801
3802 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003803 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003804
3805 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003806 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003807
3808 /* per-type data */
3809 union {
3810 void *obj;
3811 struct k_poll_signal *signal;
3812 struct k_sem *sem;
3813 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003814 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003815 };
3816};
3817
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003818#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003819 { \
3820 .poller = NULL, \
3821 .type = event_type, \
3822 .state = K_POLL_STATE_NOT_READY, \
3823 .mode = event_mode, \
3824 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003825 { .obj = event_obj }, \
3826 }
3827
3828#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3829 event_tag) \
3830 { \
3831 .type = event_type, \
3832 .tag = event_tag, \
3833 .state = K_POLL_STATE_NOT_READY, \
3834 .mode = event_mode, \
3835 .unused = 0, \
3836 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003837 }
3838
3839/**
3840 * @brief Initialize one struct k_poll_event instance
3841 *
3842 * After this routine is called on a poll event, the event it ready to be
3843 * placed in an event array to be passed to k_poll().
3844 *
3845 * @param event The event to initialize.
3846 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3847 * values. Only values that apply to the same object being polled
3848 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3849 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003850 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003851 * @param obj Kernel object or poll signal.
3852 *
3853 * @return N/A
3854 */
3855
Kumar Galacc334c72017-04-21 10:55:34 -05003856extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003857 int mode, void *obj);
3858
3859/**
3860 * @brief Wait for one or many of multiple poll events to occur
3861 *
3862 * This routine allows a thread to wait concurrently for one or many of
3863 * multiple poll events to have occurred. Such events can be a kernel object
3864 * being available, like a semaphore, or a poll signal event.
3865 *
3866 * When an event notifies that a kernel object is available, the kernel object
3867 * is not "given" to the thread calling k_poll(): it merely signals the fact
3868 * that the object was available when the k_poll() call was in effect. Also,
3869 * all threads trying to acquire an object the regular way, i.e. by pending on
3870 * the object, have precedence over the thread polling on the object. This
3871 * means that the polling thread will never get the poll event on an object
3872 * until the object becomes available and its pend queue is empty. For this
3873 * reason, the k_poll() call is more effective when the objects being polled
3874 * only have one thread, the polling thread, trying to acquire them.
3875 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003876 * When k_poll() returns 0, the caller should loop on all the events that were
3877 * passed to k_poll() and check the state field for the values that were
3878 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003879 *
3880 * Before being reused for another call to k_poll(), the user has to reset the
3881 * state field to K_POLL_STATE_NOT_READY.
3882 *
3883 * @param events An array of pointers to events to be polled for.
3884 * @param num_events The number of events in the array.
3885 * @param timeout Waiting period for an event to be ready (in milliseconds),
3886 * or one of the special values K_NO_WAIT and K_FOREVER.
3887 *
3888 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003889 * @retval -EAGAIN Waiting period timed out.
3890 */
3891
3892extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003893 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003894
3895/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003896 * @brief Initialize a poll signal object.
3897 *
3898 * Ready a poll signal object to be signaled via k_poll_signal().
3899 *
3900 * @param signal A poll signal.
3901 *
3902 * @return N/A
3903 */
3904
3905extern void k_poll_signal_init(struct k_poll_signal *signal);
3906
3907/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003908 * @brief Signal a poll signal object.
3909 *
3910 * This routine makes ready a poll signal, which is basically a poll event of
3911 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3912 * made ready to run. A @a result value can be specified.
3913 *
3914 * The poll signal contains a 'signaled' field that, when set by
3915 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3916 * be reset by the user before being passed again to k_poll() or k_poll() will
3917 * consider it being signaled, and will return immediately.
3918 *
3919 * @param signal A poll signal.
3920 * @param result The value to store in the result field of the signal.
3921 *
3922 * @retval 0 The signal was delivered successfully.
3923 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3924 */
3925
3926extern int k_poll_signal(struct k_poll_signal *signal, int result);
3927
3928/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003929extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003930
3931/**
3932 * @} end defgroup poll_apis
3933 */
3934
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003935/**
3936 * @brief Make the CPU idle.
3937 *
3938 * This function makes the CPU idle until an event wakes it up.
3939 *
3940 * In a regular system, the idle thread should be the only thread responsible
3941 * for making the CPU idle and triggering any type of power management.
3942 * However, in some more constrained systems, such as a single-threaded system,
3943 * the only thread would be responsible for this if needed.
3944 *
3945 * @return N/A
3946 */
3947extern void k_cpu_idle(void);
3948
3949/**
3950 * @brief Make the CPU idle in an atomic fashion.
3951 *
3952 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3953 * must be done atomically before making the CPU idle.
3954 *
3955 * @param key Interrupt locking key obtained from irq_lock().
3956 *
3957 * @return N/A
3958 */
3959extern void k_cpu_atomic_idle(unsigned int key);
3960
Kumar Galacc334c72017-04-21 10:55:34 -05003961extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003962
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003963#include <arch/cpu.h>
3964
Andrew Boiecdb94d62017-04-18 15:22:05 -07003965#ifdef _ARCH_EXCEPT
3966/* This archtecture has direct support for triggering a CPU exception */
3967#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3968#else
3969
3970#include <misc/printk.h>
3971
3972/* NOTE: This is the implementation for arches that do not implement
3973 * _ARCH_EXCEPT() to generate a real CPU exception.
3974 *
3975 * We won't have a real exception frame to determine the PC value when
3976 * the oops occurred, so print file and line number before we jump into
3977 * the fatal error handler.
3978 */
3979#define _k_except_reason(reason) do { \
3980 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3981 _NanoFatalErrorHandler(reason, &_default_esf); \
3982 CODE_UNREACHABLE; \
3983 } while (0)
3984
3985#endif /* _ARCH__EXCEPT */
3986
3987/**
3988 * @brief Fatally terminate a thread
3989 *
3990 * This should be called when a thread has encountered an unrecoverable
3991 * runtime condition and needs to terminate. What this ultimately
3992 * means is determined by the _fatal_error_handler() implementation, which
3993 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3994 *
3995 * If this is called from ISR context, the default system fatal error handler
3996 * will treat it as an unrecoverable system error, just like k_panic().
3997 */
3998#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3999
4000/**
4001 * @brief Fatally terminate the system
4002 *
4003 * This should be called when the Zephyr kernel has encountered an
4004 * unrecoverable runtime condition and needs to terminate. What this ultimately
4005 * means is determined by the _fatal_error_handler() implementation, which
4006 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4007 */
4008#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4009
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004010/*
4011 * private APIs that are utilized by one or more public APIs
4012 */
4013
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004014#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004015extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004016#else
4017#define _init_static_threads() do { } while ((0))
4018#endif
4019
4020extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05004021extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004022
Andrew Boiedc5d9352017-06-02 12:56:47 -07004023/* arch/cpu.h may declare an architecture or platform-specific macro
4024 * for properly declaring stacks, compatible with MMU/MPU constraints if
4025 * enabled
4026 */
4027#ifdef _ARCH_THREAD_STACK_DEFINE
4028#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4029#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4030 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4031#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4032#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004033static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4034{
4035 return _ARCH_THREAD_STACK_BUFFER(sym);
4036}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004037#else
4038/**
4039 * @brief Declare a toplevel thread stack memory region
4040 *
4041 * This declares a region of memory suitable for use as a thread's stack.
4042 *
4043 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4044 * 'noinit' section so that it isn't zeroed at boot
4045 *
Andrew Boie507852a2017-07-25 18:47:07 -07004046 * The declared symbol will always be a k_thread_stack_t which can be passed to
4047 * k_thread_create, but should otherwise not be manipulated. If the buffer
4048 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004049 *
4050 * It is legal to precede this definition with the 'static' keyword.
4051 *
4052 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4053 * parameter of k_thread_create(), it may not be the same as the
4054 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4055 *
4056 * @param sym Thread stack symbol name
4057 * @param size Size of the stack memory region
4058 */
4059#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004060 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004061
4062/**
4063 * @brief Declare a toplevel array of thread stack memory regions
4064 *
4065 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4066 * definition for additional details and constraints.
4067 *
4068 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4069 * 'noinit' section so that it isn't zeroed at boot
4070 *
4071 * @param sym Thread stack symbol name
4072 * @param nmemb Number of stacks to declare
4073 * @param size Size of the stack memory region
4074 */
4075
4076#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004077 struct _k_thread_stack_element __noinit \
4078 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004079
4080/**
4081 * @brief Declare an embedded stack memory region
4082 *
4083 * Used for stacks embedded within other data structures. Use is highly
4084 * discouraged but in some cases necessary. For memory protection scenarios,
4085 * it is very important that any RAM preceding this member not be writable
4086 * by threads else a stack overflow will lead to silent corruption. In other
4087 * words, the containing data structure should live in RAM owned by the kernel.
4088 *
4089 * @param sym Thread stack symbol name
4090 * @param size Size of the stack memory region
4091 */
4092#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004093 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004094
4095/**
4096 * @brief Return the size in bytes of a stack memory region
4097 *
4098 * Convenience macro for passing the desired stack size to k_thread_create()
4099 * since the underlying implementation may actually create something larger
4100 * (for instance a guard area).
4101 *
4102 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004103 * passed to K_THREAD_STACK_DEFINE.
4104 *
4105 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4106 * it is not guaranteed to return the original value since each array
4107 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004108 *
4109 * @param sym Stack memory symbol
4110 * @return Size of the stack
4111 */
4112#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4113
4114/**
4115 * @brief Get a pointer to the physical stack buffer
4116 *
4117 * Convenience macro to get at the real underlying stack buffer used by
4118 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4119 * This is really only intended for diagnostic tools which want to examine
4120 * stack memory contents.
4121 *
4122 * @param sym Declared stack symbol name
4123 * @return The buffer itself, a char *
4124 */
Andrew Boie507852a2017-07-25 18:47:07 -07004125static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4126{
4127 return (char *)sym;
4128}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004129
4130#endif /* _ARCH_DECLARE_STACK */
4131
Chunlin Hane9c97022017-07-07 20:29:30 +08004132/**
4133 * @defgroup mem_domain_apis Memory domain APIs
4134 * @ingroup kernel_apis
4135 * @{
4136 */
4137
4138/** @def MEM_PARTITION_ENTRY
4139 * @brief Used to declare a memory partition entry
4140 */
4141#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4142 {\
4143 .start = _start, \
4144 .size = _size, \
4145 .attr = _attr, \
4146 }
4147
4148/** @def K_MEM_PARTITION_DEFINE
4149 * @brief Used to declare a memory partition
4150 */
4151#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4152#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4153 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
4154 struct k_mem_partition name = \
4155 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4156#else
4157#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4158 struct k_mem_partition name = \
4159 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4160#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4161
4162
4163/* memory partition */
4164struct k_mem_partition {
4165 /* start address of memory partition */
4166 u32_t start;
4167 /* size of memory partition */
4168 u32_t size;
4169 /* attribute of memory partition */
4170 u32_t attr;
4171};
4172
4173#if defined(CONFIG_USERSPACE)
4174/* memory domian */
4175struct k_mem_domain {
4176 /* number of partitions in the domain */
4177 u32_t num_partitions;
4178 /* partitions in the domain */
4179 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
4180 /* domain q */
4181 sys_dlist_t mem_domain_q;
4182};
4183#endif /* CONFIG_USERSPACE */
4184
4185/**
4186 * @brief Initialize a memory domain.
4187 *
4188 * Initialize a memory domain with given name and memory partitions.
4189 *
4190 * @param domain The memory domain to be initialized.
4191 * @param num_parts The number of array items of "parts" parameter.
4192 * @param parts An array of pointers to the memory partitions. Can be NULL
4193 * if num_parts is zero.
4194 */
4195
4196extern void k_mem_domain_init(struct k_mem_domain *domain, u32_t num_parts,
4197 struct k_mem_partition *parts[]);
4198/**
4199 * @brief Destroy a memory domain.
4200 *
4201 * Destroy a memory domain.
4202 *
4203 * @param domain The memory domain to be destroyed.
4204 */
4205
4206extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4207
4208/**
4209 * @brief Add a memory partition into a memory domain.
4210 *
4211 * Add a memory partition into a memory domain.
4212 *
4213 * @param domain The memory domain to be added a memory partition.
4214 * @param part The memory partition to be added
4215 */
4216
4217extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4218 struct k_mem_partition *part);
4219
4220/**
4221 * @brief Remove a memory partition from a memory domain.
4222 *
4223 * Remove a memory partition from a memory domain.
4224 *
4225 * @param domain The memory domain to be removed a memory partition.
4226 * @param part The memory partition to be removed
4227 */
4228
4229extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4230 struct k_mem_partition *part);
4231
4232/**
4233 * @brief Add a thread into a memory domain.
4234 *
4235 * Add a thread into a memory domain.
4236 *
4237 * @param domain The memory domain that the thread is going to be added into.
4238 * @param thread ID of thread going to be added into the memory domain.
4239 */
4240
4241extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4242 k_tid_t thread);
4243
4244/**
4245 * @brief Remove a thread from its memory domain.
4246 *
4247 * Remove a thread from its memory domain.
4248 *
4249 * @param thread ID of thread going to be removed from its memory domain.
4250 */
4251
4252extern void k_mem_domain_remove_thread(k_tid_t thread);
4253
4254/**
4255 * @} end defgroup mem_domain_apis
4256 */
4257
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004258#ifdef __cplusplus
4259}
4260#endif
4261
Andrew Boiee004dec2016-11-07 09:01:19 -08004262#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4263/*
4264 * Define new and delete operators.
4265 * At this moment, the operators do nothing since objects are supposed
4266 * to be statically allocated.
4267 */
4268inline void operator delete(void *ptr)
4269{
4270 (void)ptr;
4271}
4272
4273inline void operator delete[](void *ptr)
4274{
4275 (void)ptr;
4276}
4277
4278inline void *operator new(size_t size)
4279{
4280 (void)size;
4281 return NULL;
4282}
4283
4284inline void *operator new[](size_t size)
4285{
4286 (void)size;
4287 return NULL;
4288}
4289
4290/* Placement versions of operator new and delete */
4291inline void operator delete(void *ptr1, void *ptr2)
4292{
4293 (void)ptr1;
4294 (void)ptr2;
4295}
4296
4297inline void operator delete[](void *ptr1, void *ptr2)
4298{
4299 (void)ptr1;
4300 (void)ptr2;
4301}
4302
4303inline void *operator new(size_t size, void *ptr)
4304{
4305 (void)size;
4306 return ptr;
4307}
4308
4309inline void *operator new[](size_t size, void *ptr)
4310{
4311 (void)size;
4312 return ptr;
4313}
4314
4315#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4316
Andrew Boiefa94ee72017-09-28 16:54:35 -07004317#include <syscalls/kernel.h>
4318
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004319#endif /* !_ASMLANGUAGE */
4320
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004321#endif /* _kernel__h_ */