blob: 51f07db623ab4ce7aab849f4b7c94d07c53ac09e [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);
218
219
220/**
221 * grant a thread access to a kernel object
222 *
223 * The thread will be granted access to the object if the caller is from
224 * supervisor mode, or the caller is from user mode AND has permissions
225 * on the object already.
226 *
227 * @param object Address of kernel object
228 * @param thread Thread to grant access to the object
229 */
Andrew Boie217017c2017-10-04 11:49:10 -0700230void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700231
Andrew Boie3b5ae802017-10-04 12:10:32 -0700232
233/**
234 * grant all present and future threads access to an object
235 *
236 * If the caller is from supervisor mode, or the caller is from user mode and
237 * have sufficient permissions on the object, then that object will have
238 * permissions granted to it for *all* current and future threads running in
239 * the system, effectively becoming a public kernel object.
240 *
241 * Use of this API should be avoided on systems that are running untrusted code
242 * as it is possible for such code to derive the addresses of kernel objects
243 * and perform unwanted operations on them.
244 *
245 * @param object Address of kernel object
246 */
247void k_object_access_all_grant(void *object);
248
Andrew Boie945af952017-08-22 13:15:23 -0700249#else
250static inline int _k_object_validate(void *obj, enum k_objects otype, int init)
251{
252 ARG_UNUSED(obj);
253 ARG_UNUSED(otype);
254 ARG_UNUSED(init);
255
256 return 0;
257}
258
259static inline void _k_object_init(void *obj)
260{
261 ARG_UNUSED(obj);
262}
263
Andrew Boie217017c2017-10-04 11:49:10 -0700264static inline void k_object_access_grant(void *object, struct k_thread *thread)
Andrew Boie945af952017-08-22 13:15:23 -0700265{
266 ARG_UNUSED(object);
267 ARG_UNUSED(thread);
268}
269#endif /* CONFIG_USERSPACE */
270
Andrew Boie73abd322017-04-04 13:19:13 -0700271/* timeouts */
272
273struct _timeout;
274typedef void (*_timeout_func_t)(struct _timeout *t);
275
276struct _timeout {
277 sys_dnode_t node;
278 struct k_thread *thread;
279 sys_dlist_t *wait_q;
280 s32_t delta_ticks_from_prev;
281 _timeout_func_t func;
282};
283
284extern s32_t _timeout_remaining_get(struct _timeout *timeout);
285
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700286/**
287 * @typedef k_thread_entry_t
288 * @brief Thread entry point function type.
289 *
290 * A thread's entry point function is invoked when the thread starts executing.
291 * Up to 3 argument values can be passed to the function.
292 *
293 * The thread terminates execution permanently if the entry point function
294 * returns. The thread is responsible for releasing any shared resources
295 * it may own (such as mutexes and dynamically allocated memory), prior to
296 * returning.
297 *
298 * @param p1 First argument.
299 * @param p2 Second argument.
300 * @param p3 Third argument.
301 *
302 * @return N/A
303 */
304typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700305
306#ifdef CONFIG_THREAD_MONITOR
307struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700308 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700309 void *parameter1;
310 void *parameter2;
311 void *parameter3;
312};
313#endif
314
315/* can be used for creating 'dummy' threads, e.g. for pending on objects */
316struct _thread_base {
317
318 /* this thread's entry in a ready/wait queue */
319 sys_dnode_t k_q_node;
320
321 /* user facing 'thread options'; values defined in include/kernel.h */
322 u8_t user_options;
323
324 /* thread state */
325 u8_t thread_state;
326
327 /*
328 * scheduler lock count and thread priority
329 *
330 * These two fields control the preemptibility of a thread.
331 *
332 * When the scheduler is locked, sched_locked is decremented, which
333 * means that the scheduler is locked for values from 0xff to 0x01. A
334 * thread is coop if its prio is negative, thus 0x80 to 0xff when
335 * looked at the value as unsigned.
336 *
337 * By putting them end-to-end, this means that a thread is
338 * non-preemptible if the bundled value is greater than or equal to
339 * 0x0080.
340 */
341 union {
342 struct {
343#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
344 u8_t sched_locked;
345 s8_t prio;
346#else /* LITTLE and PDP */
347 s8_t prio;
348 u8_t sched_locked;
349#endif
350 };
351 u16_t preempt;
352 };
353
354 /* data returned by APIs */
355 void *swap_data;
356
357#ifdef CONFIG_SYS_CLOCK_EXISTS
358 /* this thread's entry in a timeout queue */
359 struct _timeout timeout;
360#endif
Andrew Boie2acfcd62017-08-30 14:31:03 -0700361
362#ifdef CONFIG_USERSPACE
363 /* Bit position in kernel object permissions bitfield for this thread */
364 unsigned int perm_index;
365#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700366};
367
368typedef struct _thread_base _thread_base_t;
369
370#if defined(CONFIG_THREAD_STACK_INFO)
371/* Contains the stack information of a thread */
372struct _thread_stack_info {
373 /* Stack Start */
374 u32_t start;
375 /* Stack Size */
376 u32_t size;
377};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700378
379typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700380#endif /* CONFIG_THREAD_STACK_INFO */
381
Chunlin Hane9c97022017-07-07 20:29:30 +0800382#if defined(CONFIG_USERSPACE)
383struct _mem_domain_info {
384 /* memory domain queue node */
385 sys_dnode_t mem_domain_q_node;
386 /* memory domain of the thread */
387 struct k_mem_domain *mem_domain;
388};
389
390#endif /* CONFIG_USERSPACE */
391
Andrew Boie73abd322017-04-04 13:19:13 -0700392struct k_thread {
393
394 struct _thread_base base;
395
396 /* defined by the architecture, but all archs need these */
397 struct _caller_saved caller_saved;
398 struct _callee_saved callee_saved;
399
400 /* static thread init data */
401 void *init_data;
402
403 /* abort function */
404 void (*fn_abort)(void);
405
406#if defined(CONFIG_THREAD_MONITOR)
407 /* thread entry and parameters description */
408 struct __thread_entry *entry;
409
410 /* next item in list of all threads */
411 struct k_thread *next_thread;
412#endif
413
414#ifdef CONFIG_THREAD_CUSTOM_DATA
415 /* crude thread-local storage */
416 void *custom_data;
417#endif
418
419#ifdef CONFIG_ERRNO
420 /* per-thread errno variable */
421 int errno_var;
422#endif
423
424#if defined(CONFIG_THREAD_STACK_INFO)
425 /* Stack Info */
426 struct _thread_stack_info stack_info;
427#endif /* CONFIG_THREAD_STACK_INFO */
428
Chunlin Hane9c97022017-07-07 20:29:30 +0800429#if defined(CONFIG_USERSPACE)
430 /* memory domain info of the thread */
431 struct _mem_domain_info mem_domain_info;
432#endif /* CONFIG_USERSPACE */
433
Andrew Boie73abd322017-04-04 13:19:13 -0700434 /* arch-specifics: must always be at the end */
435 struct _thread_arch arch;
436};
437
438typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400439typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700440#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400441
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400442enum execution_context_types {
443 K_ISR = 0,
444 K_COOP_THREAD,
445 K_PREEMPT_THREAD,
446};
447
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400448/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100449 * @defgroup profiling_apis Profiling APIs
450 * @ingroup kernel_apis
451 * @{
452 */
453
454/**
455 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
456 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700457 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100458 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
459 *
460 * CONFIG_MAIN_STACK_SIZE
461 * CONFIG_IDLE_STACK_SIZE
462 * CONFIG_ISR_STACK_SIZE
463 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
464 *
465 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
466 * produce output.
467 *
468 * @return N/A
469 */
470extern void k_call_stacks_analyze(void);
471
472/**
473 * @} end defgroup profiling_apis
474 */
475
476/**
Allan Stephensc98da842016-11-11 15:45:03 -0500477 * @defgroup thread_apis Thread APIs
478 * @ingroup kernel_apis
479 * @{
480 */
481
Benjamin Walshed240f22017-01-22 13:05:08 -0500482#endif /* !_ASMLANGUAGE */
483
484
485/*
486 * Thread user options. May be needed by assembly code. Common part uses low
487 * bits, arch-specific use high bits.
488 */
489
490/* system thread that must not abort */
491#define K_ESSENTIAL (1 << 0)
492
493#if defined(CONFIG_FP_SHARING)
494/* thread uses floating point registers */
495#define K_FP_REGS (1 << 1)
496#endif
497
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700498/* This thread has dropped from supervisor mode to user mode and consequently
499 * has additional restrictions
500 */
501#define K_USER (1 << 2)
502
Benjamin Walshed240f22017-01-22 13:05:08 -0500503#ifdef CONFIG_X86
504/* x86 Bitmask definitions for threads user options */
505
506#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
507/* thread uses SSEx (and also FP) registers */
508#define K_SSE_REGS (1 << 7)
509#endif
510#endif
511
512/* end - thread options */
513
514#if !defined(_ASMLANGUAGE)
515
Andrew Boie507852a2017-07-25 18:47:07 -0700516/* Using typedef deliberately here, this is quite intended to be an opaque
517 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
518 *
519 * The purpose of this data type is to clearly distinguish between the
520 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
521 * buffer which composes the stack data actually used by the underlying
522 * thread; they cannot be used interchangably as some arches precede the
523 * stack buffer region with guard areas that trigger a MPU or MMU fault
524 * if written to.
525 *
526 * APIs that want to work with the buffer inside should continue to use
527 * char *.
528 *
529 * Stacks should always be created with K_THREAD_STACK_DEFINE().
530 */
531struct __packed _k_thread_stack_element {
532 char data;
533};
534typedef struct _k_thread_stack_element *k_thread_stack_t;
535
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400536
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400537/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700538 * @brief Create a thread.
539 *
540 * This routine initializes a thread, then schedules it for execution.
541 *
542 * The new thread may be scheduled for immediate execution or a delayed start.
543 * If the newly spawned thread does not have a delayed start the kernel
544 * scheduler may preempt the current thread to allow the new thread to
545 * execute.
546 *
547 * Thread options are architecture-specific, and can include K_ESSENTIAL,
548 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
549 * them using "|" (the logical OR operator).
550 *
551 * Historically, users often would use the beginning of the stack memory region
552 * to store the struct k_thread data, although corruption will occur if the
553 * stack overflows this region and stack protection features may not detect this
554 * situation.
555 *
556 * @param new_thread Pointer to uninitialized struct k_thread
557 * @param stack Pointer to the stack space.
558 * @param stack_size Stack size in bytes.
559 * @param entry Thread entry function.
560 * @param p1 1st entry point parameter.
561 * @param p2 2nd entry point parameter.
562 * @param p3 3rd entry point parameter.
563 * @param prio Thread priority.
564 * @param options Thread options.
565 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
566 *
567 * @return ID of new thread.
568 */
Andrew Boie507852a2017-07-25 18:47:07 -0700569extern k_tid_t k_thread_create(struct k_thread *new_thread,
570 k_thread_stack_t stack,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700571 size_t stack_size,
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700572 k_thread_entry_t entry,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700573 void *p1, void *p2, void *p3,
574 int prio, u32_t options, s32_t delay);
575
Andrew Boie3f091b52017-08-30 14:34:14 -0700576/**
577 * @brief Drop a thread's privileges permanently to user mode
578 *
579 * @param entry Function to start executing from
580 * @param p1 1st entry point parameter
581 * @param p2 2nd entry point parameter
582 * @param p3 3rd entry point parameter
583 */
584extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
585 void *p1, void *p2,
586 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700587
Andrew Boied26cf2d2017-03-30 13:07:02 -0700588/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500589 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400590 *
Allan Stephensc98da842016-11-11 15:45:03 -0500591 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500592 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400593 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500594 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400595 *
596 * @return N/A
597 */
Kumar Galacc334c72017-04-21 10:55:34 -0500598extern void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400599
600/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500601 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400602 *
603 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500604 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400605 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400606 * @return N/A
607 */
Kumar Galacc334c72017-04-21 10:55:34 -0500608extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400609
610/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500611 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400612 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500613 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400614 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500615 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400616 *
617 * @return N/A
618 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400619extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400620
621/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500622 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400623 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500624 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500626 * If @a thread is not currently sleeping, the routine has no effect.
627 *
628 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400629 *
630 * @return N/A
631 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400632extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400633
634/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500635 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400636 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500637 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400638 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400639extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400640
641/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500642 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400643 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500644 * This routine prevents @a thread from executing if it has not yet started
645 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400646 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500647 * @param thread ID of thread to cancel.
648 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700649 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500650 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400651 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400652extern int k_thread_cancel(k_tid_t thread);
653
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400654/**
Allan Stephensc98da842016-11-11 15:45:03 -0500655 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400656 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500657 * This routine permanently stops execution of @a thread. The thread is taken
658 * off all kernel queues it is part of (i.e. the ready queue, the timeout
659 * queue, or a kernel object wait queue). However, any kernel resources the
660 * thread might currently own (such as mutexes or memory blocks) are not
661 * released. It is the responsibility of the caller of this routine to ensure
662 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400663 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500664 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400665 *
666 * @return N/A
667 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400668extern void k_thread_abort(k_tid_t thread);
669
Andrew Boie7d627c52017-08-30 11:01:56 -0700670
671/**
672 * @brief Start an inactive thread
673 *
674 * If a thread was created with K_FOREVER in the delay parameter, it will
675 * not be added to the scheduling queue until this function is called
676 * on it.
677 *
678 * @param thread thread to start
679 */
680extern void k_thread_start(k_tid_t thread);
681
Allan Stephensc98da842016-11-11 15:45:03 -0500682/**
683 * @cond INTERNAL_HIDDEN
684 */
685
Benjamin Walshd211a522016-12-06 11:44:01 -0500686/* timeout has timed out and is not on _timeout_q anymore */
687#define _EXPIRED (-2)
688
689/* timeout is not in use */
690#define _INACTIVE (-1)
691
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400692struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700693 struct k_thread *init_thread;
Andrew Boie507852a2017-07-25 18:47:07 -0700694 k_thread_stack_t init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400695 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700696 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500697 void *init_p1;
698 void *init_p2;
699 void *init_p3;
700 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500701 u32_t init_options;
702 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500703 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500704 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400705};
706
Andrew Boied26cf2d2017-03-30 13:07:02 -0700707#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400708 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500709 prio, options, delay, abort, groups) \
710 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700711 .init_thread = (thread), \
712 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500713 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700714 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400715 .init_p1 = (void *)p1, \
716 .init_p2 = (void *)p2, \
717 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500718 .init_prio = (prio), \
719 .init_options = (options), \
720 .init_delay = (delay), \
721 .init_abort = (abort), \
722 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400723 }
724
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400725/**
Allan Stephensc98da842016-11-11 15:45:03 -0500726 * INTERNAL_HIDDEN @endcond
727 */
728
729/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500730 * @brief Statically define and initialize a thread.
731 *
732 * The thread may be scheduled for immediate execution or a delayed start.
733 *
734 * Thread options are architecture-specific, and can include K_ESSENTIAL,
735 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
736 * them using "|" (the logical OR operator).
737 *
738 * The ID of the thread can be accessed using:
739 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500740 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500741 *
742 * @param name Name of the thread.
743 * @param stack_size Stack size in bytes.
744 * @param entry Thread entry function.
745 * @param p1 1st entry point parameter.
746 * @param p2 2nd entry point parameter.
747 * @param p3 3rd entry point parameter.
748 * @param prio Thread priority.
749 * @param options Thread options.
750 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400751 *
752 * @internal It has been observed that the x86 compiler by default aligns
753 * these _static_thread_data structures to 32-byte boundaries, thereby
754 * wasting space. To work around this, force a 4-byte alignment.
755 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500756#define K_THREAD_DEFINE(name, stack_size, \
757 entry, p1, p2, p3, \
758 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700759 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700760 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500761 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500762 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700763 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
764 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500765 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700766 NULL, 0); \
767 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400768
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400769/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500770 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400771 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500772 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400773 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500774 * @param thread ID of thread whose priority is needed.
775 *
776 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400777 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500778extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400779
780/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500781 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400782 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500783 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400784 *
785 * Rescheduling can occur immediately depending on the priority @a thread is
786 * set to:
787 *
788 * - If its priority is raised above the priority of the caller of this
789 * function, and the caller is preemptible, @a thread will be scheduled in.
790 *
791 * - If the caller operates on itself, it lowers its priority below that of
792 * other threads in the system, and the caller is preemptible, the thread of
793 * highest priority will be scheduled in.
794 *
795 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
796 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
797 * highest priority.
798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500799 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400800 * @param prio New priority.
801 *
802 * @warning Changing the priority of a thread currently involved in mutex
803 * priority inheritance may result in undefined behavior.
804 *
805 * @return N/A
806 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400807extern void k_thread_priority_set(k_tid_t thread, int prio);
808
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400809/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500810 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400811 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500812 * This routine prevents the kernel scheduler from making @a thread the
813 * current thread. All other internal operations on @a thread are still
814 * performed; for example, any timeout it is waiting on keeps ticking,
815 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500817 * If @a thread is already suspended, the routine has no effect.
818 *
819 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400820 *
821 * @return N/A
822 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400823extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400824
825/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500826 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400827 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500828 * This routine allows the kernel scheduler to make @a thread the current
829 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400830 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500831 * If @a thread is not currently suspended, the routine has no effect.
832 *
833 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400834 *
835 * @return N/A
836 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400837extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400838
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400839/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500840 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400841 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500842 * This routine specifies how the scheduler will perform time slicing of
843 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500845 * To enable time slicing, @a slice must be non-zero. The scheduler
846 * ensures that no thread runs for more than the specified time limit
847 * before other threads of that priority are given a chance to execute.
848 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700849 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400850 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500851 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400852 * execute. Once the scheduler selects a thread for execution, there is no
853 * minimum guaranteed time the thread will execute before threads of greater or
854 * equal priority are scheduled.
855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500856 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400857 * for execution, this routine has no effect; the thread is immediately
858 * rescheduled after the slice period expires.
859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500860 * To disable timeslicing, set both @a slice and @a prio to zero.
861 *
862 * @param slice Maximum time slice length (in milliseconds).
863 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400864 *
865 * @return N/A
866 */
Kumar Galacc334c72017-04-21 10:55:34 -0500867extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400868
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400869/**
Allan Stephensc98da842016-11-11 15:45:03 -0500870 * @} end defgroup thread_apis
871 */
872
873/**
874 * @addtogroup isr_apis
875 * @{
876 */
877
878/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500879 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400880 *
Allan Stephensc98da842016-11-11 15:45:03 -0500881 * This routine allows the caller to customize its actions, depending on
882 * whether it is a thread or an ISR.
883 *
884 * @note Can be called by ISRs.
885 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500886 * @return 0 if invoked by a thread.
887 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400888 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500889extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400890
Benjamin Walsh445830d2016-11-10 15:54:27 -0500891/**
892 * @brief Determine if code is running in a preemptible thread.
893 *
Allan Stephensc98da842016-11-11 15:45:03 -0500894 * This routine allows the caller to customize its actions, depending on
895 * whether it can be preempted by another thread. The routine returns a 'true'
896 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500897 *
Allan Stephensc98da842016-11-11 15:45:03 -0500898 * - The code is running in a thread, not at ISR.
899 * - The thread's priority is in the preemptible range.
900 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500901 *
Allan Stephensc98da842016-11-11 15:45:03 -0500902 * @note Can be called by ISRs.
903 *
904 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500905 * @return Non-zero if invoked by a preemptible thread.
906 */
907extern int k_is_preempt_thread(void);
908
Allan Stephensc98da842016-11-11 15:45:03 -0500909/**
910 * @} end addtogroup isr_apis
911 */
912
913/**
914 * @addtogroup thread_apis
915 * @{
916 */
917
918/**
919 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500920 *
Allan Stephensc98da842016-11-11 15:45:03 -0500921 * This routine prevents the current thread from being preempted by another
922 * thread by instructing the scheduler to treat it as a cooperative thread.
923 * If the thread subsequently performs an operation that makes it unready,
924 * it will be context switched out in the normal manner. When the thread
925 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500926 *
Allan Stephensc98da842016-11-11 15:45:03 -0500927 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500928 *
Allan Stephensc98da842016-11-11 15:45:03 -0500929 * @note k_sched_lock() and k_sched_unlock() should normally be used
930 * when the operation being performed can be safely interrupted by ISRs.
931 * However, if the amount of processing involved is very small, better
932 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500933 *
934 * @return N/A
935 */
936extern void k_sched_lock(void);
937
Allan Stephensc98da842016-11-11 15:45:03 -0500938/**
939 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500940 *
Allan Stephensc98da842016-11-11 15:45:03 -0500941 * This routine reverses the effect of a previous call to k_sched_lock().
942 * A thread must call the routine once for each time it called k_sched_lock()
943 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500944 *
945 * @return N/A
946 */
947extern void k_sched_unlock(void);
948
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400949/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500950 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400951 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500952 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400953 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500954 * Custom data is not used by the kernel itself, and is freely available
955 * for a thread to use as it sees fit. It can be used as a framework
956 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400957 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500958 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400959 *
960 * @return N/A
961 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400962extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400963
964/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500965 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400966 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500967 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400968 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500969 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400970 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400971extern void *k_thread_custom_data_get(void);
972
973/**
Allan Stephensc98da842016-11-11 15:45:03 -0500974 * @} end addtogroup thread_apis
975 */
976
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400977#include <sys_clock.h>
978
Allan Stephensc2f15a42016-11-17 12:24:22 -0500979/**
980 * @addtogroup clock_apis
981 * @{
982 */
983
984/**
985 * @brief Generate null timeout delay.
986 *
987 * This macro generates a timeout delay that that instructs a kernel API
988 * not to wait if the requested operation cannot be performed immediately.
989 *
990 * @return Timeout delay value.
991 */
992#define K_NO_WAIT 0
993
994/**
995 * @brief Generate timeout delay from milliseconds.
996 *
997 * This macro generates a timeout delay that that instructs a kernel API
998 * to wait up to @a ms milliseconds to perform the requested operation.
999 *
1000 * @param ms Duration in milliseconds.
1001 *
1002 * @return Timeout delay value.
1003 */
Johan Hedberg14471692016-11-13 10:52:15 +02001004#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001005
1006/**
1007 * @brief Generate timeout delay from seconds.
1008 *
1009 * This macro generates a timeout delay that that instructs a kernel API
1010 * to wait up to @a s seconds to perform the requested operation.
1011 *
1012 * @param s Duration in seconds.
1013 *
1014 * @return Timeout delay value.
1015 */
Johan Hedberg14471692016-11-13 10:52:15 +02001016#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001017
1018/**
1019 * @brief Generate timeout delay from minutes.
1020 *
1021 * This macro generates a timeout delay that that instructs a kernel API
1022 * to wait up to @a m minutes to perform the requested operation.
1023 *
1024 * @param m Duration in minutes.
1025 *
1026 * @return Timeout delay value.
1027 */
Johan Hedberg14471692016-11-13 10:52:15 +02001028#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001029
1030/**
1031 * @brief Generate timeout delay from hours.
1032 *
1033 * This macro generates a timeout delay that that instructs a kernel API
1034 * to wait up to @a h hours to perform the requested operation.
1035 *
1036 * @param h Duration in hours.
1037 *
1038 * @return Timeout delay value.
1039 */
Johan Hedberg14471692016-11-13 10:52:15 +02001040#define K_HOURS(h) K_MINUTES((h) * 60)
1041
Allan Stephensc98da842016-11-11 15:45:03 -05001042/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001043 * @brief Generate infinite timeout delay.
1044 *
1045 * This macro generates a timeout delay that that instructs a kernel API
1046 * to wait as long as necessary to perform the requested operation.
1047 *
1048 * @return Timeout delay value.
1049 */
1050#define K_FOREVER (-1)
1051
1052/**
1053 * @} end addtogroup clock_apis
1054 */
1055
1056/**
Allan Stephensc98da842016-11-11 15:45:03 -05001057 * @cond INTERNAL_HIDDEN
1058 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001059
Benjamin Walsh62092182016-12-20 14:39:08 -05001060/* kernel clocks */
1061
1062#if (sys_clock_ticks_per_sec == 1000) || \
1063 (sys_clock_ticks_per_sec == 500) || \
1064 (sys_clock_ticks_per_sec == 250) || \
1065 (sys_clock_ticks_per_sec == 125) || \
1066 (sys_clock_ticks_per_sec == 100) || \
1067 (sys_clock_ticks_per_sec == 50) || \
1068 (sys_clock_ticks_per_sec == 25) || \
1069 (sys_clock_ticks_per_sec == 20) || \
1070 (sys_clock_ticks_per_sec == 10) || \
1071 (sys_clock_ticks_per_sec == 1)
1072
1073 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1074#else
1075 /* yields horrible 64-bit math on many architectures: try to avoid */
1076 #define _NON_OPTIMIZED_TICKS_PER_SEC
1077#endif
1078
1079#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001080extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001081#else
Kumar Galacc334c72017-04-21 10:55:34 -05001082static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001083{
Kumar Galacc334c72017-04-21 10:55:34 -05001084 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001085}
1086#endif
1087
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001088/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001089#ifdef CONFIG_TICKLESS_KERNEL
1090#define _TICK_ALIGN 0
1091#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001092#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001093#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001094
Kumar Galacc334c72017-04-21 10:55:34 -05001095static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001096{
Benjamin Walsh62092182016-12-20 14:39:08 -05001097#ifdef CONFIG_SYS_CLOCK_EXISTS
1098
1099#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001100 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001101#else
Kumar Galacc334c72017-04-21 10:55:34 -05001102 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001103#endif
1104
1105#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001106 __ASSERT(ticks == 0, "");
1107 return 0;
1108#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001109}
1110
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001111struct k_timer {
1112 /*
1113 * _timeout structure must be first here if we want to use
1114 * dynamic timer allocation. timeout.node is used in the double-linked
1115 * list of free timers
1116 */
1117 struct _timeout timeout;
1118
Allan Stephens45bfa372016-10-12 12:39:42 -05001119 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001120 _wait_q_t wait_q;
1121
1122 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001123 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001124
1125 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001126 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001127
1128 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001129 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001130
Allan Stephens45bfa372016-10-12 12:39:42 -05001131 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001132 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001133
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001134 /* user-specific data, also used to support legacy features */
1135 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001136
Anas Nashif2f203c22016-12-18 06:57:45 -05001137 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001138};
1139
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001140#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001141 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001142 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001143 .timeout.wait_q = NULL, \
1144 .timeout.thread = NULL, \
1145 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001146 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001147 .expiry_fn = expiry, \
1148 .stop_fn = stop, \
1149 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001150 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001151 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001152 }
1153
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001154#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1155
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001156/**
Allan Stephensc98da842016-11-11 15:45:03 -05001157 * INTERNAL_HIDDEN @endcond
1158 */
1159
1160/**
1161 * @defgroup timer_apis Timer APIs
1162 * @ingroup kernel_apis
1163 * @{
1164 */
1165
1166/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001167 * @typedef k_timer_expiry_t
1168 * @brief Timer expiry function type.
1169 *
1170 * A timer's expiry function is executed by the system clock interrupt handler
1171 * each time the timer expires. The expiry function is optional, and is only
1172 * invoked if the timer has been initialized with one.
1173 *
1174 * @param timer Address of timer.
1175 *
1176 * @return N/A
1177 */
1178typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1179
1180/**
1181 * @typedef k_timer_stop_t
1182 * @brief Timer stop function type.
1183 *
1184 * A timer's stop function is executed if the timer is stopped prematurely.
1185 * The function runs in the context of the thread that stops the timer.
1186 * The stop function is optional, and is only invoked if the timer has been
1187 * initialized with one.
1188 *
1189 * @param timer Address of timer.
1190 *
1191 * @return N/A
1192 */
1193typedef void (*k_timer_stop_t)(struct k_timer *timer);
1194
1195/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001196 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001197 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001198 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001199 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001200 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001201 *
1202 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001203 * @param expiry_fn Function to invoke each time the timer expires.
1204 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001205 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001206#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001207 struct k_timer name \
1208 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001209 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001210
Allan Stephens45bfa372016-10-12 12:39:42 -05001211/**
1212 * @brief Initialize a timer.
1213 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001214 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001215 *
1216 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001217 * @param expiry_fn Function to invoke each time the timer expires.
1218 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001219 *
1220 * @return N/A
1221 */
1222extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001223 k_timer_expiry_t expiry_fn,
1224 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001225
Allan Stephens45bfa372016-10-12 12:39:42 -05001226/**
1227 * @brief Start a timer.
1228 *
1229 * This routine starts a timer, and resets its status to zero. The timer
1230 * begins counting down using the specified duration and period values.
1231 *
1232 * Attempting to start a timer that is already running is permitted.
1233 * The timer's status is reset to zero and the timer begins counting down
1234 * using the new duration and period values.
1235 *
1236 * @param timer Address of timer.
1237 * @param duration Initial timer duration (in milliseconds).
1238 * @param period Timer period (in milliseconds).
1239 *
1240 * @return N/A
1241 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001242extern void k_timer_start(struct k_timer *timer,
Kumar Galacc334c72017-04-21 10:55:34 -05001243 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001244
1245/**
1246 * @brief Stop a timer.
1247 *
1248 * This routine stops a running timer prematurely. The timer's stop function,
1249 * if one exists, is invoked by the caller.
1250 *
1251 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001252 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001253 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001254 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1255 * if @a k_timer_stop is to be called from ISRs.
1256 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001257 * @param timer Address of timer.
1258 *
1259 * @return N/A
1260 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001261extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001262
1263/**
1264 * @brief Read timer status.
1265 *
1266 * This routine reads the timer's status, which indicates the number of times
1267 * it has expired since its status was last read.
1268 *
1269 * Calling this routine resets the timer's status to zero.
1270 *
1271 * @param timer Address of timer.
1272 *
1273 * @return Timer status.
1274 */
Kumar Galacc334c72017-04-21 10:55:34 -05001275extern u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001276
1277/**
1278 * @brief Synchronize thread to timer expiration.
1279 *
1280 * This routine blocks the calling thread until the timer's status is non-zero
1281 * (indicating that it has expired at least once since it was last examined)
1282 * or the timer is stopped. If the timer status is already non-zero,
1283 * or the timer is already stopped, the caller continues without waiting.
1284 *
1285 * Calling this routine resets the timer's status to zero.
1286 *
1287 * This routine must not be used by interrupt handlers, since they are not
1288 * allowed to block.
1289 *
1290 * @param timer Address of timer.
1291 *
1292 * @return Timer status.
1293 */
Kumar Galacc334c72017-04-21 10:55:34 -05001294extern u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001295
1296/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001297 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001298 *
1299 * This routine computes the (approximate) time remaining before a running
1300 * timer next expires. If the timer is not running, it returns zero.
1301 *
1302 * @param timer Address of timer.
1303 *
1304 * @return Remaining time (in milliseconds).
1305 */
Kumar Galacc334c72017-04-21 10:55:34 -05001306static inline s32_t k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001307{
1308 return _timeout_remaining_get(&timer->timeout);
1309}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001310
Allan Stephensc98da842016-11-11 15:45:03 -05001311/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001312 * @brief Associate user-specific data with a timer.
1313 *
1314 * This routine records the @a user_data with the @a timer, to be retrieved
1315 * later.
1316 *
1317 * It can be used e.g. in a timer handler shared across multiple subsystems to
1318 * retrieve data specific to the subsystem this timer is associated with.
1319 *
1320 * @param timer Address of timer.
1321 * @param user_data User data to associate with the timer.
1322 *
1323 * @return N/A
1324 */
1325static inline void k_timer_user_data_set(struct k_timer *timer,
1326 void *user_data)
1327{
1328 timer->user_data = user_data;
1329}
1330
1331/**
1332 * @brief Retrieve the user-specific data from a timer.
1333 *
1334 * @param timer Address of timer.
1335 *
1336 * @return The user data.
1337 */
1338static inline void *k_timer_user_data_get(struct k_timer *timer)
1339{
1340 return timer->user_data;
1341}
1342
1343/**
Allan Stephensc98da842016-11-11 15:45:03 -05001344 * @} end defgroup timer_apis
1345 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001346
Allan Stephensc98da842016-11-11 15:45:03 -05001347/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001348 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001349 * @{
1350 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001351
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001352/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001353 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001355 * This routine returns the elapsed time since the system booted,
1356 * in milliseconds.
1357 *
1358 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001359 */
Kumar Galacc334c72017-04-21 10:55:34 -05001360extern s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001361
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001362#ifdef CONFIG_TICKLESS_KERNEL
1363/**
1364 * @brief Enable clock always on in tickless kernel
1365 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001366 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001367 * there are no timer events programmed in tickless kernel
1368 * scheduling. This is necessary if the clock is used to track
1369 * passage of time.
1370 *
1371 * @retval prev_status Previous status of always on flag
1372 */
1373static inline int k_enable_sys_clock_always_on(void)
1374{
1375 int prev_status = _sys_clock_always_on;
1376
1377 _sys_clock_always_on = 1;
1378 _enable_sys_clock();
1379
1380 return prev_status;
1381}
1382
1383/**
1384 * @brief Disable clock always on in tickless kernel
1385 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001386 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001387 * there are no timer events programmed in tickless kernel
1388 * scheduling. To save power, this routine should be called
1389 * immediately when clock is not used to track time.
1390 */
1391static inline void k_disable_sys_clock_always_on(void)
1392{
1393 _sys_clock_always_on = 0;
1394}
1395#else
1396#define k_enable_sys_clock_always_on() do { } while ((0))
1397#define k_disable_sys_clock_always_on() do { } while ((0))
1398#endif
1399
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001400/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001401 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001402 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001403 * This routine returns the lower 32-bits of the elapsed time since the system
1404 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001406 * This routine can be more efficient than k_uptime_get(), as it reduces the
1407 * need for interrupt locking and 64-bit math. However, the 32-bit result
1408 * cannot hold a system uptime time larger than approximately 50 days, so the
1409 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001410 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001411 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001412 */
Kumar Galacc334c72017-04-21 10:55:34 -05001413extern u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001414
1415/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001416 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001418 * This routine computes the elapsed time between the current system uptime
1419 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001420 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001421 * @param reftime Pointer to a reference time, which is updated to the current
1422 * uptime upon return.
1423 *
1424 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001425 */
Kumar Galacc334c72017-04-21 10:55:34 -05001426extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001427
1428/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001429 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001430 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001431 * This routine computes the elapsed time between the current system uptime
1432 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001433 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001434 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1435 * need for interrupt locking and 64-bit math. However, the 32-bit result
1436 * cannot hold an elapsed time larger than approximately 50 days, so the
1437 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001438 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001439 * @param reftime Pointer to a reference time, which is updated to the current
1440 * uptime upon return.
1441 *
1442 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001443 */
Kumar Galacc334c72017-04-21 10:55:34 -05001444extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001445
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001446/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001447 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001448 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001449 * This routine returns the current time, as measured by the system's hardware
1450 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001451 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001452 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001453 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001454#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001455
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001456/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001457 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001458 */
1459
Allan Stephensc98da842016-11-11 15:45:03 -05001460/**
1461 * @cond INTERNAL_HIDDEN
1462 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001463
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001464struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001465 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001466 union {
1467 _wait_q_t wait_q;
1468
1469 _POLL_EVENT;
1470 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001471
1472 _OBJECT_TRACING_NEXT_PTR(k_queue);
1473};
1474
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001475#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001476 { \
1477 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1478 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001479 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001480 _OBJECT_TRACING_INIT \
1481 }
1482
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001483#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1484
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001485/**
1486 * INTERNAL_HIDDEN @endcond
1487 */
1488
1489/**
1490 * @defgroup queue_apis Queue APIs
1491 * @ingroup kernel_apis
1492 * @{
1493 */
1494
1495/**
1496 * @brief Initialize a queue.
1497 *
1498 * This routine initializes a queue object, prior to its first use.
1499 *
1500 * @param queue Address of the queue.
1501 *
1502 * @return N/A
1503 */
1504extern void k_queue_init(struct k_queue *queue);
1505
1506/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001507 * @brief Cancel waiting on a queue.
1508 *
1509 * This routine causes first thread pending on @a queue, if any, to
1510 * return from k_queue_get() call with NULL value (as if timeout expired).
1511 *
1512 * @note Can be called by ISRs.
1513 *
1514 * @param queue Address of the queue.
1515 *
1516 * @return N/A
1517 */
1518extern void k_queue_cancel_wait(struct k_queue *queue);
1519
1520/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001521 * @brief Append an element to the end of a queue.
1522 *
1523 * This routine appends a data item to @a queue. A queue data item must be
1524 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1525 * reserved for the kernel's use.
1526 *
1527 * @note Can be called by ISRs.
1528 *
1529 * @param queue Address of the queue.
1530 * @param data Address of the data item.
1531 *
1532 * @return N/A
1533 */
1534extern void k_queue_append(struct k_queue *queue, void *data);
1535
1536/**
1537 * @brief Prepend an element to a queue.
1538 *
1539 * This routine prepends a data item to @a queue. A queue data item must be
1540 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1541 * reserved for the kernel's use.
1542 *
1543 * @note Can be called by ISRs.
1544 *
1545 * @param queue Address of the queue.
1546 * @param data Address of the data item.
1547 *
1548 * @return N/A
1549 */
1550extern void k_queue_prepend(struct k_queue *queue, void *data);
1551
1552/**
1553 * @brief Inserts an element to a queue.
1554 *
1555 * This routine inserts a data item to @a queue after previous item. A queue
1556 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1557 * item are reserved for the kernel's use.
1558 *
1559 * @note Can be called by ISRs.
1560 *
1561 * @param queue Address of the queue.
1562 * @param prev Address of the previous data item.
1563 * @param data Address of the data item.
1564 *
1565 * @return N/A
1566 */
1567extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1568
1569/**
1570 * @brief Atomically append a list of elements to a queue.
1571 *
1572 * This routine adds a list of data items to @a queue in one operation.
1573 * The data items must be in a singly-linked list, with the first 32 bits
1574 * in each data item pointing to the next data item; the list must be
1575 * NULL-terminated.
1576 *
1577 * @note Can be called by ISRs.
1578 *
1579 * @param queue Address of the queue.
1580 * @param head Pointer to first node in singly-linked list.
1581 * @param tail Pointer to last node in singly-linked list.
1582 *
1583 * @return N/A
1584 */
1585extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1586
1587/**
1588 * @brief Atomically add a list of elements to a queue.
1589 *
1590 * This routine adds a list of data items to @a queue in one operation.
1591 * The data items must be in a singly-linked list implemented using a
1592 * sys_slist_t object. Upon completion, the original list is empty.
1593 *
1594 * @note Can be called by ISRs.
1595 *
1596 * @param queue Address of the queue.
1597 * @param list Pointer to sys_slist_t object.
1598 *
1599 * @return N/A
1600 */
1601extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1602
1603/**
1604 * @brief Get an element from a queue.
1605 *
1606 * This routine removes first data item from @a queue. The first 32 bits of the
1607 * data item are reserved for the kernel's use.
1608 *
1609 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1610 *
1611 * @param queue Address of the queue.
1612 * @param timeout Waiting period to obtain a data item (in milliseconds),
1613 * or one of the special values K_NO_WAIT and K_FOREVER.
1614 *
1615 * @return Address of the data item if successful; NULL if returned
1616 * without waiting, or waiting period timed out.
1617 */
Kumar Galacc334c72017-04-21 10:55:34 -05001618extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001619
1620/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001621 * @brief Remove an element from a queue.
1622 *
1623 * This routine removes data item from @a queue. The first 32 bits of the
1624 * data item are reserved for the kernel's use. Removing elements from k_queue
1625 * rely on sys_slist_find_and_remove which is not a constant time operation.
1626 *
1627 * @note Can be called by ISRs
1628 *
1629 * @param queue Address of the queue.
1630 * @param data Address of the data item.
1631 *
1632 * @return true if data item was removed
1633 */
1634static inline bool k_queue_remove(struct k_queue *queue, void *data)
1635{
1636 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1637}
1638
1639/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001640 * @brief Query a queue to see if it has data available.
1641 *
1642 * Note that the data might be already gone by the time this function returns
1643 * if other threads are also trying to read from the queue.
1644 *
1645 * @note Can be called by ISRs.
1646 *
1647 * @param queue Address of the queue.
1648 *
1649 * @return Non-zero if the queue is empty.
1650 * @return 0 if data is available.
1651 */
1652static inline int k_queue_is_empty(struct k_queue *queue)
1653{
1654 return (int)sys_slist_is_empty(&queue->data_q);
1655}
1656
1657/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001658 * @brief Peek element at the head of queue.
1659 *
1660 * Return element from the head of queue without removing it.
1661 *
1662 * @param queue Address of the queue.
1663 *
1664 * @return Head element, or NULL if queue is empty.
1665 */
1666static inline void *k_queue_peek_head(struct k_queue *queue)
1667{
1668 return sys_slist_peek_head(&queue->data_q);
1669}
1670
1671/**
1672 * @brief Peek element at the tail of queue.
1673 *
1674 * Return element from the tail of queue without removing it.
1675 *
1676 * @param queue Address of the queue.
1677 *
1678 * @return Tail element, or NULL if queue is empty.
1679 */
1680static inline void *k_queue_peek_tail(struct k_queue *queue)
1681{
1682 return sys_slist_peek_tail(&queue->data_q);
1683}
1684
1685/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001686 * @brief Statically define and initialize a queue.
1687 *
1688 * The queue can be accessed outside the module where it is defined using:
1689 *
1690 * @code extern struct k_queue <name>; @endcode
1691 *
1692 * @param name Name of the queue.
1693 */
1694#define K_QUEUE_DEFINE(name) \
1695 struct k_queue name \
1696 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001697 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001698
1699/**
1700 * @} end defgroup queue_apis
1701 */
1702
1703/**
1704 * @cond INTERNAL_HIDDEN
1705 */
1706
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001707struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001708 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001709};
1710
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001711#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001712 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001713 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001714 }
1715
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001716#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1717
Allan Stephensc98da842016-11-11 15:45:03 -05001718/**
1719 * INTERNAL_HIDDEN @endcond
1720 */
1721
1722/**
1723 * @defgroup fifo_apis Fifo APIs
1724 * @ingroup kernel_apis
1725 * @{
1726 */
1727
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001728/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001729 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001730 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001731 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001732 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001733 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001734 *
1735 * @return N/A
1736 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001737#define k_fifo_init(fifo) \
1738 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001739
1740/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001741 * @brief Cancel waiting on a fifo.
1742 *
1743 * This routine causes first thread pending on @a fifo, if any, to
1744 * return from k_fifo_get() call with NULL value (as if timeout
1745 * expired).
1746 *
1747 * @note Can be called by ISRs.
1748 *
1749 * @param fifo Address of the fifo.
1750 *
1751 * @return N/A
1752 */
1753#define k_fifo_cancel_wait(fifo) \
1754 k_queue_cancel_wait((struct k_queue *) fifo)
1755
1756/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001757 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001758 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001759 * This routine adds a data item to @a fifo. A fifo data item must be
1760 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1761 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001762 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001763 * @note Can be called by ISRs.
1764 *
1765 * @param fifo Address of the fifo.
1766 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001767 *
1768 * @return N/A
1769 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001770#define k_fifo_put(fifo, data) \
1771 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001772
1773/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001774 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001775 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001776 * This routine adds a list of data items to @a fifo in one operation.
1777 * The data items must be in a singly-linked list, with the first 32 bits
1778 * each data item pointing to the next data item; the list must be
1779 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001780 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001781 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001782 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001783 * @param fifo Address of the fifo.
1784 * @param head Pointer to first node in singly-linked list.
1785 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001786 *
1787 * @return N/A
1788 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001789#define k_fifo_put_list(fifo, head, tail) \
1790 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001791
1792/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001793 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001794 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001795 * This routine adds a list of data items to @a fifo in one operation.
1796 * The data items must be in a singly-linked list implemented using a
1797 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001798 * and must be re-initialized via sys_slist_init().
1799 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001800 * @note Can be called by ISRs.
1801 *
1802 * @param fifo Address of the fifo.
1803 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001804 *
1805 * @return N/A
1806 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001807#define k_fifo_put_slist(fifo, list) \
1808 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001809
1810/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001811 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001812 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001813 * This routine removes a data item from @a fifo in a "first in, first out"
1814 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001815 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001816 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1817 *
1818 * @param fifo Address of the fifo.
1819 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001820 * or one of the special values K_NO_WAIT and K_FOREVER.
1821 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001822 * @return Address of the data item if successful; NULL if returned
1823 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001824 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001825#define k_fifo_get(fifo, timeout) \
1826 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001827
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001828/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001829 * @brief Query a fifo to see if it has data available.
1830 *
1831 * Note that the data might be already gone by the time this function returns
1832 * if other threads is also trying to read from the fifo.
1833 *
1834 * @note Can be called by ISRs.
1835 *
1836 * @param fifo Address of the fifo.
1837 *
1838 * @return Non-zero if the fifo is empty.
1839 * @return 0 if data is available.
1840 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001841#define k_fifo_is_empty(fifo) \
1842 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001843
1844/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001845 * @brief Peek element at the head of fifo.
1846 *
1847 * Return element from the head of fifo without removing it. A usecase
1848 * for this is if elements of the fifo are themselves containers. Then
1849 * on each iteration of processing, a head container will be peeked,
1850 * and some data processed out of it, and only if the container is empty,
1851 * it will be completely remove from the fifo.
1852 *
1853 * @param fifo Address of the fifo.
1854 *
1855 * @return Head element, or NULL if the fifo is empty.
1856 */
1857#define k_fifo_peek_head(fifo) \
1858 k_queue_peek_head((struct k_queue *) fifo)
1859
1860/**
1861 * @brief Peek element at the tail of fifo.
1862 *
1863 * Return element from the tail of fifo (without removing it). A usecase
1864 * for this is if elements of the fifo are themselves containers. Then
1865 * it may be useful to add more data to the last container in fifo.
1866 *
1867 * @param fifo Address of the fifo.
1868 *
1869 * @return Tail element, or NULL if fifo is empty.
1870 */
1871#define k_fifo_peek_tail(fifo) \
1872 k_queue_peek_tail((struct k_queue *) fifo)
1873
1874/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001875 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001876 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001877 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001878 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001879 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001881 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001882 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001883#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001884 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001885 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001886 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001887
Allan Stephensc98da842016-11-11 15:45:03 -05001888/**
1889 * @} end defgroup fifo_apis
1890 */
1891
1892/**
1893 * @cond INTERNAL_HIDDEN
1894 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001895
1896struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001897 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001898};
1899
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001900#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001901 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001902 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001903 }
1904
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001905#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1906
Allan Stephensc98da842016-11-11 15:45:03 -05001907/**
1908 * INTERNAL_HIDDEN @endcond
1909 */
1910
1911/**
1912 * @defgroup lifo_apis Lifo APIs
1913 * @ingroup kernel_apis
1914 * @{
1915 */
1916
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001917/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001918 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001919 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001920 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001921 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001922 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001923 *
1924 * @return N/A
1925 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001926#define k_lifo_init(lifo) \
1927 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001928
1929/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001930 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001931 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001932 * This routine adds a data item to @a lifo. A lifo data item must be
1933 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1934 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001935 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001936 * @note Can be called by ISRs.
1937 *
1938 * @param lifo Address of the lifo.
1939 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001940 *
1941 * @return N/A
1942 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001943#define k_lifo_put(lifo, data) \
1944 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001945
1946/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001947 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001948 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001949 * This routine removes a data item from @a lifo in a "last in, first out"
1950 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001951 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001952 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1953 *
1954 * @param lifo Address of the lifo.
1955 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001956 * or one of the special values K_NO_WAIT and K_FOREVER.
1957 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001958 * @return Address of the data item if successful; NULL if returned
1959 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001960 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001961#define k_lifo_get(lifo, timeout) \
1962 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001963
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001964/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001965 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001966 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001967 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001968 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001969 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001971 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001972 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001973#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001974 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001975 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001976 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001977
Allan Stephensc98da842016-11-11 15:45:03 -05001978/**
1979 * @} end defgroup lifo_apis
1980 */
1981
1982/**
1983 * @cond INTERNAL_HIDDEN
1984 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001985
1986struct k_stack {
1987 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001988 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001989
Anas Nashif2f203c22016-12-18 06:57:45 -05001990 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001991};
1992
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001993#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05001994 { \
1995 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1996 .base = stack_buffer, \
1997 .next = stack_buffer, \
1998 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001999 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002000 }
2001
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002002#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2003
Allan Stephensc98da842016-11-11 15:45:03 -05002004/**
2005 * INTERNAL_HIDDEN @endcond
2006 */
2007
2008/**
2009 * @defgroup stack_apis Stack APIs
2010 * @ingroup kernel_apis
2011 * @{
2012 */
2013
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002014/**
2015 * @brief Initialize a stack.
2016 *
2017 * This routine initializes a stack object, prior to its first use.
2018 *
2019 * @param stack Address of the stack.
2020 * @param buffer Address of array used to hold stacked values.
2021 * @param num_entries Maximum number of values that can be stacked.
2022 *
2023 * @return N/A
2024 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05002025extern void k_stack_init(struct k_stack *stack,
Kumar Galacc334c72017-04-21 10:55:34 -05002026 u32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002027
2028/**
2029 * @brief Push an element onto a stack.
2030 *
2031 * This routine adds a 32-bit value @a data to @a stack.
2032 *
2033 * @note Can be called by ISRs.
2034 *
2035 * @param stack Address of the stack.
2036 * @param data Value to push onto the stack.
2037 *
2038 * @return N/A
2039 */
Kumar Galacc334c72017-04-21 10:55:34 -05002040extern void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002041
2042/**
2043 * @brief Pop an element from a stack.
2044 *
2045 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2046 * manner and stores the value in @a data.
2047 *
2048 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2049 *
2050 * @param stack Address of the stack.
2051 * @param data Address of area to hold the value popped from the stack.
2052 * @param timeout Waiting period to obtain a value (in milliseconds),
2053 * or one of the special values K_NO_WAIT and K_FOREVER.
2054 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002055 * @retval 0 Element popped from stack.
2056 * @retval -EBUSY Returned without waiting.
2057 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002058 */
Kumar Galacc334c72017-04-21 10:55:34 -05002059extern int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002060
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002061/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002062 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002063 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002064 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002065 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002066 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002067 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002068 * @param name Name of the stack.
2069 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002070 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002071#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002072 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002073 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002074 struct k_stack name \
2075 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002076 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002077 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002078
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002079/**
Allan Stephensc98da842016-11-11 15:45:03 -05002080 * @} end defgroup stack_apis
2081 */
2082
Allan Stephens6bba9b02016-11-16 14:56:54 -05002083struct k_work;
2084
Allan Stephensc98da842016-11-11 15:45:03 -05002085/**
2086 * @defgroup workqueue_apis Workqueue Thread APIs
2087 * @ingroup kernel_apis
2088 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002089 */
2090
Allan Stephens6bba9b02016-11-16 14:56:54 -05002091/**
2092 * @typedef k_work_handler_t
2093 * @brief Work item handler function type.
2094 *
2095 * A work item's handler function is executed by a workqueue's thread
2096 * when the work item is processed by the workqueue.
2097 *
2098 * @param work Address of the work item.
2099 *
2100 * @return N/A
2101 */
2102typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002103
2104/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002105 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002106 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002107
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002108struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002109 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002110 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002111};
2112
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002113enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002114 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002115};
2116
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002117struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002118 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002119 k_work_handler_t handler;
2120 atomic_t flags[1];
2121};
2122
Allan Stephens6bba9b02016-11-16 14:56:54 -05002123struct k_delayed_work {
2124 struct k_work work;
2125 struct _timeout timeout;
2126 struct k_work_q *work_q;
2127};
2128
2129extern struct k_work_q k_sys_work_q;
2130
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002131/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002132 * INTERNAL_HIDDEN @endcond
2133 */
2134
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002135#define _K_WORK_INITIALIZER(work_handler) \
2136 { \
2137 ._reserved = NULL, \
2138 .handler = work_handler, \
2139 .flags = { 0 } \
2140 }
2141
2142#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2143
Allan Stephens6bba9b02016-11-16 14:56:54 -05002144/**
2145 * @brief Initialize a statically-defined work item.
2146 *
2147 * This macro can be used to initialize a statically-defined workqueue work
2148 * item, prior to its first use. For example,
2149 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002150 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002151 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002152 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002153 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002154 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002155#define K_WORK_DEFINE(work, work_handler) \
2156 struct k_work work \
2157 __in_section(_k_work, static, work) = \
2158 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002159
2160/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002161 * @brief Initialize a work item.
2162 *
2163 * This routine initializes a workqueue work item, prior to its first use.
2164 *
2165 * @param work Address of work item.
2166 * @param handler Function to invoke each time work item is processed.
2167 *
2168 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002169 */
2170static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2171{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002172 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002173 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002174 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002175}
2176
2177/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002178 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002179 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002180 * This routine submits work item @a work to be processed by workqueue
2181 * @a work_q. If the work item is already pending in the workqueue's queue
2182 * as a result of an earlier submission, this routine has no effect on the
2183 * work item. If the work item has already been processed, or is currently
2184 * being processed, its work is considered complete and the work item can be
2185 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002186 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002187 * @warning
2188 * A submitted work item must not be modified until it has been processed
2189 * by the workqueue.
2190 *
2191 * @note Can be called by ISRs.
2192 *
2193 * @param work_q Address of workqueue.
2194 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002195 *
2196 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002197 */
2198static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2199 struct k_work *work)
2200{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002201 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002202 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002203 }
2204}
2205
2206/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002207 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002208 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002209 * This routine indicates if work item @a work is pending in a workqueue's
2210 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002211 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002212 * @note Can be called by ISRs.
2213 *
2214 * @param work Address of work item.
2215 *
2216 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002217 */
2218static inline int k_work_pending(struct k_work *work)
2219{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002220 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002221}
2222
2223/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002224 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002225 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002226 * This routine starts workqueue @a work_q. The workqueue spawns its work
2227 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002228 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002229 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002230 * @param stack Pointer to work queue thread's stack space, as defined by
2231 * K_THREAD_STACK_DEFINE()
2232 * @param stack_size Size of the work queue thread's stack (in bytes), which
2233 * should either be the same constant passed to
2234 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002235 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002236 *
2237 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002238 */
Andrew Boie507852a2017-07-25 18:47:07 -07002239extern void k_work_q_start(struct k_work_q *work_q,
2240 k_thread_stack_t stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002241 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002242
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002243/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002244 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002245 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002246 * This routine initializes a workqueue delayed work item, prior to
2247 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002248 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002249 * @param work Address of delayed work item.
2250 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002251 *
2252 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002253 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002254extern void k_delayed_work_init(struct k_delayed_work *work,
2255 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002256
2257/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002258 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002259 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002260 * This routine schedules work item @a work to be processed by workqueue
2261 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002262 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002263 * Only when the countdown completes is the work item actually submitted to
2264 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002265 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002266 * Submitting a previously submitted delayed work item that is still
2267 * counting down cancels the existing submission and restarts the countdown
2268 * using the new delay. If the work item is currently pending on the
2269 * workqueue's queue because the countdown has completed it is too late to
2270 * resubmit the item, and resubmission fails without impacting the work item.
2271 * If the work item has already been processed, or is currently being processed,
2272 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002273 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002274 * @warning
2275 * A delayed work item must not be modified until it has been processed
2276 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002277 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002278 * @note Can be called by ISRs.
2279 *
2280 * @param work_q Address of workqueue.
2281 * @param work Address of delayed work item.
2282 * @param delay Delay before submitting the work item (in milliseconds).
2283 *
2284 * @retval 0 Work item countdown started.
2285 * @retval -EINPROGRESS Work item is already pending.
2286 * @retval -EINVAL Work item is being processed or has completed its work.
2287 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002288 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002289extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2290 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002291 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002292
2293/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002294 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002295 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002296 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002297 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002298 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002299 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002300 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002301 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002302 * @param work Address of delayed work item.
2303 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002304 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002305 * @retval -EINPROGRESS Work item is already pending.
2306 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002307 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002308extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002309
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002310/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002311 * @brief Submit a work item to the system workqueue.
2312 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002313 * This routine submits work item @a work to be processed by the system
2314 * workqueue. If the work item is already pending in the workqueue's queue
2315 * as a result of an earlier submission, this routine has no effect on the
2316 * work item. If the work item has already been processed, or is currently
2317 * being processed, its work is considered complete and the work item can be
2318 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002319 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002320 * @warning
2321 * Work items submitted to the system workqueue should avoid using handlers
2322 * that block or yield since this may prevent the system workqueue from
2323 * processing other work items in a timely manner.
2324 *
2325 * @note Can be called by ISRs.
2326 *
2327 * @param work Address of work item.
2328 *
2329 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002330 */
2331static inline void k_work_submit(struct k_work *work)
2332{
2333 k_work_submit_to_queue(&k_sys_work_q, work);
2334}
2335
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002336/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002337 * @brief Submit a delayed work item to the system workqueue.
2338 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002339 * This routine schedules work item @a work to be processed by the system
2340 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002341 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002342 * Only when the countdown completes is the work item actually submitted to
2343 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002344 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002345 * Submitting a previously submitted delayed work item that is still
2346 * counting down cancels the existing submission and restarts the countdown
2347 * using the new delay. If the work item is currently pending on the
2348 * workqueue's queue because the countdown has completed it is too late to
2349 * resubmit the item, and resubmission fails without impacting the work item.
2350 * If the work item has already been processed, or is currently being processed,
2351 * its work is considered complete and the work item can be resubmitted.
2352 *
2353 * @warning
2354 * Work items submitted to the system workqueue should avoid using handlers
2355 * that block or yield since this may prevent the system workqueue from
2356 * processing other work items in a timely manner.
2357 *
2358 * @note Can be called by ISRs.
2359 *
2360 * @param work Address of delayed work item.
2361 * @param delay Delay before submitting the work item (in milliseconds).
2362 *
2363 * @retval 0 Work item countdown started.
2364 * @retval -EINPROGRESS Work item is already pending.
2365 * @retval -EINVAL Work item is being processed or has completed its work.
2366 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002367 */
2368static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002369 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002370{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002371 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002372}
2373
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002374/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002375 * @brief Get time remaining before a delayed work gets scheduled.
2376 *
2377 * This routine computes the (approximate) time remaining before a
2378 * delayed work gets executed. If the delayed work is not waiting to be
2379 * schedules, it returns zero.
2380 *
2381 * @param work Delayed work item.
2382 *
2383 * @return Remaining time (in milliseconds).
2384 */
Kumar Galacc334c72017-04-21 10:55:34 -05002385static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002386{
2387 return _timeout_remaining_get(&work->timeout);
2388}
2389
2390/**
Allan Stephensc98da842016-11-11 15:45:03 -05002391 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002392 */
2393
Allan Stephensc98da842016-11-11 15:45:03 -05002394/**
2395 * @cond INTERNAL_HIDDEN
2396 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002397
2398struct k_mutex {
2399 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002400 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002401 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002402 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002403
Anas Nashif2f203c22016-12-18 06:57:45 -05002404 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002405};
2406
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002407#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002408 { \
2409 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2410 .owner = NULL, \
2411 .lock_count = 0, \
2412 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002413 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002414 }
2415
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002416#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2417
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002418/**
Allan Stephensc98da842016-11-11 15:45:03 -05002419 * INTERNAL_HIDDEN @endcond
2420 */
2421
2422/**
2423 * @defgroup mutex_apis Mutex APIs
2424 * @ingroup kernel_apis
2425 * @{
2426 */
2427
2428/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002429 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002430 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002431 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002432 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002433 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002434 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002435 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002436 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002437#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002438 struct k_mutex name \
2439 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002440 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002441
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002442/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002443 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002444 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002445 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002446 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002447 * Upon completion, the mutex is available and does not have an owner.
2448 *
2449 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002450 *
2451 * @return N/A
2452 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002453extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002454
2455/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002456 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002457 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002458 * This routine locks @a mutex. If the mutex is locked by another thread,
2459 * the calling thread waits until the mutex becomes available or until
2460 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002461 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002462 * A thread is permitted to lock a mutex it has already locked. The operation
2463 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002464 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002465 * @param mutex Address of the mutex.
2466 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002467 * or one of the special values K_NO_WAIT and K_FOREVER.
2468 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002469 * @retval 0 Mutex locked.
2470 * @retval -EBUSY Returned without waiting.
2471 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002472 */
Kumar Galacc334c72017-04-21 10:55:34 -05002473extern int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002474
2475/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002476 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002477 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002478 * This routine unlocks @a mutex. The mutex must already be locked by the
2479 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002480 *
2481 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002482 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002483 * thread.
2484 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002485 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002486 *
2487 * @return N/A
2488 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002489extern void k_mutex_unlock(struct k_mutex *mutex);
2490
Allan Stephensc98da842016-11-11 15:45:03 -05002491/**
2492 * @} end defgroup mutex_apis
2493 */
2494
2495/**
2496 * @cond INTERNAL_HIDDEN
2497 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002498
2499struct k_sem {
2500 _wait_q_t wait_q;
2501 unsigned int count;
2502 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002503 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002504
Anas Nashif2f203c22016-12-18 06:57:45 -05002505 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002506};
2507
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002508#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002509 { \
2510 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2511 .count = initial_count, \
2512 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002513 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002514 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002515 }
2516
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002517#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2518
Allan Stephensc98da842016-11-11 15:45:03 -05002519/**
2520 * INTERNAL_HIDDEN @endcond
2521 */
2522
2523/**
2524 * @defgroup semaphore_apis Semaphore APIs
2525 * @ingroup kernel_apis
2526 * @{
2527 */
2528
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002529/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002530 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002531 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002532 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002533 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002534 * @param sem Address of the semaphore.
2535 * @param initial_count Initial semaphore count.
2536 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002537 *
2538 * @return N/A
2539 */
Andrew Boie99280232017-09-29 14:17:47 -07002540__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2541 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002542
2543/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002544 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002545 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002546 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002547 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002548 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2549 *
2550 * @param sem Address of the semaphore.
2551 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002552 * or one of the special values K_NO_WAIT and K_FOREVER.
2553 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002554 * @note When porting code from the nanokernel legacy API to the new API, be
2555 * careful with the return value of this function. The return value is the
2556 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2557 * non-zero means failure, while the nano_sem_take family returns 1 for success
2558 * and 0 for failure.
2559 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002560 * @retval 0 Semaphore taken.
2561 * @retval -EBUSY Returned without waiting.
2562 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002563 */
Andrew Boie99280232017-09-29 14:17:47 -07002564__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002565
2566/**
2567 * @brief Give a semaphore.
2568 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002569 * This routine gives @a sem, unless the semaphore is already at its maximum
2570 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002571 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002572 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002573 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002574 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002575 *
2576 * @return N/A
2577 */
Andrew Boie99280232017-09-29 14:17:47 -07002578__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002579
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002580/**
2581 * @brief Reset a semaphore's count to zero.
2582 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002583 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002584 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002585 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002586 *
2587 * @return N/A
2588 */
Andrew Boie990bf162017-10-03 12:36:49 -07002589__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002590
2591static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002592{
2593 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002594}
2595
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002596/**
2597 * @brief Get a semaphore's count.
2598 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002599 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002600 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002601 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002602 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002603 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002604 */
Andrew Boie990bf162017-10-03 12:36:49 -07002605__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002606
2607static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002608{
2609 return sem->count;
2610}
2611
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002612/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002613 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002614 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002615 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002616 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002617 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002618 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002619 * @param name Name of the semaphore.
2620 * @param initial_count Initial semaphore count.
2621 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002622 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002623#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002624 struct k_sem name \
2625 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002626 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002627
Allan Stephensc98da842016-11-11 15:45:03 -05002628/**
2629 * @} end defgroup semaphore_apis
2630 */
2631
2632/**
2633 * @defgroup alert_apis Alert APIs
2634 * @ingroup kernel_apis
2635 * @{
2636 */
2637
Allan Stephens5eceb852016-11-16 10:16:30 -05002638/**
2639 * @typedef k_alert_handler_t
2640 * @brief Alert handler function type.
2641 *
2642 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002643 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002644 * and is only invoked if the alert has been initialized with one.
2645 *
2646 * @param alert Address of the alert.
2647 *
2648 * @return 0 if alert has been consumed; non-zero if alert should pend.
2649 */
2650typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002651
2652/**
2653 * @} end defgroup alert_apis
2654 */
2655
2656/**
2657 * @cond INTERNAL_HIDDEN
2658 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002659
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002660#define K_ALERT_DEFAULT NULL
2661#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002662
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002663struct k_alert {
2664 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002665 atomic_t send_count;
2666 struct k_work work_item;
2667 struct k_sem sem;
2668
Anas Nashif2f203c22016-12-18 06:57:45 -05002669 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002670};
2671
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002672extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002673
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002674#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002675 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002676 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002677 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002678 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2679 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002680 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002681 }
2682
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002683#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2684
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002685/**
Allan Stephensc98da842016-11-11 15:45:03 -05002686 * INTERNAL_HIDDEN @endcond
2687 */
2688
2689/**
2690 * @addtogroup alert_apis
2691 * @{
2692 */
2693
2694/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002695 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002696 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002697 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002698 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002699 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002700 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002701 * @param name Name of the alert.
2702 * @param alert_handler Action to take when alert is sent. Specify either
2703 * the address of a function to be invoked by the system workqueue
2704 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2705 * K_ALERT_DEFAULT (which causes the alert to pend).
2706 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002707 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002708#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002709 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002710 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002711 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002712 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002713
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002714/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002715 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002716 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002717 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002718 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002719 * @param alert Address of the alert.
2720 * @param handler Action to take when alert is sent. Specify either the address
2721 * of a function to be invoked by the system workqueue thread,
2722 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2723 * K_ALERT_DEFAULT (which causes the alert to pend).
2724 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002725 *
2726 * @return N/A
2727 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002728extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2729 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002730
2731/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002732 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002733 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002734 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002735 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002736 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2737 *
2738 * @param alert Address of the alert.
2739 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002740 * or one of the special values K_NO_WAIT and K_FOREVER.
2741 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002742 * @retval 0 Alert received.
2743 * @retval -EBUSY Returned without waiting.
2744 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002745 */
Kumar Galacc334c72017-04-21 10:55:34 -05002746extern int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002747
2748/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002749 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002750 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002751 * This routine signals @a alert. The action specified for @a alert will
2752 * be taken, which may trigger the execution of an alert handler function
2753 * and/or cause the alert to pend (assuming the alert has not reached its
2754 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002755 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002756 * @note Can be called by ISRs.
2757 *
2758 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002759 *
2760 * @return N/A
2761 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002762extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002763
2764/**
Allan Stephensc98da842016-11-11 15:45:03 -05002765 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002766 */
2767
Allan Stephensc98da842016-11-11 15:45:03 -05002768/**
2769 * @cond INTERNAL_HIDDEN
2770 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002771
2772struct k_msgq {
2773 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002774 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002775 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002776 char *buffer_start;
2777 char *buffer_end;
2778 char *read_ptr;
2779 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002780 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002781
Anas Nashif2f203c22016-12-18 06:57:45 -05002782 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002783};
2784
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002785#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002786 { \
2787 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002788 .max_msgs = q_max_msgs, \
2789 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002790 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002791 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002792 .read_ptr = q_buffer, \
2793 .write_ptr = q_buffer, \
2794 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002795 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002796 }
2797
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002798#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2799
Peter Mitsis1da807e2016-10-06 11:36:59 -04002800/**
Allan Stephensc98da842016-11-11 15:45:03 -05002801 * INTERNAL_HIDDEN @endcond
2802 */
2803
2804/**
2805 * @defgroup msgq_apis Message Queue APIs
2806 * @ingroup kernel_apis
2807 * @{
2808 */
2809
2810/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002811 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002812 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002813 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2814 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002815 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2816 * message is similarly aligned to this boundary, @a q_msg_size must also be
2817 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002818 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002819 * The message queue can be accessed outside the module where it is defined
2820 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002821 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002822 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002823 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002824 * @param q_name Name of the message queue.
2825 * @param q_msg_size Message size (in bytes).
2826 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002827 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002828 */
2829#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2830 static char __noinit __aligned(q_align) \
2831 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002832 struct k_msgq q_name \
2833 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002834 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002835 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002836
Peter Mitsisd7a37502016-10-13 11:37:40 -04002837/**
2838 * @brief Initialize a message queue.
2839 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002840 * This routine initializes a message queue object, prior to its first use.
2841 *
Allan Stephensda827222016-11-09 14:23:58 -06002842 * The message queue's ring buffer must contain space for @a max_msgs messages,
2843 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2844 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2845 * that each message is similarly aligned to this boundary, @a q_msg_size
2846 * must also be a multiple of N.
2847 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002848 * @param q Address of the message queue.
2849 * @param buffer Pointer to ring buffer that holds queued messages.
2850 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002851 * @param max_msgs Maximum number of messages that can be queued.
2852 *
2853 * @return N/A
2854 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002855extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002856 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002857
2858/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002859 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002860 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002861 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002862 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002863 * @note Can be called by ISRs.
2864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002865 * @param q Address of the message queue.
2866 * @param data Pointer to the message.
2867 * @param timeout Waiting period to add the message (in milliseconds),
2868 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002869 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002870 * @retval 0 Message sent.
2871 * @retval -ENOMSG Returned without waiting or queue purged.
2872 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002873 */
Kumar Galacc334c72017-04-21 10:55:34 -05002874extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002875
2876/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002877 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002878 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002879 * This routine receives a message from message queue @a q in a "first in,
2880 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002881 *
Allan Stephensc98da842016-11-11 15:45:03 -05002882 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002884 * @param q Address of the message queue.
2885 * @param data Address of area to hold the received message.
2886 * @param timeout Waiting period to receive the message (in milliseconds),
2887 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002888 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002889 * @retval 0 Message received.
2890 * @retval -ENOMSG Returned without waiting.
2891 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002892 */
Kumar Galacc334c72017-04-21 10:55:34 -05002893extern int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002894
2895/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002896 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002897 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002898 * This routine discards all unreceived messages in a message queue's ring
2899 * buffer. Any threads that are blocked waiting to send a message to the
2900 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002901 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002902 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002903 *
2904 * @return N/A
2905 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002906extern void k_msgq_purge(struct k_msgq *q);
2907
Peter Mitsis67be2492016-10-07 11:44:34 -04002908/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002909 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002910 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002911 * This routine returns the number of unused entries in a message queue's
2912 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002913 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002914 * @param q Address of the message queue.
2915 *
2916 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002917 */
Kumar Galacc334c72017-04-21 10:55:34 -05002918static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002919{
2920 return q->max_msgs - q->used_msgs;
2921}
2922
Peter Mitsisd7a37502016-10-13 11:37:40 -04002923/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002924 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002925 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002926 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002927 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002928 * @param q Address of the message queue.
2929 *
2930 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002931 */
Kumar Galacc334c72017-04-21 10:55:34 -05002932static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002933{
2934 return q->used_msgs;
2935}
2936
Allan Stephensc98da842016-11-11 15:45:03 -05002937/**
2938 * @} end defgroup msgq_apis
2939 */
2940
2941/**
2942 * @defgroup mem_pool_apis Memory Pool APIs
2943 * @ingroup kernel_apis
2944 * @{
2945 */
2946
Andy Ross73cb9582017-05-09 10:42:39 -07002947/* Note on sizing: the use of a 20 bit field for block means that,
2948 * assuming a reasonable minimum block size of 16 bytes, we're limited
2949 * to 16M of memory managed by a single pool. Long term it would be
2950 * good to move to a variable bit size based on configuration.
2951 */
2952struct k_mem_block_id {
2953 u32_t pool : 8;
2954 u32_t level : 4;
2955 u32_t block : 20;
2956};
2957
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002958struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002959 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002960 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002961};
2962
Allan Stephensc98da842016-11-11 15:45:03 -05002963/**
2964 * @} end defgroup mem_pool_apis
2965 */
2966
2967/**
2968 * @defgroup mailbox_apis Mailbox APIs
2969 * @ingroup kernel_apis
2970 * @{
2971 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002972
2973struct k_mbox_msg {
2974 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002975 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002976 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002977 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002978 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002979 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002980 /** sender's message data buffer */
2981 void *tx_data;
2982 /** internal use only - needed for legacy API support */
2983 void *_rx_data;
2984 /** message data block descriptor */
2985 struct k_mem_block tx_block;
2986 /** source thread id */
2987 k_tid_t rx_source_thread;
2988 /** target thread id */
2989 k_tid_t tx_target_thread;
2990 /** internal use only - thread waiting on send (may be a dummy) */
2991 k_tid_t _syncing_thread;
2992#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2993 /** internal use only - semaphore used during asynchronous send */
2994 struct k_sem *_async_sem;
2995#endif
2996};
2997
Allan Stephensc98da842016-11-11 15:45:03 -05002998/**
2999 * @cond INTERNAL_HIDDEN
3000 */
3001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003002struct k_mbox {
3003 _wait_q_t tx_msg_queue;
3004 _wait_q_t rx_msg_queue;
3005
Anas Nashif2f203c22016-12-18 06:57:45 -05003006 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003007};
3008
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003009#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003010 { \
3011 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3012 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003013 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003014 }
3015
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003016#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3017
Peter Mitsis12092702016-10-14 12:57:23 -04003018/**
Allan Stephensc98da842016-11-11 15:45:03 -05003019 * INTERNAL_HIDDEN @endcond
3020 */
3021
3022/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003023 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003024 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003025 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003026 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003027 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003028 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003029 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003030 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003031#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003032 struct k_mbox name \
3033 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003034 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003035
Peter Mitsis12092702016-10-14 12:57:23 -04003036/**
3037 * @brief Initialize a mailbox.
3038 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003039 * This routine initializes a mailbox object, prior to its first use.
3040 *
3041 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003042 *
3043 * @return N/A
3044 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003045extern void k_mbox_init(struct k_mbox *mbox);
3046
Peter Mitsis12092702016-10-14 12:57:23 -04003047/**
3048 * @brief Send a mailbox message in a synchronous manner.
3049 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003050 * This routine sends a message to @a mbox and waits for a receiver to both
3051 * receive and process it. The message data may be in a buffer, in a memory
3052 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003053 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003054 * @param mbox Address of the mailbox.
3055 * @param tx_msg Address of the transmit message descriptor.
3056 * @param timeout Waiting period for the message to be received (in
3057 * milliseconds), or one of the special values K_NO_WAIT
3058 * and K_FOREVER. Once the message has been received,
3059 * this routine waits as long as necessary for the message
3060 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003061 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003062 * @retval 0 Message sent.
3063 * @retval -ENOMSG Returned without waiting.
3064 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003065 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003066extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003067 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003068
Peter Mitsis12092702016-10-14 12:57:23 -04003069/**
3070 * @brief Send a mailbox message in an asynchronous manner.
3071 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003072 * This routine sends a message to @a mbox without waiting for a receiver
3073 * to process it. The message data may be in a buffer, in a memory pool block,
3074 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3075 * will be given when the message has been both received and completely
3076 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003077 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003078 * @param mbox Address of the mailbox.
3079 * @param tx_msg Address of the transmit message descriptor.
3080 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003081 *
3082 * @return N/A
3083 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003084extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003085 struct k_sem *sem);
3086
Peter Mitsis12092702016-10-14 12:57:23 -04003087/**
3088 * @brief Receive a mailbox message.
3089 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003090 * This routine receives a message from @a mbox, then optionally retrieves
3091 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003092 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003093 * @param mbox Address of the mailbox.
3094 * @param rx_msg Address of the receive message descriptor.
3095 * @param buffer Address of the buffer to receive data, or NULL to defer data
3096 * retrieval and message disposal until later.
3097 * @param timeout Waiting period for a message to be received (in
3098 * milliseconds), or one of the special values K_NO_WAIT
3099 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003100 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003101 * @retval 0 Message received.
3102 * @retval -ENOMSG Returned without waiting.
3103 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003104 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003105extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003106 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003107
3108/**
3109 * @brief Retrieve mailbox message data into a buffer.
3110 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003111 * This routine completes the processing of a received message by retrieving
3112 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003113 *
3114 * Alternatively, this routine can be used to dispose of a received message
3115 * without retrieving its data.
3116 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003117 * @param rx_msg Address of the receive message descriptor.
3118 * @param buffer Address of the buffer to receive data, or NULL to discard
3119 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003120 *
3121 * @return N/A
3122 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003123extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003124
3125/**
3126 * @brief Retrieve mailbox message data into a memory pool block.
3127 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003128 * This routine completes the processing of a received message by retrieving
3129 * its data into a memory pool block, then disposing of the message.
3130 * The memory pool block that results from successful retrieval must be
3131 * returned to the pool once the data has been processed, even in cases
3132 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003133 *
3134 * Alternatively, this routine can be used to dispose of a received message
3135 * without retrieving its data. In this case there is no need to return a
3136 * memory pool block to the pool.
3137 *
3138 * This routine allocates a new memory pool block for the data only if the
3139 * data is not already in one. If a new block cannot be allocated, the routine
3140 * returns a failure code and the received message is left unchanged. This
3141 * permits the caller to reattempt data retrieval at a later time or to dispose
3142 * of the received message without retrieving its data.
3143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003144 * @param rx_msg Address of a receive message descriptor.
3145 * @param pool Address of memory pool, or NULL to discard data.
3146 * @param block Address of the area to hold memory pool block info.
3147 * @param timeout Waiting period to wait for a memory pool block (in
3148 * milliseconds), or one of the special values K_NO_WAIT
3149 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003150 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003151 * @retval 0 Data retrieved.
3152 * @retval -ENOMEM Returned without waiting.
3153 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003154 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003155extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003156 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003157 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003158
Allan Stephensc98da842016-11-11 15:45:03 -05003159/**
3160 * @} end defgroup mailbox_apis
3161 */
3162
3163/**
3164 * @cond INTERNAL_HIDDEN
3165 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003166
3167struct k_pipe {
3168 unsigned char *buffer; /* Pipe buffer: may be NULL */
3169 size_t size; /* Buffer size */
3170 size_t bytes_used; /* # bytes used in buffer */
3171 size_t read_index; /* Where in buffer to read from */
3172 size_t write_index; /* Where in buffer to write */
3173
3174 struct {
3175 _wait_q_t readers; /* Reader wait queue */
3176 _wait_q_t writers; /* Writer wait queue */
3177 } wait_q;
3178
Anas Nashif2f203c22016-12-18 06:57:45 -05003179 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003180};
3181
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003182#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003183 { \
3184 .buffer = pipe_buffer, \
3185 .size = pipe_buffer_size, \
3186 .bytes_used = 0, \
3187 .read_index = 0, \
3188 .write_index = 0, \
3189 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3190 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003191 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003192 }
3193
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003194#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3195
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003196/**
Allan Stephensc98da842016-11-11 15:45:03 -05003197 * INTERNAL_HIDDEN @endcond
3198 */
3199
3200/**
3201 * @defgroup pipe_apis Pipe APIs
3202 * @ingroup kernel_apis
3203 * @{
3204 */
3205
3206/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003207 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003208 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003209 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003210 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003211 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003212 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003213 * @param name Name of the pipe.
3214 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3215 * or zero if no ring buffer is used.
3216 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003217 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003218#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3219 static unsigned char __noinit __aligned(pipe_align) \
3220 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003221 struct k_pipe name \
3222 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003223 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003224
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003225/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003226 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003227 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003228 * This routine initializes a pipe object, prior to its first use.
3229 *
3230 * @param pipe Address of the pipe.
3231 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3232 * is used.
3233 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3234 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003235 *
3236 * @return N/A
3237 */
3238extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3239 size_t size);
3240
3241/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003242 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003243 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003244 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003245 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003246 * @param pipe Address of the pipe.
3247 * @param data Address of data to write.
3248 * @param bytes_to_write Size of data (in bytes).
3249 * @param bytes_written Address of area to hold the number of bytes written.
3250 * @param min_xfer Minimum number of bytes to write.
3251 * @param timeout Waiting period to wait for the data to be written (in
3252 * milliseconds), or one of the special values K_NO_WAIT
3253 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003254 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003255 * @retval 0 At least @a min_xfer bytes of data were written.
3256 * @retval -EIO Returned without waiting; zero data bytes were written.
3257 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003258 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003259 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003260extern int k_pipe_put(struct k_pipe *pipe, void *data,
3261 size_t bytes_to_write, size_t *bytes_written,
Kumar Galacc334c72017-04-21 10:55:34 -05003262 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003263
3264/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003265 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003266 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003267 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003268 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003269 * @param pipe Address of the pipe.
3270 * @param data Address to place the data read from pipe.
3271 * @param bytes_to_read Maximum number of data bytes to read.
3272 * @param bytes_read Address of area to hold the number of bytes read.
3273 * @param min_xfer Minimum number of data bytes to read.
3274 * @param timeout Waiting period to wait for the data to be read (in
3275 * milliseconds), or one of the special values K_NO_WAIT
3276 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003277 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003278 * @retval 0 At least @a min_xfer bytes of data were read.
3279 * @retval -EIO Returned without waiting; zero data bytes were read.
3280 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003281 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003282 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003283extern int k_pipe_get(struct k_pipe *pipe, void *data,
3284 size_t bytes_to_read, size_t *bytes_read,
Kumar Galacc334c72017-04-21 10:55:34 -05003285 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003286
3287/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003288 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003289 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003290 * This routine writes the data contained in a memory block to @a pipe.
3291 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003292 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003293 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003294 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003295 * @param block Memory block containing data to send
3296 * @param size Number of data bytes in memory block to send
3297 * @param sem Semaphore to signal upon completion (else NULL)
3298 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003299 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003300 */
3301extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3302 size_t size, struct k_sem *sem);
3303
3304/**
Allan Stephensc98da842016-11-11 15:45:03 -05003305 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003306 */
3307
Allan Stephensc98da842016-11-11 15:45:03 -05003308/**
3309 * @cond INTERNAL_HIDDEN
3310 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003311
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003312struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003313 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003314 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003315 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003316 char *buffer;
3317 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003318 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003319
Anas Nashif2f203c22016-12-18 06:57:45 -05003320 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003321};
3322
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003323#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003324 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003325 { \
3326 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003327 .num_blocks = slab_num_blocks, \
3328 .block_size = slab_block_size, \
3329 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003330 .free_list = NULL, \
3331 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003332 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003333 }
3334
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003335#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3336
3337
Peter Mitsis578f9112016-10-07 13:50:31 -04003338/**
Allan Stephensc98da842016-11-11 15:45:03 -05003339 * INTERNAL_HIDDEN @endcond
3340 */
3341
3342/**
3343 * @defgroup mem_slab_apis Memory Slab APIs
3344 * @ingroup kernel_apis
3345 * @{
3346 */
3347
3348/**
Allan Stephensda827222016-11-09 14:23:58 -06003349 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003350 *
Allan Stephensda827222016-11-09 14:23:58 -06003351 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003352 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003353 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3354 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003355 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003356 *
Allan Stephensda827222016-11-09 14:23:58 -06003357 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003358 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003359 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003360 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003361 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003362 * @param name Name of the memory slab.
3363 * @param slab_block_size Size of each memory block (in bytes).
3364 * @param slab_num_blocks Number memory blocks.
3365 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003366 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003367#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3368 char __noinit __aligned(slab_align) \
3369 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3370 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003371 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003372 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003373 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003374
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003375/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003376 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003377 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003378 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003379 *
Allan Stephensda827222016-11-09 14:23:58 -06003380 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3381 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3382 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3383 * To ensure that each memory block is similarly aligned to this boundary,
3384 * @a slab_block_size must also be a multiple of N.
3385 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003386 * @param slab Address of the memory slab.
3387 * @param buffer Pointer to buffer used for the memory blocks.
3388 * @param block_size Size of each memory block (in bytes).
3389 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003390 *
3391 * @return N/A
3392 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003393extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003394 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003395
3396/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003397 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003398 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003399 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003400 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003401 * @param slab Address of the memory slab.
3402 * @param mem Pointer to block address area.
3403 * @param timeout Maximum time to wait for operation to complete
3404 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3405 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003406 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003407 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003408 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003409 * @retval -ENOMEM Returned without waiting.
3410 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003411 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003412extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003413 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003414
3415/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003416 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003418 * This routine releases a previously allocated memory block back to its
3419 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003420 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003421 * @param slab Address of the memory slab.
3422 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003423 *
3424 * @return N/A
3425 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003426extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003427
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003428/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003429 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003430 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003431 * This routine gets the number of memory blocks that are currently
3432 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003433 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003434 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003435 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003436 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003437 */
Kumar Galacc334c72017-04-21 10:55:34 -05003438static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003439{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003440 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003441}
3442
Peter Mitsisc001aa82016-10-13 13:53:37 -04003443/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003444 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003445 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003446 * This routine gets the number of memory blocks that are currently
3447 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003448 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003449 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003450 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003451 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003452 */
Kumar Galacc334c72017-04-21 10:55:34 -05003453static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003454{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003455 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003456}
3457
Allan Stephensc98da842016-11-11 15:45:03 -05003458/**
3459 * @} end defgroup mem_slab_apis
3460 */
3461
3462/**
3463 * @cond INTERNAL_HIDDEN
3464 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003465
Andy Ross73cb9582017-05-09 10:42:39 -07003466struct k_mem_pool_lvl {
3467 union {
3468 u32_t *bits_p;
3469 u32_t bits;
3470 };
3471 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003472};
3473
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003474struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003475 void *buf;
3476 size_t max_sz;
3477 u16_t n_max;
3478 u8_t n_levels;
3479 u8_t max_inline_level;
3480 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003481 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003482};
3483
Andy Ross73cb9582017-05-09 10:42:39 -07003484#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003485
Andy Ross73cb9582017-05-09 10:42:39 -07003486#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3487
3488#define _MPOOL_LVLS(maxsz, minsz) \
3489 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3490 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3491 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3492 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3493 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3494 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3495 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3496 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3497 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3498 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3499 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3500 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3501 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3502 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3503 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3504 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3505
3506/* Rounds the needed bits up to integer multiples of u32_t */
3507#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3508 ((((n_max) << (2*(l))) + 31) / 32)
3509
3510/* One word gets stored free unioned with the pointer, otherwise the
3511 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003512 */
Andy Ross73cb9582017-05-09 10:42:39 -07003513#define _MPOOL_LBIT_WORDS(n_max, l) \
3514 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3515 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003516
Andy Ross73cb9582017-05-09 10:42:39 -07003517/* How many bytes for the bitfields of a single level? */
3518#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3519 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3520 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003521
Andy Ross73cb9582017-05-09 10:42:39 -07003522/* Size of the bitmap array that follows the buffer in allocated memory */
3523#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3524 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3525 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3526 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3527 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3528 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3529 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3530 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3531 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3532 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3533 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3534 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3535 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3536 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3537 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3538 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3539 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003540
3541/**
Allan Stephensc98da842016-11-11 15:45:03 -05003542 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003543 */
3544
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003545/**
Allan Stephensc98da842016-11-11 15:45:03 -05003546 * @addtogroup mem_pool_apis
3547 * @{
3548 */
3549
3550/**
3551 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003552 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003553 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3554 * long. The memory pool allows blocks to be repeatedly partitioned into
3555 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003556 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003557 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003558 * If the pool is to be accessed outside the module where it is defined, it
3559 * can be declared via
3560 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003561 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003562 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003563 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003564 * @param minsz Size of the smallest blocks in the pool (in bytes).
3565 * @param maxsz Size of the largest blocks in the pool (in bytes).
3566 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003567 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003568 */
Andy Ross73cb9582017-05-09 10:42:39 -07003569#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3570 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3571 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3572 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3573 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3574 .buf = _mpool_buf_##name, \
3575 .max_sz = maxsz, \
3576 .n_max = nmax, \
3577 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3578 .levels = _mpool_lvls_##name, \
3579 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003580
Peter Mitsis937042c2016-10-13 13:18:26 -04003581/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003582 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003583 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003584 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003585 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003586 * @param pool Address of the memory pool.
3587 * @param block Pointer to block descriptor for the allocated memory.
3588 * @param size Amount of memory to allocate (in bytes).
3589 * @param timeout Maximum time to wait for operation to complete
3590 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3591 * or K_FOREVER to wait as long as necessary.
3592 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003593 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003594 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003595 * @retval -ENOMEM Returned without waiting.
3596 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003597 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003598extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003599 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003600
3601/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003602 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003603 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003604 * This routine releases a previously allocated memory block back to its
3605 * memory pool.
3606 *
3607 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003608 *
3609 * @return N/A
3610 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003611extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003612
3613/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003614 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003615 *
Andy Ross73cb9582017-05-09 10:42:39 -07003616 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003617 *
Andy Ross73cb9582017-05-09 10:42:39 -07003618 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003619 *
3620 * @return N/A
3621 */
Andy Ross73cb9582017-05-09 10:42:39 -07003622static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003623
3624/**
Allan Stephensc98da842016-11-11 15:45:03 -05003625 * @} end addtogroup mem_pool_apis
3626 */
3627
3628/**
3629 * @defgroup heap_apis Heap Memory Pool APIs
3630 * @ingroup kernel_apis
3631 * @{
3632 */
3633
3634/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003635 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003636 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003637 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003638 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003639 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003640 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003641 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003642 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003643 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003644extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003645
3646/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003647 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003648 *
3649 * This routine provides traditional free() semantics. The memory being
3650 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003651 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003652 * If @a ptr is NULL, no operation is performed.
3653 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003654 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003655 *
3656 * @return N/A
3657 */
3658extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003659
Allan Stephensc98da842016-11-11 15:45:03 -05003660/**
3661 * @} end defgroup heap_apis
3662 */
3663
Benjamin Walshacc68c12017-01-29 18:57:45 -05003664/* polling API - PRIVATE */
3665
Benjamin Walshb0179862017-02-02 16:39:57 -05003666#ifdef CONFIG_POLL
3667#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3668#else
3669#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3670#endif
3671
Benjamin Walshacc68c12017-01-29 18:57:45 -05003672/* private - implementation data created as needed, per-type */
3673struct _poller {
3674 struct k_thread *thread;
3675};
3676
3677/* private - types bit positions */
3678enum _poll_types_bits {
3679 /* can be used to ignore an event */
3680 _POLL_TYPE_IGNORE,
3681
3682 /* to be signaled by k_poll_signal() */
3683 _POLL_TYPE_SIGNAL,
3684
3685 /* semaphore availability */
3686 _POLL_TYPE_SEM_AVAILABLE,
3687
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003688 /* queue/fifo/lifo data availability */
3689 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003690
3691 _POLL_NUM_TYPES
3692};
3693
3694#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3695
3696/* private - states bit positions */
3697enum _poll_states_bits {
3698 /* default state when creating event */
3699 _POLL_STATE_NOT_READY,
3700
Benjamin Walshacc68c12017-01-29 18:57:45 -05003701 /* signaled by k_poll_signal() */
3702 _POLL_STATE_SIGNALED,
3703
3704 /* semaphore is available */
3705 _POLL_STATE_SEM_AVAILABLE,
3706
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003707 /* data is available to read on queue/fifo/lifo */
3708 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003709
3710 _POLL_NUM_STATES
3711};
3712
3713#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3714
3715#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003716 (32 - (0 \
3717 + 8 /* tag */ \
3718 + _POLL_NUM_TYPES \
3719 + _POLL_NUM_STATES \
3720 + 1 /* modes */ \
3721 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003722
3723#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3724#error overflow of 32-bit word in struct k_poll_event
3725#endif
3726
3727/* end of polling API - PRIVATE */
3728
3729
3730/**
3731 * @defgroup poll_apis Async polling APIs
3732 * @ingroup kernel_apis
3733 * @{
3734 */
3735
3736/* Public polling API */
3737
3738/* public - values for k_poll_event.type bitfield */
3739#define K_POLL_TYPE_IGNORE 0
3740#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3741#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003742#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3743#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003744
3745/* public - polling modes */
3746enum k_poll_modes {
3747 /* polling thread does not take ownership of objects when available */
3748 K_POLL_MODE_NOTIFY_ONLY = 0,
3749
3750 K_POLL_NUM_MODES
3751};
3752
3753/* public - values for k_poll_event.state bitfield */
3754#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003755#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3756#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003757#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3758#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003759
3760/* public - poll signal object */
3761struct k_poll_signal {
3762 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003763 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003764
3765 /*
3766 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3767 * user resets it to 0.
3768 */
3769 unsigned int signaled;
3770
3771 /* custom result value passed to k_poll_signal() if needed */
3772 int result;
3773};
3774
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003775#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003776 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003777 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003778 .signaled = 0, \
3779 .result = 0, \
3780 }
3781
3782struct k_poll_event {
3783 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003784 sys_dnode_t _node;
3785
3786 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003787 struct _poller *poller;
3788
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003789 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003790 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003791
Benjamin Walshacc68c12017-01-29 18:57:45 -05003792 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003793 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003794
3795 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003796 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003797
3798 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003799 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003800
3801 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003802 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003803
3804 /* per-type data */
3805 union {
3806 void *obj;
3807 struct k_poll_signal *signal;
3808 struct k_sem *sem;
3809 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003810 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003811 };
3812};
3813
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003814#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003815 { \
3816 .poller = NULL, \
3817 .type = event_type, \
3818 .state = K_POLL_STATE_NOT_READY, \
3819 .mode = event_mode, \
3820 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003821 { .obj = event_obj }, \
3822 }
3823
3824#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3825 event_tag) \
3826 { \
3827 .type = event_type, \
3828 .tag = event_tag, \
3829 .state = K_POLL_STATE_NOT_READY, \
3830 .mode = event_mode, \
3831 .unused = 0, \
3832 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003833 }
3834
3835/**
3836 * @brief Initialize one struct k_poll_event instance
3837 *
3838 * After this routine is called on a poll event, the event it ready to be
3839 * placed in an event array to be passed to k_poll().
3840 *
3841 * @param event The event to initialize.
3842 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3843 * values. Only values that apply to the same object being polled
3844 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3845 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003846 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003847 * @param obj Kernel object or poll signal.
3848 *
3849 * @return N/A
3850 */
3851
Kumar Galacc334c72017-04-21 10:55:34 -05003852extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003853 int mode, void *obj);
3854
3855/**
3856 * @brief Wait for one or many of multiple poll events to occur
3857 *
3858 * This routine allows a thread to wait concurrently for one or many of
3859 * multiple poll events to have occurred. Such events can be a kernel object
3860 * being available, like a semaphore, or a poll signal event.
3861 *
3862 * When an event notifies that a kernel object is available, the kernel object
3863 * is not "given" to the thread calling k_poll(): it merely signals the fact
3864 * that the object was available when the k_poll() call was in effect. Also,
3865 * all threads trying to acquire an object the regular way, i.e. by pending on
3866 * the object, have precedence over the thread polling on the object. This
3867 * means that the polling thread will never get the poll event on an object
3868 * until the object becomes available and its pend queue is empty. For this
3869 * reason, the k_poll() call is more effective when the objects being polled
3870 * only have one thread, the polling thread, trying to acquire them.
3871 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003872 * When k_poll() returns 0, the caller should loop on all the events that were
3873 * passed to k_poll() and check the state field for the values that were
3874 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003875 *
3876 * Before being reused for another call to k_poll(), the user has to reset the
3877 * state field to K_POLL_STATE_NOT_READY.
3878 *
3879 * @param events An array of pointers to events to be polled for.
3880 * @param num_events The number of events in the array.
3881 * @param timeout Waiting period for an event to be ready (in milliseconds),
3882 * or one of the special values K_NO_WAIT and K_FOREVER.
3883 *
3884 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003885 * @retval -EAGAIN Waiting period timed out.
3886 */
3887
3888extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003889 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003890
3891/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003892 * @brief Initialize a poll signal object.
3893 *
3894 * Ready a poll signal object to be signaled via k_poll_signal().
3895 *
3896 * @param signal A poll signal.
3897 *
3898 * @return N/A
3899 */
3900
3901extern void k_poll_signal_init(struct k_poll_signal *signal);
3902
3903/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003904 * @brief Signal a poll signal object.
3905 *
3906 * This routine makes ready a poll signal, which is basically a poll event of
3907 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3908 * made ready to run. A @a result value can be specified.
3909 *
3910 * The poll signal contains a 'signaled' field that, when set by
3911 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3912 * be reset by the user before being passed again to k_poll() or k_poll() will
3913 * consider it being signaled, and will return immediately.
3914 *
3915 * @param signal A poll signal.
3916 * @param result The value to store in the result field of the signal.
3917 *
3918 * @retval 0 The signal was delivered successfully.
3919 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3920 */
3921
3922extern int k_poll_signal(struct k_poll_signal *signal, int result);
3923
3924/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003925extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003926
3927/**
3928 * @} end defgroup poll_apis
3929 */
3930
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003931/**
3932 * @brief Make the CPU idle.
3933 *
3934 * This function makes the CPU idle until an event wakes it up.
3935 *
3936 * In a regular system, the idle thread should be the only thread responsible
3937 * for making the CPU idle and triggering any type of power management.
3938 * However, in some more constrained systems, such as a single-threaded system,
3939 * the only thread would be responsible for this if needed.
3940 *
3941 * @return N/A
3942 */
3943extern void k_cpu_idle(void);
3944
3945/**
3946 * @brief Make the CPU idle in an atomic fashion.
3947 *
3948 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3949 * must be done atomically before making the CPU idle.
3950 *
3951 * @param key Interrupt locking key obtained from irq_lock().
3952 *
3953 * @return N/A
3954 */
3955extern void k_cpu_atomic_idle(unsigned int key);
3956
Kumar Galacc334c72017-04-21 10:55:34 -05003957extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003958
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003959#include <arch/cpu.h>
3960
Andrew Boiecdb94d62017-04-18 15:22:05 -07003961#ifdef _ARCH_EXCEPT
3962/* This archtecture has direct support for triggering a CPU exception */
3963#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3964#else
3965
3966#include <misc/printk.h>
3967
3968/* NOTE: This is the implementation for arches that do not implement
3969 * _ARCH_EXCEPT() to generate a real CPU exception.
3970 *
3971 * We won't have a real exception frame to determine the PC value when
3972 * the oops occurred, so print file and line number before we jump into
3973 * the fatal error handler.
3974 */
3975#define _k_except_reason(reason) do { \
3976 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3977 _NanoFatalErrorHandler(reason, &_default_esf); \
3978 CODE_UNREACHABLE; \
3979 } while (0)
3980
3981#endif /* _ARCH__EXCEPT */
3982
3983/**
3984 * @brief Fatally terminate a thread
3985 *
3986 * This should be called when a thread has encountered an unrecoverable
3987 * runtime condition and needs to terminate. What this ultimately
3988 * means is determined by the _fatal_error_handler() implementation, which
3989 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3990 *
3991 * If this is called from ISR context, the default system fatal error handler
3992 * will treat it as an unrecoverable system error, just like k_panic().
3993 */
3994#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3995
3996/**
3997 * @brief Fatally terminate the system
3998 *
3999 * This should be called when the Zephyr kernel has encountered an
4000 * unrecoverable runtime condition and needs to terminate. What this ultimately
4001 * means is determined by the _fatal_error_handler() implementation, which
4002 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4003 */
4004#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4005
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004006/*
4007 * private APIs that are utilized by one or more public APIs
4008 */
4009
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004010#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004011extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004012#else
4013#define _init_static_threads() do { } while ((0))
4014#endif
4015
4016extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05004017extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004018
Andrew Boiedc5d9352017-06-02 12:56:47 -07004019/* arch/cpu.h may declare an architecture or platform-specific macro
4020 * for properly declaring stacks, compatible with MMU/MPU constraints if
4021 * enabled
4022 */
4023#ifdef _ARCH_THREAD_STACK_DEFINE
4024#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4025#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4026 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4027#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4028#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004029static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4030{
4031 return _ARCH_THREAD_STACK_BUFFER(sym);
4032}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004033#else
4034/**
4035 * @brief Declare a toplevel thread stack memory region
4036 *
4037 * This declares a region of memory suitable for use as a thread's stack.
4038 *
4039 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4040 * 'noinit' section so that it isn't zeroed at boot
4041 *
Andrew Boie507852a2017-07-25 18:47:07 -07004042 * The declared symbol will always be a k_thread_stack_t which can be passed to
4043 * k_thread_create, but should otherwise not be manipulated. If the buffer
4044 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004045 *
4046 * It is legal to precede this definition with the 'static' keyword.
4047 *
4048 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4049 * parameter of k_thread_create(), it may not be the same as the
4050 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4051 *
4052 * @param sym Thread stack symbol name
4053 * @param size Size of the stack memory region
4054 */
4055#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004056 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004057
4058/**
4059 * @brief Declare a toplevel array of thread stack memory regions
4060 *
4061 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4062 * definition for additional details and constraints.
4063 *
4064 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4065 * 'noinit' section so that it isn't zeroed at boot
4066 *
4067 * @param sym Thread stack symbol name
4068 * @param nmemb Number of stacks to declare
4069 * @param size Size of the stack memory region
4070 */
4071
4072#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004073 struct _k_thread_stack_element __noinit \
4074 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004075
4076/**
4077 * @brief Declare an embedded stack memory region
4078 *
4079 * Used for stacks embedded within other data structures. Use is highly
4080 * discouraged but in some cases necessary. For memory protection scenarios,
4081 * it is very important that any RAM preceding this member not be writable
4082 * by threads else a stack overflow will lead to silent corruption. In other
4083 * words, the containing data structure should live in RAM owned by the kernel.
4084 *
4085 * @param sym Thread stack symbol name
4086 * @param size Size of the stack memory region
4087 */
4088#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004089 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004090
4091/**
4092 * @brief Return the size in bytes of a stack memory region
4093 *
4094 * Convenience macro for passing the desired stack size to k_thread_create()
4095 * since the underlying implementation may actually create something larger
4096 * (for instance a guard area).
4097 *
4098 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004099 * passed to K_THREAD_STACK_DEFINE.
4100 *
4101 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4102 * it is not guaranteed to return the original value since each array
4103 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004104 *
4105 * @param sym Stack memory symbol
4106 * @return Size of the stack
4107 */
4108#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4109
4110/**
4111 * @brief Get a pointer to the physical stack buffer
4112 *
4113 * Convenience macro to get at the real underlying stack buffer used by
4114 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4115 * This is really only intended for diagnostic tools which want to examine
4116 * stack memory contents.
4117 *
4118 * @param sym Declared stack symbol name
4119 * @return The buffer itself, a char *
4120 */
Andrew Boie507852a2017-07-25 18:47:07 -07004121static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4122{
4123 return (char *)sym;
4124}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004125
4126#endif /* _ARCH_DECLARE_STACK */
4127
Chunlin Hane9c97022017-07-07 20:29:30 +08004128/**
4129 * @defgroup mem_domain_apis Memory domain APIs
4130 * @ingroup kernel_apis
4131 * @{
4132 */
4133
4134/** @def MEM_PARTITION_ENTRY
4135 * @brief Used to declare a memory partition entry
4136 */
4137#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4138 {\
4139 .start = _start, \
4140 .size = _size, \
4141 .attr = _attr, \
4142 }
4143
4144/** @def K_MEM_PARTITION_DEFINE
4145 * @brief Used to declare a memory partition
4146 */
4147#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4148#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4149 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
4150 struct k_mem_partition name = \
4151 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4152#else
4153#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4154 struct k_mem_partition name = \
4155 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4156#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4157
4158
4159/* memory partition */
4160struct k_mem_partition {
4161 /* start address of memory partition */
4162 u32_t start;
4163 /* size of memory partition */
4164 u32_t size;
4165 /* attribute of memory partition */
4166 u32_t attr;
4167};
4168
4169#if defined(CONFIG_USERSPACE)
4170/* memory domian */
4171struct k_mem_domain {
4172 /* number of partitions in the domain */
4173 u32_t num_partitions;
4174 /* partitions in the domain */
4175 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
4176 /* domain q */
4177 sys_dlist_t mem_domain_q;
4178};
4179#endif /* CONFIG_USERSPACE */
4180
4181/**
4182 * @brief Initialize a memory domain.
4183 *
4184 * Initialize a memory domain with given name and memory partitions.
4185 *
4186 * @param domain The memory domain to be initialized.
4187 * @param num_parts The number of array items of "parts" parameter.
4188 * @param parts An array of pointers to the memory partitions. Can be NULL
4189 * if num_parts is zero.
4190 */
4191
4192extern void k_mem_domain_init(struct k_mem_domain *domain, u32_t num_parts,
4193 struct k_mem_partition *parts[]);
4194/**
4195 * @brief Destroy a memory domain.
4196 *
4197 * Destroy a memory domain.
4198 *
4199 * @param domain The memory domain to be destroyed.
4200 */
4201
4202extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4203
4204/**
4205 * @brief Add a memory partition into a memory domain.
4206 *
4207 * Add a memory partition into a memory domain.
4208 *
4209 * @param domain The memory domain to be added a memory partition.
4210 * @param part The memory partition to be added
4211 */
4212
4213extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4214 struct k_mem_partition *part);
4215
4216/**
4217 * @brief Remove a memory partition from a memory domain.
4218 *
4219 * Remove a memory partition from a memory domain.
4220 *
4221 * @param domain The memory domain to be removed a memory partition.
4222 * @param part The memory partition to be removed
4223 */
4224
4225extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4226 struct k_mem_partition *part);
4227
4228/**
4229 * @brief Add a thread into a memory domain.
4230 *
4231 * Add a thread into a memory domain.
4232 *
4233 * @param domain The memory domain that the thread is going to be added into.
4234 * @param thread ID of thread going to be added into the memory domain.
4235 */
4236
4237extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4238 k_tid_t thread);
4239
4240/**
4241 * @brief Remove a thread from its memory domain.
4242 *
4243 * Remove a thread from its memory domain.
4244 *
4245 * @param thread ID of thread going to be removed from its memory domain.
4246 */
4247
4248extern void k_mem_domain_remove_thread(k_tid_t thread);
4249
4250/**
4251 * @} end defgroup mem_domain_apis
4252 */
4253
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004254#ifdef __cplusplus
4255}
4256#endif
4257
Andrew Boiee004dec2016-11-07 09:01:19 -08004258#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4259/*
4260 * Define new and delete operators.
4261 * At this moment, the operators do nothing since objects are supposed
4262 * to be statically allocated.
4263 */
4264inline void operator delete(void *ptr)
4265{
4266 (void)ptr;
4267}
4268
4269inline void operator delete[](void *ptr)
4270{
4271 (void)ptr;
4272}
4273
4274inline void *operator new(size_t size)
4275{
4276 (void)size;
4277 return NULL;
4278}
4279
4280inline void *operator new[](size_t size)
4281{
4282 (void)size;
4283 return NULL;
4284}
4285
4286/* Placement versions of operator new and delete */
4287inline void operator delete(void *ptr1, void *ptr2)
4288{
4289 (void)ptr1;
4290 (void)ptr2;
4291}
4292
4293inline void operator delete[](void *ptr1, void *ptr2)
4294{
4295 (void)ptr1;
4296 (void)ptr2;
4297}
4298
4299inline void *operator new(size_t size, void *ptr)
4300{
4301 (void)size;
4302 return ptr;
4303}
4304
4305inline void *operator new[](size_t size, void *ptr)
4306{
4307 (void)size;
4308 return ptr;
4309}
4310
4311#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4312
Andrew Boiefa94ee72017-09-28 16:54:35 -07004313#include <syscalls/kernel.h>
4314
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004315#endif /* !_ASMLANGUAGE */
4316
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004317#endif /* _kernel__h_ */